UNPKG

@b8n/nestjs-rapidoc

Version:
176 lines (175 loc) 8.65 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RapidocModule = void 0; const get_global_prefix_1 = require("@nestjs/swagger/dist/utils/get-global-prefix"); const normalize_rel_path_1 = require("@nestjs/swagger/dist/utils/normalize-rel-path"); const resolve_path_util_1 = require("@nestjs/swagger/dist/utils/resolve-path.util"); const validate_global_prefix_util_1 = require("@nestjs/swagger/dist/utils/validate-global-prefix.util"); const validate_path_util_1 = require("@nestjs/swagger/dist/utils/validate-path.util"); const fs_1 = require("fs"); const jsyaml = __importStar(require("js-yaml")); const path_1 = require("path"); const constants_1 = require("./constants"); const helpers_1 = require("./helpers"); class RapidocModule { static serveOAuthReceiver(finalPath, urlLastSubdirectory, httpAdapter) { const baseUrlForRapidoc = (0, normalize_rel_path_1.normalizeRelPath)(`./${urlLastSubdirectory}/`); let html; return httpAdapter.get((0, normalize_rel_path_1.normalizeRelPath)(`${finalPath}${constants_1.OAUTH_RECEIVER_PATH}`), (req, res) => { res.type("text/html"); if (!html) { html = (0, helpers_1.buildOauthReceiverHtml)(baseUrlForRapidoc); } res.send(html); }); } static serveDocuments(finalPath, urlLastSubdirectory, httpAdapter, documentOrFactory, options) { let document; const lazyBuildDocument = () => { const document = typeof documentOrFactory === "function" ? documentOrFactory() : documentOrFactory; if (!Array.isArray(document.servers) || document.servers.length === 0) { document.servers = [{ url: "/" }]; } return document; }; const baseUrlForRapidoc = (0, normalize_rel_path_1.normalizeRelPath)(`./${urlLastSubdirectory}/`); let html; const handleHTMLRequest = (req, res) => { res.type("text/html"); if (!document) { document = lazyBuildDocument(); } if (options.rapidocOptions.patchDocumentOnRequest) { const documentToSerialize = options.rapidocOptions.patchDocumentOnRequest(req, res, document); const htmlPerRequest = (0, helpers_1.buildRapidocHtml)(baseUrlForRapidoc, documentToSerialize, options.rapidocOptions); return res.send(htmlPerRequest); } if (!html) { html = (0, helpers_1.buildRapidocHtml)(baseUrlForRapidoc, document, options.rapidocOptions); } res.send(html); }; httpAdapter.get(finalPath, handleHTMLRequest); try { httpAdapter.get((0, normalize_rel_path_1.normalizeRelPath)(`${finalPath}/`), handleHTMLRequest); } catch (e) { } RapidocModule.serveOAuthReceiver(finalPath, urlLastSubdirectory, httpAdapter); httpAdapter.get((0, normalize_rel_path_1.normalizeRelPath)(options.jsonDocumentUrl), (req, res) => { res.type("application/json"); if (!document) { document = lazyBuildDocument(); } const documentToSerialize = options.rapidocOptions.patchDocumentOnRequest ? options.rapidocOptions.patchDocumentOnRequest(req, res, document) : document; res.send(JSON.stringify(documentToSerialize)); }); httpAdapter.get((0, normalize_rel_path_1.normalizeRelPath)(options.yamlDocumentUrl), (req, res) => { res.type("text/yaml"); if (!document) { document = lazyBuildDocument(); } const documentToSerialize = options.rapidocOptions.patchDocumentOnRequest ? options.rapidocOptions.patchDocumentOnRequest(req, res, document) : document; const yamlDocument = jsyaml.dump(documentToSerialize, { skipInvalid: true, noRefs: true, }); res.send(yamlDocument); }); } static getRapidocAssetsPath(customStaticPath) { let rapidocAssetsPath; if (customStaticPath) { rapidocAssetsPath = (0, resolve_path_util_1.resolvePath)(customStaticPath); } else { const modulePath = require.resolve("rapidoc"); rapidocAssetsPath = (0, path_1.dirname)(modulePath); } return rapidocAssetsPath; } static serveDefaultRapidocFavicon(finalPath, app, customStaticPath) { const httpAdapter = app.getHttpAdapter(); const rapidocAssetsPath = RapidocModule.getRapidocAssetsPath(customStaticPath); const logoDirectoryPath = (0, path_1.dirname)(rapidocAssetsPath); httpAdapter.get((0, normalize_rel_path_1.normalizeRelPath)(`${finalPath}/favicon.png`), (req, res) => { const fileStream = (0, fs_1.createReadStream)(`${logoDirectoryPath}/logo.png`); res.type("image/png").send(fileStream); }); } static serveStatic(finalPath, app, customStaticPath) { const httpAdapter = app.getHttpAdapter(); const rapidocAssetsPath = RapidocModule.getRapidocAssetsPath(customStaticPath); if (httpAdapter && httpAdapter.getType() === "fastify") { app.useStaticAssets({ root: rapidocAssetsPath, prefix: finalPath, decorateReply: false, preCompressed: true, }); } else { app.useStaticAssets(rapidocAssetsPath, { prefix: finalPath, }); } } static setup(path, app, documentOrFactory, options) { const globalPrefix = (0, get_global_prefix_1.getGlobalPrefix)(app); const finalPath = (0, validate_path_util_1.validatePath)(options?.useGlobalPrefix && (0, validate_global_prefix_util_1.validateGlobalPrefix)(globalPrefix) ? `${globalPrefix}${(0, validate_path_util_1.validatePath)(path)}` : path); const urlLastSubdirectory = finalPath.split("/").slice(-1).pop() || ""; const validatedGlobalPrefix = options?.useGlobalPrefix && (0, validate_global_prefix_util_1.validateGlobalPrefix)(globalPrefix) ? (0, validate_path_util_1.validatePath)(globalPrefix) : ""; const finalJSONDocumentPath = options?.jsonDocumentUrl ? `${validatedGlobalPrefix}${(0, validate_path_util_1.validatePath)(options.jsonDocumentUrl)}` : `${finalPath}${constants_1.OPENAPI_JSON_PATH}`; const finalYAMLDocumentPath = options?.yamlDocumentUrl ? `${validatedGlobalPrefix}${(0, validate_path_util_1.validatePath)(options.yamlDocumentUrl)}` : `${finalPath}${constants_1.OPENAPI_YAML_PATH}`; const httpAdapter = app.getHttpAdapter(); RapidocModule.serveDocuments(finalPath, urlLastSubdirectory, httpAdapter, documentOrFactory, { jsonDocumentUrl: finalJSONDocumentPath, yamlDocumentUrl: finalYAMLDocumentPath, rapidocOptions: options || {}, }); RapidocModule.serveStatic(finalPath, app, options?.customRapidocAssetsPath); const serveStaticSlashEndingPath = `${finalPath}/${urlLastSubdirectory}`; if (serveStaticSlashEndingPath !== finalPath) { RapidocModule.serveStatic(serveStaticSlashEndingPath, app); } RapidocModule.serveDefaultRapidocFavicon(finalPath, app, options?.customRapidocAssetsPath); } } exports.RapidocModule = RapidocModule;