UNPKG

yahoi

Version:

Yet Another Highly Opinionated Isomorphic Framework

160 lines (134 loc) 4.46 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var RouteParser = require('route-parser'); var path = require('path'); var Route = function () { function Route(options) { _classCallCheck(this, Route); if (typeof options.app == 'undefined' || options.app == null) { throw Error('FATAL: No app reference defined for Route: ' + options.path); } this.app = options.app; if (typeof options.type == 'undefined') { this.__type = 'get'; } else { this.__type = options.type.toLowerCase(); } this.__router = null; if (typeof options.path === 'function') { this.path = options.path; this.__routeParser = null; } else { this.path = options.path; this.__routeParser = new RouteParser(options.path); } if (typeof options.router != 'undefined') { this.__router = null; } else { this.__controllerName = options.controller; this.__actionName = options.action; this.__controller = this.loadController(options.controller); } this.loadController = this.loadController.bind(this); } _createClass(Route, [{ key: 'getType', value: function getType() { return this.__type; } }, { key: 'setAppReference', value: function setAppReference(app) { this.app = app; } }, { key: 'getRouter', value: function getRouter() { return this.__router; } }, { key: 'loadRouter', value: function loadRouter(routerName) { var router = this.app.createRouter(routerName, { app: this.app }); router.setPrefix(this.path); return router; } }, { key: 'loadController', value: function loadController(controller) { return this.app.getController(controller); } }, { key: 'getController', value: function getController() { return this.__controller; } }, { key: 'getActionName', value: function getActionName() { return this.__actionName; } }, { key: 'getControllerName', value: function getControllerName() { return this.__controllerName; } }, { key: 'renderAction', value: function renderAction(req, res, match) { return this.getController().renderAction(this.getActionName(), req, res, match); } }, { key: 'getPath', value: function getPath() { return this.path; } }, { key: 'buildPathToMatch', value: function buildPathToMatch(path) { var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; prefix = prefix.replace('*wild', ""); if (prefix.length > 0) { path = prefix + path; } else { path = path; } path = path.replace(/([^:]\/)\/+/g, "$1"); return path; } }, { key: 'match', value: function match(req) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var prefix = options.prefix || null; var requestUrl = req.url; var path = this.path; var routeParser = this.__routeParser; if (typeof this.path === 'function') { path = this.path(req); routeParser = new RouteParser(path); } //console.log(`compare: ${req.type} == ${this.getType()}`, ) if (req.method.toLowerCase() != this.getType()) { return null; } var match = routeParser.match(requestUrl); var isMatchX = match ? "X" : ""; //console.log(`matching ${req.url} against ${path} => ${isMatchX}`) if (match) { return { route: this, match: match }; } else { return null; } } }]); return Route; }(); exports.default = Route;