@squareboat/nest-eyewitness
Version:
A simple easy-to-use package to receive error reports directly to your inbox whenever any exception is witnessed 👀 in your NestJS application.
96 lines (95 loc) • 4.61 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
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());
});
};
var EyewitnessService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EyewitnessService = void 0;
const interfaces_1 = require("./interfaces");
const http_1 = require("./http");
const common_1 = require("@nestjs/common");
const provider_map_1 = require("./provider.map");
const nest_mailman_1 = require("@squareboat/nest-mailman");
const template_1 = require("./resources/template");
let EyewitnessService = EyewitnessService_1 = class EyewitnessService {
constructor(config) {
EyewitnessService_1.config = config;
}
static getConfig() {
return EyewitnessService_1.config;
}
static alert(exception, host) {
return __awaiter(this, void 0, void 0, function* () {
const { emails, subject, webhooks } = EyewitnessService_1.config;
const payload = this.buildPayload(exception, host);
const finalSubject = (subject || ":error Error Captured").replace(":error", payload.exception.name);
yield nest_mailman_1.Mailman.init()
.to(emails)
.subject(finalSubject)
.template(template_1.TEMPLATE, payload)
.send();
if (Array.isArray(webhooks) && webhooks.length > 0) {
webhooks.forEach((e) => {
EyewitnessService_1.handleAPICall(e, payload);
});
}
return;
});
}
static handleAPICall(webhookOptions, defaultPayload) {
const { url, method, header } = webhookOptions;
let httpCallOptions = {
url: url,
payload: defaultPayload,
header: header || {},
};
if (webhookOptions.requestBuilder) {
httpCallOptions.payload = webhookOptions.requestBuilder(defaultPayload);
}
if (method == interfaces_1.WebhookSupportMethod.GET) {
return http_1.CustomHttpService.get(httpCallOptions);
}
return http_1.CustomHttpService.post(httpCallOptions);
}
static buildPayload(exception, host) {
const ctx = host.switchToHttp();
const request = ctx.getRequest();
return {
exception: {
name: exception.constructor.name,
message: exception.message,
stack: exception.stack.toString(),
},
request: {
timestamp: new Date().toString(),
headers: JSON.stringify(request.headers),
payload: JSON.stringify(Object.assign(Object.assign(Object.assign({}, request.query), request.body), request.params)),
url: request.protocol + "://" + request.get("host") + request.originalUrl,
},
};
}
};
EyewitnessService = EyewitnessService_1 = __decorate([
common_1.Injectable(),
__param(0, common_1.Inject(provider_map_1.EYEWITNESS_OPTIONS)),
__metadata("design:paramtypes", [Object])
], EyewitnessService);
exports.EyewitnessService = EyewitnessService;