import {z} from 'zod' export type GetApiWhatsappPhoneNumbersArgs = {phone_number_id?: string | undefined, business_account_id?: string | undefined, customer_id?: string | undefined, messaging_enabled?: boolean | undefined, name_contains?: string | undefined, created_after?: string | undefined, created_before?: string | undefined, per_page?: number | undefined, page?: number | undefined}; export type GetApiWhatsappPhoneNumbersOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const whatsappPhoneNumber = z.object({id: z.string(), internal_id: z.string(), phone_number_id: z.string(), name: z.string(), business_account_id: z.string().nullable().optional(), is_coexistence: z.boolean().nullable().optional(), inbound_processing_enabled: z.boolean().optional(), calls_enabled: z.boolean().optional(), webhook_verified_at: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string(), customer_id: z.string().nullable().optional(), display_name: z.string().nullable().optional(), display_phone_number: z.string().nullable().optional(), display_phone_number_normalized: z.string().nullable().optional(), verified_name: z.string().nullable().optional(), quality_rating: z.string().nullable().optional(), code_verification_status: z.string().nullable().optional(), name_status: z.string().nullable().optional(), status: z.string().nullable().optional(), throughput_tier: z.string().nullable().optional(), whatsapp_business_manager_messaging_limit: z.union([z.number().int(), z.string()]).nullable().optional(), is_official_business_account: z.boolean().nullable().optional(), is_pin_enabled: z.boolean().nullable().optional()}); export const paginationMeta = z.object({page: z.number().int().gte(1), per_page: z.number().int().gte(1), total_pages: z.number().int().gte(0), total_count: z.number().int().gte(0)}); export const whatsappPhoneNumberListResponse = z.object({data: z.array(whatsappPhoneNumber), meta: paginationMeta}); 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 phone numbers * * GET /whatsapp/phone_numbers */ export const getApiWhatsappPhoneNumbers = async (args: GetApiWhatsappPhoneNumbersArgs, options?: GetApiWhatsappPhoneNumbersOptions) => { const {baseUrl = "https://api.kapso.ai/platform/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.phone_number_id != null) query.append("phone_number_id", String(args.phone_number_id)) if (args.business_account_id != null) query.append("business_account_id", String(args.business_account_id)) if (args.customer_id != null) query.append("customer_id", String(args.customer_id)) if (args.messaging_enabled != null) query.append("messaging_enabled", String(args.messaging_enabled)) if (args.name_contains != null) query.append("name_contains", String(args.name_contains)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.page != null) query.append("page", String(args.page)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/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(whatsappPhoneNumberListResponse, await res.json()) };