import {z} from 'zod' export type LogicalOp = 'and' | 'or'; export type PairOperator = '=' | '!='; export type DimensionCondition = {items: Array>, op: LogicalOp, type: 'group'} | {key: string, operator: PairOperator, type: 'pair', value: string}; export type TargetFolders = {kind: 'all'} | {folder_ids: Array, kind: 'some'}; export type IncidentMode = 'muted' | 'none'; export type TargetModule = 'alerts' | 'anomaly_detections' | 'synthetics' | 'slos'; export type SloCorrectionMode = 'exclude' | 'count_as_good'; export type DowntimeTarget = {folders: TargetFolders, ids?: Array | undefined, incident_mode?: IncidentMode | undefined, module: TargetModule, slo_mode?: (SloCorrectionMode | null) | undefined, tags?: Array | undefined}; export type PreviewRequest = {condition?: (DimensionCondition | null) | undefined, targets: Array}; export type CreateApiApiV2OrgIdDowntimesPreviewArgs = {org_id: string, body: PreviewRequest}; export type CreateApiApiV2OrgIdDowntimesPreviewOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const previewMatch = z.object({applies: z.boolean().nullable().optional(), folder_id: z.string(), id: z.string(), matched_by: z.string().nullable().optional(), missing: z.string().nullable().optional(), name: z.string()}); export const previewResponse = z.object({alerts: z.array(previewMatch), alerts_total: z.number().int().gte(0), anomalies: z.array(previewMatch), anomalies_total: z.number().int().gte(0), resolved_at_fire_time: z.array(previewMatch), slos: z.array(previewMatch), slos_total: z.number().int().gte(0), synthetics: z.array(previewMatch), synthetics_total: z.number().int().gte(0), undecidable: z.record(z.string(), z.array(previewMatch))}); 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 }; /** * Preview what a downtime covers * * POST /api/v2/{org_id}/downtimes/preview */ export const createApiApiV2OrgIdDowntimesPreview = async (args: CreateApiApiV2OrgIdDowntimesPreviewArgs, options: CreateApiApiV2OrgIdDowntimesPreviewOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/${encodeURIComponent(String(args.org_id))}/downtimes/preview${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(previewResponse, await res.json()) };