UNPKG

typescript-swagger

Version:

Generate Swagger files from a decorator library like typescript-rest or a @decorators/express.

81 lines 3.87 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ControllerGenerator = void 0; var typescript_1 = require("typescript"); var type_1 = require("../decorator/type"); var endpointGenerator_1 = require("./endpointGenerator"); var methodGenerator_1 = require("./methodGenerator"); var ControllerGenerator = /** @class */ (function (_super) { __extends(ControllerGenerator, _super); // -------------------------------------------------------------------- function ControllerGenerator(node, current) { var _this = _super.call(this, node, current) || this; _this.genMethods = new Set(); _this.generatePath('CLASS_PATH'); return _this; } ControllerGenerator.prototype.isValid = function () { return !!this.path || this.path === ''; }; ControllerGenerator.prototype.generate = function () { 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.'); } var sourceFile = this.node.parent.getSourceFile(); this.debugger('Generating Metadata for controller %s', this.getCurrentLocation()); this.debugger('Controller path: %s', this.path); var controllerMetadata = { consumes: this.getConsumes(), location: sourceFile.fileName, methods: this.buildMethods(), name: this.getCurrentLocation(), path: this.path || '', produces: this.getProduces(), responses: this.getResponses(), security: this.getSecurity(), tags: this.getTags(), }; this.debugger('Generated Metadata for controller %s: %j', this.getCurrentLocation(), controllerMetadata); return controllerMetadata; }; ControllerGenerator.prototype.getCurrentLocation = function () { return this.node.name.text; }; ControllerGenerator.prototype.buildMethods = function () { var _this = this; var handler = type_1.Decorator.getRepresentationHandler('SWAGGER_HIDDEN', this.current.decoratorMap); return this.node.members .filter(function (method) { return (method.kind === typescript_1.SyntaxKind.MethodDeclaration); }) .filter(function (method) { return !handler.isPresentOnNode(method); }) .map(function (method) { return new methodGenerator_1.MethodGenerator(method, _this.current, _this.path || ''); }) .filter(function (generator) { if (generator.isValid() && !_this.genMethods.has(generator.getMethodName())) { _this.genMethods.add(generator.getMethodName()); return true; } return false; }) .map(function (generator) { return generator.generate(); }); }; return ControllerGenerator; }(endpointGenerator_1.EndpointGenerator)); exports.ControllerGenerator = ControllerGenerator; //# sourceMappingURL=controllerGenerator.js.map