import {z} from 'zod' export type ResolutionType = {height?: number | undefined, width?: number | undefined}; export type TimeBaseType = {denominator?: number | undefined, numerator?: number | undefined}; export type TimeCodeType = {frames_number?: number | undefined, is_drop_frame?: boolean | undefined, time_base?: TimeBaseType | undefined}; export type PlaylistKeyframeCreateSchema = {content_type?: string | undefined, date_created?: string | undefined, date_modified?: string | undefined, filename?: string | undefined, format?: (string | null) | undefined, id?: string | undefined, is_custom_keyframe?: boolean | undefined, is_public?: boolean | undefined, name?: string | undefined, playlist_id?: string | undefined, resolution?: ResolutionType | undefined, rotation?: (number | null) | undefined, size?: (number | null) | undefined, status?: ('OPEN' | 'GROWING' | 'AWAITED' | 'CLOSED' | 'FAILED' | 'ARCHIVED' | 'MISSING' | 'REDISCOVERED' | 'DELETED') | undefined, storage_id?: string | undefined, time_code?: TimeCodeType | undefined, type: 'KEYFRAME_MAP' | 'KEYFRAME' | 'POSTER', upload_credentials?: {[key: string]: unknown} | undefined, upload_headers?: {[key: string]: unknown} | undefined, upload_method?: string | undefined, upload_url?: string | undefined, url?: string | undefined}; export type CreateApiV1PlaylistsPlaylistIdKeyframesArgs = {'App-ID': string, 'Auth-Token': string, playlist_id: string, body: PlaylistKeyframeCreateSchema}; export type CreateApiV1PlaylistsPlaylistIdKeyframesOptions = {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 playlistKeyframeCreateSchema = z.object({content_type: z.string().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(), playlist_id: 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(), 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()}); 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 and associate to playlist * * POST /v1/playlists/{playlist_id}/keyframes/ */ export const createApiV1PlaylistsPlaylistIdKeyframes = async (args: CreateApiV1PlaylistsPlaylistIdKeyframesArgs, options: CreateApiV1PlaylistsPlaylistIdKeyframesOptions) => { 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'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/playlists/${encodeURIComponent(String(args.playlist_id))}/keyframes/${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(playlistKeyframeCreateSchema, await res.json()) };