@trapi/metadata
Version:
Generate REST-API metadata scheme from TypeScript Decorators.
68 lines • 2.54 kB
JavaScript
"use strict";
/*
* Copyright (c) 2021-2023.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ControllerGenerator = void 0;
const typescript_1 = require("typescript");
const decorator_1 = require("../../decorator");
const abstract_1 = require("../abstract");
const method_1 = require("../method");
class ControllerGenerator extends abstract_1.AbstractGenerator {
// eslint-disable-next-line no-useless-constructor,@typescript-eslint/no-useless-constructor
constructor(node, current) {
super(node, current);
}
isValid() {
const isController = this.current.decoratorResolver.match(decorator_1.DecoratorID.CONTROLLER, this.node);
return !!isController;
}
generate() {
if (!this.node.parent) {
throw new Error('Controller node doesn\'t have a valid parent source file.');
}
if (!this.node.name) {
throw new Error('Controller node doesn\'t have a valid name.');
}
const sourceFile = this.node.parent.getSourceFile();
const path = this.buildPath();
return {
consumes: this.getConsumes(),
hidden: this.isHidden(this.node),
location: sourceFile.fileName,
name: this.getCurrentLocation(),
path,
produces: this.getProduces(),
responses: this.buildResponses(),
security: this.getSecurity(),
tags: this.getTags(),
methods: this.buildMethods(path),
};
}
getCurrentLocation() {
return this.node.name.text;
}
buildMethods(controllerPath) {
const set = new Set();
const output = [];
for (let i = 0; i < this.node.members.length; i++) {
const node = this.node.members[i];
if (!(0, typescript_1.isMethodDeclaration)(node) || this.isHidden(node)) {
continue;
}
const generator = new method_1.MethodGenerator(node, this.current);
const methodName = generator.getMethodName();
if (set.has(methodName) || !generator.isValid()) {
continue;
}
set.add(methodName);
output.push(generator.generate(controllerPath));
}
return output;
}
}
exports.ControllerGenerator = ControllerGenerator;
//# sourceMappingURL=module.js.map