import {z} from 'zod' export type VideoImageUrl = {url: string}; export type VideoFrameImage = {type: string, image_url: VideoImageUrl, frame_type: string}; export type VideoInputReference = {type: string, image_url?: ({url: string} | null) | undefined, video_url?: ({url: string} | null) | undefined}; export type VideosGenerationsRequest = {model: string, prompt: string, duration?: (number | null) | undefined, resolution?: (string | null) | undefined, aspect_ratio?: (string | null) | undefined, frame_images?: (Array | null) | undefined, input_references?: (Array | null) | undefined, generate_audio?: (boolean | null) | undefined, seed?: (number | null) | undefined}; export type CreateApiV1VideosArgs = {body: VideosGenerationsRequest}; export type CreateApiV1VideosOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const videoJob = z.object({id: z.string(), object: z.string(), model: z.string(), status: z.string(), polling_url: z.string(), created_at: z.number().int(), completed_at: z.number().int().nullable().optional(), duration: z.number().int(), resolution: z.string(), aspect_ratio: z.string(), urls: z.array(z.string()).nullable().optional(), usage: z.object({cost: z.string(), seconds: z.number().int()}).nullable().optional(), error: z.object({message: z.string(), type: z.string()}).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 }; /** * Create a video * * POST /v1/videos * * Also accepts auth: xApiKeyAuth */ export const createApiV1Videos = async (args: CreateApiV1VideosArgs, options?: CreateApiV1VideosOptions) => { const {baseUrl = "https://api.naga.ac", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) 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(videoJob, await res.json()) };