Skip to content

Commit ff18934

Browse files
OskarLebudaRihanArfan
authored andcommitted
feat(openapi): add swagger ui config (#4185)
1 parent 3360bf6 commit ff18934

3 files changed

Lines changed: 59 additions & 5 deletions

File tree

docs/3.config/0.index.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,21 @@ openAPI: {
207207
}
208208
```
209209

210+
If you like to customize the Swagger UI, you can pass any [Swagger UI configuration option](https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/):
211+
212+
```js
213+
openAPI: {
214+
ui: {
215+
swagger: {
216+
persistAuthorization: true,
217+
deepLinking: true,
218+
docExpansion: 'none',
219+
filter: true,
220+
}
221+
}
222+
}
223+
```
224+
210225
Or if you want to customize the endpoints:
211226

212227
```js

src/runtime/internal/routes/swagger.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ export default defineHandler((event) => {
1010
const description = runtimeConfig.nitro?.openAPI?.meta?.description || "";
1111
const openAPIEndpoint = runtimeConfig.nitro?.openAPI?.route || "./_openapi.json";
1212

13+
const { route: _route, ...swaggerConfig } =
14+
(runtimeConfig.nitro?.openAPI?.ui?.swagger as Record<string, unknown>) || {};
15+
16+
const safeSwaggerConfig = JSON.stringify(swaggerConfig).replace(/<\//g, "<\\/");
17+
1318
const CDN_BASE = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@^5";
1419
event.res.headers.set("Content-Type", "text/html");
1520
return /* html */ `<!doctype html>
@@ -31,13 +36,14 @@ export default defineHandler((event) => {
3136
<script>
3237
window.onload = () => {
3338
window.ui = SwaggerUIBundle({
34-
url: ${JSON.stringify(openAPIEndpoint)},
35-
dom_id: "#swagger-ui",
3639
presets: [
3740
SwaggerUIBundle.presets.apis,
3841
SwaggerUIStandalonePreset,
3942
],
40-
layout2: "StandaloneLayout",
43+
layout: "StandaloneLayout",
44+
...${safeSwaggerConfig},
45+
url: ${JSON.stringify(openAPIEndpoint)},
46+
dom_id: "#swagger-ui",
4147
});
4248
};
4349
</script>

src/types/openapi.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
// import type { ApiReferenceConfiguration as ScalarConfig } from "@scalar/api-reference";
22

3+
/**
4+
* Swagger UI configuration options.
5+
*
6+
* @see https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/
7+
*/
8+
export interface SwaggerUIConfig {
9+
deepLinking?: boolean;
10+
displayOperationId?: boolean;
11+
defaultModelsExpandDepth?: number;
12+
defaultModelExpandDepth?: number;
13+
defaultModelRendering?: "example" | "model";
14+
displayRequestDuration?: boolean;
15+
docExpansion?: "list" | "full" | "none";
16+
filter?: boolean | string;
17+
persistAuthorization?: boolean;
18+
requestSnippetsEnabled?: boolean;
19+
showExtensions?: boolean;
20+
showCommonExtensions?: boolean;
21+
/** Only "alpha" is supported (function values are not JSON-serializable). */
22+
tagsSorter?: "alpha";
23+
/**
24+
* Note: function callbacks cannot be passed via Nitro configuration (not JSON-serializable).
25+
*/
26+
onComplete?: never;
27+
layout?: string;
28+
configUrl?: string;
29+
oauth2RedirectUrl?: string;
30+
withCredentials?: boolean;
31+
[key: string]: unknown;
32+
}
33+
334
/**
435
* Nitro OpenAPI configuration.
536
*
@@ -57,16 +88,18 @@ export interface NitroOpenAPIConfig {
5788
* Swagger UI configuration.
5889
*
5990
* Set to `false` to disable.
91+
*
92+
* @see https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/
6093
*/
6194
swagger?:
6295
| false
63-
| {
96+
| (SwaggerUIConfig & {
6497
/**
6598
* Route for Swagger UI.
6699
*
67100
* @default "/_swagger"
68101
*/
69102
route?: string;
70-
};
103+
});
71104
};
72105
}

0 commit comments

Comments
 (0)