UNPKG

aztec

Version:

Node Js Framework for creating API Services

141 lines (140 loc) 6.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const http = require("http"); const middleware_func_1 = require("../middleware.func"); const lib_1 = require("../lib"); const base_class_1 = require("../base.class"); const lodash_1 = require("lodash"); const handler_class_1 = require("../handler.class"); class RouterController extends base_class_1.Base { constructor(app) { super(); this.app = app; } parseParams(url, path, req) { if (path.indexOf('/:') > -1) { let pattern = /:([a-z]|-[a-z])+/g; let params = path.match(pattern); params.forEach(param => { path.split(param).forEach(item => { if (url.search(item) > -1 && item.length) { let slicedItem = url.slice(item.length); let match = slicedItem.match('/'); if (match) { let result = slicedItem.slice(0, match.index); req.params[lodash_1.camelCase(param.slice(1))] = result; path = lodash_1.replace(path, param, result); } else if (lodash_1.endsWith(url, slicedItem)) { let result = slicedItem.slice(0); req.params[lodash_1.camelCase(param.slice(1))] = result; path = lodash_1.replace(path, param, result); } } }); }); } return path; } parseRequest(svc, req, res) { let { url, method } = req; let statusCode = 404; const Router = this.app.get('router'); const ControllersManager = this.app.get('controllersManager'); let Guardian; let app; if (url.indexOf('.html') > -1) { statusCode = 200; if (svc.get('public-path')) { return res.render(`${svc.get('public-path')}${url}`).end(); } else { return handler_class_1.Handler.error('Cannot get "public-path". Please, set "public-path" in your service.'); } } if (url.indexOf('.css') > -1) { statusCode = 200; if (svc.get('public-path')) { return res.render(`${svc.get('public-path')}${url}`, 'css').end(); } else { return handler_class_1.Handler.error('Cannot get "public-path". Please, set "public-path" in your service.'); } } if (url.indexOf('.js') > -1) { statusCode = 200; if (svc.get('public-path')) { return res.render(`${svc.get('public-path')}${url}`, 'js').end(); } else { return handler_class_1.Handler.error('Cannot get "public-path". Please, set "public-path" in your service.'); } } if (url.indexOf('.xml') > -1) { statusCode = 200; if (svc.get('public-path')) { return res.render(`${svc.get('public-path')}${url}`, 'xml').end(); } else { return handler_class_1.Handler.error('Cannot get "public-path". Please, set "public-path" in your service.'); } } Router.get('routers').forEach((router) => { url = lib_1.slashValid(url); router.rootPath = lib_1.slashValid(router.rootPath); router.routes.forEach((route) => { if (route.action && route.middlewares) { return handler_class_1.Handler.error('Property "action" can\'t work with "middlewares". Please, choose one property.'); } app = lib_1.dropIfNotDefault(route.basePath); Guardian = app.get('guardian'); let path = lib_1.slashValid(`${router.rootPath}${route.path}`); path = this.parseParams(url, path, req); if (route.action) { let [componentName, action] = route.action.split(ControllersManager.get('prefix')); let controllerName = `${componentName}Controller`; const run = () => { const RequiredController = app.get('controllers')[controllerName]; try { RequiredController[action].bind(RequiredController); } catch (e) { handler_class_1.Handler.error(`There is no action "${action}" in controller "${controllerName}"`); } RequiredController.model = app.get('models')[componentName]; RequiredController[action](req, res); }; if (url === path && method === route.method) { statusCode = 200; if (Guardian && route.guard) { return Guardian(req, res, run); } run(); } } else if (route.middlewares) { if (url === path && method === route.method) { statusCode = 200; const run = () => { middleware_func_1.middleware(req, res, route.middlewares); }; if (Guardian && route.guard) { return Guardian(req, res, run); } run(); } } else { handler_class_1.Handler.error('Setup one of the: "action", "middlewares", "render" property in a route.'); } }); }); if (statusCode !== 200 && app.get('error-handler')) { app.get('error-handler')(req, res, statusCode); } else if (statusCode !== 200) { res.status(statusCode).send({ message: http.STATUS_CODES[statusCode], code: statusCode }); } } } exports.RouterController = RouterController;