import {z} from 'zod' export type VoicemailRequest = {pin?: string | undefined, enabled?: boolean | undefined, greeting?: ({mode?: ('default' | 'custom_greeting') | undefined, media_name?: (string | null) | undefined}) | undefined}; export type PatchApiPhoneNumbersPhoneNumberIdVoicemailArgs = {body: VoicemailRequest}; export type PatchApiPhoneNumbersPhoneNumberIdVoicemailOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const voicemailPrefResponse = z.object({enabled: z.boolean().optional(), pin: z.string().optional(), greeting: z.object({mode: z.enum(["default", "custom_greeting"]).optional(), media_name: z.string().nullable().optional()}).optional()}); export const patchApiPhoneNumbersPhoneNumberIdVoicemailResponse = z.object({data: voicemailPrefResponse.optional()}); 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 voicemail * * PATCH /phone_numbers/{phone_number_id}/voicemail */ export const patchApiPhoneNumbersPhoneNumberIdVoicemail = async (args: PatchApiPhoneNumbersPhoneNumberIdVoicemailArgs, options?: PatchApiPhoneNumbersPhoneNumberIdVoicemailOptions) => { const {baseUrl = "https://api.telnyx.com/v2", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/phone_numbers/${encodeURIComponent(String(args.phone_number_id))}/voicemail${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiPhoneNumbersPhoneNumberIdVoicemailResponse, await res.json()) };