UNPKG

aztec

Version:

Node Js Framework for creating API Services

203 lines (202 loc) 7.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = require("lodash"); const base_class_1 = require("../base.class"); const lib_1 = require("../lib"); class Router extends base_class_1.Base { constructor(svc) { super(); this.svc = svc; this.routers = []; } rest(path, handler, param = 'id') { let routes; if (lodash_1.isString(handler)) { routes = [ { method: 'GET', path: `/`, action: `${handler}@index`, basePath: this.basePath }, { method: 'GET', path: `/:${param}`, action: `${handler}@show`, basePath: this.basePath }, { method: 'POST', path: `/`, action: `${handler}@create`, basePath: this.basePath }, { method: 'PUT', path: `/:${param}`, action: `${handler}@update`, basePath: this.basePath }, { method: 'DELETE', path: `/:${param}`, action: `${handler}@destroy`, basePath: this.basePath }, { method: 'DELETE', path: `/`, action: `${handler}@destroyAll`, basePath: this.basePath } ]; } else if (lodash_1.isArray(handler)) { routes = [ { method: 'GET', path: `/`, middlewares: [handler[0]], basePath: this.basePath }, { method: 'GET', path: `/:${param}`, middlewares: [handler[1]], basePath: this.basePath }, { method: 'POST', path: `/`, middlewares: [handler[2]], basePath: this.basePath }, { method: 'PUT', path: `/:${param}`, middlewares: [handler[3]], basePath: this.basePath }, { method: 'DELETE', path: `/:${param}`, middlewares: [handler[4]], basePath: this.basePath }, { method: 'DELETE', path: `/`, middlewares: [handler[5]], basePath: this.basePath } ]; } if (routes) { this.routers.push({ rootPath: `api/${this.svc.get('api-prefix')}1${path}`, routes: routes }); if (this.svc.get('v2')) { this.routers.push({ rootPath: `api/${this.svc.get('api-prefix')}2${path}`, routes: routes }); } } } useRoute(route) { let prepareRoute, prepareRoute2; if (!route.render) { prepareRoute = { rootPath: `api/${this.svc.get('api-prefix')}1${route.path}`, routes: null }; if (this.svc.get('v2')) { prepareRoute2 = { rootPath: `api/${this.svc.get('api-prefix')}2${route.path}`, routes: null }; } } else { prepareRoute = { rootPath: route.path, routes: null }; } route = lodash_1.extend(route, { basePath: this.basePath }); route.path = '/'; prepareRoute.routes = [route]; this.routers.push(prepareRoute); if (prepareRoute2) { prepareRoute2.routes = [route]; this.routers.push(prepareRoute2); } } useRoutes(rootPath, routes) { rootPath = lib_1.slashAdd(rootPath); routes = lodash_1.reverse(routes); routes = lodash_1.uniqBy(routes, 'path'); routes = routes.map(route => lodash_1.extend(route, { basePath: this.basePath })); this.routers.push({ rootPath: `api/${this.svc.get('api-prefix')}1${rootPath}`, routes: routes, }); if (this.svc.get('v2')) { this.routers.push({ rootPath: `api/${this.svc.get('api-prefix')}2${rootPath}`, routes: routes, }); } } mergeRoutes() { let routers = {}; this.routers.forEach(router => { const regex = new RegExp(`(${this.svc.get('api-perfix')}1|${this.svc.get('api-perfix')}2)`, 'g'); if (router.rootPath.match(regex) && router.rootPath.match(regex).length > 1) { return router = undefined; } if (!routers[router.rootPath]) { routers[router.rootPath] = router; } else if (routers[router.rootPath]) { router.routes = lodash_1.reverse([...routers[router.rootPath].routes, ...router.routes]); routers[router.rootPath] = router; } }); routers = lodash_1.map(routers, (val, key) => routers[key]); this.routers = lodash_1.uniqBy(lodash_1.reverse(routers), 'rootPath'); } mergeAppRoutes(root = '') { root = lib_1.slashAdd(root); root = lib_1.slashValid(root); this.routers = this.routers.map(router => { router.routes.forEach(route => { if (root.length > 1 && !route.render) { let partialPath = router.rootPath.split(`api/${this.svc.get('api-prefix')}1`); router.rootPath = `/api/${this.svc.get('api-prefix')}1${root}${partialPath[0] || partialPath[1]}`; } else { router.rootPath = `${root}${router.rootPath}`; } }); return router; }); if (this.svc.get('v2')) { let localRouters = []; this.routers.forEach(router => { router.routes.forEach(route => { if (root.length > 1 && !route.render) { let partialPath = router.rootPath.split(`api/${this.svc.get('api-prefix')}1`); localRouters.push({ rootPath: `/api/${this.svc.get('api-prefix')}2${partialPath[1]}`, routes: router.routes }); } }); }); this.routers = [...this.routers, ...localRouters]; } } } exports.Router = Router;