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.
5.9 kB ยท 213 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214import { safeUrlSchema } from "@openstatus/utils";import { createInsertSchema, createSelectSchema } from "drizzle-zod";import * as z from "zod";
import { notificationProvider } from "./constants";import { notification } from "./notification";import { notificationDeadLetter, notificationOutbox } from "./outbox";
export const notificationProviderSchema = z.enum(notificationProvider);
export const selectNotificationSchema = createSelectSchema(notification).extend( { data: z .preprocess((val) => { return String(val); }, z.string()) .prefault("{}"), },);
// we need to extend, otherwise data can be `null` or `undefined` - default is notexport const insertNotificationSchema = createInsertSchema(notification).extend( { data: z.string().prefault("{}"), monitors: z.array(z.number()).prefault([]), },);
export type InsertNotification = z.infer<typeof insertNotificationSchema>;export type Notification = z.infer<typeof selectNotificationSchema>;export type NotificationProvider = z.infer<typeof notificationProviderSchema>;
const phoneRegex = new RegExp( /^([+]?[\s0-9]+)?(\d{3}|[(]?[0-9]+[)])?([-]?[\s]?[0-9])+$/,);
export const phoneSchema = z.string().regex(phoneRegex, "Invalid Number!");export const emailSchema = z.email();export const urlSchema = safeUrlSchema;
export const ntfyDataSchema = z.object({ ntfy: z.object({ topic: z.string().prefault(""), serverUrl: safeUrlSchema.prefault("https://ntfy.sh"), token: z.string().optional(), }),});export const webhookDataSchema = z.object({ webhook: z.object({ endpoint: safeUrlSchema, headers: z .array(z.object({ key: z.string(), value: z.string() })) .optional(), }),});export const emailDataSchema = z.object({ email: emailSchema });export const phoneDataSchema = z.object({ sms: phoneSchema });export const slackDataSchema = z.object({ slack: urlSchema });export const discordDataSchema = z.object({ discord: urlSchema });export const googleChatDataSchema = z.object({ "google-chat": urlSchema });export const pagerdutyDataSchema = z.object({ pagerduty: z.string() });export const opsgenieDataSchema = z.object({ opsgenie: z.object({ apiKey: z.string(), region: z.enum(["us", "eu"], { message: "Please select a region", }), }),});export const telegramDataSchema = z.object({ telegram: z.object({ chatId: z.string() }),});
export const grafanaOncallDataSchema = z.object({ "grafana-oncall": z.object({ webhookUrl: safeUrlSchema, }),});
export const msTeamsDataSchema = z.object({ "ms-teams": z.object({ webhookUrl: safeUrlSchema, }),});
export const whatsappDataSchema = z.object({ whatsapp: phoneSchema,});
export const NotificationDataSchema = z.union([ discordDataSchema, emailDataSchema, grafanaOncallDataSchema, msTeamsDataSchema, ntfyDataSchema, opsgenieDataSchema, pagerdutyDataSchema, phoneDataSchema, telegramDataSchema, slackDataSchema, webhookDataSchema, whatsappDataSchema, googleChatDataSchema,]);
export const InsertNotificationWithDataSchema = z.discriminatedUnion( "provider", [ insertNotificationSchema.extend( z.object({ provider: z.literal("discord"), data: discordDataSchema, }).shape, ), insertNotificationSchema.extend( z.object({ provider: z.literal("email"), data: emailDataSchema, }).shape, ), insertNotificationSchema.extend( z.object({ provider: z.literal("google-chat"), data: googleChatDataSchema, }).shape, ), insertNotificationSchema.extend( z.object({ provider: z.literal("grafana-oncall"), data: grafanaOncallDataSchema, }).shape, ), insertNotificationSchema.extend( z.object({ provider: z.literal("ms-teams"), data: msTeamsDataSchema, }).shape, ), insertNotificationSchema.extend( z.object({ provider: z.literal("ntfy"), data: ntfyDataSchema, }).shape, ), insertNotificationSchema.extend( z.object({ provider: z.literal("pagerduty"), data: pagerdutyDataSchema, }).shape, ), insertNotificationSchema.extend( z.object({ provider: z.literal("opsgenie"), data: opsgenieDataSchema, }).shape, ), insertNotificationSchema.extend( z.object({ provider: z.literal("sms"), data: phoneDataSchema, }).shape, ), insertNotificationSchema.extend( z.object({ provider: z.literal("slack"), data: slackDataSchema, }).shape, ), insertNotificationSchema.extend( z.object({ provider: z.literal("telegram"), data: telegramDataSchema, }).shape, ), insertNotificationSchema.extend( z.object({ provider: z.literal("webhook"), data: webhookDataSchema, }).shape, ), insertNotificationSchema.extend( z.object({ provider: z.literal("whatsapp"), data: whatsappDataSchema, }).shape, ), ],);
export type InsertNotificationWithData = z.infer< typeof InsertNotificationWithDataSchema>;
export const notificationOutboxPayloadSchema = z.object({ regions: z.array(z.string()), statusCode: z.number().optional(), message: z.string().optional(), latency: z.number().optional(),});
export type NotificationOutboxPayload = z.infer< typeof notificationOutboxPayloadSchema>;
export const selectNotificationOutboxSchema = createSelectSchema(notificationOutbox);export const selectNotificationDeadLetterSchema = createSelectSchema( notificationDeadLetter,);
export type NotificationOutboxRow = typeof notificationOutbox.$inferSelect;export type NotificationDeadLetterRow = typeof notificationDeadLetter.$inferSelect;