diff --git a/types/config.ts b/types/config.ts index 014c424..aae9954 100644 --- a/types/config.ts +++ b/types/config.ts @@ -58,11 +58,19 @@ export type Notification = { webhook?: Webhook } -export type Webhook = { +export type Webhook = { url: string method?: 'POST' | 'GET' headers?: { [key: string]: string | number } timeout?: number + body?: ( + env: TEnv, + monitor: MonitorTarget, + isUp: boolean, + timeIncidentStart: number, + timeNow: number, + reason: string + ) => { [key: string]: any } | string } export type Callbacks = { diff --git a/uptime.config.ts b/uptime.config.ts index a7b6175..cd63ee3 100644 --- a/uptime.config.ts +++ b/uptime.config.ts @@ -102,6 +102,19 @@ const workerConfig: WorkerConfig = { // [Optional] header defaults to Content-Type: application/json, additional properties will be merged, example of authorization header below 'Authorization': 'Bearer YOUR_TOKEN_HERE', }, + // [Optional] body to be sent, could be an object or a string + // if it's an object, it will be sent as JSON + // if it's a string, it will be sent as-is + body: (env, monitor, isUp, timeIncidentStart, timeNow, reason) => { + return { + event: 'status_change', + monitor, + isUp, + timeIncidentStart, + timeNow, + reason, + } + }, // [Optional] timeout in millisecond, defaults to 30000 timeout: 30000, }, diff --git a/worker/src/index.ts b/worker/src/index.ts index 6af02cf..d292243 100644 --- a/worker/src/index.ts +++ b/worker/src/index.ts @@ -50,6 +50,25 @@ const Worker = { const controller = new AbortController() const timeoutId = setTimeout(() => controller.abort(), workerConfig.notification.webhook.timeout ?? 30000) // 30 second default timeout const headers = { 'Content-Type': 'application/json' } + let body: { [key: string]: any } | string + if(workerConfig.notification.webhook.body) { + body = workerConfig.notification.webhook.body( + env, + monitor, + isUp, + timeIncidentStart, + timeNow, + reason + ) + } else { + body = { + monitor, + isUp, + timeIncidentStart, + timeNow, + reason, + } + } if(workerConfig.notification.webhook.headers) { Object.assign(headers, workerConfig.notification.webhook.headers) } @@ -57,14 +76,7 @@ const Worker = { await fetch(workerConfig.notification.webhook.url, { method: workerConfig.notification.webhook.method ?? 'POST', headers, - body: JSON.stringify({ - event: 'status_change', - monitor, - isUp, - timeIncidentStart, - timeNow, - reason, - }), + body: typeof body === 'object' ? JSON.stringify(body) : body, signal: controller.signal, }) } finally {