import {z} from 'zod' export type GetApiApiV1EventsCountArgs = {pretty?: boolean | undefined, priority?: string | undefined, source?: string | undefined, filter?: string | undefined, rule?: string | undefined, tags?: string | undefined, since?: number | undefined}; export type GetApiApiV1EventsCountOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const modelsStatistics = z.object({all: z.number().int().optional(), distincts: z.number().int().optional(), returned: z.number().int().optional()}); export const modelsResultsCount = z.object({statistics: modelsStatistics.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 }; /** * Count Events * * GET /api/v1/events/count */ export const getApiApiV1EventsCount = async (args: GetApiApiV1EventsCountArgs, options?: GetApiApiV1EventsCountOptions) => { const {baseUrl = "http://:2802/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pretty != null) query.append("pretty", String(args.pretty)) if (args.priority != null) query.append("priority", String(args.priority)) if (args.source != null) query.append("source", String(args.source)) if (args.filter != null) query.append("filter", String(args.filter)) if (args.rule != null) query.append("rule", String(args.rule)) if (args.tags != null) query.append("tags", String(args.tags)) if (args.since != null) query.append("since", String(args.since)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/events/count${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(modelsResultsCount, await res.json()) };