Skip to content

Commit 37e0697

Browse files
authored
Merge pull request #18 from mohankumargupta/feat-swaggerui-options
swagger ui options
2 parents 929c08d + c726674 commit 37e0697

File tree

5 files changed

+100
-7
lines changed

5 files changed

+100
-7
lines changed

example/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ const app = new Elysia({
3333
}
3434
}
3535
}
36-
}
36+
},
37+
swaggerOptions: {
38+
persistAuthorization: true
39+
},
3740
})
3841
)
3942
.use(plugin)

example/index2.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Elysia } from 'elysia'
2+
import { swagger } from '../src/index'
3+
import { plugin } from './plugin'
4+
5+
const app = new Elysia({
6+
// aot: false
7+
})
8+
.use(
9+
swagger({
10+
documentation: {
11+
info: {
12+
title: 'Elysia',
13+
version: '0.6.10'
14+
},
15+
tags: [
16+
{
17+
name: 'Test',
18+
description: 'Hello'
19+
}
20+
],
21+
security: [
22+
{JwtAuth: []}
23+
],
24+
components: {
25+
schemas: {
26+
User: {
27+
description: 'string'
28+
}
29+
},
30+
securitySchemes: {
31+
JwtAuth: {
32+
type: 'http',
33+
scheme: 'bearer',
34+
bearerFormat: 'JWT',
35+
description: 'Enter JWT Bearer token **_only_**'
36+
}
37+
}
38+
}
39+
},
40+
swaggerOptions: {
41+
persistAuthorization: true
42+
},
43+
})
44+
)
45+
.use(plugin)
46+
.listen(8080)

src/index.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ export const swagger =
1717
version = '4.18.2',
1818
excludeStaticFile = true,
1919
path = '/swagger' as Path,
20-
exclude = []
20+
exclude = [],
21+
swaggerOptions = {},
2122
}: ElysiaSwaggerConfig<Path> = {
2223
documentation: {},
2324
version: '4.18.2',
2425
excludeStaticFile: true,
2526
path: '/swagger' as Path,
26-
exclude: []
27+
exclude: [],
28+
swaggerOptions: {},
2729
}
2830
) =>
2931
(app: Elysia) => {
@@ -40,6 +42,22 @@ export const swagger =
4042
const pathWithPrefix = `${app.config.prefix}${path}`;
4143

4244
app.get(path, () => {
45+
const combinedSwaggerOptions = {
46+
url: '${pathWithPrefix}/json',
47+
dom_id: '#swagger-ui',
48+
...swaggerOptions
49+
}
50+
const stringifiedSwaggerOptions = JSON.stringify(combinedSwaggerOptions,
51+
(key,value) => {
52+
if (typeof value == "function") {
53+
return undefined;
54+
}
55+
else {
56+
return value;
57+
}
58+
}
59+
)
60+
4361
return new Response(
4462
`<!DOCTYPE html>
4563
<html lang="en">
@@ -62,10 +80,7 @@ export const swagger =
6280
<script src="https://unpkg.com/swagger-ui-dist@${version}/swagger-ui-bundle.js" crossorigin></script>
6381
<script>
6482
window.onload = () => {
65-
window.ui = SwaggerUIBundle({
66-
url: '${pathWithPrefix}/json',
67-
dom_id: '#swagger-ui',
68-
});
83+
window.ui = SwaggerUIBundle(${stringifiedSwaggerOptions});
6984
};
7085
</script>
7186
</body>

src/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { OpenAPIV3 } from 'openapi-types'
2+
import {SwaggerUIOptions} from 'swagger-ui'
23

34
export interface ElysiaSwaggerConfig<Path extends string = '/swagger'> {
45
/**
@@ -37,4 +38,15 @@ export interface ElysiaSwaggerConfig<Path extends string = '/swagger'> {
3738
* @default []
3839
*/
3940
exclude?: string | RegExp | (string | RegExp)[]
41+
/**
42+
* Options to send to SwaggerUIBundle
43+
* Currently, options that are defined as functions such as requestInterceptor
44+
* and onComplete are not supported.
45+
*/
46+
swaggerOptions?: Omit<Partial<SwaggerUIOptions>,
47+
'dom_id'|'dom_node'|'spec'|'url'|'urls'
48+
|'layout' | 'pluginsOptions' | 'plugins'|'presets'
49+
|'onComplete' |'requestInterceptor'|'responseInterceptor'
50+
|'modelPropertyMacro'|'parameterMacro'
51+
>
4052
}

test/index.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ describe('Swagger', () => {
6666
expect(res.status).toBe(200)
6767
})
6868

69+
it('Swagger UI options', async () => {
70+
const app = new Elysia().use(
71+
swagger({
72+
swaggerOptions: {
73+
persistAuthorization: true
74+
}
75+
})
76+
)
77+
const res = await app.handle(req('/swagger')).then((x) => x.text())
78+
const expected = `
79+
window.onload = () => {
80+
window.ui = SwaggerUIBundle({"url":"/swagger/json","dom_id":"#swagger-ui","persistAuthorization":true});
81+
};
82+
`
83+
expect(res.trim().includes(expected.trim())).toBe(true)
84+
})
85+
6986
it('should not return content response when using Void type', async () => {
7087
const app = new Elysia().use(
7188
swagger())

0 commit comments

Comments
 (0)