import {z} from 'zod' export type GetApiApiV1ChecksBulkStatsArgs = {pk: Array, start_date?: string | undefined, end_date?: string | undefined, include_alerts?: boolean | undefined}; export type GetApiApiV1ChecksBulkStatsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const checksBulkStats = z.object({pk: z.array(z.number().int()), start_date: z.string().optional(), end_date: z.string().optional(), include_alerts: z.boolean().optional(), checks: z.array(z.object({})).optional(), totals: 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 }; /** * Bulk API: Get statistics & response time for multiple checks * * GET /api/v1/checks/bulk/stats/ */ export const getApiApiV1ChecksBulkStats = async (args: GetApiApiV1ChecksBulkStatsArgs, options: GetApiApiV1ChecksBulkStatsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.pk ?? []) query.append("pk", String(item)) 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.include_alerts != null) query.append("include_alerts", String(args.include_alerts)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/checks/bulk/stats/${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(checksBulkStats, await res.json()) };