Add ability to customize webhook body
parent
9634398538
commit
63c09f401a
|
|
@ -58,11 +58,19 @@ export type Notification = {
|
|||
webhook?: Webhook
|
||||
}
|
||||
|
||||
export type Webhook = {
|
||||
export type Webhook<TEnv = Env> = {
|
||||
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<TEnv = Env> = {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue