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 ExportRequest = {key: string, data: Array, begin: string, end?: string | undefined, format?: 'csv' | undefined, fullname?: boolean | undefined, includetoday?: boolean | undefined, limit?: number | undefined, offset?: number | undefined, sort?: Array | undefined, sum?: Array | undefined, reportName?: string | undefined, filter?: Filter | undefined, allownull?: boolean | undefined, formatdata?: boolean | undefined, nodatatiercheck?: string | undefined, period?: string | undefined, dataColumns?: Array | undefined}; export type CreateApiBatchArgs = {body: ExportRequest}; export type CreateApiBatchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const exportResponse = z.object({batchId: z.number().int().optional(), status: z.string().optional(), submittedAt: z.string().optional(), reportName: 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 }; /** * Submit an async export job * * POST /batch */ export const createApiBatch = async (args: CreateApiBatchArgs, options?: CreateApiBatchOptions) => { 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(/\/+$/, '')}/batch${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(exportResponse, await res.json()) };