UNPKG

4.01 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12exports.SwaggerModule = void 0;
13const load_package_util_1 = require("@nestjs/common/utils/load-package.util");
14const swagger_scanner_1 = require("./swagger-scanner");
15const assign_two_levels_deep_1 = require("./utils/assign-two-levels-deep");
16const get_global_prefix_1 = require("./utils/get-global-prefix");
17const validate_path_util_1 = require("./utils/validate-path.util");
18class SwaggerModule {
19 static createDocument(app, config, options = {}) {
20 const swaggerScanner = new swagger_scanner_1.SwaggerScanner();
21 const document = swaggerScanner.scanApplication(app, options);
22 document.components = assign_two_levels_deep_1.assignTwoLevelsDeep({}, config.components, document.components);
23 return Object.assign(Object.assign({ openapi: '3.0.0', paths: {} }, config), document);
24 }
25 static setup(path, app, document, options) {
26 const httpAdapter = app.getHttpAdapter();
27 const globalPrefix = get_global_prefix_1.getGlobalPrefix(app);
28 const finalPath = validate_path_util_1.validatePath(((options === null || options === void 0 ? void 0 : options.useGlobalPrefix) && globalPrefix && !globalPrefix.match(/^(\/?)$/))
29 ? `${globalPrefix}${validate_path_util_1.validatePath(path)}`
30 : path);
31 if (httpAdapter && httpAdapter.getType() === 'fastify') {
32 return this.setupFastify(finalPath, httpAdapter, document, options);
33 }
34 return this.setupExpress(finalPath, app, document, options);
35 }
36 static setupExpress(path, app, document, options) {
37 const httpAdapter = app.getHttpAdapter();
38 const swaggerUi = load_package_util_1.loadPackage('swagger-ui-express', 'SwaggerModule', () => require('swagger-ui-express'));
39 const swaggerHtml = swaggerUi.generateHTML(document, options);
40 app.use(path, swaggerUi.serveFiles(document, options));
41 httpAdapter.get(path, (req, res) => res.send(swaggerHtml));
42 httpAdapter.get(path + '-json', (req, res) => res.json(document));
43 }
44 static setupFastify(path, httpServer, document, options) {
45 const hasParserGetterDefined = Object.getPrototypeOf(httpServer).hasOwnProperty('isParserRegistered');
46 if (hasParserGetterDefined && !httpServer.isParserRegistered) {
47 httpServer.registerParserMiddleware();
48 }
49 httpServer.register((httpServer) => __awaiter(this, void 0, void 0, function* () {
50 httpServer.register(load_package_util_1.loadPackage('fastify-swagger', 'SwaggerModule', () => require('fastify-swagger')), {
51 swagger: document,
52 exposeRoute: true,
53 routePrefix: path,
54 mode: 'static',
55 specification: {
56 document
57 },
58 uiConfig: options === null || options === void 0 ? void 0 : options.uiConfig,
59 initOAuth: options === null || options === void 0 ? void 0 : options.initOAuth,
60 staticCSP: options === null || options === void 0 ? void 0 : options.staticCSP,
61 transformStaticCSP: options === null || options === void 0 ? void 0 : options.transformStaticCSP
62 });
63 }));
64 }
65}
66exports.SwaggerModule = SwaggerModule;