import {z} from 'zod' export type ChecksV1AlertSettingsRunBasedEscalation = {failedRunThreshold?: (1 | 2 | 3 | 4 | 5) | undefined}; export type ChecksV1AlertSettingsTimeBasedEscalation = {minutesFailingThreshold?: (5 | 10 | 15 | 30) | undefined}; export type ChecksV1AlertSettingsCreate = {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?: ChecksV1AlertSettingsRunBasedEscalation | undefined, timeBasedEscalation?: ChecksV1AlertSettingsTimeBasedEscalation | undefined, parallelRunFailureThreshold?: ({enabled?: boolean | undefined, percentage?: (10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100) | undefined}) | undefined}; export type ChecksV1AlertChannelSubscription = {alertChannelId: number, activated: boolean}; export type ChecksV1RetryStrategy = {type: 'FIXED' | 'LINEAR' | 'EXPONENTIAL' | 'SINGLE_RETRY', baseBackoffSeconds?: number | undefined, sameRegion?: boolean | undefined, maxRetries?: number | undefined, maxDurationSeconds?: number | undefined, onlyOn?: Array<'NETWORK_ERROR'> | undefined} | null; export type ChecksV1TriggerIncident = {serviceId: string, severity: 'CRITICAL' | 'MAJOR' | 'MEDIUM' | 'MINOR', name: string, description: string, notifySubscribers: boolean} | null; export type ChecksV1EnvironmentVariable = {key: string, value: string | null, locked?: boolean | undefined, secret?: boolean | undefined}; export type ChecksV1Dependency = {path: string, content: string}; export type ChecksV1DependencyList = Array | null; export type ChecksV1MultiStepCheckCreate = {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?: ChecksV1AlertSettingsCreate | 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 | undefined, retryStrategy?: ChecksV1RetryStrategy | undefined, triggerIncident?: ChecksV1TriggerIncident | undefined, runParallel?: boolean | undefined, description?: (string | null) | undefined, intent?: ({goal: string, requiredOutcomes?: Array | undefined, mustPreserve?: Array | undefined} | null) | undefined, aiAutoRepairEnabled?: (boolean | null) | undefined, checkType?: 'MULTI_STEP' | undefined, environmentVariables?: (Array | null) | undefined, frequency?: (1 | 2 | 5 | 10 | 15 | 30 | 60 | 120 | 180 | 360 | 720 | 1440) | undefined, script: string | null, scriptPath?: (string | null) | undefined, privateLocations?: (Array | null) | undefined, dependencies?: ChecksV1DependencyList | undefined}; export type CreateApiV1ChecksMultistepArgs = {autoAssignAlerts?: boolean | undefined, 'x-checkly-account'?: string | undefined, body: ChecksV1MultiStepCheckCreate}; export type CreateApiV1ChecksMultistepOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const checksV1AlertSettingsRunBasedEscalation = z.object({failedRunThreshold: z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5)]).optional()}); export const checksV1AlertSettingsTimeBasedEscalation = z.object({minutesFailingThreshold: z.union([z.literal(5), z.literal(10), z.literal(15), z.literal(30)]).optional()}); export const checksV1AlertSettings = 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: checksV1AlertSettingsRunBasedEscalation.optional(), timeBasedEscalation: checksV1AlertSettingsTimeBasedEscalation.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 checksV1AlertChannelSubscription = z.object({alertChannelId: z.number(), activated: z.boolean()}); export const checksV1RetryStrategy = z.object({type: z.enum(["FIXED", "LINEAR", "EXPONENTIAL", "SINGLE_RETRY"]), baseBackoffSeconds: z.number().optional(), sameRegion: z.boolean().optional(), maxRetries: z.number().gte(1).lte(10).optional(), maxDurationSeconds: z.number().gte(0).lte(600).optional(), onlyOn: z.array(z.literal("NETWORK_ERROR")).optional()}).nullable(); export const checksV1TriggerIncident = 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 checksV1AlertEmail = z.object({address: z.string().min(1).optional()}); export const checksV1KeyValue = z.object({key: z.string().min(1), value: z.string(), locked: z.boolean().optional()}); export const checksV1AlertWebhook = z.object({name: z.string().min(1).optional(), url: z.string().min(1).optional(), method: z.string().min(1).nullable().optional(), headers: z.array(checksV1KeyValue).optional(), queryParameters: z.array(checksV1KeyValue).optional()}); export const checksV1AlertSlack = z.object({url: z.string().min(1).optional()}); export const checksV1AlertSms = z.object({number: z.string().min(1).optional(), name: z.string()}); export const checksV1AlertChannels = z.object({email: z.array(checksV1AlertEmail).optional(), webhook: z.array(checksV1AlertWebhook).optional(), slack: z.array(checksV1AlertSlack).optional(), sms: z.array(checksV1AlertSms).optional()}).nullable(); export const checksV1CheckProject = z.object({id: z.string().optional(), logicalId: z.string().optional(), name: z.string().optional()}).nullable(); export const bindingProjectV1 = z.object({id: z.string(), logicalId: z.string(), name: z.string()}); export const projectBindingDetailsV1 = z.object({logicalId: z.string(), pending: z.boolean(), sourceFile: z.string().nullable().optional()}); export const projectBindingOwnerV1 = z.object({project: bindingProjectV1, binding: projectBindingDetailsV1}).nullable(); export const projectBindingV1 = z.object({project: bindingProjectV1, binding: projectBindingDetailsV1}); export const projectBindingReferencesV1 = z.array(projectBindingV1); export const projectBindingsV1 = z.object({owner: projectBindingOwnerV1, references: projectBindingReferencesV1}); export const checksV1EnvironmentVariable = z.object({key: z.string().min(1), value: z.string().nullable(), locked: z.boolean().optional(), secret: z.boolean().optional()}); export const checksV1MultiStepCheck = z.object({name: z.string().min(1), activated: z.boolean().optional(), muted: z.boolean().optional(), doubleCheck: z.boolean().optional(), shouldFail: z.boolean().optional(), locations: z.array(z.enum(["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"])).nullable().optional(), tags: z.array(z.string().min(1)).optional(), alertSettings: checksV1AlertSettings.optional(), useGlobalAlertSettings: z.boolean().optional(), groupId: z.number().nullable().optional(), groupOrder: z.number().gte(0).nullable().optional(), runtimeId: z.enum(["2026.04", "2025.04", "2024.09", "2024.02", "2023.09", "2023.02", "2022.10", "null"]).nullable().optional(), alertChannelSubscriptions: z.array(checksV1AlertChannelSubscription).optional(), retryStrategy: checksV1RetryStrategy.optional(), triggerIncident: checksV1TriggerIncident.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(), id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().nullable().optional(), alertChannels: checksV1AlertChannels.optional(), project: checksV1CheckProject.optional(), logicalId: z.string().nullable().optional(), member: z.boolean().nullable().optional(), pending: z.boolean().nullable().optional(), projectBindings: projectBindingsV1.optional(), privateLocations: z.array(z.string().min(1)).nullable().optional(), checkType: z.literal("MULTI_STEP").optional(), frequency: z.union([z.literal(1), z.literal(2), z.literal(5), z.literal(10), z.literal(15), z.literal(30), z.literal(60), z.literal(120), z.literal(180), z.literal(360), z.literal(720), z.literal(1440)]).optional(), script: z.string().nullable(), environmentVariables: z.array(checksV1EnvironmentVariable).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 multi-step check * * POST /v1/checks/multistep */ export const createApiV1ChecksMultistep = async (args: CreateApiV1ChecksMultistepArgs, options?: CreateApiV1ChecksMultistepOptions) => { 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/multistep${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(checksV1MultiStepCheck, await res.json()) };