import {z} from 'zod' export type GetApiV1AccountsAccountIdVoiceLibraryArgs = {language?: string | undefined, gender?: ('male' | 'female' | 'neutral') | undefined, provider?: string | undefined, isPolyVoice?: boolean | undefined}; export type GetApiV1AccountsAccountIdVoiceLibraryOptions = {baseUrl?: string | undefined, polyApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const voiceResponse = z.object({id: z.string(), name: z.string(), provider: z.string(), providerVoiceId: z.string(), config: z.object({}), metadata: z.object({}).optional(), isPolyVoice: z.boolean().optional()}); export const listVoicesResponse = z.object({voices: z.array(voiceResponse)}); 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 all voices in the account's voice library * * GET /v1/accounts/{accountId}/voice-library */ export const getApiV1AccountsAccountIdVoiceLibrary = async (args: GetApiV1AccountsAccountIdVoiceLibraryArgs, options?: GetApiV1AccountsAccountIdVoiceLibraryOptions) => { const {baseUrl = "https://api.us.poly.ai", fetch: fetchFn = fetch, polyApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.language != null) query.append("language", String(args.language)) if (args.gender != null) query.append("gender", String(args.gender)) if (args.provider != null) query.append("provider", String(args.provider)) if (args.isPolyVoice != null) query.append("isPolyVoice", String(args.isPolyVoice)) if (polyApiKey !== undefined) headers.set("X-API-KEY", polyApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.accountId))}/voice-library${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(listVoicesResponse, await res.json()) };