light-ning
Version:
(ALPHA) framework without dependecies...
121 lines (94 loc) • 4.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _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"); } }; }();
var _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; }; }();
var _app = require('./app');
var _app2 = _interopRequireDefault(_app);
var _lib = require('./lib');
var _middlewares = require('./middlewares');
var _middlewares2 = _interopRequireDefault(_middlewares);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Router = function () {
function Router() {
_classCallCheck(this, Router);
this.routers = [];
this.rootPaths = [];
}
_createClass(Router, [{
key: 'setControllersPath',
value: function setControllersPath(path) {
this.controllersPath = path;
}
}, {
key: 'setGuardian',
value: function setGuardian(guardian) {
this.guardian = guardian;
}
}, {
key: 'routes',
value: function routes(rootPath, _routes) {
if ((0, _lib.isArray)(_routes)) {
this.rootPaths.push(rootPath);
this.routers.push(_routes);
} else {
throw new Error('routes in "Router" must be array.');
}
}
}, {
key: 'route',
value: function route(req, res) {
var _this = this;
var url = req.url,
method = req.method;
this.routers.map(function (router, routerIndex) {
router.map(function (route) {
route.method = route.method.toUpperCase();
var response = new _app.Response(res);
var rootPath = _this.rootPaths[routerIndex];
var run = function run() {
if (route.middleWares && !route.action) {
return (0, _middlewares2.default)(req, res, route.middleWares);
} else if (route.middleWares && route.action) {
throw new Error('params: action and middleWares cannot work together');
}
var urlCondition = (url === '/' ? '' : url) + '/';
if (rootPath === '/' && route.path === '/') rootPath = '';
if ((urlCondition === '' + rootPath + route.path || url === '' + rootPath + route.path) && method === route.method) {
var _route$action$split = route.action.split('@'),
_route$action$split2 = _slicedToArray(_route$action$split, 2),
name = _route$action$split2[0],
action = _route$action$split2[1];
var Controller = void 0,
controllerName = (0, _lib.capitalize)(name);
try {
Controller = new (require(_this.controllersPath + '/' + name)[controllerName])();
} catch (e) {
throw new Error(controllerName + ' controller contains error or cannot find "' + _this.controllersPath + '/' + name + '" in ' + _app2.default.get('base'));
}
_this.action(req, res, Controller[action].bind(Controller), response);
}
};
if (route.guard) {
if (_this.guardian) {
return _this.guardian({ req: req, res: res, next: run, response: response });
} else {
throw new Error('"guardian" is undefined. Please, set guardian in app.');
}
}
run();
});
});
}
}, {
key: 'action',
value: function action(req, res, method, response) {
method({ req: req, res: res, response: response });
}
}]);
return Router;
}();
Router.displayName = 'Router';
exports.default = new Router();