import {z} from 'zod' export type PhoneNumberStatus = 'submitted' | 'in_review' | 'verified' | 'unsuccessful' | 'suspended' | 'expired' | 'permanently_rejected'; export type GetApiDirDirIdPhoneNumberBatchesArgs = {dir_id: string, 'page[number]'?: number | undefined, 'page[size]'?: number | undefined, 'filter[status]'?: PhoneNumberStatus | undefined}; export type GetApiDirDirIdPhoneNumberBatchesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const phoneNumberStatus = z.enum(["submitted", "in_review", "verified", "unsuccessful", "suspended", "expired", "permanently_rejected"]); export const document = z.object({document_id: z.string(), document_type: z.enum(["letter_of_authorization", "business_registration", "articles_of_incorporation", "certificate_of_good_standing", "tax_document", "ein_letter", "trademark_registration", "website_ownership", "business_license", "professional_license", "government_id", "utility_bill", "bank_statement", "business_credit_report", "bank_reference_letter", "other"]), description: z.string().max(255).optional()}); export const rejectionReason = z.object({code: z.string().optional(), title: z.string().optional(), detail: z.string().optional(), message: z.string().nullable().optional()}); export const dirPhoneNumber = z.object({id: z.string().optional(), dir_id: z.string().optional(), enterprise_id: z.string().optional(), phone_number: z.string().optional(), batch_id: z.string().nullable().optional(), loa_document_id: z.string().nullable().optional(), status: phoneNumberStatus.optional(), rejection_reason: rejectionReason.optional(), created_at: z.string().optional(), updated_at: z.string().optional(), verified_at: z.string().nullable().optional()}); export const phoneNumberBatch = z.object({batch_id: z.string().optional(), dir_id: z.string().optional(), dir_display_name: z.string().optional(), enterprise_id: z.string().optional(), status: phoneNumberStatus.optional(), total_count: z.number().int().optional(), submitted_at: z.string().optional(), documents: z.array(document).optional(), phone_numbers: z.array(dirPhoneNumber).optional()}); export const brandedCallingPaginationMeta = z.object({total_pages: z.number().int(), total_results: z.number().int(), page_number: z.number().int(), page_size: z.number().int()}); export const phoneNumberBatchList = z.object({data: z.array(phoneNumberBatch), meta: brandedCallingPaginationMeta}); 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-number batches for a DIR * * GET /dir/{dir_id}/phone_number_batches */ export const getApiDirDirIdPhoneNumberBatches = async (args: GetApiDirDirIdPhoneNumberBatchesArgs, options?: GetApiDirDirIdPhoneNumberBatchesOptions) => { 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[number]'] != null) query.append("page[number]", String(args['page[number]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['filter[status]'] != null) query.append("filter[status]", String(args['filter[status]'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/dir/${encodeURIComponent(String(args.dir_id))}/phone_number_batches${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(phoneNumberBatchList, await res.json()) };