Open-source status page, incident response and uptime monitoring
TypeScript alertingincident-managementmcpmcp-servermonitoringmonitoring-as-codeobservabilityon-callopen-sourcesrestatus-pagestatuspagesynthetic-monitoringterraformtinybirdtursouptimeuptime-checkeruptime-monitor
import type { NotificationContext } from "@openstatus/notification-base";
import { safeFetch, transformHeaders } from "@openstatus/utils";
import { PayloadSchema, WebhookSchema } from "./schema";
export const sendAlert = async ({
monitor,
notification,
cronTimestamp,
statusCode,
latency,
message,
}: NotificationContext) => {
const notificationData = WebhookSchema.parse(JSON.parse(notification.data));
const body = PayloadSchema.parse({
monitor: monitor,
cronTimestamp,
status: "error",
statusCode,
latency,
errorMessage: message,
});
const res = await safeFetch(notificationData.webhook.endpoint, {
method: "post",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
...transformHeaders(notificationData.webhook.headers ?? []),
},
});
if (!res.ok) {
throw new Error(`Failed to send webhook notification: ${res.statusText}`);
}
};
export const sendRecovery = async ({
monitor,
notification,
cronTimestamp,
latency,
statusCode,
message,
}: NotificationContext) => {
const notificationData = WebhookSchema.parse(JSON.parse(notification.data));
const body = PayloadSchema.parse({
monitor: monitor,
cronTimestamp,
status: "recovered",
statusCode,
latency,
errorMessage: message,
});
const url = notificationData.webhook.endpoint;
const res = await safeFetch(url, {
method: "post",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
...transformHeaders(notificationData.webhook.headers ?? []),
},
});
if (!res.ok) {
throw new Error(`Failed to send webhook notification: ${res.statusText}`);
}
};
export const sendDegraded = async ({
monitor,
notification,
cronTimestamp,
latency,
statusCode,
message,
}: NotificationContext) => {
const notificationData = WebhookSchema.parse(JSON.parse(notification.data));
const body = PayloadSchema.parse({
monitor: monitor,
cronTimestamp,
status: "degraded",
statusCode,
latency,
errorMessage: message,
});
const res = await safeFetch(notificationData.webhook.endpoint, {
method: "post",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
...transformHeaders(notificationData.webhook.headers ?? []),
},
});
if (!res.ok) {
throw new Error(`Failed to send webhook notification: ${res.statusText}`);
}
};
export const sendTest = async ({
url,
headers,
}: {
url: string;
headers?: { key: string; value: string }[];
}) => {
const body = PayloadSchema.parse({
monitor: {
id: 1,
name: "test",
url: "http://openstat.us",
},
cronTimestamp: Date.now(),
status: "recovered",
statusCode: 200,
latency: 1337,
});
try {
const response = await safeFetch(url, {
method: "post",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
...transformHeaders(headers ?? []),
},
});
if (!response.ok) {
throw new Error("Failed to send test");
}
return true;
} catch (err) {
console.log(err);
throw new Error("Failed to send test");
}
};