@b8n/nestjs-rapidoc
Version:
NestJS module for RapiDoc
74 lines (73 loc) • 2.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildOauthReceiverHtml = exports.buildRapidocHtml = exports.buildRapidocOptions = void 0;
const constants_1 = require("./constants");
function toExternalScriptTag(url) {
return `<script src='${url}'></script>`;
}
function toInlineScriptTag(jsCode) {
return `<script>${jsCode}</script>`;
}
function toExternalStylesheetTag(url) {
return `<link href='${url}' rel='stylesheet'>`;
}
function toTags(customCode, toScript) {
if (!customCode) {
return "";
}
if (typeof customCode === "string") {
return toScript(customCode);
}
else {
return customCode.map(toScript).join("\n");
}
}
function toLogoTag(customLogo) {
if (!customLogo) {
return "";
}
return `<img slot="logo" src='${customLogo}' style="height:36px;width:36px;margin-left:5px" />`;
}
function buildRapidocOptions(rapidocOptions) {
const options = [];
if (!("allowSpecFileLoad" in rapidocOptions)) {
rapidocOptions.allowSpecFileLoad = false;
}
if (!("allowSpecUrlLoad" in rapidocOptions)) {
rapidocOptions.allowSpecUrlLoad = false;
}
for (const [key, value] of Object.entries(rapidocOptions)) {
const kebabCaseKey = key.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
if (typeof value === "boolean") {
options.push(`${kebabCaseKey}="${value.toString()}"`);
}
else {
options.push(`${kebabCaseKey}="${value}"`);
}
}
return options.join(" ");
}
exports.buildRapidocOptions = buildRapidocOptions;
function buildRapidocHtml(baseUrl, openAPIDoc, customOptions = {}) {
const { customCss = "", customCssUrl = "", customJs = "", customJsStr = "", customFavIcon = false, customSiteTitle = "RapiDoc", customLogo, } = customOptions;
const favIconString = customFavIcon
? `<link rel='icon' href='${customFavIcon}' />`
: constants_1.favIconHtml;
return constants_1.rapidocHtml
.replace("<% customCss %>", customCss)
.replace("<% favIconString %>", favIconString)
.replace(/<% baseUrl %>/g, baseUrl)
.replace("<% customJs %>", toTags(customJs, toExternalScriptTag))
.replace("<% customJsStr %>", toTags(customJsStr, toInlineScriptTag))
.replace("<% customCssUrl %>", toTags(customCssUrl, toExternalStylesheetTag))
.replace("<% customLogo %>", toLogoTag(customLogo))
.replace("<% title %>", customSiteTitle)
.replace("<% rapidocOptions %>", buildRapidocOptions(customOptions.rapidocOptions || {}))
.replace("<% openAPIDoc %>", JSON.stringify(openAPIDoc))
.trim();
}
exports.buildRapidocHtml = buildRapidocHtml;
function buildOauthReceiverHtml(baseUrl) {
return constants_1.oauthReceiverHtml.replace(/<% baseUrl %>/g, baseUrl);
}
exports.buildOauthReceiverHtml = buildOauthReceiverHtml;