import {z} from 'zod' export type CreateApiV1AvatarsArgs = {body: {kind: 'prompt' | 'photo', name: string, prompt?: string | undefined, file_url?: string | undefined, asset_id?: string | undefined, consent?: boolean | undefined, reference_images?: Array | undefined}}; export type CreateApiV1AvatarsOptions = {baseUrl?: string | undefined, ApiKeyBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const avatar = z.object({id: z.string().optional(), object: z.string().optional(), name: z.string().optional(), kind: z.enum(["prompt", "photo"]).optional(), status: z.string().optional(), ready: z.boolean().optional(), gender: z.string().optional(), preview_image_url: z.string().optional(), preview_video_url: z.string().optional(), looks: z.array(z.record(z.string(), z.unknown())).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 }; /** * Generate a new avatar (async) * * POST /v1/avatars */ export const createApiV1Avatars = async (args: CreateApiV1AvatarsArgs, options?: CreateApiV1AvatarsOptions) => { 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 (ApiKeyBearer !== undefined) headers.set('authorization', `Bearer ${ApiKeyBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/avatars${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(avatar, await res.json()) };