UNPKG

zents

Version:

ZenTS is a Node.js & TypeScript MVC-Framework for building rich web applications, released as free and open-source software under the MIT License. It is designed for building web applications with modern tools and design patterns.

96 lines 4.14 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RouterFactory = void 0; const enums_1 = require("../types/enums"); const config_1 = require("../config/config"); const find_my_way_1 = __importDefault(require("find-my-way")); const serve_static_1 = __importDefault(require("serve-static")); class RouterFactory { generate(controllers, securityProviders, handler) { var _a; const router = find_my_way_1.default((_a = config_1.config.web) === null || _a === void 0 ? void 0 : _a.router); this.handler = handler; this.bindSecurityProviderRoutes(router, securityProviders); this.bindStaticRoute(router); for (const [key, controllerDeclaration] of controllers) { this.bindController(router, key, controllerDeclaration.routes); } return router; } bindController(router, key, routes) { for (const route of routes) { router.on(route.method, route.path, (req, res, params) => { const handlerConfig = { type: enums_1.REQUEST_TYPE.CONTROLLER, controllerMethod: route.controllerMethod, controllerKey: key, }; if (typeof route.authProvider === 'string') { handlerConfig.authProvider = route.authProvider; } this.handler(handlerConfig, route, req, res, params); }); } } bindStaticRoute(router) { var _a, _b, _c; if (typeof ((_a = config_1.config.paths) === null || _a === void 0 ? void 0 : _a.public) !== 'string' || typeof ((_b = config_1.config.web) === null || _b === void 0 ? void 0 : _b.publicPath) !== 'string') { return; } const handler = serve_static_1.default(config_1.config.paths.public); let publicRoutePath = (_c = config_1.config.web) === null || _c === void 0 ? void 0 : _c.publicPath; if (!publicRoutePath.startsWith('/')) { publicRoutePath = `/${publicRoutePath}`; } if (!publicRoutePath.endsWith('/')) { publicRoutePath = `${publicRoutePath}/`; } router.on('GET', `${publicRoutePath}*`, (req, res) => { req.url = req.url.substr(publicRoutePath.length); // @ts-ignore handler(req, res, () => { res.setHeader('Content-Type', 'application/json'); res.writeHead(404); res.end(JSON.stringify({ statusCode: 404, error: 'Not Found', message: 'File not found', })); }); }); } bindSecurityProviderRoutes(router, securityProviders) { for (const provider of securityProviders.values()) { const options = provider.options; if (typeof options.loginUrl === 'string') { router.on('POST', options.loginUrl, (req, res, params) => { this.handler({ type: enums_1.REQUEST_TYPE.SECURITY, action: enums_1.SECURITY_ACTION.LOGIN, provider, }, { method: 'POST', path: options.loginUrl, }, req, res, params); }); } if (typeof options.logoutUrl === 'string') { router.on('GET', options.logoutUrl, (req, res, params) => { this.handler({ type: enums_1.REQUEST_TYPE.SECURITY, action: enums_1.SECURITY_ACTION.LOGOUT, provider, }, { method: 'GET', path: options.logoutUrl, }, req, res, params); }); } } } } exports.RouterFactory = RouterFactory; //# sourceMappingURL=RouterFactory.js.map