import {z} from 'zod' export type GetApiV1VideosArgs = {folder_id?: string | undefined, status?: ('queued' | 'processing' | 'completed' | 'failed' | 'preview') | undefined, limit?: number | undefined}; export type GetApiV1VideosOptions = {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 getApiV1VideosResponse = z.object({object: z.string().optional(), data: z.array(video).optional(), count: z.number().int().optional(), total: z.number().int().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 }; /** * List videos * * GET /v1/videos */ export const getApiV1Videos = async (args: GetApiV1VideosArgs, options?: GetApiV1VideosOptions) => { const {baseUrl = "https://api.vivideo.ai", fetch: fetchFn = fetch, ApiKeyBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.folder_id != null) query.append("folder_id", String(args.folder_id)) if (args.status != null) query.append("status", String(args.status)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKeyBearer !== undefined) headers.set('authorization', `Bearer ${ApiKeyBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/videos${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(getApiV1VideosResponse, await res.json()) };