import {z} from 'zod' export type ZonedDateTime = string; export type GetApiV6EventStreamsEventStreamIdEventsArgs = {eventStreamId: string, limit?: number | undefined, offset?: number | undefined, start_date?: ZonedDateTime | undefined, end_date?: ZonedDateTime | undefined, json_path_filters?: string | undefined}; export type GetApiV6EventStreamsEventStreamIdEventsOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const zonedDateTime = z.string(); export const eventStreamEventResponse = z.object({event: z.object({}), event_id: z.string(), event_stream_id: z.string(), event_time: zonedDateTime}); export const getApiV6EventStreamsEventStreamIdEventsResponse = z.array(eventStreamEventResponse); 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 }; /** * List events from an event stream * * GET /v6/event-streams/{eventStreamId}/events * * Also accepts auth: QUERY, COOKIE */ export const getApiV6EventStreamsEventStreamIdEvents = async (args: GetApiV6EventStreamsEventStreamIdEventsArgs, options?: GetApiV6EventStreamsEventStreamIdEventsOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.json_path_filters != null) query.append("json_path_filters", String(args.json_path_filters)) if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v6/event-streams/${encodeURIComponent(String(args.eventStreamId))}/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(getApiV6EventStreamsEventStreamIdEventsResponse, await res.json()) };