File tree Expand file tree Collapse file tree 5 files changed +100
-7
lines changed Expand file tree Collapse file tree 5 files changed +100
-7
lines changed Original file line number Diff line number Diff line change @@ -33,7 +33,10 @@ const app = new Elysia({
33
33
}
34
34
}
35
35
}
36
- }
36
+ } ,
37
+ swaggerOptions : {
38
+ persistAuthorization : true
39
+ } ,
37
40
} )
38
41
)
39
42
. use ( plugin )
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change @@ -17,13 +17,15 @@ export const swagger =
17
17
version = '4.18.2' ,
18
18
excludeStaticFile = true ,
19
19
path = '/swagger' as Path ,
20
- exclude = [ ]
20
+ exclude = [ ] ,
21
+ swaggerOptions = { } ,
21
22
} : ElysiaSwaggerConfig < Path > = {
22
23
documentation : { } ,
23
24
version : '4.18.2' ,
24
25
excludeStaticFile : true ,
25
26
path : '/swagger' as Path ,
26
- exclude : [ ]
27
+ exclude : [ ] ,
28
+ swaggerOptions : { } ,
27
29
}
28
30
) =>
29
31
( app : Elysia ) => {
@@ -40,6 +42,22 @@ export const swagger =
40
42
const pathWithPrefix = `${ app . config . prefix } ${ path } ` ;
41
43
42
44
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
+
43
61
return new Response (
44
62
`<!DOCTYPE html>
45
63
<html lang="en">
@@ -62,10 +80,7 @@ export const swagger =
62
80
<script src="https://unpkg.com/swagger-ui-dist@${ version } /swagger-ui-bundle.js" crossorigin></script>
63
81
<script>
64
82
window.onload = () => {
65
- window.ui = SwaggerUIBundle({
66
- url: '${ pathWithPrefix } /json',
67
- dom_id: '#swagger-ui',
68
- });
83
+ window.ui = SwaggerUIBundle(${ stringifiedSwaggerOptions } );
69
84
};
70
85
</script>
71
86
</body>
Original file line number Diff line number Diff line change 1
1
import type { OpenAPIV3 } from 'openapi-types'
2
+ import { SwaggerUIOptions } from 'swagger-ui'
2
3
3
4
export interface ElysiaSwaggerConfig < Path extends string = '/swagger' > {
4
5
/**
@@ -37,4 +38,15 @@ export interface ElysiaSwaggerConfig<Path extends string = '/swagger'> {
37
38
* @default []
38
39
*/
39
40
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
+ >
40
52
}
Original file line number Diff line number Diff line change @@ -66,6 +66,23 @@ describe('Swagger', () => {
66
66
expect ( res . status ) . toBe ( 200 )
67
67
} )
68
68
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
+
69
86
it ( 'should not return content response when using Void type' , async ( ) => {
70
87
const app = new Elysia ( ) . use (
71
88
swagger ( ) )
You can’t perform that action at this time.
0 commit comments