import {z} from 'zod' export type GetApiV1AvatarVideosArgs = {status?: ('pending' | 'running' | 'finalizing' | 'completed' | 'failed' | 'cancelled') | undefined, createdAfter?: string | undefined, createdBefore?: string | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiV1AvatarVideosOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const avatarVideo = z.object({id: z.string(), status: z.enum(["pending", "running", "finalizing", "completed", "failed", "cancelled"]), completionReason: z.enum(["script_completed", "max_duration_reached", "cancelled", "null"]).nullable().optional(), truncated: z.boolean(), durationSeconds: z.number().nullable().optional(), effectiveMaxDurationSeconds: z.number().int(), sessionId: z.string().nullable().optional(), region: z.enum(["eu", "us", "null"]).nullable(), zeroDataRetention: z.boolean(), content: z.object({available: z.boolean(), deleteAt: z.string().nullable(), url: z.string().optional(), expiresAt: z.string().optional()}), failure: z.object({code: z.string().optional(), message: z.string().optional(), retryable: z.boolean().optional()}).nullable().optional(), retryOf: z.string().nullable().optional(), createdAt: z.string(), startedAt: z.string().nullable().optional(), completedAt: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown())}); export const getApiV1AvatarVideosResponse = z.object({data: z.array(avatarVideo), nextCursor: z.string().nullable()}); 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 avatar videos * * GET /v1/avatar-videos */ export const getApiV1AvatarVideos = async (args: GetApiV1AvatarVideosArgs, options?: GetApiV1AvatarVideosOptions) => { const {baseUrl = "https://api.anam.ai", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.createdAfter != null) query.append("createdAfter", String(args.createdAfter)) if (args.createdBefore != null) query.append("createdBefore", String(args.createdBefore)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/avatar-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(getApiV1AvatarVideosResponse, await res.json()) };