import {z} from 'zod' export type CreateApiV1AssetsAssetIdCustomKeyframeArgs = {'App-ID': string, 'Auth-Token': string, asset_id: string, content_type?: string | undefined}; export type CreateApiV1AssetsAssetIdCustomKeyframeOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const resolutionType = z.object({height: z.number().int().gte(-2147483648).lte(2147483647).optional(), width: z.number().int().gte(-2147483648).lte(2147483647).optional()}); export const timeBaseType = z.object({denominator: z.number().int().gte(-2147483648).lte(2147483647).optional(), numerator: z.number().int().gte(-2147483648).lte(2147483647).optional()}); export const timeCodeType = z.object({frames_number: z.number().int().gte(-9223372036854776000).lte(9223372036854776000).optional(), is_drop_frame: z.boolean().optional(), time_base: timeBaseType.optional()}); export const keyframeSchema = z.object({asset_id: z.string().nullable().optional(), collection_id: z.string().nullable().optional(), date_created: z.string().optional(), date_modified: z.string().optional(), filename: z.string().optional(), format: z.string().nullable().optional(), id: z.string().optional(), is_custom_keyframe: z.boolean().optional(), is_public: z.boolean().optional(), name: z.string().optional(), resolution: resolutionType.optional(), rotation: z.number().int().gte(-2147483648).lte(2147483647).nullable().optional(), size: z.number().int().gte(-9223372036854776000).lte(9223372036854776000).nullable().optional(), status: z.enum(["OPEN", "GROWING", "AWAITED", "CLOSED", "FAILED", "ARCHIVED", "MISSING", "REDISCOVERED", "DELETED"]).optional(), storage_id: z.string().optional(), storage_method: z.string().optional(), time_code: timeCodeType.optional(), type: z.enum(["KEYFRAME_MAP", "KEYFRAME", "POSTER"]), upload_credentials: z.record(z.string(), z.unknown()).optional(), upload_headers: z.record(z.string(), z.unknown()).optional(), upload_method: z.string().optional(), upload_url: z.string().optional(), url: z.string().optional(), version_id: z.string().nullable().optional()}); 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 keyframe of type poster for asset * * POST /v1/assets/{asset_id}/custom_keyframe/ */ export const createApiV1AssetsAssetIdCustomKeyframe = async (args: CreateApiV1AssetsAssetIdCustomKeyframeArgs, options: CreateApiV1AssetsAssetIdCustomKeyframeOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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.content_type != null) query.append("content_type", String(args.content_type)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/assets/${encodeURIComponent(String(args.asset_id))}/custom_keyframe/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(keyframeSchema, await res.json()) };