UNPKG

@o-galaxy/ether

Version:

Rest Api Framework

62 lines 2.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const express_1 = require("express"); const route_wrapper_1 = require("./../common/factory/route-wrapper"); class ControllerDefContainer { constructor() { this.methodDefinitionsMap = new Map(); this.path = '/'; } addMiddleware(cb, classMethodName) { if (this.methodDefinitionsMap.has(classMethodName)) { const def = this.methodDefinitionsMap.get(classMethodName); def.middlewares.push(cb); this.methodDefinitionsMap.set(classMethodName, def); } } addWrapper(cb, classMethodName) { if (this.methodDefinitionsMap.has(classMethodName)) { const def = this.methodDefinitionsMap.get(classMethodName); def.wrappers.push(cb); this.methodDefinitionsMap.set(classMethodName, def); } } setPath(path) { this.path = path; } addMethod(route, validators, apiMethod, classMethodName) { this.methodDefinitionsMap.set(classMethodName, { middlewares: [], wrappers: [], route, validators, classMethodName, apiMethod }); } applyWrappers(originMethod, wrappers) { return wrappers.reduce((origin, wrapper) => { return route_wrapper_1.wrapperApplier(origin, wrapper); }, originMethod); } initRouter(router, classContext) { const controllerRouter = express_1.Router(); Array.from(this.methodDefinitionsMap.values()).forEach(m => { const apiMethod = String(m.apiMethod).toLowerCase(); const classMethodFunction = classContext[m.classMethodName]; const requestMethod = (m.wrappers && m.wrappers.length > 0) ? this.applyWrappers(classMethodFunction, m.wrappers) : classMethodFunction; if (classMethodFunction) { const boundClassMethod = requestMethod.bind(classContext); controllerRouter[apiMethod](m.route, ...m.middlewares, ...m.validators, boundClassMethod); } else { // console.log(classMethodFunction) } }); return router.use(this.path, controllerRouter); } } exports.ControllerDefContainer = ControllerDefContainer; //# sourceMappingURL=controller-def-container.js.map