import {z} from 'zod' export type LabelID = string; export type MonitorUpsertRequest = {alertOnNoData?: boolean | undefined, aplQuery?: string | undefined, columnName?: string | undefined, compareDays?: number | undefined, description?: string | undefined, disabled?: boolean | undefined, disabledUntil?: (string | null) | undefined, includeChart?: boolean | undefined, intervalMinutes?: number | undefined, mplQuery?: string | undefined, name: string, notifierIds?: Array | undefined, notifyByGroup?: boolean | undefined, notifyEveryRun?: boolean | undefined, operator?: ('Below' | 'BelowOrEqual' | 'Above' | 'AboveOrEqual' | 'AboveOrBelow') | undefined, rangeMinutes?: number | undefined, resolvable?: boolean | undefined, secondDelay?: number | undefined, skipResolved?: boolean | undefined, threshold?: number | undefined, tolerance?: number | undefined, triggerAfterNPositiveResults?: number | undefined, triggerFromNRuns?: number | undefined, type: 'Threshold' | 'MatchEvent' | 'AnomalyDetection', labelIds?: Array | undefined}; export type CreateApiMonitorsArgs = {body: MonitorUpsertRequest}; export type CreateApiMonitorsOptions = {baseUrl?: string | undefined, Auth?: string | undefined, fetch?: typeof fetch | undefined}; export const labelID = z.string(); export const labelColor = z.object({name: z.enum(["red", "coral", "orange", "amber", "yellow", "lime", "green", "teal", "blue", "iris", "purple", "pink"]), type: z.literal("palette")}); export const label = z.object({color: labelColor, description: z.string().nullable().optional(), id: labelID, name: z.string()}); export const monitorWithId = z.object({alertOnNoData: z.boolean().optional(), aplQuery: z.string().optional(), columnName: z.string().optional(), compareDays: z.number().lte(7).optional(), description: z.string().optional(), disabled: z.boolean().optional(), disabledUntil: z.string().nullable().optional(), includeChart: z.boolean().optional(), intervalMinutes: z.number().int().gte(1).optional(), mplQuery: z.string().optional(), name: z.string(), notifierIds: z.array(z.string()).optional(), notifyByGroup: z.boolean().optional(), notifyEveryRun: z.boolean().optional(), operator: z.enum(["Below", "BelowOrEqual", "Above", "AboveOrEqual", "AboveOrBelow"]).optional(), rangeMinutes: z.number().int().gte(1).optional(), resolvable: z.boolean().optional(), secondDelay: z.number().lte(86400).optional(), skipResolved: z.boolean().optional(), threshold: z.number().optional(), tolerance: z.number().lte(100).optional(), triggerAfterNPositiveResults: z.number().optional(), triggerFromNRuns: z.number().optional(), type: z.enum(["Threshold", "MatchEvent", "AnomalyDetection"]), createdAt: z.string().optional(), createdBy: z.string().optional(), createdByAgent: z.boolean().optional(), datasets: z.array(z.string()).optional(), id: z.string(), labelIds: z.array(labelID), labelNames: z.array(z.string()), labels: z.array(label).optional(), lastError: z.string().optional(), updatedAt: z.string().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 }; /** * Creates a monitor. Axiom runs the monitor's query once before saving it and rejects the request if the query fails, for example because it's invalid or references a dataset that doesn't exist. `labelIds` replaces assignments when supplied; [] clears them. Omitted or null `labelIds` preserves assignments on updates and creates an unlabeled resource on creation. IDs must belong to existing labels in your organization and are validated before saving. * * POST /monitors */ export const createApiMonitors = async (args: CreateApiMonitorsArgs, options?: CreateApiMonitorsOptions) => { const {baseUrl = "https://api.axiom.co/v2", fetch: fetchFn = fetch, Auth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Auth !== undefined) headers.set('authorization', `Bearer ${Auth}`) const url = `${baseUrl.replace(/\/+$/, '')}/monitors${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(monitorWithId, await res.json()) };