import {z} from 'zod' export type MonitorTemplateCreateSchema = {projectId?: string | undefined, templateName: string, templateDescription?: string | undefined, monitorName?: string | undefined, monitorDescription?: string | undefined, monitorType: 'Manual' | 'Website' | 'API' | 'Ping' | 'Kubernetes' | 'Docker' | 'Host' | 'Podman' | 'Docker Swarm' | 'Proxmox' | 'OpenStack' | 'VMware' | 'Ceph' | 'Storage Array' | 'IoT Device' | 'IP' | 'Incoming Request' | 'Incoming Email' | 'Port' | 'Server' | 'SSL Certificate' | 'SQL Query' | 'Database' | 'Synthetic Monitor' | 'Custom JavaScript Code' | 'Logs' | 'Metrics' | 'Traces' | 'Exceptions' | 'Profiles' | 'Security Events' | 'AI / LLM' | 'Network Device' | 'DNS' | 'DNSSEC' | 'NTP' | 'Domain' | 'External Status Page' | 'Probe Status', monitorSteps?: Record | undefined, monitoringInterval?: string | undefined, labels?: (Array<{id?: string | undefined}>) | undefined, customFields?: Record | undefined, minimumProbeAgreement?: number | undefined}; export type CreateApiMonitorTemplateArgs = {body: {data: MonitorTemplateCreateSchema}}; export type CreateApiMonitorTemplateOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const monitorTemplateReadSchema = z.object({_id: z.string().optional(), createdAt: z.object({}).optional(), updatedAt: z.object({}).optional(), deletedAt: z.object({}).optional(), projectId: z.string().optional(), templateName: z.string(), templateDescription: z.string().optional(), slug: z.string().optional(), monitorName: z.string().optional(), monitorDescription: z.string().optional(), monitorType: z.enum(["Manual", "Website", "API", "Ping", "Kubernetes", "Docker", "Host", "Podman", "Docker Swarm", "Proxmox", "OpenStack", "VMware", "Ceph", "Storage Array", "IoT Device", "IP", "Incoming Request", "Incoming Email", "Port", "Server", "SSL Certificate", "SQL Query", "Database", "Synthetic Monitor", "Custom JavaScript Code", "Logs", "Metrics", "Traces", "Exceptions", "Profiles", "Security Events", "AI / LLM", "Network Device", "DNS", "DNSSEC", "NTP", "Domain", "External Status Page", "Probe Status"]), monitorSteps: z.object({}).optional(), monitoringInterval: z.string().optional(), labels: z.array(z.object({id: z.string().optional()})).optional(), customFields: z.object({}).optional(), minimumProbeAgreement: z.number().optional(), createdByUserId: z.string().optional()}); export const createApiMonitorTemplateResponse = z.object({data: monitorTemplateReadSchema.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 }; /** * Create Monitor Template * * POST /monitor-template */ export const createApiMonitorTemplate = async (args: CreateApiMonitorTemplateArgs, options?: CreateApiMonitorTemplateOptions) => { const {baseUrl = "https://oneuptime.com/api", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("APIKey", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/monitor-template${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(createApiMonitorTemplateResponse, await res.json()) };