Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100import * as assertions from "@openstatus/assertions";import { headerPairSchema } from "@openstatus/utils";import { createInsertSchema, createSelectSchema } from "drizzle-zod";import { z } from "zod";
import { monitorPeriodicitySchema, monitorRegionSchema } from "../constants";import { grpcTlsModes, monitorJobTypes, monitorMethods, monitorStatus,} from "./constants";import { monitor } from "./monitor";
export const monitorMethodsSchema = z.enum(monitorMethods);export const monitorStatusSchema = z.enum(monitorStatus);export const monitorJobTypesSchema = z.enum(monitorJobTypes);export const grpcTlsModesSchema = z.enum(grpcTlsModes);
// TODO: shared function// oxlint-disable-next-line eslint/no-unused-varsfunction stringToArrayProcess<T>(_string: T) {}
const regionsToArraySchema = z.preprocess((val) => { if (String(val).length > 0) { return String(val).split(","); } return [];}, z.array(monitorRegionSchema));
const bodyToStringSchema = z.preprocess((val) => { return String(val);}, z.string());
const headersToArraySchema = z.preprocess( (val) => { // early return in case the header is already an array if (Array.isArray(val)) { return val; } if (typeof val === "string" && String(val).length > 0) { return JSON.parse(String(val)); } return []; }, z.array(z.object({ key: z.string(), value: z.string() })).prefault([]),);
export const selectMonitorSchema = createSelectSchema(monitor, { periodicity: monitorPeriodicitySchema.prefault("10m"), status: monitorStatusSchema.prefault("active"), jobType: monitorJobTypesSchema.prefault("http"), // milliseconds, matches the column default timeout: z.number().prefault(45_000), followRedirects: z.boolean().prefault(true), retry: z.number().prefault(3), regions: regionsToArraySchema.prefault([]),}).extend({ headers: headersToArraySchema.prefault([]), otelHeaders: headersToArraySchema.prefault([]), body: bodyToStringSchema.prefault(""), // for tcp monitors the method is not needed method: monitorMethodsSchema.prefault("GET"),});
const headersSchema = z.array(headerPairSchema).optional();
export const insertMonitorSchema = createInsertSchema(monitor, { name: z .string() .trim() .min(1, "Name must be at least 1 character long") .max(255, "Name must be at most 255 characters long"), periodicity: monitorPeriodicitySchema.prefault("10m"), status: monitorStatusSchema.prefault("active"), regions: z.array(monitorRegionSchema).prefault([]).optional(), headers: headersSchema.prefault([]), otelHeaders: headersSchema.prefault([]),}).extend({ method: monitorMethodsSchema.prefault("GET"), notifications: z.array(z.number()).prefault([]), pages: z.array(z.number()).prefault([]), body: z.string().prefault("").optional(), tags: z.array(z.number()).prefault([]), statusAssertions: z.array(assertions.statusAssertion).optional(), headerAssertions: z.array(assertions.headerAssertion).optional(), textBodyAssertions: z.array(assertions.textBodyAssertion).optional(), timeout: z.coerce.number().gte(0).lte(60000).prefault(45000), degradedAfter: z.coerce.number().gte(0).lte(60000).nullish(),});
export type InsertMonitor = z.infer<typeof insertMonitorSchema>;export type Monitor = z.infer<typeof selectMonitorSchema>;export type MonitorStatus = z.infer<typeof monitorStatusSchema>;export type MonitorPeriodicity = z.infer<typeof monitorPeriodicitySchema>;export type MonitorMethod = z.infer<typeof monitorMethodsSchema>;export type MonitorRegion = z.infer<typeof monitorRegionSchema>;export type MonitorJobType = z.infer<typeof monitorJobTypesSchema>;export type GrpcTlsMode = z.infer<typeof grpcTlsModesSchema>;