UNPKG

4.46 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
8
9var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
10
11var _index = require('./index');
12
13var _index2 = _interopRequireDefault(_index);
14
15var _lib = require('./lib');
16
17var _middlewares = require('./middlewares');
18
19var _middlewares2 = _interopRequireDefault(_middlewares);
20
21var _fs = require('fs');
22
23var _fs2 = _interopRequireDefault(_fs);
24
25function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26
27function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
28
29var Router = function () {
30 function Router() {
31 _classCallCheck(this, Router);
32 }
33
34 _createClass(Router, [{
35 key: 'setControllersPath',
36 value: function setControllersPath(path) {
37 this.controllersPath = path;
38 }
39 }, {
40 key: 'routes',
41 value: function routes(rootPath, _routes) {
42 if ((0, _lib.isArray)(_routes)) {
43 this.rootPath = rootPath;
44 this.routesArray = _routes;
45 } else {
46 throw new Error('routes in "Router" must be array.');
47 }
48 }
49 }, {
50 key: 'route',
51 value: function route(req, res) {
52 var _this = this;
53
54 var url = req.url,
55 method = req.method;
56
57
58 this.routesArray.map(function (route) {
59 route.method = route.method.toUpperCase();
60
61 var response = new _index.Response(res);
62
63 var run = function run() {
64 if (route.middleWares && !route.action) {
65 return (0, _middlewares2.default)(req, res, route.middleWares);
66 } else if (route.middleWares && route.action) {
67 throw new Error('params: action and middleWares cannot work together');
68 }
69
70 if ((url + '/' === '' + _this.rootPath + route.path || url === '' + _this.rootPath + route.path) && method === route.method) {
71 var _route$action$split = route.action.split('@'),
72 _route$action$split2 = _slicedToArray(_route$action$split, 2),
73 name = _route$action$split2[0],
74 action = _route$action$split2[1];
75
76 _this.actionName = action;
77 _this.controllerName = (0, _lib.capitalize)(name);
78 var Controller = void 0;
79
80 try {
81 Controller = new (require(_this.controllersPath + '/' + name).default)();
82 } catch (e) {
83 try {
84 Controller = new (require(_this.controllersPath + '/' + name)[_this.controllerName])();
85 } catch (e) {
86 throw new Error(_this.controllerName + ' controller contains error or cannot find "' + _this.controllersPath + '/' + name + '" in ' + _index2.default.get('base'));
87 }
88 }
89
90 _this.action(req, res, Controller[action].bind(Controller), response);
91 }
92 };
93
94 if (route.guard) return route.guard({ req: req, res: res, next: run, response: response });
95
96 run();
97 });
98 }
99 }, {
100 key: 'action',
101 value: function action(req, res, method, response) {
102 method({ req: req, res: res, response: response });
103 }
104 }]);
105
106 return Router;
107}();
108
109Router.displayName = 'Router';
110exports.default = new Router();
\No newline at end of file