import {z} from 'zod' export type GetApiPhoneNumbersJobsArgs = {page?: ({size?: number | undefined, number?: number | undefined}) | undefined, sort?: 'created_at' | undefined, filter?: ({type?: ('update_emergency_settings' | 'delete_phone_numbers' | 'update_phone_numbers') | undefined, customer_reference?: string | undefined, phone_number?: (string | Array) | undefined, status?: (Array<'pending' | 'in_progress' | 'completed' | 'failed' | 'expired'>) | undefined}) | undefined}; export type GetApiPhoneNumbersJobsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const phoneNumbersJobPhoneNumber = z.object({phone_number: z.string().optional(), id: z.string().optional()}); export const phoneNumbersJobSuccessfulOperation = z.object({phone_number: z.string().optional(), id: z.string().optional()}); export const phoneNumbersJobPendingOperation = z.object({phone_number: z.string().optional(), id: z.string().optional()}); export const numbersError73c9788e53 = z.object({code: z.string().optional(), title: z.string().optional(), detail: z.string().optional(), source: z.object({pointer: z.string().optional(), parameter: z.string().optional()}).optional(), meta: z.object({url: z.string().optional()}).optional()}); export const numbersPhoneNumbersJobFailedOperation = z.object({phone_number: z.string().optional(), id: z.string().optional(), errors: z.array(numbersError73c9788e53).optional()}); export const numbersPhoneNumbersJob = z.object({parameters: z.object({phone_numbers: z.array(z.union([z.string(), z.number().int()])).optional(), external_pin: z.string().nullable().optional(), connection_id: z.string().nullable().optional(), customer_reference: z.string().nullable().optional(), billing_group_id: z.string().nullable().optional(), hd_voice_enabled: z.boolean().nullable().optional(), deletion_lock_enabled: z.boolean().nullable().optional(), tags: z.array(z.string()).nullable().optional(), add_tags: z.array(z.string()).nullable().optional(), remove_tags: z.array(z.string()).nullable().optional(), voice: z.object({tech_prefix_enabled: z.boolean().nullable().optional(), caller_id_name_enabled: z.boolean().nullable().optional(), usage_payment_method: z.string().nullable().optional(), inbound_call_screening: z.string().nullable().optional(), call_forwarding: z.object({forwards_to: z.string().nullable().optional(), forwarding_type: z.string().nullable().optional(), call_forwarding_enabled: z.boolean().nullable().optional()}).optional(), call_recording: z.object({inbound_call_recording_enabled: z.boolean().nullable().optional(), inbound_call_recording_channels: z.string().nullable().optional(), inbound_call_recording_format: z.string().nullable().optional(), record_continue_on_hold: z.boolean().nullable().optional()}).optional(), media_features: z.object({rtp_auto_adjust_enabled: z.boolean().nullable().optional(), accept_any_rtp_packets_enabled: z.boolean().nullable().optional(), t38_fax_gateway_enabled: z.boolean().nullable().optional()}).optional(), cnam_listing: z.object({cnam_listing_enabled: z.boolean().nullable().optional(), cnam_listing_details: z.string().nullable().optional()}).optional()}).optional(), emergency_enabled: z.boolean().nullable().optional(), emergency_address_id: z.string().nullable().optional()}).optional(), id: z.string().optional(), record_type: z.string().optional(), status: z.enum(["pending", "in_progress", "completed", "failed", "expired"]).optional(), type: z.enum(["update_emergency_settings", "delete_phone_numbers", "update_phone_numbers"]).optional(), etc: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), phone_numbers: z.array(phoneNumbersJobPhoneNumber).optional(), successful_operations: z.array(phoneNumbersJobSuccessfulOperation).optional(), pending_operations: z.array(phoneNumbersJobPendingOperation).optional(), failed_operations: z.array(numbersPhoneNumbersJobFailedOperation).optional()}); export const paginationMeta = z.object({total_pages: z.number().int().optional(), total_results: z.number().int().optional(), page_number: z.number().int().optional(), page_size: z.number().int().optional()}); export const getApiPhoneNumbersJobsResponse = z.object({data: z.array(numbersPhoneNumbersJob).optional(), meta: paginationMeta.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 }; /** * Lists the phone numbers jobs * * GET /phone_numbers/jobs */ export const getApiPhoneNumbersJobs = async (args: GetApiPhoneNumbersJobsArgs, options?: GetApiPhoneNumbersJobsOptions) => { const {baseUrl = "https://api.telnyx.com/v2", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", JSON.stringify(args.page)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.filter != null) query.append("filter", JSON.stringify(args.filter)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/phone_numbers/jobs${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(getApiPhoneNumbersJobsResponse, await res.json()) };