import {z} from 'zod' export type GetApiV4PhoneNumbersAvailableArgs = {offset?: number | undefined, limit?: number | undefined, 'sort[id]'?: ('asc' | 'desc') | undefined, 'filters[city]'?: string | undefined, 'filters[country]'?: string | undefined, 'filters[country_code]'?: string | undefined, 'filters[is_toll_free]'?: string | undefined, 'filters[npa]'?: string | undefined, 'filters[nxx]'?: string | undefined, 'filters[phone_number]'?: string | undefined, 'filters[price]'?: string | undefined, 'filters[province]'?: string | undefined, 'filters[xxxx]'?: string | undefined, 'filters[description]'?: string | undefined, 'filters[tf_pc]'?: string | undefined, mode?: ('brief' | 'full') | undefined}; export type GetApiV4PhoneNumbersAvailableOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const listResponse = z.object({filters: z.object({}).optional(), sort: z.object({}).optional(), total: z.number().int().optional(), limit: z.number().int().optional(), offset: z.number().int().optional(), items: z.array(z.object({})).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 }; /** * List Available Phone Numbers * * GET /v4/phone-numbers/available */ export const getApiV4PhoneNumbersAvailable = async (args: GetApiV4PhoneNumbersAvailableArgs, options?: GetApiV4PhoneNumbersAvailableOptions) => { const {baseUrl = "https://api.phone.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args['sort[id]'] != null) query.append("sort[id]", String(args['sort[id]'])) if (args['filters[city]'] != null) query.append("filters[city]", String(args['filters[city]'])) if (args['filters[country]'] != null) query.append("filters[country]", String(args['filters[country]'])) if (args['filters[country_code]'] != null) query.append("filters[country_code]", String(args['filters[country_code]'])) if (args['filters[is_toll_free]'] != null) query.append("filters[is_toll_free]", String(args['filters[is_toll_free]'])) if (args['filters[npa]'] != null) query.append("filters[npa]", String(args['filters[npa]'])) if (args['filters[nxx]'] != null) query.append("filters[nxx]", String(args['filters[nxx]'])) if (args['filters[phone_number]'] != null) query.append("filters[phone_number]", String(args['filters[phone_number]'])) if (args['filters[price]'] != null) query.append("filters[price]", String(args['filters[price]'])) if (args['filters[province]'] != null) query.append("filters[province]", String(args['filters[province]'])) if (args['filters[xxxx]'] != null) query.append("filters[xxxx]", String(args['filters[xxxx]'])) if (args['filters[description]'] != null) query.append("filters[description]", String(args['filters[description]'])) if (args['filters[tf_pc]'] != null) query.append("filters[tf_pc]", String(args['filters[tf_pc]'])) if (args.mode != null) query.append("mode", String(args.mode)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v4/phone-numbers/available${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(listResponse, await res.json()) };