hypershield
Version:
Middleware suite for high-performance and resilient APIs
57 lines • 2.18 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlertSenderFactory = void 0;
class ConsoleAlertSender {
send(alert) {
return __awaiter(this, void 0, void 0, function* () {
const color = this.getSeverityColor(alert.severity);
console.log(`[${alert.severity.toUpperCase()}] ${color}${alert.message}\x1b[0m`);
});
}
getSeverityColor(severity) {
const colors = {
info: '\x1b[36m',
warning: '\x1b[33m',
error: '\x1b[31m',
critical: '\x1b[41m'
};
return colors[severity];
}
}
class WebhookAlertSender {
constructor(url) {
this.url = url;
}
send(alert) {
return __awaiter(this, void 0, void 0, function* () {
yield fetch(this.url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(alert)
});
});
}
}
class AlertSenderFactory {
static createSender(destination, config) {
switch (destination) {
case 'console':
return new ConsoleAlertSender();
case 'webhook':
return new WebhookAlertSender(config.webhookUrl);
default:
throw new Error(`Alert destination ${destination} not implemented`);
}
}
}
exports.AlertSenderFactory = AlertSenderFactory;
//# sourceMappingURL=alertSender.js.map