import {z} from 'zod' export type GetApiV1EventsArgs = {startDate?: string | undefined, endDate?: string | undefined, ingestStartDate?: string | undefined, ingestEndDate?: string | undefined, customerId?: string | undefined, eventTypeId?: string | undefined, source?: string | undefined, limit?: number | undefined, cursor?: string | undefined, orderDirection?: string | undefined, skipDeletionEvents?: boolean | undefined}; export type GetApiV1EventsOptions = {baseUrl?: string | undefined, customHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const eventDto = z.object({manufacturerId: z.string().optional(), eventTypeId: z.string().optional(), metadata: z.record(z.string(), z.object({})).optional(), value: z.string().optional(), differentiator: z.string().optional(), invoiceGroup: z.string().optional(), idempotencyKey: z.string().optional(), datetime: z.string().optional(), customerId: z.string().optional(), parentEventId: z.string().optional(), source: z.string().optional(), deleted: z.boolean().optional()}); export const eventsQueryResponse = z.object({events: z.array(eventDto).optional(), count: z.number().int().optional(), nextCursor: z.string().optional(), hasMore: z.boolean().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 all events * * GET /v1/events */ export const getApiV1Events = async (args: GetApiV1EventsArgs, options?: GetApiV1EventsOptions) => { const {baseUrl = "https://integrators.prod.api.tabsplatform.com", fetch: fetchFn = fetch, customHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.ingestStartDate != null) query.append("ingestStartDate", String(args.ingestStartDate)) if (args.ingestEndDate != null) query.append("ingestEndDate", String(args.ingestEndDate)) if (args.customerId != null) query.append("customerId", String(args.customerId)) if (args.eventTypeId != null) query.append("eventTypeId", String(args.eventTypeId)) if (args.source != null) query.append("source", String(args.source)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.orderDirection != null) query.append("orderDirection", String(args.orderDirection)) if (args.skipDeletionEvents != null) query.append("skipDeletionEvents", String(args.skipDeletionEvents)) if (customHeader !== undefined) headers.set("Authorization", customHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/events${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(eventsQueryResponse, await res.json()) };