import {z} from 'zod' export type CreateVideoAuto = {mode: 'auto', prompt: string, duration_seconds?: number | undefined, avatar_id?: string | undefined, voice_id?: string | undefined, brand_kit_id?: string | undefined, style_id?: string | undefined, file_asset_ids?: Array | undefined, orientation?: ('landscape' | 'portrait') | undefined, folder_id?: string | undefined, name?: string | undefined}; export type CreateVideoManual = {mode: 'manual', model: string, prompt: string, duration_seconds?: number | undefined, resolution?: string | undefined, aspect_ratio?: string | undefined, negative_prompt?: string | undefined, seed?: number | undefined, generate_audio?: boolean | undefined, style?: string | undefined, movement_amplitude?: string | undefined, cfg_scale?: number | undefined, camera_fixed?: boolean | undefined, auto_fix?: boolean | undefined, image_url?: string | undefined, first_frame_url?: string | undefined, last_frame_url?: string | undefined, end_image_url?: string | undefined, image_urls?: Array | undefined, folder_id?: string | undefined, name?: string | undefined}; export type CreateApiV1VideosArgs = {'Idempotency-Key'?: string | undefined, body: CreateVideoAuto | CreateVideoManual}; export type CreateApiV1VideosOptions = {baseUrl?: string | undefined, ApiKeyBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const video = z.object({id: z.string().optional(), object: z.string().optional(), mode: z.enum(["auto", "manual"]).optional(), model: z.string().optional(), status: z.enum(["queued", "processing", "completed", "failed", "preview"]).optional(), name: z.string().optional(), folder_id: z.string().optional(), created_at: z.string().optional(), credits_charged: z.number().optional(), poll_interval_seconds: z.number().int().optional(), video_url: z.string().optional(), video_url_no_captions: z.string().optional(), caption_url: z.string().optional(), thumbnail_url: z.string().optional(), duration_seconds: z.number().optional(), progress: z.object({status: z.string().optional(), queue_position: z.number().int().nullable().optional(), percent: z.number().nullable().optional()}).optional(), error: z.object({code: z.string().optional(), message: z.string().optional(), credits_refunded: z.boolean().optional()}).optional(), preview: z.object({image_url: z.string().nullable().optional(), image_note: z.string().optional(), message: z.string().optional(), upgrade_url: z.string().optional()}).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 a video (async) * * POST /v1/videos */ export const createApiV1Videos = async (args: CreateApiV1VideosArgs, options?: CreateApiV1VideosOptions) => { const {baseUrl = "https://api.vivideo.ai", fetch: fetchFn = fetch, ApiKeyBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (ApiKeyBearer !== undefined) headers.set('authorization', `Bearer ${ApiKeyBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/videos${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(video, await res.json()) };