import {z} from 'zod' export type CreateApiCallReportsArgs = {body: {name: string, type: 'NORMAL' | 'EXTENSION_GROUP_CALL_COST' | 'QUEUE_PERFORMANCE' | 'QUEUE_DETAILED_STATISTICS' | 'QUEUE_ABANDONED_CALLS' | 'QUEUE_SLA_STATISTICS' | 'QUEUE_BREACHES_SLA_STATISTICS' | 'QUEUE_CALLBACKS' | 'QUEUE_FAILED_CALLBACKS' | 'TEAM_QUEUE_GENERAL_STATISTICS' | 'RING_GROUP_STATISTICS', email: string, format: 'CSV' | 'HTML', range: 'TODAY' | 'YESTERDAY' | 'LAST_WEEK' | 'LAST_SEVEN_DAYS' | 'LAST_MONTH' | 'LAST_THIRTY_DAYS' | 'CUSTOM', started_at?: unknown | undefined, ended_at?: unknown | undefined, cron_expr?: string | undefined, filter: {from?: ('ANY' | 'EXTENSION_OR_EXTENSION_RANGE' | 'NUMBER' | 'NUMBERS_START_WITH' | 'NUMBERS_CONTAIN' | 'INTERNAL_EXTENSIONS' | 'EXTERNAL_NUMBERS') | undefined, from_value?: string | undefined, to?: ('ANY' | 'EXTENSION_OR_EXTENSION_RANGE' | 'NUMBER' | 'NUMBERS_START_WITH' | 'NUMBERS_CONTAIN' | 'INTERNAL_EXTENSIONS' | 'EXTERNAL_NUMBERS') | undefined, to_value?: string | undefined, answer_state?: ('ANY' | 'ANSWERED' | 'NOT_ANSWERED') | undefined, duration?: ('ANY' | 'RING_DURATION' | 'TALK_DURATION') | undefined, duration_from?: number | undefined, duration_to?: number | undefined} | {extension?: ('ANY' | 'EXTENSION_OR_EXTENSION_RANGE') | undefined, extension_value?: string | undefined, type?: ('ANY' | 'INTERNAL' | 'EXTERNAL') | undefined} | {type?: ('ANY' | 'LOCAL' | 'NATIONAL' | 'INTERNATIONAL' | 'MOBILE') | undefined, groups?: Array | undefined} | {enable_exclude_calls_dropped_before_seconds?: boolean | undefined, exclude_calls_dropped_before_seconds?: number | undefined, call_queues?: Array | undefined} | {enable_exclude_calls_dropped_before_seconds?: boolean | undefined, exclude_calls_dropped_before_seconds?: number | undefined, call_queues?: Array | undefined} | {enable_exclude_calls_dropped_before_seconds?: boolean | undefined, exclude_calls_dropped_before_seconds?: number | undefined, call_queues?: Array | undefined} | {enable_exclude_calls_dropped_before_seconds?: boolean | undefined, exclude_calls_dropped_before_seconds?: number | undefined, call_queues?: Array | undefined} | {enable_exclude_calls_dropped_before_seconds?: boolean | undefined, exclude_calls_dropped_before_seconds?: number | undefined, call_queues?: Array | undefined} | {enable_exclude_calls_dropped_before_seconds?: boolean | undefined, exclude_calls_dropped_before_seconds?: number | undefined, call_queues?: Array | undefined} | {call_queues?: Array | undefined} | {enable_exclude_calls_dropped_before_seconds?: boolean | undefined, exclude_calls_dropped_before_seconds?: number | undefined, call_queues?: Array | undefined} | {ring_groups?: Array | undefined}}}; export type CreateApiCallReportsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCallReportsResponse = z.object({id: z.string().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 }; /** * Create new call report * * POST /call_reports */ export const createApiCallReports = async (args: CreateApiCallReportsArgs, options?: CreateApiCallReportsOptions) => { const {baseUrl = "https://HOSTNAME:8887/api", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/call_reports${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiCallReportsResponse, await res.json()) };