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.
3.2 kB ยท 137 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138import 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"); }};