UNPKG

adonis5-swagger

Version:
76 lines (75 loc) 3.41 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SwaggerController = void 0; const mime_1 = __importDefault(require("mime")); const buildJsDocConfig_1 = __importDefault(require("./Utils/buildJsDocConfig")); const swagger_jsdoc_1 = __importDefault(require("swagger-jsdoc")); const fs = __importStar(require("fs")); const fs_1 = require("fs"); const path_1 = require("path"); class SwaggerController { constructor(IoC) { this.IoC = IoC; this.config = IoC.use('Adonis/Core/Config'); this.app = this.IoC.use('Adonis/Core/Application'); } async swaggerUI({ params, response }) { const swaggerUiAssetPath = this.config.get('swagger.swaggerUiDistPath') || require('swagger-ui-dist').getAbsoluteFSPath(); if (!params.fileName) { const baseUrl = this.config.get('swagger.uiUrl', '/docs').replace('/', ''); return response.redirect(`/${baseUrl}/index.html`); } const fileName = params.fileName ? params.fileName : 'index.html'; const path = (0, path_1.join)(swaggerUiAssetPath, fileName); const contentType = mime_1.default.getType(path); if (fileName.includes('initializer')) { const initializer = await fs_1.promises.readFile(path, 'utf-8'); return response .header('Content-Type', contentType) .send(initializer.replace('https://petstore.swagger.io/v2/swagger.json', this.config.get('swagger.specUrl'))); } else { return response.header('Content-Type', contentType).stream(fs.createReadStream(path)); } } async swaggerFile({ response }) { const mode = this.config.get('swagger.mode', 'RUNTIME'); if (mode === 'RUNTIME') { response.send((0, swagger_jsdoc_1.default)((0, buildJsDocConfig_1.default)(this.config.get('swagger.options', {})))); } else { response .safeHeader('Content-type', 'application/json') .stream(this.getSwaggerSpecFileContent(), () => { return ['Unable to find file', 404]; }); } } getSwaggerSpecFileContent() { const filePath = (0, path_1.join)(this.app.appRoot, this.config.get('swagger.specFilePath')); return fs.createReadStream(filePath); } } exports.SwaggerController = SwaggerController;