import {z} from 'zod' export type WhatsappPhoneNumberCreateRequest = {whatsapp_phone_number: {name: string, kind?: ('production' | 'sandbox') | undefined, phone_number_id: string, business_account_id: string, access_token: string, webhook_verify_token?: (string | null) | undefined, webhook_destination_url?: (string | null) | undefined, inbound_processing_enabled?: boolean | undefined, calls_enabled?: boolean | undefined}}; export type CreateApiCustomersCustomerIdWhatsappPhoneNumbersArgs = {body: WhatsappPhoneNumberCreateRequest}; export type CreateApiCustomersCustomerIdWhatsappPhoneNumbersOptions = {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 whatsappPhoneNumberResponse = z.object({data: whatsappPhoneNumber}); 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 }; /** * Connect phone number * * POST /customers/{customer_id}/whatsapp/phone_numbers */ export const createApiCustomersCustomerIdWhatsappPhoneNumbers = async (args: CreateApiCustomersCustomerIdWhatsappPhoneNumbersArgs, options?: CreateApiCustomersCustomerIdWhatsappPhoneNumbersOptions) => { const {baseUrl = "https://api.kapso.ai/platform/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/customers/${encodeURIComponent(String(args.customer_id))}/whatsapp/phone_numbers${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(whatsappPhoneNumberResponse, await res.json()) };