import {z} from 'zod' export type GetApiV1ChecksHeartbeatsCheckIdEventsIdArgs = {checkId: string, id: string, 'x-checkly-account'?: string | undefined}; export type GetApiV1ChecksHeartbeatsCheckIdEventsIdOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const heartbeatsV1Event = z.object({id: z.string(), state: z.enum(["FAILING", "EARLY", "RECEIVED", "GRACE", "LATE"]).optional(), timestamp: z.string().optional(), source: z.string().nullable().optional(), userAgent: z.string().nullable().optional()}); export const heartbeatsV1AvailabilitySuccessRatio = z.object({previousPeriod: z.number().optional(), currentPeriod: z.number().optional()}); export const heartbeatsV1AvailabilityResponse = z.object({successRatio: heartbeatsV1AvailabilitySuccessRatio.optional(), totalEntitiesCurrentPeriod: z.number().optional()}).nullable(); export const heartbeatsV1EventStats = z.object({last24Hours: heartbeatsV1AvailabilityResponse.optional(), last7Days: heartbeatsV1AvailabilityResponse.optional()}); export const heartbeatsV1EventResponse = z.object({event: heartbeatsV1Event.optional(), stats: heartbeatsV1EventStats.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 a specific Heartbeat event * * GET /v1/checks/heartbeats/{checkId}/events/{id} */ export const getApiV1ChecksHeartbeatsCheckIdEventsId = async (args: GetApiV1ChecksHeartbeatsCheckIdEventsIdArgs, options?: GetApiV1ChecksHeartbeatsCheckIdEventsIdOptions) => { 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 (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/checks/heartbeats/${encodeURIComponent(String(args.checkId))}/events/${encodeURIComponent(String(args.id))}${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(heartbeatsV1EventResponse, await res.json()) };