@nodearch/express
Version:
nodearch express server
104 lines • 4.12 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 { Service } from '@nodearch/core';
import { camelToTitle } from '@nodearch/core/utils';
import { HttpMethod } from '../express/enums.js';
import { ExpressParser } from '../express/express-parser.js';
import { ExpressConfig } from '../express/express.config.js';
let OpenAPIParser = class OpenAPIParser {
constructor(expressConfig, expressParser) {
this.expressConfig = expressConfig;
this.expressParser = expressParser;
this.appMap = [];
}
parse() {
const providerData = {
servers: this.getServers(),
routes: this.getRoutes()
};
return providerData;
}
getRoutes() {
const routes = [];
const expressInfo = this.expressParser.getExpressInfo();
expressInfo.routers.forEach(router => {
router.routes.forEach(route => {
const urlPath = this.mergePaths(router.path, route.path);
const pathInfo = this.getPathInfo(urlPath);
routes.push({
app: {
component: router.controllerInfo.getClass(),
method: route.controllerMethod
},
schema: {
path: pathInfo.path,
method: route.method,
data: this.getOperationObject(pathInfo.params, route.controllerMethod, route.method)
}
});
});
});
return routes;
}
getServers() {
return [{
url: 'http://' + this.expressConfig.hostname + ':' + this.expressConfig.port,
description: 'Express Server URL'
}];
}
getOperationObject(params, controllerMethod, httpMethod) {
const opObj = {
summary: controllerMethod,
description: camelToTitle(controllerMethod),
parameters: this.getPathParams(params),
responses: {
200: { description: 'OK' },
500: { description: 'Internal Server Error' }
}
};
if ([HttpMethod.POST, HttpMethod.PUT, HttpMethod.PATCH].includes(httpMethod)) {
opObj['requestBody'] = { content: { 'application/json': { schema: { type: 'object' } } } };
}
return opObj;
}
getPathParams(params) {
return params.map(param => {
return {
name: param,
in: 'path',
schema: { type: 'string' },
required: true
};
});
}
mergePaths(...paths) {
return paths.reverse().reduce((acc, curr) => {
curr = curr.endsWith('/') ? curr.slice(0, curr.length - 1) : curr;
return curr += acc;
}, '') || '/';
}
getPathInfo(urlPath) {
let path = urlPath;
const params = [];
const matches = urlPath.matchAll(/(\/|\.)?:([A-Za-z0-9_]+)(\/|\.)?/g);
for (const match of matches) {
params.push(match[2]);
path = path.replace(':' + match[2], `{${match[2]}}`);
}
return { path, params };
}
};
OpenAPIParser = __decorate([
Service(),
__metadata("design:paramtypes", [ExpressConfig,
ExpressParser])
], OpenAPIParser);
export { OpenAPIParser };
//# sourceMappingURL=parser.js.map