import {z} from 'zod' export type Impact = 'MAINTENANCE' | 'MAJOR' | 'MINOR'; export type Model41 = 'INVESTIGATING' | 'IDENTIFIED' | 'MONITORING' | 'RESOLVED' | 'MAINTENANCE'; export type Model42 = {status: Model41, description: string}; export type IncidentUpdates = Array; export type Model43 = {name: string, impact: Impact, startedAt?: (string | null) | undefined, stoppedAt?: (string | null) | undefined, dashboardId: number, incidentUpdates: IncidentUpdates}; export type CreateApiV1IncidentsArgs = {'x-checkly-account'?: string | undefined, body: Model43}; export type CreateApiV1IncidentsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const impact = z.enum(["MAINTENANCE", "MAJOR", "MINOR"]); export const model44 = z.enum(["INVESTIGATING", "IDENTIFIED", "MONITORING", "RESOLVED", "MAINTENANCE"]); export const model45 = z.object({status: model44, description: z.string(), id: z.string(), created_at: z.string(), updated_at: z.string().nullable()}); export const model46 = z.array(model45); export const model47 = z.object({name: z.string(), impact: impact, startedAt: z.string().nullable().optional(), stoppedAt: z.string().nullable().optional(), dashboardId: z.number(), id: z.string(), created_at: z.string(), updated_at: z.string().nullable(), incidentUpdates: model46}); 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 incident * * POST /v1/incidents */ export const createApiV1Incidents = async (args: CreateApiV1IncidentsArgs, options?: CreateApiV1IncidentsOptions) => { 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(/\/+$/, '')}/v1/incidents${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(model47, await res.json()) };