import {z} from 'zod' export type GetApiProjectsOccurrencesArgs = {period?: ('hour' | 'day' | 'week' | 'month') | undefined, environment?: string | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiProjectsOccurrencesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const occurrenceSeries = z.object({project_id: z.string(), buckets: z.array(z.object({timestamp: z.number().int(), count: z.number().int()}))}); export const pagination = z.object({page: z.number().int().gte(1), per_page: z.number().int().gte(1).lte(100)}); export const offsetLinks = z.object({self: z.string(), first: z.string().optional(), prev: z.string().nullable().optional(), next: z.string().nullable().optional()}); export const occurrenceMeta = z.object({request_id: z.string().optional(), period: z.enum(["hour", "day", "week", "month"]).optional(), interval: z.enum(["minute", "hour", "day"]).optional()}); export const getApiProjectsOccurrencesResponse = z.object({data: z.array(occurrenceSeries), pagination: pagination.optional(), links: offsetLinks.optional(), meta: occurrenceMeta.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 occurrence counts for all projects * * GET /projects/occurrences */ export const getApiProjectsOccurrences = async (args: GetApiProjectsOccurrencesArgs, options?: GetApiProjectsOccurrencesOptions) => { const {baseUrl = "https://app.honeybadger.io/v3", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.period != null) query.append("period", String(args.period)) if (args.environment != null) query.append("environment", String(args.environment)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/projects/occurrences${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(getApiProjectsOccurrencesResponse, await res.json()) };