import {z} from 'zod' export type PublicStatusPageV3AutomationRuleTags = Array; export type PublicStatusPageV3AutomationRuleTargetImpact = 'UNDER_MAINTENANCE' | 'DEGRADED_PERFORMANCE' | 'PARTIAL_OUTAGE' | 'MAJOR_OUTAGE'; export type PublicStatusPageV3AutomationRuleComponent = {componentId: string, targetImpact: PublicStatusPageV3AutomationRuleTargetImpact}; export type PublicStatusPageV3AutomationRuleComponents = Array; export type PublicStatusPageV3AutomationRuleMutation = {name: string, enabled?: boolean | undefined, firstUpdate: string, lastUpdate: string, notifySubscribers?: boolean | undefined, coolDownWindowMinutes?: number | undefined, tags: PublicStatusPageV3AutomationRuleTags, components?: PublicStatusPageV3AutomationRuleComponents | undefined}; export type CreateApiV3StatusPagesStatusPageIdAutomationRulesArgs = {'x-checkly-account'?: string | undefined, statusPageId: string, body: PublicStatusPageV3AutomationRuleMutation}; export type CreateApiV3StatusPagesStatusPageIdAutomationRulesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const publicStatusPageV3AutomationRuleTags = z.array(z.string()); export const publicStatusPageV3AutomationRuleTargetImpact = z.enum(["UNDER_MAINTENANCE", "DEGRADED_PERFORMANCE", "PARTIAL_OUTAGE", "MAJOR_OUTAGE"]); export const publicStatusPageV3AutomationRuleComponentResponse = z.object({componentId: z.string(), targetImpact: publicStatusPageV3AutomationRuleTargetImpact, id: z.string()}); export const publicStatusPageV3AutomationRuleComponentsResponse = z.array(publicStatusPageV3AutomationRuleComponentResponse); export const publicStatusPageV3AutomationRule = z.object({name: z.string().max(255), enabled: z.boolean().optional(), firstUpdate: z.string(), lastUpdate: z.string(), notifySubscribers: z.boolean().optional(), coolDownWindowMinutes: z.number().int().gte(0).lte(1440).optional(), tags: publicStatusPageV3AutomationRuleTags, id: z.string(), statusPageId: z.string(), incidentId: z.string().nullable().optional(), components: publicStatusPageV3AutomationRuleComponentsResponse, created_at: z.string(), 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 an automation rule on a v3 status page. * * POST /v3/status-pages/{statusPageId}/automation-rules */ export const createApiV3StatusPagesStatusPageIdAutomationRules = async (args: CreateApiV3StatusPagesStatusPageIdAutomationRulesArgs, options?: CreateApiV3StatusPagesStatusPageIdAutomationRulesOptions) => { 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['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(/\/+$/, '')}/v3/status-pages/${encodeURIComponent(String(args.statusPageId))}/automation-rules${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(publicStatusPageV3AutomationRule, await res.json()) };