import {z} from 'zod' export type GetApiV2SupportedRateCentersArgs = {busOrgId?: string | undefined, distanceUnit?: ('MILES' | 'KILOMETERS' | 'RADIANS') | undefined, latitude?: number | undefined, longitude?: number | undefined, maxResults?: number | undefined, state?: string | undefined, type?: ('LEVEL_3' | 'NO_API' | 'E911' | 'TELUS' | 'TELUS_API' | 'TELUS_ONEVOICE' | 'PROVTEL' | 'VERIZON_CARIANZA' | 'ONVOY_CARIANZA' | 'LEVEL3_CARIANZA' | 'BANDWIDTH' | 'LUMEN_IMS' | 'LUMEN_VOICE_LI_ELS' | 'SINCH' | 'SERVICE_PROVIDER_CARRIER') | undefined}; export type GetApiV2SupportedRateCentersOptions = {baseUrl?: string | undefined, XAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const telephoneNumberPrefixViewX = z.object({prefix: z.string().optional(), wireless: z.boolean().optional(), wirelessBlockNumbers: z.array(z.string()).optional()}); export const rateCenterSearchViewX = z.object({name: z.string().optional(), abbrName: z.string().optional(), reorderName: z.string().optional(), reorderNameAlias: z.string().optional(), state: z.string().optional(), country: z.string().optional(), latitude: z.number().optional(), longitude: z.number().optional(), msa: z.string().optional(), zips: z.array(z.string()).optional(), prefixes: z.array(telephoneNumberPrefixViewX).optional(), distance: z.number().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 }; /** * Get carrier-supported rate centers by proximity * * GET /v2/supported-rate-centers */ export const getApiV2SupportedRateCenters = async (args: GetApiV2SupportedRateCentersArgs, options?: GetApiV2SupportedRateCentersOptions) => { const {baseUrl = "https://api.alianza.com", fetch: fetchFn = fetch, XAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.busOrgId != null) query.append("busOrgId", String(args.busOrgId)) if (args.distanceUnit != null) query.append("distanceUnit", String(args.distanceUnit)) if (args.latitude != null) query.append("latitude", String(args.latitude)) if (args.longitude != null) query.append("longitude", String(args.longitude)) if (args.maxResults != null) query.append("maxResults", String(args.maxResults)) if (args.state != null) query.append("state", String(args.state)) if (args.type != null) query.append("type", String(args.type)) if (XAuthToken !== undefined) headers.set("X-AUTH-TOKEN", XAuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/v2/supported-rate-centers${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(rateCenterSearchViewX, await res.json()) };