uptime-kuma/server/notification-providers/notification-provider.js

74 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-09-07 10:42:46 -04:00
class NotificationProvider {
/**
* Notification Provider Name
* @type {string}
2021-09-07 10:42:46 -04:00
*/
name = undefined;
/**
* Send a notification
* @param {BeanModel} notification Notification to send
* @param {string} msg General Message
* @param {?object} monitorJSON Monitor details (For Up/Down only)
* @param {?object} heartbeatJSON Heartbeat details (For Up/Down only)
2021-09-07 10:42:46 -04:00
* @returns {Promise<string>} Return Successful Message
* @throws Error with fail msg
2021-09-07 10:42:46 -04:00
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
throw new Error("Have to override Notification.send(...)");
}
/**
* Extracts the address from a monitor JSON object based on its type.
* @param {?object} monitorJSON Monitor details (For Up/Down only)
* @returns {string} The extracted address based on the monitor type.
*/
2024-06-13 11:04:38 -04:00
extractAdress(monitorJSON) {
if (!monitorJSON) {
2024-06-13 11:04:38 -04:00
return "";
}
switch (monitorJSON["type"]) {
case "push":
return "Heartbeat";
case "ping":
return monitorJSON["hostname"];
case "port":
case "dns":
case "gamedig":
case "steam":
if (monitorJSON["port"]) {
return monitorJSON["hostname"] + ":" + monitorJSON["port"];
}
2024-06-13 11:04:38 -04:00
return monitorJSON["hostname"];
default:
if (![ "https://", "http://", "" ].includes(monitorJSON["url"])) {
return monitorJSON["url"];
}
return "";
}
}
/**
* Throws an error
* @param {any} error The error to throw
* @returns {void}
* @throws {any} The error specified
*/
2021-09-07 10:42:46 -04:00
throwGeneralAxiosError(error) {
let msg = "Error: " + error + " ";
if (error.response && error.response.data) {
if (typeof error.response.data === "string") {
msg += error.response.data;
} else {
2022-04-13 12:30:32 -04:00
msg += JSON.stringify(error.response.data);
2021-09-07 10:42:46 -04:00
}
}
2022-04-13 12:30:32 -04:00
throw new Error(msg);
2021-09-07 10:42:46 -04:00
}
}
module.exports = NotificationProvider;