-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(): routing platform independence (fastify-swagger options backwa…
…rd compatibility)
- Loading branch information
Showing
5 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { OpenAPIObject } from './open-api-spec.interface'; | ||
export * from './swagger-custom-options.interface'; | ||
export * from './swagger-document-options.interface'; | ||
export * from './swagger-custom-options-legacy.instace'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
interface _CommonSwaggerCustomOptions { | ||
useGlobalPrefix?: boolean; | ||
} | ||
|
||
export interface ExpressSwaggerCustomOptions | ||
extends _CommonSwaggerCustomOptions { | ||
explorer?: boolean; | ||
swaggerOptions?: Record<string, any>; | ||
customCss?: string; | ||
customCssUrl?: string; | ||
customJs?: string; | ||
customfavIcon?: string; | ||
swaggerUrl?: string; | ||
customSiteTitle?: string; | ||
validatorUrl?: string; | ||
url?: string; | ||
urls?: Record<'url' | 'name', string>[]; | ||
} | ||
|
||
/* | ||
* @deprecated | ||
*/ | ||
export interface FastifySwaggerCustomOptions | ||
extends _CommonSwaggerCustomOptions { | ||
uiConfig?: Partial<{ | ||
deepLinking: boolean; | ||
displayOperationId: boolean; | ||
defaultModelsExpandDepth: number; | ||
defaultModelExpandDepth: number; | ||
defaultModelRendering: string; | ||
displayRequestDuration: boolean; | ||
docExpansion: string; | ||
filter: boolean | string; | ||
layout: string; | ||
maxDisplayedTags: number; | ||
showExtensions: boolean; | ||
showCommonExtensions: boolean; | ||
useUnsafeMarkdown: boolean; | ||
syntaxHighlight: | ||
| { | ||
activate?: boolean; | ||
theme?: string; | ||
} | ||
| false; | ||
tryItOutEnabled: boolean; | ||
validatorUrl: string | null; | ||
persistAuthorization: boolean; | ||
tagsSorter: string; | ||
operationsSorter: string; | ||
queryConfigEnabled: boolean; | ||
}>; | ||
initOAuth?: Record<string, any>; | ||
staticCSP?: boolean | string | Record<string, string | string[]>; | ||
transformStaticCSP?: (header: string) => string; | ||
uiHooks?: { | ||
onRequest?: Function; | ||
preHandler?: Function; | ||
}; | ||
} | ||
|
||
/* | ||
* TODO: remove entire file when FastifySwaggerCustomOptions backward compatibility layer is no longer needed | ||
* and use SwaggerCustomOptions in SwaggerModule.setup | ||
*/ | ||
export type SwaggerCustomOptionsLegacy = | ||
| FastifySwaggerCustomOptions | ||
| ExpressSwaggerCustomOptions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
FastifySwaggerCustomOptions, | ||
SwaggerCustomOptions, | ||
SwaggerCustomOptionsLegacy | ||
} from '../interfaces'; | ||
|
||
export interface FastifyExtra { | ||
initOAuth?: Record<string, any>; | ||
staticCSP?: boolean | string | Record<string, string | string[]>; | ||
transformStaticCSP?: (header: string) => string; | ||
uiHooks?: { | ||
onRequest?: Function; | ||
preHandler?: Function; | ||
}; | ||
} | ||
|
||
export interface ProcessSwaggerOptionsOutput { | ||
customOptions: SwaggerCustomOptions; | ||
extra: FastifyExtra; | ||
} | ||
|
||
export function processSwaggerOptions( | ||
options: SwaggerCustomOptionsLegacy = {} | ||
): ProcessSwaggerOptionsOutput { | ||
const unifiedOptions: SwaggerCustomOptions = options; | ||
const fastifyOptions: FastifySwaggerCustomOptions = options; | ||
|
||
const customOptions: SwaggerCustomOptions = { | ||
useGlobalPrefix: unifiedOptions.useGlobalPrefix, | ||
explorer: unifiedOptions.explorer, | ||
customCss: unifiedOptions.customCss, | ||
customCssUrl: unifiedOptions.customCssUrl, | ||
customJs: unifiedOptions.customJs, | ||
customfavIcon: unifiedOptions.customfavIcon, | ||
swaggerUrl: unifiedOptions.swaggerUrl, | ||
customSiteTitle: unifiedOptions.customSiteTitle, | ||
validatorUrl: unifiedOptions.validatorUrl, | ||
url: unifiedOptions.url, | ||
urls: unifiedOptions.urls, | ||
|
||
swaggerOptions: unifiedOptions.swaggerOptions || fastifyOptions.uiConfig | ||
}; | ||
|
||
const extra = { | ||
initOAuth: fastifyOptions.initOAuth, | ||
uiHooks: fastifyOptions.uiHooks | ||
}; | ||
|
||
return { customOptions, extra }; | ||
} |