import {z} from 'zod' export type AlertGroupRef = {alert_id: string, group_key?: string | undefined, group_labels?: ({[key: string]: string} | null) | undefined}; 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 EndOnClear = {alert_id: string, group_key?: (string | null) | undefined}; export type NotificationEvents = {cancelled?: boolean | undefined, ended?: boolean | undefined, ending_soon?: boolean | undefined, extended?: boolean | undefined, started?: boolean | undefined}; export type DowntimeNotifications = {continues?: (string | null) | undefined, destinations?: Array | undefined, ending_soon_lead_secs?: number | undefined, events?: NotificationEvents | undefined}; export type Repeat = 'none' | 'daily' | 'weekly'; export type DowntimeSchedule = {duration_secs: number, ends_at?: (number | null) | undefined, repeat: Repeat, start_time_local?: (string | null) | undefined, starts_at: number, timezone: string, weekdays?: Array | undefined}; 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 DowntimeRequest = {alert_groups?: (Array | null) | undefined, condition?: (DimensionCondition | null) | undefined, end_on_clear?: (EndOnClear | null) | undefined, folder_id?: string | undefined, name?: (string | null) | undefined, notifications?: (DowntimeNotifications | null) | undefined, reason?: (string | null) | undefined, schedule: DowntimeSchedule, show_banner?: boolean | undefined, targets: Array, version?: (number | null) | undefined}; export type CreateApiApiV2OrgIdDowntimesArgs = {org_id: string, folder?: string | undefined, body: DowntimeRequest}; export type CreateApiApiV2OrgIdDowntimesOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const createDowntimeResponse = z.object({id: z.string()}); 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 downtime * * POST /api/v2/{org_id}/downtimes */ export const createApiApiV2OrgIdDowntimes = async (args: CreateApiApiV2OrgIdDowntimesArgs, options: CreateApiApiV2OrgIdDowntimesOptions) => { 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 (args.folder != null) query.append("folder", String(args.folder)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/${encodeURIComponent(String(args.org_id))}/downtimes${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(createDowntimeResponse, await res.json()) };