import {z} from 'zod' export type Filter = {attribute: string, values?: Array | undefined}; export type GetMetricDataInputCsidFilter = {id: string, csidFilter?: Array | undefined, filters?: Array | undefined, aggregations?: Array | undefined, timezone: string}; export type Interval = {start: string, end: string}; export type Preset = {value: string}; export type PeriodFilter = Interval | Preset; export type GetMetricDataInputPeriodFilter = {id: string, periodFilter: PeriodFilter, filters?: Array | undefined, aggregations?: Array | undefined, timezone: string}; export type GetMetricDataInput = GetMetricDataInputCsidFilter | GetMetricDataInputPeriodFilter; export type CreateApiBetaAnalyticsMetricsArgs = {body: GetMetricDataInput}; export type CreateApiBetaAnalyticsMetricsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const status = z.object({status: z.string(), description: z.string().optional()}); export const presenceDuration = z.object({value: z.number(), status: status}); export const agentPresenceDurationValue = z.object({value: z.array(presenceDuration).optional(), description: z.string().optional()}); export const doubleAggregateValue = z.object({value: z.number(), measure: z.string()}); export const longAggregateValue = z.object({value: z.number().int(), measure: z.string()}); export const aggregateValue = z.union([agentPresenceDurationValue, doubleAggregateValue, longAggregateValue]); export const metricData = z.object({id: z.string(), aggregates: z.array(aggregateValue).optional()}); export const getMetricDataOutput = z.object({data: metricData}); 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 }; /** * Get Metric Data * * POST /beta/analytics/metrics */ export const createApiBetaAnalyticsMetrics = async (args: CreateApiBetaAnalyticsMetricsArgs, options?: CreateApiBetaAnalyticsMetricsOptions) => { const {baseUrl = "https://dev.dixa.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/beta/analytics/metrics${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(getMetricDataOutput, await res.json()) };