Skip to content

Commit

Permalink
feat(SwaggerCustomOptions)!: extend custom{Js,JsStr,Css} to include i…
Browse files Browse the repository at this point in the history
…ntegrity
  • Loading branch information
tafaust committed Oct 29, 2023
1 parent f189131 commit b29efb0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
27 changes: 23 additions & 4 deletions lib/interfaces/swagger-custom-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@ import { SwaggerUiOptions } from './swagger-ui-options.interface';
import { SwaggerDocumentOptions } from './swagger-document-options.interface';
import { OpenAPIObject } from './open-api-spec.interface';

export interface CustomCssUrl {
customCssUrl: string | string[];
customCssUrlIntegrity?: string | string[];
}

export interface CustomJs {
customJs: string | string[];
customJsIntegrity?: string | string[];
}

export interface CustomJsStr {
customJsStr: string | string[];
customJsStrIntegrity?: string | string[];
}

export interface SwaggerCustomOptions {
useGlobalPrefix?: boolean;
explorer?: boolean;
swaggerOptions?: SwaggerUiOptions;
customCss?: string;
customCssUrl?: string | string[];
customJs?: string | string[];
customJsStr?: string | string[];
customCssUrl?: CustomCssUrl;
customJs?: CustomJs;
customJsStr?: CustomJsStr;
customfavIcon?: string;
customSwaggerUiPath?: string;
swaggerUrl?: string;
Expand All @@ -19,5 +34,9 @@ export interface SwaggerCustomOptions {
urls?: Record<'url' | 'name', string>[];
jsonDocumentUrl?: string;
yamlDocumentUrl?: string;
patchDocumentOnRequest?: <TRequest = any, TResponse = any> (req: TRequest, res: TResponse, document: OpenAPIObject) => OpenAPIObject;
patchDocumentOnRequest?: <TRequest = any, TResponse = any>(
req: TRequest,
res: TResponse,
document: OpenAPIObject
) => OpenAPIObject;
}
41 changes: 29 additions & 12 deletions lib/swagger-ui/swagger-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function buildSwaggerInitJS(
};

const jsInitOptions = buildJSInitOptions(swaggerInitOptions);
// Cannot dynamically compute integrity: https://github.com/nestjs/swagger/issues/2667#issuecomment-1780515512
return jsTemplateString.replace('<% swaggerOptions %>', jsInitOptions);
}

Expand All @@ -34,20 +35,30 @@ export function getSwaggerAssetsAbsoluteFSPath() {
return swaggerAssetsAbsoluteFSPath;
}

function toExternalScriptTag(url: string) {
return `<script src='${url}'></script>`;
function toExternalScriptTag(url: string, integrity?: string) {
// Cannot dynamically compute integrity: https://github.com/nestjs/swagger/issues/2667#issuecomment-1780515512
return `<script src='${url}' ${
!!integrity ? `integrity="${integrity}"` : ''
}></script>`.replace(/\s+>/g, '>');
}

function toInlineScriptTag(jsCode: string) {
return `<script>${jsCode}</script>`;
function toInlineScriptTag(jsCode: string, integrity?: string) {
// Cannot dynamically compute integrity: https://github.com/nestjs/swagger/issues/2667#issuecomment-1780515512
return `<script ${
!!integrity ? `integrity="${integrity}"` : ''
}>${jsCode}</script>`.replace(/\s+>/g, '>');
}

function toExternalStylesheetTag(url: string) {
return `<link href='${url}' rel='stylesheet'>`;
function toExternalStylesheetTag(url: string, integrity?: string) {
// Cannot dynamically compute integrity: https://github.com/nestjs/swagger/issues/2667#issuecomment-1780515512
return `<link href='${url}' rel='stylesheet' ${
!!integrity ? `integrity="${integrity}"` : ''
}>`.replace(/\s+>/g, '>');
}

function toTags(
customCode: string | string[] | undefined,
integrity: typeof customCode,
toScript: (url: string) => string
) {
if (!customCode) {
Expand All @@ -71,11 +82,11 @@ export function buildSwaggerHTML(
) {
const {
customCss = '',
customJs = '',
customJsStr = '',
customJs: { customJs = '', customJsIntegrity = null },
customJsStr: { customJsStr = '', customJsStrIntegrity = null },
customfavIcon = false,
customSiteTitle = 'Swagger UI',
customCssUrl = '',
customCssUrl: { customCssUrl = '', customCssUrlIntegrity = null },
explorer = false
} = customOptions;

Expand All @@ -91,11 +102,17 @@ export function buildSwaggerHTML(
.replace('<% explorerCss %>', explorerCss)
.replace('<% favIconString %>', favIconString)
.replace(/<% baseUrl %>/g, baseUrl)
.replace('<% customJs %>', toTags(customJs, toExternalScriptTag))
.replace('<% customJsStr %>', toTags(customJsStr, toInlineScriptTag))
.replace(
'<% customJs %>',
toTags(customJs, customJsIntegrity, toExternalScriptTag)
)
.replace(
'<% customJsStr %>',
toTags(customJsStr, customJsStrIntegrity, toInlineScriptTag)
)
.replace(
'<% customCssUrl %>',
toTags(customCssUrl, toExternalStylesheetTag)
toTags(customCssUrl, customCssUrlIntegrity, toExternalStylesheetTag)
)
.replace('<% title %>', customSiteTitle);
}

0 comments on commit b29efb0

Please sign in to comment.