import {z} from 'zod' export type GetApiMonitorsIdArgs = {id: string, exclude?: 'labels' | undefined}; export type GetApiMonitorsIdOptions = {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 }; /** * Retrieves a monitor by ID. Returns `404` if the monitor doesn't exist or the caller can't query its datasets. Labels are resolved unless `exclude=labels`. * * GET /monitors/{id} */ export const getApiMonitorsId = async (args: GetApiMonitorsIdArgs, options?: GetApiMonitorsIdOptions) => { const {baseUrl = "https://api.axiom.co/v2", fetch: fetchFn = fetch, Auth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.exclude != null) query.append("exclude", String(args.exclude)) if (Auth !== undefined) headers.set('authorization', `Bearer ${Auth}`) const url = `${baseUrl.replace(/\/+$/, '')}/monitors/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) 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()) };