UNPKG

node-rigorous

Version:
238 lines (191 loc) 10 kB
"use strict"; function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } 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; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } /* eslint class-methods-use-this: 0 */ var _ = require('lodash'); var _require = require('../../factory/RigorousError/index'), RigorousError = _require.RigorousError, errorTypes = _require.errorTypes; var RigorousRoute = require('../RigorousRoute'); var rigorousConfig = require('../../config'); var h_format_checker = require('../../helpers/h_format_checker'); var h_secure_user_input = require('../../helpers/h_secure_user_input'); /** * * &reverse= true|false indique la direction (depuis la fin ou depuis le début) * &lastPaginateId = xxxxxxxxxx (_id d'un objet de la collection) indique le point de départ * * Note: * /switlist/me/read/paginate/all?reverse=true <=> /switlist/me/read/paginate/all?reverse=true&lastPaginateId=ffffffffffffffffffffffff * /switlist/me/read/paginate/all?reverse=false <=> /switlist/me/read/paginate/all?reverse=false&lastPaginateId=111111111111111111111111 * */ var Route = /*#__PURE__*/ function (_RigorousRoute) { _inherits(Route, _RigorousRoute); function Route(middlewares, modelService, queryAuthorization, queryReadSearch, operationParams) { var _this; _classCallCheck(this, Route); _this = _possibleConstructorReturn(this, _getPrototypeOf(Route).call(this, middlewares, operationParams, operationParams)); _this.modelService = modelService; _this.userIdAsking = null; _this.queryAuthorization = queryAuthorization; _this.queryReadSearch = queryReadSearch; _this.queryReadSearchVar = null; _this.queryAuthorizationVar = null; return _this; } _createClass(Route, [{ key: "secureInput", value: function () { var _secureInput = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee(req) { var inputs; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: this.userIdAsking = req.authuser.user_id; inputs = { reverse: _.get(req.query, 'reverse') === 'true', lastPaginateId: h_secure_user_input.escapeHtml(_.get(req.query, 'lastPaginateId'), false), value: h_secure_user_input.escapeHtml(_.get(req.query, 'value'), false) }; if (h_format_checker.isNil(inputs.lastPaginateId)) { inputs.lastPaginateId = inputs.reverse ? rigorousConfig.PAGINATION_LAST_ID : rigorousConfig.PAGINATION_INITIAL_ID; } return _context.abrupt("return", inputs); case 4: case "end": return _context.stop(); } } }, _callee, this); })); function secureInput(_x) { return _secureInput.apply(this, arguments); } return secureInput; }() }, { key: "checkConformityInput", value: function () { var _checkConformityInput = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee2(inputs) { return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: this.queryReadSearchVar = this.queryReadSearch(inputs.value); case 1: case "end": return _context2.stop(); } } }, _callee2, this); })); function checkConformityInput(_x2) { return _checkConformityInput.apply(this, arguments); } return checkConformityInput; }() }, { key: "checkAuthorization", value: function () { var _checkAuthorization = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee3(checker) { return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: this.queryAuthorizationVar = this.queryAuthorization(this.userIdAsking); case 1: case "end": return _context3.stop(); } } }, _callee3, this); })); function checkAuthorization(_x3) { return _checkAuthorization.apply(this, arguments); } return checkAuthorization; }() }, { key: "processData", value: function () { var _processData = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee4(inputs, checker) { var queryRead, objects; return regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: queryRead = _objectSpread({}, this.queryAuthorizationVar, this.queryReadSearchVar); _context4.next = 3; return this.modelService.readMany(queryRead, inputs.lastPaginateId, inputs.reverse, this.operationParams); case 3: objects = _context4.sent; if (!h_format_checker.isNil(objects)) { _context4.next = 6; break; } throw new RigorousError(errorTypes.RESPONSE_ERROR_DATA_NOT_CONFORM); case 6: return _context4.abrupt("return", { result: objects, notificationParam: null }); case 7: case "end": return _context4.stop(); } } }, _callee4, this); })); function processData(_x4, _x5) { return _processData.apply(this, arguments); } return processData; }() }, { key: "sendNotification", value: function () { var _sendNotification = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee5(inputs, notificationParam) { return regeneratorRuntime.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: case "end": return _context5.stop(); } } }, _callee5); })); function sendNotification(_x6, _x7) { return _sendNotification.apply(this, arguments); } return sendNotification; }() }]); return Route; }(RigorousRoute); module.exports = Route;