UNPKG

@ivamuno/nestjs-openapi

Version:
50 lines (49 loc) 2.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OpenApiModule = void 0; const swagger_1 = require("@nestjs/swagger"); const redoc_module_1 = require("../redoc/redoc.module"); class OpenApiModule { static createDocument(app, config, options) { const document = swagger_1.SwaggerModule.createDocument(app, config, options); Object.keys(document.paths).forEach(pathKey => { const path = document.paths[pathKey]; Object.keys(path).forEach(operationKey => { const operationMetadata = path[operationKey]; const operation = operationMetadata; if (operation) { OpenApiModule.mapRequestBodyExamples(operationMetadata, operation); OpenApiModule.mapResponseExamples(operation); } }); }); return document; } static setup(path, app, document, options) { const defaultOptions = { docName: 'openapi', title: 'OpenApi documentation' }; redoc_module_1.RedocModule.setup(path, app, document, Object.assign(Object.assign({}, defaultOptions), options)); } static mapExamples(metadata, target) { const requestBodyExamples = metadata.examples; if (requestBodyExamples) { Object.keys(target.content).forEach(contentTypeKey => { const contentType = target.content[contentTypeKey]; contentType.examples = requestBodyExamples; }); delete metadata.examples; } } static mapRequestBodyExamples(operationMetadata, operation) { const requestBody = operation.requestBody; if (requestBody) { OpenApiModule.mapExamples(operationMetadata, requestBody); } } static mapResponseExamples(operation) { Object.keys(operation.responses).forEach(responseKey => { const response = operation.responses[responseKey]; OpenApiModule.mapExamples(response, response); }); } } exports.OpenApiModule = OpenApiModule;