diff --git a/dist/lib/classes/Handler.js b/dist/lib/classes/Handler.js new file mode 100644 index 0000000..48a44af --- /dev/null +++ b/dist/lib/classes/Handler.js @@ -0,0 +1,115 @@ +"use strict"; +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 __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var consts_1 = require("@scode/consts"); +var Transaction_1 = __importDefault(require("./Transaction")); +var Handler = /** @class */ (function () { + function Handler() { + this._handlers = []; + this._method_handlers = {}; + } + Handler.prototype.register_handler = function (f, method) { + if (typeof method === 'undefined') { + this._handlers.push(f); + } + else { + var m = method.toUpperCase(); + if (typeof this._method_handlers[m] !== 'undefined') + throw new Error("Handler for " + m + " already registered"); + this._method_handlers[m] = f; + } + }; + Handler.prototype.run_method_handler = function (method, t) { + return __awaiter(this, void 0, void 0, function () { + var m; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + m = method.toUpperCase(); + if (!(typeof this._method_handlers[m] !== 'undefined')) return [3 /*break*/, 2]; + return [4 /*yield*/, this._method_handlers[m](t)]; + case 1: + _a.sent(); + _a.label = 2; + case 2: return [2 /*return*/]; + } + }); + }); + }; + Handler.prototype.run_http_handler = function (req, res) { + return __awaiter(this, void 0, void 0, function () { + var t, _i, _a, handler; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: + t = new Transaction_1.default(req, res); + _i = 0, _a = this._handlers; + _b.label = 1; + case 1: + if (!(_i < _a.length)) return [3 /*break*/, 4]; + handler = _a[_i]; + return [4 /*yield*/, handler(t)]; + case 2: + // eslint-disable-next-line no-await-in-loop + if ((_b.sent()) === false) { + if (!t.has_status) + t.status = consts_1.http.status_internal_server_error; + t.finalize(); + return [2 /*return*/]; + } + _b.label = 3; + case 3: + _i++; + return [3 /*break*/, 1]; + case 4: return [4 /*yield*/, this.run_method_handler('ALL', t)]; + case 5: + _b.sent(); + return [4 /*yield*/, this.run_method_handler(t.req.method, t)]; + case 6: + _b.sent(); + return [2 /*return*/]; + } + }); + }); + }; + return Handler; +}()); +exports.default = Handler; diff --git a/dist/lib/classes/Session.js b/dist/lib/classes/Session.js new file mode 100644 index 0000000..bb5526a --- /dev/null +++ b/dist/lib/classes/Session.js @@ -0,0 +1,8 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var Session = /** @class */ (function () { + function Session() { + } + return Session; +}()); +exports.default = Session; diff --git a/dist/lib/classes/Status.js b/dist/lib/classes/Status.js new file mode 100644 index 0000000..04ff7c9 --- /dev/null +++ b/dist/lib/classes/Status.js @@ -0,0 +1,50 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var consts_1 = require("@scode/consts"); +var Status = /** @class */ (function () { + function Status() { + this._status = -1; + } + Object.defineProperty(Status.prototype, "status", { + get: function () { + if (this._status === -1) + throw new Error('status undefined'); + return this._status; + }, + set: function (value) { + this._status = value; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Status.prototype, "has_status", { + get: function () { + return this._status !== -1; + }, + enumerable: true, + configurable: true + }); + /* + * Status setters + */ + Status.prototype.ok = function () { + this._status = consts_1.http.status_ok; + }; + Status.prototype.ok_no_content = function () { + this._status = consts_1.http.status_ok_no_content; + }; + Status.prototype.bad_request = function () { + this._status = consts_1.http.status_bad_request; + }; + Status.prototype.unauthorized = function () { + this._status = consts_1.http.status_unauthorized; + }; + Status.prototype.forbidden = function () { + this._status = consts_1.http.status_forbidden; + }; + Status.prototype.not_found = function () { + this._status = consts_1.http.status_not_found; + }; + return Status; +}()); +exports.default = Status; diff --git a/dist/lib/classes/Transaction.js b/dist/lib/classes/Transaction.js new file mode 100644 index 0000000..486e1d5 --- /dev/null +++ b/dist/lib/classes/Transaction.js @@ -0,0 +1,38 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var consts_1 = require("@scode/consts"); +var Transaction = /** @class */ (function () { + /* constructor */ + function Transaction(req, res) { + /* public */ + this.status = -1; + this._req = req; + this._res = res; + } + Object.defineProperty(Transaction.prototype, "req", { + get: function () { return this._req; }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transaction.prototype, "res", { + get: function () { return this._res; }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transaction.prototype, "has_status", { + get: function () { + return Object.values(consts_1.http.status_codes) + .includes(this.status); + }, + enumerable: true, + configurable: true + }); + /* methods */ + Transaction.prototype.finalize = function (data) { + if (this.has_status) + this._res.status(this.status); + this._res.end(data); + }; + return Transaction; +}()); +exports.default = Transaction; diff --git a/dist/lib/classes/status.js b/dist/lib/classes/status.js new file mode 100644 index 0000000..958cf8f --- /dev/null +++ b/dist/lib/classes/status.js @@ -0,0 +1,53 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var consts_1 = __importDefault(require("@scode/consts")); +var Status = /** @class */ (function () { + function Status() { + this._status = -1; + } + Object.defineProperty(Status.prototype, "status", { + get: function () { + if (this._status === -1) + throw new Error('status undefined'); + return this._status; + }, + set: function (value) { + this._status = value; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Status.prototype, "has_status", { + get: function () { + return this._status !== -1; + }, + enumerable: true, + configurable: true + }); + /* + * Status setters + */ + Status.prototype.ok = function () { + this._status = consts_1.default.status_ok; + }; + Status.prototype.ok_no_content = function () { + this._status = consts_1.default.status_ok_no_content; + }; + Status.prototype.bad_request = function () { + this._status = consts_1.default.status_bad_request; + }; + Status.prototype.unauthorized = function () { + this._status = consts_1.default.status_unauthorized; + }; + Status.prototype.forbidden = function () { + this._status = consts_1.default.status_forbidden; + }; + Status.prototype.not_found = function () { + this._status = consts_1.default.status_not_found; + }; + return Status; +}()); +exports.default = Status; diff --git a/dist/lib/classes/transaction.js b/dist/lib/classes/transaction.js new file mode 100644 index 0000000..d0f81ad --- /dev/null +++ b/dist/lib/classes/transaction.js @@ -0,0 +1,38 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var Status_ts_1 = __importDefault(require("./Status.ts")); +var Transaction = /** @class */ (function () { + /* constructor */ + function Transaction(req, res) { + this._req = req; + this._res = res; + this._status = new Status_ts_1.default; + } + Object.defineProperty(Transaction.prototype, "req", { + /* public */ + get: function () { return this._req; }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transaction.prototype, "res", { + get: function () { return this._res; }, + enumerable: true, + configurable: true + }); + Object.defineProperty(Transaction.prototype, "status", { + get: function () { return this._status; }, + enumerable: true, + configurable: true + }); + /* methods */ + Transaction.prototype.finalize = function () { + if (this._status.has_status()) + this._res.status(this.status.status); + this._res.end(data); + }; + return Transaction; +}()); +exports.default = Transaction; diff --git a/dist/lib/index.js b/dist/lib/index.js new file mode 100644 index 0000000..cc03429 --- /dev/null +++ b/dist/lib/index.js @@ -0,0 +1,21 @@ +"use strict"; +function __export(m) { + for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; +} +Object.defineProperty(exports, "__esModule", { value: true }); +/** + * register an array of handlers to an express app + * + * @param {Application} app express app + * @param {Array} handlers handlers to register + */ +function load_handlers(app, handlers) { + for (var _i = 0, handlers_1 = handlers; _i < handlers_1.length; _i++) { + var h = handlers_1[_i]; + app.use(h.path, h.run_http_handler); + } +} +exports.default = load_handlers; +__export(require("./classes/Session")); +__export(require("./classes/Transaction")); +__export(require("./classes/Handler")); diff --git a/dist/test/main.js b/dist/test/main.js new file mode 100644 index 0000000..b405654 --- /dev/null +++ b/dist/test/main.js @@ -0,0 +1,9 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var ava_1 = __importDefault(require("ava")); +ava_1.default('testing', function (t) { + t.is(true, true); +}); diff --git a/package.json b/package.json index 2876119..f460d9a 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "test": "nyc ava", - "lint": "eslint .", + "lint": "eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs", "ci": "yarn --frozen-lockfile && node jenkins.js", "mutate": "stryker run" },