import {z} from 'zod' export type GetApiV1AudioVoicesArgs = {limit?: number | undefined, offset?: number | undefined, type?: ('all' | 'custom' | 'preset') | undefined}; export type GetApiV1AudioVoicesOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const voiceKind = z.enum(["ivc", "pvc"]); export const voiceResponse = z.object({name: z.string(), slug: z.string().nullable().optional(), languages: z.array(z.string()).optional(), gender: z.string().nullable().optional(), age: z.number().int().nullable().optional(), tags: z.array(z.string()).nullable().optional(), color: z.string().nullable().optional(), description: z.string().max(500).nullable().optional(), retention_notice: z.number().int().optional(), id: z.string(), created_at: z.string(), user_id: z.string().nullable(), trimmed_seconds: z.number().nullable().optional(), kind: voiceKind.optional(), type: z.enum(["preset", "custom"])}); export const voiceListResponse = z.object({items: z.array(voiceResponse), total: z.number().int(), page: z.number().int(), page_size: z.number().int(), total_pages: 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 all voices * * GET /v1/audio/voices */ export const getApiV1AudioVoices = async (args: GetApiV1AudioVoicesArgs, options?: GetApiV1AudioVoicesOptions) => { const {baseUrl = "https://api.mistral.ai", fetch: fetchFn = fetch, ApiKey} = 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.offset != null) query.append("offset", String(args.offset)) if (args.type != null) query.append("type", String(args.type)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/audio/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()) };