import {z} from 'zod' export type GetApiV2WhatsappPhoneNumbersArgs = {page?: ({number?: number | undefined, size?: number | undefined}) | undefined}; export type GetApiV2WhatsappPhoneNumbersOptions = {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 messagingPaginationMeta0b38e7044b = z.object({total_pages: z.number().int(), total_results: z.number().int(), page_number: z.number().int(), page_size: z.number().int()}); export const getApiV2WhatsappPhoneNumbersResponse = z.object({data: z.array(whatsappPhoneResponse).optional(), meta: messagingPaginationMeta0b38e7044b.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 }; /** * List Whatsapp phone numbers * * GET /v2/whatsapp/phone_numbers */ export const getApiV2WhatsappPhoneNumbers = async (args: GetApiV2WhatsappPhoneNumbersArgs, options?: GetApiV2WhatsappPhoneNumbersOptions) => { 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 (args.page != null) query.append("page", JSON.stringify(args.page)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/whatsapp/phone_numbers${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(getApiV2WhatsappPhoneNumbersResponse, await res.json()) };