import {z} from 'zod' export type GetApiV1AvatarsArgs = {limit?: number | undefined, after?: (string | null) | undefined, 'x-api-key': string}; export type GetApiV1AvatarsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const publicPairedVoice = z.object({id: z.string(), object: z.literal("voice").optional(), name: z.string(), source: z.enum(["stock", "custom"]), preview_url: z.string().min(1).max(2083).nullable().optional()}); export const publicAvatar = z.object({id: z.string(), object: z.literal("avatar").optional(), name: z.string(), description: z.string().nullable().optional(), source: z.enum(["stock", "custom"]), thumbnail_url: z.string().min(1).max(2083), preview_url: z.string().min(1).max(2083).nullable().optional(), aspect_ratio: z.enum(["9:16", "16:9"]), paired_voice: publicPairedVoice.nullable().optional()}); export const catalogPagePublicAvatar = z.object({data: z.array(publicAvatar), has_more: z.boolean(), next_cursor: 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 Avatars * * GET /v1/avatars */ export const getApiV1Avatars = async (args: GetApiV1AvatarsArgs, options: GetApiV1AvatarsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.after != null) query.append("after", String(args.after)) if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/avatars${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(catalogPagePublicAvatar, await res.json()) };