import {z} from 'zod' export type GetApiV2VoiceReportsPhoneNumbersOutboundReportIdArgs = {reportId: string, PageSize?: number | undefined, Page?: number | undefined, PageToken?: string | undefined}; export type GetApiV2VoiceReportsPhoneNumbersOutboundReportIdOptions = {baseUrl?: string | undefined, accountSidAuthToken?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const countyCarrierValue = z.object({country: z.string().optional(), carriers: z.array(z.object({carrier: z.string().optional(), total_calls: z.number().int().optional(), blocked_calls: z.number().int().optional(), blocked_calls_percentage: z.number().optional()})).optional()}); export const insightsV2OutboundPhoneNumberReport = z.object({handle: z.string().optional(), total_calls: z.number().int().optional(), call_answer_score: z.number().optional(), calls_by_device_type: z.record(z.string(), z.number().int()).optional(), answer_rate_device_type: z.record(z.string(), z.number()).optional(), call_state_percentage: z.object({completed: z.number().optional(), fail: z.number().optional(), busy: z.number().optional(), noanswer: z.number().optional(), canceled: z.number().optional()}).optional(), blocked_calls_by_carrier: z.array(countyCarrierValue).optional(), silent_calls_percentage: z.number().optional(), short_duration_calls_percentage: z.number().optional(), long_duration_calls_percentage: z.number().optional(), potential_robocalls_percentage: z.number().optional(), answering_machine_detection: z.object({total_calls: z.number().int().optional(), answered_by_human_percentage: z.number().optional(), answered_by_machine_percentage: z.number().optional()}).optional()}); export const getApiV2VoiceReportsPhoneNumbersOutboundReportIdResponse = z.object({reports: z.array(insightsV2OutboundPhoneNumberReport).optional(), meta: z.object({first_page_url: z.string().optional(), key: z.string().optional(), next_page_url: z.string().nullable().optional(), page: z.number().int().optional(), page_size: z.number().int().optional(), previous_page_url: z.string().nullable().optional(), url: z.string().optional()}).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 Outbound Phone Numbers Level Reports * * GET /v2/Voice/Reports/PhoneNumbers/Outbound/{reportId} */ export const getApiV2VoiceReportsPhoneNumbersOutboundReportId = async (args: GetApiV2VoiceReportsPhoneNumbersOutboundReportIdArgs, options?: GetApiV2VoiceReportsPhoneNumbersOutboundReportIdOptions) => { const {baseUrl = "https://insights.twilio.com", fetch: fetchFn = fetch, accountSidAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.PageSize != null) query.append("PageSize", String(args.PageSize)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.PageToken != null) query.append("PageToken", String(args.PageToken)) if (accountSidAuthToken !== undefined) headers.set('authorization', `Basic ${btoa(`${accountSidAuthToken.username}:${accountSidAuthToken.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/Voice/Reports/PhoneNumbers/Outbound/${encodeURIComponent(String(args.reportId))}${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(getApiV2VoiceReportsPhoneNumbersOutboundReportIdResponse, await res.json()) };