import {z} from 'zod' export type GetApiV2VoiceReportsReportIdArgs = {reportId: string}; export type GetApiV2VoiceReportsReportIdOptions = {baseUrl?: string | undefined, accountSidAuthToken?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const reportStatus = z.enum(["created", "running", "completed"]); export const reportFilter = z.object({key: z.string().optional(), values: z.array(z.string()).optional()}); export const reportMetadata = z.object({start_datetime: z.string().optional(), end_datetime: z.string().optional(), filters: z.array(reportFilter).optional()}); 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 brandedUseCaseDetail = z.object({use_case: z.string().optional(), enabled_phonenumbers: z.number().int().optional(), total_calls: z.number().int().optional(), answer_rate: z.number().optional(), human_answer_rate: z.number().optional(), engagement_rate: z.number().optional()}); export const brandedCalling = z.object({total_branded_calls: z.number().int().optional(), percent_branded_calls: z.number().optional(), answer_rate: z.number().optional(), human_answer_rate: z.number().optional(), engagement_rate: z.number().optional(), by_use_case: z.array(brandedUseCaseDetail).optional()}); export const voiceIntegrityCallsPerBundle = z.object({bundle_sid: z.string().optional(), enabled_phonenumbers: z.number().int().optional(), total_calls: z.number().int().optional(), answer_rate: z.number().optional(), human_answer_rate: z.number().optional()}); export const voiceIntegrity = z.object({enabled_calls: z.number().int().optional(), enabled_percentage: z.number().optional(), calls_per_bundle: z.array(voiceIntegrityCallsPerBundle).optional()}); export const stirShaken = z.object({call_count: z.object({stsh_a: z.number().int().optional(), stsh_b: z.number().int().optional(), stsh_c: z.number().int().optional()}).optional(), percentage: z.object({stsh_a: z.number().optional(), stsh_b: z.number().optional(), stsh_c: z.number().optional()}).optional(), answer_rate: z.object({stsh_a: z.number().optional(), stsh_b: z.number().optional(), stsh_c: z.number().optional()}).optional()}); export const accountReport = z.object({call_deliverability_score: z.number().optional(), call_answer_score: z.number().optional(), total_calls: z.number().int().optional(), call_direction: z.object({outbound: z.number().int().optional(), inbound: z.number().int().optional()}).optional(), call_state: z.object({completed: z.number().int().optional(), fail: z.number().int().optional(), busy: z.number().int().optional(), noanswer: z.number().int().optional(), canceled: z.number().int().optional()}).optional(), call_type: z.object({carrier: z.number().int().optional(), sip: z.number().int().optional(), trunking: z.number().int().optional(), client: z.number().int().optional(), whatsapp: z.number().int().optional()}).optional(), aloc: z.number().optional(), twilio_edge_location: z.record(z.string(), z.number().int()).optional(), caller_country_code: z.record(z.string(), z.number().int()).optional(), callee_country_code: z.record(z.string(), z.number().int()).optional(), average_queue_time_ms: z.number().optional(), silent_calls_percentage: z.number().optional(), network_issues: z.object({sdk: z.object({ice_failures_percentage: z.number().optional(), high_latency_percentage: z.number().optional(), high_packet_loss_percentage: z.number().optional(), high_jitter_percentage: z.number().optional()}).optional(), twilio_gateway: z.object({high_latency_percentage: z.number().optional(), high_packet_loss_percentage: z.number().optional(), high_jitter_percentage: z.number().optional()}).optional()}).optional(), KYT: z.object({outbound_carrier_calling: z.object({unique_calling_numbers: z.number().int().optional(), unique_called_numbers: z.number().int().optional(), blocked_calls_by_carrier: z.array(countyCarrierValue).optional(), short_duration_calls_percentage: z.number().optional(), long_duration_calls_percentage: z.number().optional(), potential_robocalls_percentage: z.number().optional(), branded_calling: brandedCalling.optional(), voice_integrity: voiceIntegrity.optional(), stir_shaken: stirShaken.optional()}).optional()}).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 insightsV2AccountReport = z.object({account_sid: z.string().min(34).max(34).optional(), report_id: z.string().optional(), status: reportStatus.optional(), request_meta: reportMetadata.optional(), report: accountReport.optional(), url: z.string().nullable().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 }; /** * Fetch Voice Insights Account Level Report * * GET /v2/Voice/Reports/{reportId} */ export const getApiV2VoiceReportsReportId = async (args: GetApiV2VoiceReportsReportIdArgs, options?: GetApiV2VoiceReportsReportIdOptions) => { 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 (accountSidAuthToken !== undefined) headers.set('authorization', `Basic ${btoa(`${accountSidAuthToken.username}:${accountSidAuthToken.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/Voice/Reports/${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(insightsV2AccountReport, await res.json()) };