1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.SwaggerModule = void 0;
|
4 | const jsyaml = require("js-yaml");
|
5 | const swagger_scanner_1 = require("./swagger-scanner");
|
6 | const swagger_ui_1 = require("./swagger-ui");
|
7 | const assign_two_levels_deep_1 = require("./utils/assign-two-levels-deep");
|
8 | const get_global_prefix_1 = require("./utils/get-global-prefix");
|
9 | const validate_path_util_1 = require("./utils/validate-path.util");
|
10 | const normalize_rel_path_1 = require("./utils/normalize-rel-path");
|
11 | class SwaggerModule {
|
12 | static createDocument(app, config, options = {}) {
|
13 | const swaggerScanner = new swagger_scanner_1.SwaggerScanner();
|
14 | const document = swaggerScanner.scanApplication(app, options);
|
15 | document.components = (0, assign_two_levels_deep_1.assignTwoLevelsDeep)({}, config.components, document.components);
|
16 | return Object.assign(Object.assign({ openapi: '3.0.0', paths: {} }, config), document);
|
17 | }
|
18 | static serveStatic(finalPath, app) {
|
19 | const httpAdapter = app.getHttpAdapter();
|
20 | const swaggerAssetsAbsoluteFSPath = (0, swagger_ui_1.getSwaggerAssetsAbsoluteFSPath)();
|
21 | if (httpAdapter && httpAdapter.getType() === 'fastify') {
|
22 | app.useStaticAssets({
|
23 | root: swaggerAssetsAbsoluteFSPath,
|
24 | prefix: finalPath,
|
25 | decorateReply: false
|
26 | });
|
27 | }
|
28 | else {
|
29 | app.useStaticAssets(swaggerAssetsAbsoluteFSPath, { prefix: finalPath });
|
30 | }
|
31 | }
|
32 | static serveDocuments(finalPath, urlLastSubdirectory, httpAdapter, swaggerInitJS, yamlDocument, jsonDocument, html) {
|
33 | httpAdapter.get((0, normalize_rel_path_1.normalizeRelPath)(`${finalPath}/swagger-ui-init.js`), (req, res) => {
|
34 | res.type('application/javascript');
|
35 | res.send(swaggerInitJS);
|
36 | });
|
37 | try {
|
38 | httpAdapter.get((0, normalize_rel_path_1.normalizeRelPath)(`${finalPath}/${urlLastSubdirectory}/swagger-ui-init.js`), (req, res) => {
|
39 | res.type('application/javascript');
|
40 | res.send(swaggerInitJS);
|
41 | });
|
42 | }
|
43 | catch (err) {
|
44 | }
|
45 | httpAdapter.get(finalPath, (req, res) => {
|
46 | res.type('text/html');
|
47 | res.send(html);
|
48 | });
|
49 | try {
|
50 | httpAdapter.get((0, normalize_rel_path_1.normalizeRelPath)(`${finalPath}/`), (req, res) => {
|
51 | res.type('text/html');
|
52 | res.send(html);
|
53 | });
|
54 | }
|
55 | catch (err) {
|
56 | }
|
57 | httpAdapter.get((0, normalize_rel_path_1.normalizeRelPath)(`${finalPath}-json`), (req, res) => {
|
58 | res.type('application/json');
|
59 | res.send(jsonDocument);
|
60 | });
|
61 | httpAdapter.get((0, normalize_rel_path_1.normalizeRelPath)(`${finalPath}-yaml`), (req, res) => {
|
62 | res.type('text/yaml');
|
63 | res.send(yamlDocument);
|
64 | });
|
65 | }
|
66 | static setup(path, app, document, options) {
|
67 | const globalPrefix = (0, get_global_prefix_1.getGlobalPrefix)(app);
|
68 | const finalPath = (0, validate_path_util_1.validatePath)((options === null || options === void 0 ? void 0 : options.useGlobalPrefix) && globalPrefix && !globalPrefix.match(/^(\/?)$/)
|
69 | ? `${globalPrefix}${(0, validate_path_util_1.validatePath)(path)}`
|
70 | : path);
|
71 | const urlLastSubdirectory = finalPath.split('/').slice(-1).pop();
|
72 | const yamlDocument = jsyaml.dump(document, { skipInvalid: true });
|
73 | const jsonDocument = JSON.stringify(document);
|
74 | const baseUrlForSwaggerUI = (0, normalize_rel_path_1.normalizeRelPath)(`./${urlLastSubdirectory}/`);
|
75 | const html = (0, swagger_ui_1.buildSwaggerHTML)(baseUrlForSwaggerUI, document, options);
|
76 | const swaggerInitJS = (0, swagger_ui_1.buildSwaggerInitJS)(document, options);
|
77 | const httpAdapter = app.getHttpAdapter();
|
78 | SwaggerModule.serveDocuments(finalPath, urlLastSubdirectory, httpAdapter, swaggerInitJS, yamlDocument, jsonDocument, html);
|
79 | SwaggerModule.serveStatic(finalPath, app);
|
80 | const serveStaticSlashEndingPath = `${finalPath}/${urlLastSubdirectory}`;
|
81 | if (serveStaticSlashEndingPath !== finalPath) {
|
82 | SwaggerModule.serveStatic(serveStaticSlashEndingPath, app);
|
83 | }
|
84 | }
|
85 | }
|
86 | exports.SwaggerModule = SwaggerModule;
|