import {z} from 'zod' export type GetApiV1WhatsappPhoneNumbersAvailabilityArgs = {country: string, numberType?: ('local' | 'mobile' | 'national' | 'toll_free') | undefined, sms?: boolean | undefined}; export type GetApiV1WhatsappPhoneNumbersAvailabilityOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1WhatsappPhoneNumbersAvailabilityResponse = z.object({country: z.string().optional(), numberType: z.string().optional(), available: z.boolean().optional(), preOrderable: z.boolean().optional(), addressConstraint: z.enum(["geo", "country", "none"]).optional(), areas: z.array(z.string()).optional(), areaOptions: z.array(z.object({ndc: z.string().optional(), name: z.string().optional(), count: z.number().int().optional()})).optional(), soldOutAreas: z.array(z.object({ndc: z.string().optional(), name: z.string().optional(), preOrderable: z.boolean().optional()})).optional(), areaAvailability: z.object({inStock: z.array(z.object({ndc: z.string().optional(), name: z.string().optional(), count: z.number().int().optional(), ndcs: z.array(z.string()).optional(), aliases: z.array(z.string()).optional()})).optional(), preOrder: z.array(z.object({ndc: z.string().optional(), name: z.string().optional(), ndcs: z.array(z.string()).optional(), aliases: z.array(z.string()).optional()})).optional(), outOfStock: z.array(z.object({ndc: z.string().optional(), name: z.string().optional(), listed: z.number().int().optional(), ndcs: z.array(z.string()).optional(), aliases: z.array(z.string()).optional()})).optional()}).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 }; /** * Check country availability * * GET /v1/whatsapp/phone-numbers/availability */ export const getApiV1WhatsappPhoneNumbersAvailability = async (args: GetApiV1WhatsappPhoneNumbersAvailabilityArgs, options?: GetApiV1WhatsappPhoneNumbersAvailabilityOptions) => { const {baseUrl = "https://zernio.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.country != null) query.append("country", String(args.country)) if (args.numberType != null) query.append("numberType", String(args.numberType)) if (args.sms != null) query.append("sms", String(args.sms)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/whatsapp/phone-numbers/availability${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(getApiV1WhatsappPhoneNumbersAvailabilityResponse, await res.json()) };