nodejs-rigorous
Version:
Rigorous Framework
167 lines (139 loc) • 5.46 kB
JavaScript
;
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
/*
* CHART CODE FOR ROUTE:
*
* 1/4 SECURE INPUT (avoid script injection)
* 2/4 CHECK CONFORMITY INPUT (to avoid integrity issues when editing multiple collections, if one fail because of validity)
* 3/4 CHECK AUTHORIZATION (is the user legitim ?)
* 4/4 PROCESS (handle not found case)
*/
/* abstract */
var RigorousRoute =
/*#__PURE__*/
function () {
function RigorousRoute(middlewares, operationParams) {
_classCallCheck(this, RigorousRoute);
this.operationParams = operationParams;
this.userIdAsking = null;
this.middlewares = middlewares;
}
_createClass(RigorousRoute, [{
key: "exec",
value: function () {
var _exec = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee(req) {
var inputs, checker, object;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return this.secureInput(req);
case 2:
inputs = _context.sent;
_context.next = 5;
return this.checkConformityInput(inputs);
case 5:
checker = _context.sent;
_context.next = 8;
return this.checkAuthorization(checker);
case 8:
_context.next = 10;
return this.processData(inputs, checker);
case 10:
object = _context.sent;
_context.next = 13;
return this.sendNotification(inputs, object.notificationParam);
case 13:
return _context.abrupt("return", object.result);
case 14:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
function exec(_x) {
return _exec.apply(this, arguments);
}
return exec;
}()
}, {
key: "sucess",
value: function sucess(res, data) {
this;
res.status(200).json({
data: data
});
}
}, {
key: "error",
value: function error(res, err) {
this;
res.status(500).json({
stack: err.stack,
response: err.response,
created_at: new Date()
});
}
/**
*
* @param {*} callType : 'get' or 'post'
* @param {*} relativePath
* @param {*} middlewares
*/
}, {
key: "routeIt",
value: function routeIt(app, callType, relativePath) {
var _this = this;
try {
app[callType]("".concat(relativePath), this.middlewares,
/*#__PURE__*/
function () {
var _ref = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee2(req, res) {
var object;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.prev = 0;
_context2.next = 3;
return _this.exec(req);
case 3:
object = _context2.sent;
_this.sucess(res, object);
_context2.next = 11;
break;
case 7:
_context2.prev = 7;
_context2.t0 = _context2["catch"](0);
console.log(_context2.t0);
_this.error(res, _context2.t0);
case 11:
case "end":
return _context2.stop();
}
}
}, _callee2, null, [[0, 7]]);
}));
return function (_x2, _x3) {
return _ref.apply(this, arguments);
};
}());
} catch (err) {
console.log('relativePath ', relativePath);
console.log('err ', err);
}
}
}]);
return RigorousRoute;
}();
module.exports = RigorousRoute;