import {z} from 'zod' export type GetApiVideoModelsOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const apiVideoMode = z.enum(["text", "image", "frames", "references", "video_to_video"]); export const apiResolution = z.enum(["480p", "720p", "1080p", "2k", "4k"]); export const apiVideoResolutionOption = z.object({resolution: apiResolution, label: z.string(), credits_per_second: z.number()}); export const apiAspectRatio = z.enum(["16:9", "9:16", "1:1"]); export const apiVideoModel = z.object({id: z.string(), title: z.string(), description: z.string(), modes: z.array(apiVideoMode), resolutions: z.array(apiVideoResolutionOption), default_resolution: apiResolution, durations: z.array(z.number().int()), default_duration: z.number().int(), aspect_ratios: z.array(apiAspectRatio), supports_audio: z.boolean(), max_reference_images: z.number().int().nullable().optional(), max_reference_videos: z.number().int().nullable().optional(), output_duration_follows_source: z.boolean()}); export const listVideoModelsResponse = z.object({models: z.array(apiVideoModel)}); 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 }; /** * List video models * * GET /video/models */ export const getApiVideoModels = async (options?: GetApiVideoModelsOptions) => { const {baseUrl = "https://api.wondercraft.ai/v1", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-API-KEY", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/video/models${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(listVideoModelsResponse, await res.json()) };