UNPKG

@nodeswork/sbase

Version:

Basic REST api foundation from Nodeswork.

44 lines (42 loc) 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); require("reflect-metadata"); const _ = require("underscore"); const Router = require("koa-router"); const utils_1 = require("./utils"); const declarations_1 = require("./declarations"); class A7Controller { get $koaRouter() { if (this.$router != null) { return this.$router; } const constructor = this.constructor; const meta = Reflect.getMetadata(declarations_1.METADATA_KEY, constructor); if (meta == null) { return null; } this.$router = new Router(meta.routerOptions); if (!_.isEmpty(meta.middlewares)) { this.$router.use(utils_1.compose(meta.middlewares)); } for (const handler in meta.handlers) { const handlerMeta = meta.handlers[handler]; let method = this[handler] || _.identity; if (handlerMeta.middleware != null) { method = utils_1.compose([handlerMeta.middleware, method]); this[handler] = method; } if (handlerMeta.path != null) { this.$router[handlerMeta.method](handlerMeta.name ? handlerMeta.name : `${constructor.name}.${handler}`, handlerMeta.path, method.bind(this)); } } return this.$router; } get $koaRouterUseArgs() { return [this.$koaRouter.routes(), this.$koaRouter.allowedMethods()]; } } exports.A7Controller = A7Controller; //# sourceMappingURL=controller.js.map