@gabliam/express
Version:
Gabliam plugin for add express
108 lines (107 loc) • 4.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExpressPlugin = 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 express_1 = require("./express");
const express_interceptor_1 = require("./express-interceptor");
const middleware_1 = require("./middleware");
const debug = (0, debug_1.default)('Gabliam:Plugin:ExpressPlugin');
let ExpressPlugin = class ExpressPlugin extends web_core_1.WebPluginBase {
bindApp(container, registry, webConfiguration) {
container.bind(web_core_1.APP).toConstantValue((0, express_1.express)());
container.bind(web_core_1.REQUEST_LISTENER_CREATOR).toConstantValue(() => {
const app = container.get(web_core_1.APP);
return app;
});
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 undefined;
}
}
async start(container, registry) {
const restConfig = container.get(web_core_1.WEB_PLUGIN_CONFIG);
const app = container.get(web_core_1.APP);
app.set('port', restConfig.port);
await super.start(container, registry);
}
async buildControllers(restMetadata, container) {
const app = container.get(web_core_1.APP);
// get the router creator
let routerCreator = () => express_1.express.Router();
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);
const router = routerCreator();
const routerPath = (0, web_core_1.cleanPath)(`${restMetadata.rootPath}${controllerPath}`);
debug(`New route : "${routerPath}"`);
for (const methodInfo of methods) {
const execCtx = new web_core_1.ExecutionContext(controller, methodInfo);
const interceptors = methodInfo.interceptors.filter((i) => (0, express_interceptor_1.isValidInterceptor)(i.instance));
// create handler
const handler = this.handlerFactory(execCtx, interceptors);
// register handler in router
router[methodInfo.method](methodInfo.methodPath, handler);
}
app.use(routerPath, router);
}
}
handlerFactory(execCtx, interceptors) {
return async (req, res, next) => {
const composeInterceptor = (0, web_core_1.compose)(interceptors, converter_value_1.converterValue);
const ctx = (0, web_core_1.getContext)(req);
const methodInfo = execCtx.getMethodInfo();
const controller = execCtx.getClass();
let expressNextCall = false;
const expressNext = () => {
expressNextCall = true;
next();
};
const callNext = async () => {
const args = await (0, core_1.toPromise)(methodInfo.extractArgs(ctx, execCtx, expressNext));
return (0, core_1.toPromise)(controller[methodInfo.methodName](...args));
};
try {
await composeInterceptor(ctx, execCtx, callNext);
if (!expressNextCall) {
(0, converter_value_1.send)(ctx, res, methodInfo.json);
}
}
catch (err) {
next(err);
}
};
}
};
ExpressPlugin = tslib_1.__decorate([
(0, core_1.Plugin)('ExpressPlugin'),
(0, core_1.Scan)()
], ExpressPlugin);
exports.ExpressPlugin = ExpressPlugin;