Skip to content

Open-source status page, incident response and uptime monitoring

refactor: notifications schema (#1624)

Maximilian Kaske17b5eb9ea624
diff --git a/apps/workflows/.dockerignore b/apps/workflows/.dockerignore
index 5dad7bbcf..c304379d7 100644
--- a/apps/workflows/.dockerignore
+++ b/apps/workflows/.dockerignore
@@ -1,4 +1,4 @@
-# This file is generated by Dofigen v2.5.1
+# This file is generated by Dofigen v2.5.0
# See https://github.com/lenra-io/dofigen
node_modules
diff --git a/apps/workflows/Dockerfile b/apps/workflows/Dockerfile
index 774c44291..a610f4a52 100644
--- a/apps/workflows/Dockerfile
+++ b/apps/workflows/Dockerfile
@@ -1,5 +1,5 @@
# syntax=docker/dockerfile:1.11
-# This file is generated by Dofigen v2.5.1
+# This file is generated by Dofigen v2.5.0
# See https://github.com/lenra-io/dofigen
# ca-certs
@@ -84,7 +84,7 @@ RUN bun install
# runtime
FROM debian@sha256:530a3348fc4b5734ffe1a137ddbcee6850154285251b53c3425c386ea8fac77b AS runtime
LABEL \
- io.dofigen.version="2.5.1" \
+ io.dofigen.version="2.5.0" \
org.opencontainers.image.authors="OpenStatus Team" \
org.opencontainers.image.base.digest="sha256:530a3348fc4b5734ffe1a137ddbcee6850154285251b53c3425c386ea8fac77b" \
org.opencontainers.image.base.name="docker.io/debian:bullseye-slim" \
diff --git a/packages/notifications/discord/src/index.ts b/packages/notifications/discord/src/index.ts
index e6c1eb50e..43900cc82 100644
--- a/packages/notifications/discord/src/index.ts
+++ b/packages/notifications/discord/src/index.ts
@@ -1,6 +1,7 @@
import type { Monitor, Notification } from "@openstatus/db/src/schema";
+import { discordDataSchema } from "@openstatus/db/src/schema";
import type { Region } from "@openstatus/db/src/schema/constants";
-import { DataSchema } from "./schema";
+
const postToWebhook = async (content: string, webhookUrl: string) => {
await fetch(webhookUrl, {
method: "POST",
@@ -32,13 +33,21 @@ export const sendAlert = async ({
latency?: number;
region?: Region;
}) => {
- const notificationData = DataSchema.parse(JSON.parse(notification.data));
+ const notificationData = discordDataSchema.parse(
+ JSON.parse(notification.data),
+ );
const { discord: webhookUrl } = notificationData; // webhook url
const { name } = monitor;
try {
await postToWebhook(
- `**🚨 Alert [${name}](<${monitor.url}>)**\nStatus Code: ${statusCode || "_empty_"}\nMessage: ${message || "_empty_"}\nCron Timestamp: ${cronTimestamp} (${new Date(cronTimestamp).toISOString()})\n> Check your [Dashboard](<https://www.openstatus.dev/app/>).\n`,
+ `**🚨 Alert [${name}](<${monitor.url}>)**\nStatus Code: ${
+ statusCode || "_empty_"
+ }\nMessage: ${
+ message || "_empty_"
+ }\nCron Timestamp: ${cronTimestamp} (${new Date(
+ cronTimestamp,
+ ).toISOString()})\n> Check your [Dashboard](<https://www.openstatus.dev/app/>).\n`,
webhookUrl,
);
} catch (err) {
@@ -66,7 +75,9 @@ export const sendRecovery = async ({
latency?: number;
region?: Region;
}) => {
- const notificationData = DataSchema.parse(JSON.parse(notification.data));
+ const notificationData = discordDataSchema.parse(
+ JSON.parse(notification.data),
+ );
const { discord: webhookUrl } = notificationData; // webhook url
const { name } = monitor;
@@ -100,7 +111,9 @@ export const sendDegraded = async ({
latency?: number;
region?: Region;
}) => {
- const notificationData = DataSchema.parse(JSON.parse(notification.data));
+ const notificationData = discordDataSchema.parse(
+ JSON.parse(notification.data),
+ );
const { discord: webhookUrl } = notificationData; // webhook url
const { name } = monitor;
diff --git a/packages/notifications/discord/src/schema.ts b/packages/notifications/discord/src/schema.ts
deleted file mode 100644
index 7e80ca73d..000000000
--- a/packages/notifications/discord/src/schema.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-import { z } from "zod";
-
-export const DataSchema = z.object({
- discord: z.string(),
-});
diff --git a/packages/notifications/ntfy/src/index.ts b/packages/notifications/ntfy/src/index.ts
index 6381c8825..51d18b78b 100644
--- a/packages/notifications/ntfy/src/index.ts
+++ b/packages/notifications/ntfy/src/index.ts
@@ -1,7 +1,7 @@
import type { Monitor, Notification } from "@openstatus/db/src/schema";
+import { ntfyDataSchema } from "@openstatus/db/src/schema";
import type { Region } from "@openstatus/db/src/schema/constants";
-import { NtfySchema } from "./schema";
export const sendAlert = async ({
monitor,
@@ -20,7 +20,7 @@ export const sendAlert = async ({
latency?: number;
region?: Region;
}) => {
- const notificationData = NtfySchema.parse(JSON.parse(notification.data));
+ const notificationData = ntfyDataSchema.parse(JSON.parse(notification.data));
const { name } = monitor;
const body = `Your monitor ${name} / ${monitor.url} is down with ${
@@ -68,7 +68,7 @@ export const sendRecovery = async ({
latency?: number;
region?: Region;
}) => {
- const notificationData = NtfySchema.parse(JSON.parse(notification.data));
+ const notificationData = ntfyDataSchema.parse(JSON.parse(notification.data));
const { name } = monitor;
const body = `Your monitor ${name} / ${monitor.url} is up again`;
@@ -110,7 +110,7 @@ export const sendDegraded = async ({
latency?: number;
region?: Region;
}) => {
- const notificationData = NtfySchema.parse(JSON.parse(notification.data));
+ const notificationData = ntfyDataSchema.parse(JSON.parse(notification.data));
const { name } = monitor;
const body = `Your monitor ${name} / ${monitor.url} is degraded `;
diff --git a/packages/notifications/opsgenie/src/schema.ts b/packages/notifications/opsgenie/src/schema.ts
index d2ffa419d..e315a31e4 100644
--- a/packages/notifications/opsgenie/src/schema.ts
+++ b/packages/notifications/opsgenie/src/schema.ts
@@ -1,11 +1,7 @@
+import { opsgenieDataSchema } from "@openstatus/db/src/schema";
import { z } from "zod";
-export const OpsGenieSchema = z.object({
- opsgenie: z.object({
- apiKey: z.string(),
- region: z.enum(["eu", "us"]),
- }),
-});
+export const OpsGenieSchema = opsgenieDataSchema;
export const OpsGeniePayloadAlert = z.object({
message: z.string(),
diff --git a/packages/notifications/slack/src/index.ts b/packages/notifications/slack/src/index.ts
index 797c51cbc..f61518a9c 100644
--- a/packages/notifications/slack/src/index.ts
+++ b/packages/notifications/slack/src/index.ts
@@ -1,6 +1,6 @@
import type { Monitor, Notification } from "@openstatus/db/src/schema";
+import { slackDataSchema } from "@openstatus/db/src/schema";
import type { Region } from "@openstatus/db/src/schema/constants";
-import { DataSchema } from "./schema";
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
const postToWebhook = async (body: any, webhookUrl: string) => {
@@ -33,7 +33,7 @@ export const sendAlert = async ({
latency?: number;
region?: Region;
}) => {
- const notificationData = DataSchema.parse(JSON.parse(notification.data));
+ const notificationData = slackDataSchema.parse(JSON.parse(notification.data));
const { slack: webhookUrl } = notificationData; // webhook url
const { name } = monitor;
@@ -94,7 +94,7 @@ export const sendRecovery = async ({
region?: Region;
latency?: number;
}) => {
- const notificationData = DataSchema.parse(JSON.parse(notification.data));
+ const notificationData = slackDataSchema.parse(JSON.parse(notification.data));
const { slack: webhookUrl } = notificationData; // webhook url
const { name } = monitor;
@@ -147,7 +147,7 @@ export const sendDegraded = async ({
region?: Region;
latency?: number;
}) => {
- const notificationData = DataSchema.parse(JSON.parse(notification.data));
+ const notificationData = slackDataSchema.parse(JSON.parse(notification.data));
const { slack: webhookUrl } = notificationData; // webhook url
const { name } = monitor;
diff --git a/packages/notifications/slack/src/mock.ts b/packages/notifications/slack/src/mock.ts
index 4bee892af..3300a9e05 100644
--- a/packages/notifications/slack/src/mock.ts
+++ b/packages/notifications/slack/src/mock.ts
@@ -27,6 +27,10 @@ const monitor: Monitor = {
status: "active",
method: "GET",
deletedAt: null,
+ otelEndpoint: null,
+ otelHeaders: [],
+ retry: null,
+ followRedirects: null,
};
const notification: Notification = {
diff --git a/packages/notifications/slack/src/schema.ts b/packages/notifications/slack/src/schema.ts
deleted file mode 100644
index efb235a97..000000000
--- a/packages/notifications/slack/src/schema.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-import { z } from "zod";
-
-export const DataSchema = z.object({
- slack: z.string(),
-});
diff --git a/packages/notifications/telegram/src/index.ts b/packages/notifications/telegram/src/index.ts
index 00bb3071e..add4a4d7b 100644
--- a/packages/notifications/telegram/src/index.ts
+++ b/packages/notifications/telegram/src/index.ts
@@ -1,7 +1,7 @@
import type { Monitor, Notification } from "@openstatus/db/src/schema";
+import { telegramDataSchema } from "@openstatus/db/src/schema";
import type { Region } from "@openstatus/db/src/schema/constants";
-import { TelegramSchema } from "./schema";
export const sendAlert = async ({
monitor,
@@ -20,7 +20,9 @@ export const sendAlert = async ({
latency?: number;
region?: Region;
}) => {
- const notificationData = TelegramSchema.parse(JSON.parse(notification.data));
+ const notificationData = telegramDataSchema.parse(
+ JSON.parse(notification.data),
+ );
const { name } = monitor;
const body = `Your monitor ${name} / ${monitor.url} is down with ${
@@ -57,7 +59,9 @@ export const sendRecovery = async ({
latency?: number;
region?: Region;
}) => {
- const notificationData = TelegramSchema.parse(JSON.parse(notification.data));
+ const notificationData = telegramDataSchema.parse(
+ JSON.parse(notification.data),
+ );
const { name } = monitor;
const body = `Your monitor ${name} / ${monitor.url} is up again`;
@@ -89,7 +93,9 @@ export const sendDegraded = async ({
latency?: number;
region?: Region;
}) => {
- const notificationData = TelegramSchema.parse(JSON.parse(notification.data));
+ const notificationData = telegramDataSchema.parse(
+ JSON.parse(notification.data),
+ );
const { name } = monitor;
const body = `Your monitor ${name} / ${monitor.url} is degraded `;
diff --git a/packages/notifications/twillio-sms/src/index.ts b/packages/notifications/twillio-sms/src/index.ts
index 041042ceb..a18332cdf 100644
--- a/packages/notifications/twillio-sms/src/index.ts
+++ b/packages/notifications/twillio-sms/src/index.ts
@@ -1,8 +1,8 @@
import type { Monitor, Notification } from "@openstatus/db/src/schema";
+import { phoneDataSchema } from "@openstatus/db/src/schema";
import type { Region } from "@openstatus/db/src/schema/constants";
import { env } from "./env";
-import { SmsConfigurationSchema } from "./schema/config";
export const sendAlert = async ({
monitor,
@@ -21,9 +21,7 @@ export const sendAlert = async ({
latency?: number;
region?: Region;
}) => {
- const notificationData = SmsConfigurationSchema.parse(
- JSON.parse(notification.data),
- );
+ const notificationData = phoneDataSchema.parse(JSON.parse(notification.data));
const { name } = monitor;
const body = new FormData();
@@ -74,9 +72,7 @@ export const sendRecovery = async ({
latency?: number;
region?: Region;
}) => {
- const notificationData = SmsConfigurationSchema.parse(
- JSON.parse(notification.data),
- );
+ const notificationData = phoneDataSchema.parse(JSON.parse(notification.data));
const { name } = monitor;
const body = new FormData();
@@ -120,9 +116,7 @@ export const sendDegraded = async ({
latency?: number;
region?: Region;
}) => {
- const notificationData = SmsConfigurationSchema.parse(
- JSON.parse(notification.data),
- );
+ const notificationData = phoneDataSchema.parse(JSON.parse(notification.data));
const { name } = monitor;
const body = new FormData();
diff --git a/packages/notifications/twillio-sms/src/schema/config.ts b/packages/notifications/twillio-sms/src/schema/config.ts
deleted file mode 100644
index 30bd1745c..000000000
--- a/packages/notifications/twillio-sms/src/schema/config.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-import isMobilephone from "validator/lib/isMobilePhone";
-import { z } from "zod";
-
-export const SmsConfigurationSchema = z.object({
- sms: z.string().refine(isMobilephone),
-});
diff --git a/packages/notifications/webhook/src/schema.ts b/packages/notifications/webhook/src/schema.ts
index 71a1ba631..5fe2144f4 100644
--- a/packages/notifications/webhook/src/schema.ts
+++ b/packages/notifications/webhook/src/schema.ts
@@ -1,13 +1,7 @@
+import { webhookDataSchema } from "@openstatus/db/src/schema";
import { z } from "zod";
-export const WebhookSchema = z.object({
- webhook: z.object({
- endpoint: z.string().url(),
- headers: z
- .array(z.object({ key: z.string(), value: z.string() }))
- .optional(),
- }),
-});
+export const WebhookSchema = webhookDataSchema;
export const PayloadSchema = z.object({
monitor: z.object({