import {z} from 'zod' export type UpdateVoiceRequest = {name?: (string | null) | undefined, description?: (string | null) | undefined, gender?: (string | null) | undefined, starred?: (boolean | null) | undefined, tags?: (Array | null) | undefined}; export type UpdateApiV1AiVoiceIdArgs = {id: string, 'x-api-key'?: string | undefined, 'lmnt-auth0'?: (string | null) | undefined, body: UpdateVoiceRequest}; export type UpdateApiV1AiVoiceIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const voice = z.object({id: z.string(), name: z.string(), state: z.enum(["preparing", "pending", "training", "ready", "broken", "cancelled"]), owner: z.enum(["system", "me", "all"]), type: z.enum(["instant", "professional", "intrinsic"]), description: z.string().nullable().optional(), gender: z.string().nullable().optional(), starred: z.boolean().nullable().optional(), tags: z.array(z.string()).nullable().optional(), preview_url: z.string().min(1).nullable().optional()}); export const updateVoiceResponse = z.object({voice: voice}); 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 }; /** * Update Handler * * PUT /v1/ai/voice/{id} */ export const updateApiV1AiVoiceId = async (args: UpdateApiV1AiVoiceIdArgs, options: UpdateApiV1AiVoiceIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() const cookies: string[] = [] headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) if (args['lmnt-auth0'] != null) cookies.push("lmnt-auth0=" + encodeURIComponent(String(args['lmnt-auth0']))) if (cookies.length > 0) headers.set('cookie', cookies.join('; ')) const url = `${baseUrl.replace(/\/+$/, '')}/v1/ai/voice/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(updateVoiceResponse, await res.json()) };