Openstatus www.openstatus.dev
at main 140 lines 3.3 kB view raw
1import type { NotificationContext } from "@openstatus/notification-base"; 2import { transformHeaders } from "@openstatus/utils"; 3import { PayloadSchema, WebhookSchema } from "./schema"; 4 5export const sendAlert = async ({ 6 monitor, 7 notification, 8 cronTimestamp, 9 statusCode, 10 latency, 11 message, 12}: NotificationContext) => { 13 const notificationData = WebhookSchema.parse(JSON.parse(notification.data)); 14 15 const body = PayloadSchema.parse({ 16 monitor: monitor, 17 cronTimestamp, 18 status: "error", 19 statusCode, 20 latency, 21 errorMessage: message, 22 }); 23 24 const res = await fetch(notificationData.webhook.endpoint, { 25 method: "post", 26 body: JSON.stringify(body), 27 headers: notificationData.webhook.headers 28 ? transformHeaders(notificationData.webhook.headers) 29 : { 30 "Content-Type": "application/json", 31 }, 32 }); 33 if (!res.ok) { 34 throw new Error(`Failed to send webhook notification: ${res.statusText}`); 35 } 36}; 37 38export const sendRecovery = async ({ 39 monitor, 40 notification, 41 cronTimestamp, 42 latency, 43 statusCode, 44 message, 45}: NotificationContext) => { 46 const notificationData = WebhookSchema.parse(JSON.parse(notification.data)); 47 48 const body = PayloadSchema.parse({ 49 monitor: monitor, 50 cronTimestamp, 51 status: "recovered", 52 statusCode, 53 latency, 54 errorMessage: message, 55 }); 56 const url = notificationData.webhook.endpoint; 57 const res = await fetch(url, { 58 method: "post", 59 body: JSON.stringify(body), 60 headers: notificationData.webhook.headers 61 ? transformHeaders(notificationData.webhook.headers) 62 : { 63 "Content-Type": "application/json", 64 }, 65 }); 66 if (!res.ok) { 67 throw new Error(`Failed to send webhook notification: ${res.statusText}`); 68 } 69}; 70 71export const sendDegraded = async ({ 72 monitor, 73 notification, 74 cronTimestamp, 75 latency, 76 statusCode, 77 message, 78}: NotificationContext) => { 79 const notificationData = WebhookSchema.parse(JSON.parse(notification.data)); 80 81 const body = PayloadSchema.parse({ 82 monitor: monitor, 83 cronTimestamp, 84 status: "degraded", 85 statusCode, 86 latency, 87 errorMessage: message, 88 }); 89 90 const res = await fetch(notificationData.webhook.endpoint, { 91 method: "post", 92 body: JSON.stringify(body), 93 headers: notificationData.webhook.headers 94 ? transformHeaders(notificationData.webhook.headers) 95 : { 96 "Content-Type": "application/json", 97 }, 98 }); 99 if (!res.ok) { 100 throw new Error(`Failed to send webhook notification: ${res.statusText}`); 101 } 102}; 103 104export const sendTest = async ({ 105 url, 106 headers, 107}: { 108 url: string; 109 headers?: { key: string; value: string }[]; 110}) => { 111 const body = PayloadSchema.parse({ 112 monitor: { 113 id: 1, 114 name: "test", 115 url: "http://openstat.us", 116 }, 117 cronTimestamp: Date.now(), 118 status: "recovered", 119 statusCode: 200, 120 latency: 1337, 121 }); 122 try { 123 const response = await fetch(url, { 124 method: "post", 125 body: JSON.stringify(body), 126 headers: headers 127 ? transformHeaders(headers) 128 : { 129 "Content-Type": "application/json", 130 }, 131 }); 132 if (!response.ok) { 133 throw new Error("Failed to send test"); 134 } 135 return true; 136 } catch (err) { 137 console.log(err); 138 throw new Error("Failed to send test"); 139 } 140};