import {z} from 'zod' export type ChartSpec = {enabled?: boolean | undefined}; export type AlertLevel = 'ok' | 'warning' | 'critical' | 'no_data'; export type SeverityFilter = {levels?: Array | undefined}; export type ContentField = {label?: string | undefined, show_when?: (SeverityFilter | null) | undefined, value?: string | undefined}; export type ContentLink = {label?: string | undefined, show_when?: (SeverityFilter | null) | undefined, url?: string | undefined}; export type RowsSpec = {columns?: (Array | null) | undefined, enabled?: boolean | undefined, format?: (string | null) | undefined, max?: number | undefined}; export type ContentSpec = {body?: string | undefined, chart?: ChartSpec | undefined, fields?: Array | undefined, links?: Array | undefined, rows?: RowsSpec | undefined, title?: string | undefined, title_overrides?: {[key: string]: string} | undefined}; export type NotificationKind = 'alert' | 'incident' | 'anomaly' | 'synthetics' | 'downtime' | 'on_call'; export type CreateApiApiOrgIdAlertsTemplatesPreviewArgs = {org_id: string, body: {channel: string, definition: ContentSpec, kind?: NotificationKind | undefined, sample: unknown, severity?: (string | null) | undefined}}; export type CreateApiApiOrgIdAlertsTemplatesPreviewOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const previewKv = z.object({label: z.string(), value: z.string()}); export const previewLink = z.object({label: z.string(), url: z.string()}); export const previewModel = z.object({body_html: z.string(), chart_placeholder: z.boolean(), color: z.string(), fields: z.array(previewKv), footer: z.string(), links: z.array(previewLink), severity: z.string().nullable().optional(), title: z.string()}); export const variableGroup = z.object({name: z.string(), variables: z.array(z.string())}); export const templatePreviewResponse = z.object({empty_variables: z.array(z.string()), payload: z.unknown(), preview_model: previewModel, unknown_variables: z.array(z.string()), variable_groups: z.array(variableGroup), variables: z.array(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 }; /** * Preview an alert template * * POST /api/{org_id}/alerts/templates/preview */ export const createApiApiOrgIdAlertsTemplatesPreview = async (args: CreateApiApiOrgIdAlertsTemplatesPreviewArgs, options: CreateApiApiOrgIdAlertsTemplatesPreviewOptions) => { 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/${encodeURIComponent(String(args.org_id))}/alerts/templates/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(templatePreviewResponse, await res.json()) };