UNPKG

@gabliam/koa

Version:
109 lines (108 loc) 4.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KoaPlugin = void 0; const tslib_1 = require("tslib"); const core_1 = require("@gabliam/core"); const web_core_1 = require("@gabliam/web-core"); const debug_1 = tslib_1.__importDefault(require("debug")); const constants_1 = require("./constants"); const converter_value_1 = require("./converter-value"); const koa_1 = require("./koa"); const koa_interceptor_1 = require("./koa-interceptor"); const middleware_1 = require("./middleware"); const debug = (0, debug_1.default)('Gabliam:Plugin:ExpressPlugin'); let KoaPlugin = class KoaPlugin extends web_core_1.WebPluginBase { bindApp(container, registry, webConfiguration) { // eslint-disable-next-line new-cap container.bind(web_core_1.APP).toConstantValue(new koa_1.koa()); container.bind(web_core_1.REQUEST_LISTENER_CREATOR).toConstantValue(() => { const app = container.get(web_core_1.APP); app.silent = true; return app.callback(); }); webConfiguration.addwebConfig({ instance: middleware_1.addMiddlewares, order: -2, }); webConfiguration.addwebConfig({ instance: middleware_1.addContextMiddleware, order: -1, }); } async destroy(container, registry) { await this.stop(container, registry); } async stop(container, registry) { try { // server can be undefined (if start is not called) const server = container.get(web_core_1.SERVER); return await new Promise((resolve) => { server.close(() => resolve()); }); } catch (e) { return Promise.resolve(); } } async buildControllers(restMetadata, container) { // get the router creator let routerCreator = (prefix) => // eslint-disable-next-line new-cap new koa_1.koaRouter({ prefix, }); try { routerCreator = container.get(constants_1.CUSTOM_ROUTER_CREATOR); // eslint-disable-next-line no-empty } catch (e) { } for (const [controllerId, { methods, controllerPath },] of restMetadata.controllerInfo) { const controller = container.get(controllerId); let routerPath = (0, web_core_1.cleanPath)(`${restMetadata.rootPath}${controllerPath}`); if (routerPath === '/') { routerPath = undefined; } debug(`New route : "${routerPath}"`); const router = routerCreator(routerPath); for (const methodInfo of methods) { const execCtx = new web_core_1.ExecutionContext(controller, methodInfo); let methodMetadataPath = methodInfo.methodPath; if (methodMetadataPath[0] !== '/') { methodMetadataPath = `/${methodMetadataPath}`; } const addJsonHandler = async (context, next) => { context.state.jsonHandler = methodInfo.json; await next(); }; const interceptors = methodInfo.interceptors.filter((i) => (0, koa_interceptor_1.isValidInterceptor)(i.instance)); // create handler const handler = this.handlerFactory(execCtx, interceptors); // register handler in router router[methodInfo.method](methodMetadataPath, addJsonHandler, handler); } const app = container.get(web_core_1.APP); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore koa-router bad definition app.use(router.routes()).use(router.allowedMethods()); } } handlerFactory(execCtx, interceptors) { return async (context, next) => { const composeInterceptor = (0, web_core_1.compose)(interceptors, converter_value_1.converterValue); const req = context.req; const ctx = (0, web_core_1.getContext)(req); const methodInfo = execCtx.getMethodInfo(); const controller = execCtx.getClass(); const callNext = async () => { const args = await methodInfo.extractArgs(ctx, execCtx, next); return (0, core_1.toPromise)(controller[methodInfo.methodName](...args)); }; await composeInterceptor(ctx, execCtx, callNext); }; } }; KoaPlugin = tslib_1.__decorate([ (0, core_1.Plugin)('KoaPlugin'), (0, core_1.Scan)() ], KoaPlugin); exports.KoaPlugin = KoaPlugin;