import {z} from 'zod' export type GetApiV2VideosVideoIdArgs = {video_id: string, 'x-api-key': string}; export type GetApiV2VideosVideoIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const mirageVideoAPIV2Output = z.object({url: z.string().min(1).max(4096), content_type: z.string(), duration_ms: z.number().int().gte(0), size_bytes: z.number().int().gte(0)}); export const mirageVideoAPIV2TerminalError = z.object({code: z.enum(["no_face_detected", "media_processing_failed", "content_policy_violation", "exceeded_quota", "rate_limit_exceeded", "internal_server_error"]), message: z.string()}); export const mirageVideoAPIV2Video = z.object({id: z.string(), object: z.literal("video").optional(), status: z.enum(["PROCESSING", "COMPLETE", "FAILED", "CANCELLED"]), model: z.literal("mirage-avatar-x-latest").optional(), aspect_ratio: z.enum(["9:16", "16:9"]).nullable().optional(), created_at: z.number().int(), completed_at: z.number().int().nullable().optional(), output: mirageVideoAPIV2Output.nullable().optional(), error: mirageVideoAPIV2TerminalError.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 }; /** * Retrieve Video (v2) * * GET /v2/videos/{video_id} */ export const getApiV2VideosVideoId = async (args: GetApiV2VideosVideoIdArgs, options: GetApiV2VideosVideoIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v2/videos/${encodeURIComponent(String(args.video_id))}${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(mirageVideoAPIV2Video, await res.json()) };