import {z} from 'zod' export type ChecksV1HeartbeatRequest = {period: number, periodUnit: 'seconds' | 'minutes' | 'hours' | 'days', grace: number, graceUnit: 'seconds' | 'minutes' | 'hours' | 'days', pingToken?: (string | null) | undefined}; export type ChecksV1HeartbeatCheckCreate = {name: string, activated?: boolean | undefined, muted?: boolean | undefined, doubleCheck?: boolean | undefined, shouldFail?: boolean | undefined, locations?: (Array<'us-east-1' | 'us-east-2' | 'us-west-1' | 'us-west-2' | 'ca-central-1' | 'sa-east-1' | 'eu-west-1' | 'eu-central-1' | 'eu-west-2' | 'eu-west-3' | 'eu-north-1' | 'eu-south-1' | 'me-south-1' | 'ap-southeast-1' | 'ap-northeast-1' | 'ap-east-1' | 'ap-southeast-2' | 'ap-southeast-3' | 'ap-northeast-2' | 'ap-northeast-3' | 'ap-south-1' | 'af-south-1'> | null) | undefined, tags?: Array | undefined, alertSettings?: ({escalationType?: ('RUN_BASED' | 'TIME_BASED') | undefined, reminders?: ({amount?: (0 | 1 | 2 | 3 | 4 | 5 | 100000) | undefined, interval?: (1 | 2 | 3 | 4 | 5 | 10 | 15 | 30) | undefined}) | undefined, sslCertificates?: ({enabled?: boolean | undefined, alertThreshold?: number | undefined}) | undefined, runBasedEscalation?: ({failedRunThreshold?: (1 | 2 | 3 | 4 | 5) | undefined}) | undefined, timeBasedEscalation?: ({minutesFailingThreshold?: (5 | 10 | 15 | 30) | undefined}) | undefined, parallelRunFailureThreshold?: ({enabled?: boolean | undefined, percentage?: (10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100) | undefined}) | undefined}) | undefined, useGlobalAlertSettings?: boolean | undefined, groupId?: (number | null) | undefined, groupOrder?: (number | null) | undefined, runtimeId?: ('2026.04' | '2025.04' | '2024.09' | '2024.02' | '2023.09' | '2023.02' | '2022.10' | 'null' | null) | undefined, alertChannelSubscriptions?: Array<{alertChannelId: number, activated: boolean}> | undefined, triggerIncident?: ({serviceId: string, severity: 'CRITICAL' | 'MAJOR' | 'MEDIUM' | 'MINOR', name: string, description: string, notifySubscribers: boolean} | null) | undefined, runParallel?: boolean | undefined, description?: (string | null) | undefined, intent?: ({goal: string, requiredOutcomes?: Array | undefined, mustPreserve?: Array | undefined} | null) | undefined, aiAutoRepairEnabled?: (boolean | null) | undefined, checkType?: 'HEARTBEAT' | undefined, frequency?: (0 | 1 | 2 | 5 | 10 | 15 | 30 | 60 | 120 | 180 | 360 | 720 | 1440) | undefined, frequencyOffset?: number | undefined, request?: ({[key: string]: unknown} | null) | undefined, heartbeat: ChecksV1HeartbeatRequest, script?: (string | null) | undefined, scriptPath?: (string | null) | undefined, sslCheckDomain?: (string | null) | undefined, environmentVariables?: (Array<{key: string, value: string | null, locked?: boolean | undefined, secret?: boolean | undefined}> | null) | undefined, setupSnippetId?: (number | null) | undefined, tearDownSnippetId?: (number | null) | undefined, localSetupScript?: (string | null) | undefined, localTearDownScript?: (string | null) | undefined, degradedResponseTime?: (number | null) | undefined, maxResponseTime?: (number | null) | undefined}; export type CreateApiV1ChecksHeartbeatArgs = {autoAssignAlerts?: boolean | undefined, 'x-checkly-account'?: string | undefined, body: ChecksV1HeartbeatCheckCreate}; export type CreateApiV1ChecksHeartbeatOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const checksV1HeartbeatResponseAlertSettings = z.object({escalationType: z.enum(["RUN_BASED", "TIME_BASED"]).optional(), reminders: z.object({amount: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(100000)]).optional(), interval: z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(10), z.literal(15), z.literal(30)]).optional()}).optional(), sslCertificates: z.object({enabled: z.boolean().optional(), alertThreshold: z.number().int().optional()}).optional(), runBasedEscalation: z.object({failedRunThreshold: z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5)]).optional()}).optional(), timeBasedEscalation: z.object({minutesFailingThreshold: z.union([z.literal(5), z.literal(10), z.literal(15), z.literal(30)]).optional()}).optional(), parallelRunFailureThreshold: z.object({enabled: z.boolean().optional(), percentage: z.union([z.literal(10), z.literal(20), z.literal(30), z.literal(40), z.literal(50), z.literal(60), z.literal(70), z.literal(80), z.literal(90), z.literal(100)]).optional()}).optional()}).nullable(); export const checksV1HeartbeatResponseAlertChannelSubscription = z.object({alertChannelId: z.number(), activated: z.boolean()}); export const checksV1HeartbeatResponseTriggerIncident = z.object({serviceId: z.string().min(1), severity: z.enum(["CRITICAL", "MAJOR", "MEDIUM", "MINOR"]), name: z.string(), description: z.string(), notifySubscribers: z.boolean()}).nullable(); export const checksV1Heartbeat = z.object({period: z.number(), periodUnit: z.enum(["seconds", "minutes", "hours", "days"]), grace: z.number(), graceUnit: z.enum(["seconds", "minutes", "hours", "days"]), pingToken: z.string().nullable().optional(), pingUrl: z.string().optional()}); export const checksV1HeartbeatResponseAlertEmail = z.object({address: z.string()}); export const checksV1HeartbeatResponseKeyValue = z.object({key: z.string(), value: z.string(), locked: z.boolean().optional()}); export const checksV1HeartbeatResponseAlertWebhook = z.object({name: z.string().optional(), url: z.string(), method: z.enum(["GET", "POST", "PUT", "HEAD", "DELETE", "PATCH"]).nullable().optional(), headers: z.array(checksV1HeartbeatResponseKeyValue).optional(), queryParameters: z.array(checksV1HeartbeatResponseKeyValue).optional()}); export const checksV1HeartbeatResponseAlertSlack = z.object({url: z.string()}); export const checksV1HeartbeatResponseAlertSms = z.object({number: z.string(), name: z.string()}); export const checksV1HeartbeatResponseAlertChannels = z.object({email: z.array(checksV1HeartbeatResponseAlertEmail).optional(), webhook: z.array(checksV1HeartbeatResponseAlertWebhook).optional(), slack: z.array(checksV1HeartbeatResponseAlertSlack).optional(), sms: z.array(checksV1HeartbeatResponseAlertSms).optional()}).nullable(); export const checksV1HeartbeatCheckMutationResponse = z.object({id: z.string().optional(), name: z.string(), activated: z.boolean().optional(), muted: z.boolean().optional(), tags: z.array(z.string()).optional(), alertSettings: checksV1HeartbeatResponseAlertSettings.optional(), useGlobalAlertSettings: z.boolean().optional(), alertChannelSubscriptions: z.array(checksV1HeartbeatResponseAlertChannelSubscription).optional(), triggerIncident: checksV1HeartbeatResponseTriggerIncident.optional(), runParallel: z.boolean().optional(), description: z.string().max(500).nullable().optional(), intent: z.object({goal: z.string().min(1).max(2000), requiredOutcomes: z.array(z.string().min(1).max(1000)).optional(), mustPreserve: z.array(z.string().min(1).max(1000)).optional()}).nullable().optional(), aiAutoRepairEnabled: z.boolean().nullable().optional(), checkType: z.literal("HEARTBEAT").optional(), heartbeat: checksV1Heartbeat.optional(), alertChannels: checksV1HeartbeatResponseAlertChannels.optional(), created_at: z.string().optional(), updated_at: z.string().nullable().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 }; /** * Create a heartbeat check * * POST /v1/checks/heartbeat */ export const createApiV1ChecksHeartbeat = async (args: CreateApiV1ChecksHeartbeatArgs, options?: CreateApiV1ChecksHeartbeatOptions) => { const {baseUrl = "https://api.checklyhq.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.autoAssignAlerts != null) query.append("autoAssignAlerts", String(args.autoAssignAlerts)) 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/heartbeat${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(checksV1HeartbeatCheckMutationResponse, await res.json()) };