import {z} from 'zod' export type GetApiWhatsappPhoneNumbersPhoneNumberArgs = {phone_number: string}; export type GetApiWhatsappPhoneNumbersPhoneNumberOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const whatsappPhoneResponse = z.object({record_type: z.string().optional(), phone_number: z.string().optional(), phone_number_id: z.string().optional(), waba_id: z.string().optional(), user_id: z.string().optional(), display_name: z.string().optional(), quality_rating: z.string().optional(), status: z.string().optional(), created_at: z.string().optional(), enabled: z.boolean().optional(), calling_enabled: z.boolean().optional(), is_on_biz_app: z.boolean().optional(), coexistence_state: z.enum(["pending_onboarding", "sync_pending", "syncing", "sync_complete", "active", "history_declined", "sync_deadline_expired", "offboarded", "disconnected", "null"]).nullable().optional(), sync_deadline: z.string().nullable().optional(), sync_progress: z.object({contacts_status: z.string().optional(), history_status: z.string().optional(), history_phase: z.number().int().nullable().optional(), history_chunk_order: z.number().int().nullable().optional(), history_progress: z.number().int().gte(0).lte(100).nullable().optional()}).nullable().optional()}); export const getApiWhatsappPhoneNumbersPhoneNumberResponse = z.object({data: whatsappPhoneResponse}); 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 }; /** * Retrieve a WhatsApp phone number * * GET /whatsapp/phone_numbers/{phone_number} */ export const getApiWhatsappPhoneNumbersPhoneNumber = async (args: GetApiWhatsappPhoneNumbersPhoneNumberArgs, options?: GetApiWhatsappPhoneNumbersPhoneNumberOptions) => { const {baseUrl = "https://api.telnyx.com/v2", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/whatsapp/phone_numbers/${encodeURIComponent(String(args.phone_number))}${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(getApiWhatsappPhoneNumbersPhoneNumberResponse, await res.json()) };