@brvhprince/termii-js
Version:
JavaScript SDK for Termii Provider
41 lines (40 loc) • 1.51 kB
JavaScript
;
/**
* Project: termii-js
* File: Webhook
* Created by pennycodes on 25/04/2023.
* Copyright termii-js
*/
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var crypto = require("crypto");
var mitt_1 = require("mitt");
var Webhook = /** @class */ (function () {
function Webhook(secret_key) {
this.emitter = (0, mitt_1.default)();
this.on = this.emitter.on;
this.off = this.emitter.off;
this.secret_key = secret_key.trim();
}
Webhook.prototype.middleware = function (request, response) {
response.sendStatus(200);
var hash = crypto.createHmac("sha512", this.secret_key).update(JSON.stringify(request.body)).digest("hex");
if (hash === request.headers["X-Termii-Signature"]) {
// Retrieve the request's body
var _a = request.body, type = _a.type, rest = __rest(_a, ["type"]);
this.emitter.emit(type, rest);
}
};
return Webhook;
}());
exports.default = Webhook;