import {z} from 'zod' export type GetApiV2ListPhoneNumbersArgs = {limit?: number | undefined, sort_order?: ('ascending' | 'descending') | undefined, pagination_key?: string | undefined}; export type GetApiV2ListPhoneNumbersOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const agentVersionReference = z.union([z.string().min(1).max(20), z.number().int().gte(0)]); export const agentWeight = z.object({agent_id: z.string().min(1), agent_version: agentVersionReference.optional(), weight: z.number().gt(0).lte(1)}); export const sipCodecList = z.array(z.string().min(1)); export const phoneNumberResponse = z.object({phone_number: z.string(), phone_number_type: z.enum(["retell-twilio", "retell-telnyx", "retell-bandwidth", "custom"]), phone_number_pretty: z.string().optional(), allowed_inbound_country_list: z.array(z.string()).nullable().optional(), allowed_outbound_country_list: z.array(z.string()).nullable().optional(), area_code: z.number().int().optional(), inbound_agents: z.array(agentWeight).nullable().optional(), outbound_agents: z.array(agentWeight).nullable().optional(), inbound_sms_agents: z.array(agentWeight).nullable().optional(), outbound_sms_agents: z.array(agentWeight).nullable().optional(), nickname: z.string().nullable().optional(), inbound_webhook_url: z.string().nullable().optional(), inbound_sms_webhook_url: z.string().nullable().optional(), last_modification_timestamp: z.number().int(), sip_outbound_trunk_config: z.object({termination_uri: z.string().nullable().optional(), auth_username: z.string().nullable().optional(), transport: z.string().nullable().optional(), codecs: sipCodecList.optional()}).nullable().optional(), fallback_number: z.string().nullable().optional(), inbound_fallback_enabled: z.boolean().optional()}); export const getApiV2ListPhoneNumbersResponse = z.object({pagination_key: z.string().optional(), has_more: z.boolean(), items: 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 phone numbers with pagination * * GET /v2/list-phone-numbers */ export const getApiV2ListPhoneNumbers = async (args: GetApiV2ListPhoneNumbersArgs, options?: GetApiV2ListPhoneNumbersOptions) => { const {baseUrl = "https://api.retellai.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.pagination_key != null) query.append("pagination_key", String(args.pagination_key)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/list-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(getApiV2ListPhoneNumbersResponse, await res.json()) };