import {z} from 'zod' export type GetApiV1VoiceVoicesArgs = {provider?: ('elevenlabs' | 'inworld' | null) | undefined, language?: (string | null) | undefined, is_active?: (boolean | null) | undefined}; export type GetApiV1VoiceVoicesOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const voiceOut = z.object({id: z.string(), provider: z.enum(["elevenlabs", "inworld"]), external_id: z.string(), name: z.string(), description: z.string().nullable().optional(), preview_url: z.string().nullable().optional(), language: z.string().nullable().optional(), accent: z.string().nullable().optional(), category: z.string().nullable().optional(), labels: z.record(z.string(), z.string()).optional(), supported_models: z.array(z.string()).optional(), is_active: z.boolean().optional(), created_at: z.string(), updated_at: z.string()}); export const voiceListResponse = z.object({voices: z.array(voiceOut).optional(), total: z.number().int()}); 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 Voices * * GET /v1/voice/voices */ export const getApiV1VoiceVoices = async (args: GetApiV1VoiceVoicesArgs, options: GetApiV1VoiceVoicesOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.provider != null) query.append("provider", String(args.provider)) if (args.language != null) query.append("language", String(args.language)) if (args.is_active != null) query.append("is_active", String(args.is_active)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/voice/voices${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(voiceListResponse, await res.json()) };