SwaggerUiSetupMiddleware.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const swagger_jsdoc_1 = __importDefault(require("swagger-jsdoc"));
  7. const http_1 = require("../base/http");
  8. const swagger_ui_express_1 = __importDefault(require("swagger-ui-express"));
  9. const i18n_1 = require("../base/i18n");
  10. class SwaggerUiSetupMiddleware extends http_1.Middleware {
  11. constructor() {
  12. super(...arguments);
  13. this.order = -96;
  14. this.route = this.context.swaggerRoute;
  15. this.action = this.getSetup();
  16. }
  17. getSetup() {
  18. if (!this.context.swaggerDocsPath)
  19. return (req, res, next) => { next(); };
  20. const swaggerOptions = {
  21. swaggerDefinition: {
  22. openapi: '3.0.1',
  23. info: {
  24. title: this.context.swaggerTitle,
  25. version: this.context.swaggerApiVersion,
  26. description: this.context.swaggerDescription,
  27. },
  28. servers: [
  29. { url: this.context.host }
  30. ],
  31. },
  32. apis: [this.context.swaggerDocsPath],
  33. };
  34. if (this.context.swaggerComponents)
  35. swaggerOptions.swaggerDefinition.components = this.context.swaggerComponents;
  36. if (this.context.swaggerSecurity)
  37. swaggerOptions.swaggerDefinition.security = this.context.swaggerSecurity;
  38. const swaggerDocs = (0, swagger_jsdoc_1.default)(swaggerOptions);
  39. this.context.logInfo((0, i18n_1.$$)('org.crazydoctor.expressts.swagger.registered', { path: this.context.swaggerRoute }));
  40. return swagger_ui_express_1.default.setup(swaggerDocs);
  41. }
  42. }
  43. exports.default = SwaggerUiSetupMiddleware;
  44. //# sourceMappingURL=SwaggerUiSetupMiddleware.js.map