import {z} from 'zod' export type GetApiV1ReportingArgs = {'x-checkly-account'?: string | undefined, from?: string | undefined, to?: string | undefined, quickRange?: ('last24Hrs' | 'last7Days' | 'last30Days' | 'thisWeek' | 'thisMonth' | 'lastWeek' | 'lastMonth') | undefined, filterByTags?: Array | undefined, deactivated?: (boolean | null) | undefined, granularity?: ('day' | 'week' | 'month') | undefined, timezone?: string | undefined}; export type GetApiV1ReportingOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const reportingTagList = z.array(z.string()); export const reportingAggregate = z.object({successRatio: z.number().gte(0), avg: z.number().gte(0), p95: z.number().gte(0), p99: z.number().gte(0)}); export const reportingBucketAggregate = z.object({successRatio: z.number().gte(0), avg: z.number().gte(0), p95: z.number().gte(0), p99: z.number().gte(0), from: z.string(), to: z.string()}); export const reportingBucketAggregateList = z.array(reportingBucketAggregate); export const reporting = z.object({name: z.string(), checkId: z.string(), checkType: z.string(), deactivated: z.boolean(), tags: reportingTagList, aggregate: reportingAggregate.optional(), aggregates: reportingBucketAggregateList.optional()}); export const reportingList = z.array(reporting); 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 }; /** * Generates a report with aggregate statistics for checks and check groups. * * GET /v1/reporting */ export const getApiV1Reporting = async (args: GetApiV1ReportingArgs, options?: GetApiV1ReportingOptions) => { const {baseUrl = "https://api.checklyhq.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-checkly-account'] != null) headers.set("x-checkly-account", String(args['x-checkly-account'])) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.quickRange != null) query.append("quickRange", String(args.quickRange)) for (const item of args.filterByTags ?? []) query.append("filterByTags", String(item)) if (args.deactivated != null) query.append("deactivated", String(args.deactivated)) if (args.granularity != null) query.append("granularity", String(args.granularity)) if (args.timezone != null) query.append("timezone", String(args.timezone)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/reporting${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(reportingList, await res.json()) };