@nodearch/express
Version:
nodearch express server
111 lines • 4.21 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { AppContext, Service } from '@nodearch/core';
import { CoreDecorator, DecoratorType } from '@nodearch/core/components';
import { ExpressDecorator } from './enums.js';
import { ExpressConfig } from './express.config.js';
let ExpressParser = class ExpressParser {
constructor(appContext, config) {
this.config = config;
this.expressInfo = { routers: [] };
const componentsInfo = appContext.getComponentRegistry().get({
id: CoreDecorator.CONTROLLER,
decoratorIds: [
ExpressDecorator.HTTP_METHOD
]
});
if (componentsInfo) {
this.expressInfo = this.parse(componentsInfo);
}
}
getExpressInfo() {
return this.expressInfo;
}
parse(componentsInfo) {
const expressInfo = { routers: [] };
componentsInfo.forEach(comp => {
const ctrlMiddleware = comp
.getDecorators({
useId: ExpressDecorator.MIDDLEWARE,
placement: [DecoratorType.CLASS]
})
.map(deco => {
return { ...deco.data, dependencyKey: deco.dependencies[0].key };
})
.reverse();
const routes = comp
.getDecorators({
id: ExpressDecorator.HTTP_METHOD
})
.map(deco => {
return this.getRouteInfo(comp, deco);
});
expressInfo.routers.push({
controllerInfo: comp,
path: this.getControllerPath(comp),
routes,
middleware: ctrlMiddleware
});
});
return expressInfo;
}
getRouteInfo(componentInfo, decoratorInfo) {
const { httpPath, httpMethod } = decoratorInfo.data;
const middleware = componentInfo
.getDecorators({
useId: ExpressDecorator.MIDDLEWARE,
method: decoratorInfo.method
})
.map(deco => {
return { ...deco.data, dependencyKey: deco.dependencies[0].key };
})
.reverse();
const inputs = componentInfo
.getDecorators({
id: ExpressDecorator.HTTP_PARAM,
method: decoratorInfo.method
})
.map(deco => deco.data);
return {
controllerMethod: decoratorInfo.method,
method: httpMethod,
path: this.formatPath(httpPath),
middleware,
inputs
};
}
getControllerPath(comp) {
let ctrlPath = '';
const ctrlPathDecorator = comp.getDecorators({ id: ExpressDecorator.HTTP_PATH })[0];
if (ctrlPathDecorator) {
ctrlPath = this.formatPath(ctrlPathDecorator.data.httpPath);
}
if (this.config.httpPath) {
ctrlPath = this.formatPath(this.config.httpPath) + ctrlPath;
}
return ctrlPath;
}
formatPath(urlPath) {
let newPath = urlPath || '';
if (!newPath.startsWith('/')) {
newPath = '/' + newPath;
}
if (newPath.length > 1 && newPath.endsWith('/')) {
newPath = newPath.slice(0, newPath.length - 1);
}
return newPath;
}
};
ExpressParser = __decorate([
Service(),
__metadata("design:paramtypes", [AppContext, ExpressConfig])
], ExpressParser);
export { ExpressParser };
//# sourceMappingURL=express-parser.js.map