import {z} from 'zod' export type GetApiV2PartitionPartitionIdAccountAccountIdDynamicInventoryTnSearchArgs = {accountId: string, npa?: string | undefined, 'npa-nxx'?: string | undefined, partitionId: string, qty?: number | undefined, sequential?: boolean | undefined}; export type GetApiV2PartitionPartitionIdAccountAccountIdDynamicInventoryTnSearchOptions = {baseUrl?: string | undefined, XAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const dynamicInventoryOrderTelephoneNumberX = z.object({tn: z.string().optional(), id: z.string().optional()}); export const dynamicInventoryX = z.object({carrierId: z.string().optional(), orderId: z.string().optional(), tns: z.array(dynamicInventoryOrderTelephoneNumberX).optional(), id: z.string().optional()}); export const getApiV2PartitionPartitionIdAccountAccountIdDynamicInventoryTnSearchResponse = z.array(dynamicInventoryX); 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 }; /** * Retrieve telephone number list matching query parameters from carrier's dynamic inventory * * GET /v2/partition/{partitionId}/account/{accountId}/dynamic-inventory/tn-search */ export const getApiV2PartitionPartitionIdAccountAccountIdDynamicInventoryTnSearch = async (args: GetApiV2PartitionPartitionIdAccountAccountIdDynamicInventoryTnSearchArgs, options?: GetApiV2PartitionPartitionIdAccountAccountIdDynamicInventoryTnSearchOptions) => { 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.npa != null) query.append("npa", String(args.npa)) if (args['npa-nxx'] != null) query.append("npa-nxx", String(args['npa-nxx'])) if (args.qty != null) query.append("qty", String(args.qty)) if (args.sequential != null) query.append("sequential", String(args.sequential)) if (XAuthToken !== undefined) headers.set("X-AUTH-TOKEN", XAuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/v2/partition/${encodeURIComponent(String(args.partitionId))}/account/${encodeURIComponent(String(args.accountId))}/dynamic-inventory/tn-search${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(getApiV2PartitionPartitionIdAccountAccountIdDynamicInventoryTnSearchResponse, await res.json()) };