import {z} from 'zod' export type GetApiV1CommunicationPhoneNumbersArgs = {capability?: (string | null) | undefined, status?: (string | null) | undefined}; export type GetApiV1CommunicationPhoneNumbersOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const phoneVendor = z.literal("twilio"); export const vendorMode = z.enum(["platform", "byo"]); export const phoneCapability = z.enum(["sms", "voice"]); export const phoneNumberStatus = z.enum(["active", "paused", "retired"]); export const phoneNumberResponse = z.object({id: z.string(), organization_id: z.string(), phone_e164: z.string(), vendor: phoneVendor, vendor_mode: vendorMode, capabilities: z.array(phoneCapability), vendor_metadata: z.record(z.string(), z.unknown()), status: phoneNumberStatus, retired_at: z.string().nullable().optional(), retired_by: z.string().nullable().optional(), restored_at: z.string().nullable().optional(), restored_by: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string()}); export const getApiV1CommunicationPhoneNumbersResponse = z.array(phoneNumberResponse); 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 org phone numbers * * GET /v1/communication/phone-numbers */ export const getApiV1CommunicationPhoneNumbers = async (args: GetApiV1CommunicationPhoneNumbersArgs, options: GetApiV1CommunicationPhoneNumbersOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.capability != null) query.append("capability", String(args.capability)) if (args.status != null) query.append("status", String(args.status)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/communication/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(getApiV1CommunicationPhoneNumbersResponse, await res.json()) };