import {z} from 'zod' export type CollectionInputSchema = {category?: (string | null) | undefined, custom_keyframe?: (string | null) | undefined, custom_poster?: (string | null) | undefined, date_created?: string | undefined, external_id?: (string | null) | undefined, is_root?: boolean | undefined, keyframe_asset_ids?: Array | undefined, parent_id?: (string | null) | undefined, status?: ('ACTIVE' | 'HIDDEN' | 'DELETED') | undefined, storage_id?: (string | null) | undefined, title: string}; export type CreateApiV1CollectionsArgs = {'App-ID': string, 'Auth-Token': string, apply_default_acls?: boolean | undefined, apply_collection_acls?: boolean | undefined, restrict_collection_acls?: boolean | undefined, apply_acl_template_id?: string | undefined, low_priority_indexing?: boolean | undefined, body: CollectionInputSchema}; export type CreateApiV1CollectionsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const collectionSchema = z.object({category: z.string().nullable().optional(), created_by_user: z.string().optional(), custom_keyframe: z.string().nullable().optional(), custom_order_status: z.enum(["DISABLED", "ENABLING", "ENABLED"]).optional(), custom_poster: z.string().nullable().optional(), date_created: z.string().optional(), date_deleted: z.string().optional(), date_modified: z.string().optional(), date_viewed: z.string().optional(), deleted_by_user: z.string().optional(), external_id: z.string().nullable().optional(), favoured: z.boolean().optional(), id: z.string().optional(), in_collections: z.array(z.string()).optional(), is_root: z.boolean().optional(), keyframe_asset_ids: z.array(z.string()).optional(), keyframes: z.array(z.object({})).optional(), metadata: z.object({}).optional(), object_type: z.string().optional(), parent_id: z.string().nullable().optional(), parents: z.array(z.string()).optional(), permissions: z.array(z.string()).optional(), position: z.number().int().gte(-2147483648).lte(2147483647).optional(), project_id: z.string().optional(), status: z.enum(["ACTIVE", "HIDDEN", "DELETED"]).optional(), storage_id: z.string().nullable().optional(), title: z.string()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create a new collection * * POST /v1/collections/ */ export const createApiV1Collections = async (args: CreateApiV1CollectionsArgs, options: CreateApiV1CollectionsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['App-ID'] != null) headers.set("App-ID", String(args['App-ID'])) if (args['Auth-Token'] != null) headers.set("Auth-Token", String(args['Auth-Token'])) if (args.apply_default_acls != null) query.append("apply_default_acls", String(args.apply_default_acls)) if (args.apply_collection_acls != null) query.append("apply_collection_acls", String(args.apply_collection_acls)) if (args.restrict_collection_acls != null) query.append("restrict_collection_acls", String(args.restrict_collection_acls)) if (args.apply_acl_template_id != null) query.append("apply_acl_template_id", String(args.apply_acl_template_id)) if (args.low_priority_indexing != null) query.append("low_priority_indexing", String(args.low_priority_indexing)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/collections/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(collectionSchema, await res.json()) };