import {z} from 'zod' export type Filter = {attribute: string, values?: Array | undefined}; export type GetMetricRecordsDataInputCsidFilter = {id: string, csidFilter?: Array | undefined, filters?: Array | undefined, timezone: string}; export type Interval = {start: string, end: string}; export type Preset = {value: string}; export type PeriodFilter = Interval | Preset; export type GetMetricRecordsDataInputPeriodFilter = {id: string, periodFilter: PeriodFilter, filters?: Array | undefined, timezone: string}; export type GetMetricRecordsDataInput = GetMetricRecordsDataInputCsidFilter | GetMetricRecordsDataInputPeriodFilter; export type CreateApiBetaAnalyticsRecordsArgs = {pageLimit?: number | undefined, pageKey?: string | undefined, body: GetMetricRecordsDataInput}; export type CreateApiBetaAnalyticsRecordsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const timestampField = z.object({name: z.string(), timestamp: z.string()}); export const booleanField = z.object({value: z.boolean()}); export const doubleField = z.object({value: z.number()}); export const instantField = z.object({value: z.string()}); export const intField = z.object({value: z.number().int()}); export const longField = z.object({value: z.number().int()}); export const stringField = z.object({value: z.string(), label: z.string().optional()}); export const uUIDField = z.object({value: z.string(), label: z.string().optional()}); export const primitiveField = z.union([booleanField, doubleField, instantField, intField, longField, stringField, uUIDField]); export const listField = z.object({values: z.array(primitiveField).optional()}); export const fieldType = z.union([booleanField, doubleField, instantField, intField, listField, longField, stringField, timestampField, uUIDField]); export const field = z.object({name: z.string(), field: fieldType.optional()}); export const metricRecord = z.object({primaryTimestampField: timestampField, value: fieldType.optional(), fields: z.array(field).optional()}); export const paginationLinks = z.object({previous: z.string().optional(), next: z.string().optional()}); export const getMetricRecordsDataOutput = z.object({data: z.array(metricRecord).optional(), meta: paginationLinks.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 }; /** * Get Metric Records Data * * POST /beta/analytics/records */ export const createApiBetaAnalyticsRecords = async (args: CreateApiBetaAnalyticsRecordsArgs, options?: CreateApiBetaAnalyticsRecordsOptions) => { 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 (args.pageLimit != null) query.append("pageLimit", String(args.pageLimit)) if (args.pageKey != null) query.append("pageKey", String(args.pageKey)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/beta/analytics/records${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(getMetricRecordsDataOutput, await res.json()) };