import {z} from 'zod' export type And = {[key: string]: unknown}; export type Not = {[key: string]: unknown}; export type Or = {[key: string]: unknown}; export type Value = {field?: string | undefined, operator?: ('EQUALS' | 'NOT' | 'IN' | 'NOT_IN' | 'LT' | 'LTE' | 'GT' | 'GTE' | 'CONTAINS' | 'NOT_CONTAINS' | 'STARTS_WITH' | 'ENDS_WITH' | 'HAS_ANY') | undefined, value?: Record | undefined} | {[key: string]: unknown}; export type FilteredColumn = {alias?: string | undefined, friendly?: string | undefined, name?: string | undefined, type: string, filter?: (Array) | undefined, nullReplacement?: Record | undefined}; export type SimpleColumn = {alias?: string | undefined, friendly?: string | undefined, name?: string | undefined, type: string}; export type Filter = {[key: string]: unknown}; export type DataRequest = {key: string, data: Array, sum?: Array | undefined, filter?: Filter | undefined, begin: string, end?: string | undefined, format?: ('human' | 'json' | 'csv' | 'excel') | undefined, formatdata?: boolean | undefined, fullname?: boolean | undefined, limit?: number | undefined, offset?: number | undefined, sort?: Array | undefined, dataColumns?: Array | undefined}; export type CreateApiApidataArgs = {body: DataRequest}; export type CreateApiApidataOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const jsonDataResponse = z.object({"report-name": z.string().optional(), "publisher-name": z.string().optional(), begin: z.string().optional(), end: z.string().optional(), columns: z.array(z.object({table: z.string().optional(), name: z.string().optional(), friendly: z.string().optional(), alias: z.string().optional(), help: z.string().optional(), format: z.string().optional(), icon: z.string().optional(), "friendly-suffix": z.string().optional(), target_direction_negative: z.boolean().optional()})).optional(), rows: z.array(z.object({})).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 }; /** * Run a reporting query (POST) * * POST /apidata */ export const createApiApidata = async (args: CreateApiApidataArgs, options?: CreateApiApidataOptions) => { const {baseUrl = "https://api3.mountain.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/apidata${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(jsonDataResponse, await res.json()) };