1
1
/* eslint-disable @typescript-eslint/ban-ts-comment */
2
- import { Elysia , type InternalRoute } from " elysia" ;
2
+ import { Elysia , type InternalRoute } from ' elysia'
3
3
4
- import { SwaggerUIRender } from " ./swagger" ;
5
- import { ScalarRender } from " ./scalar" ;
4
+ import { SwaggerUIRender } from ' ./swagger'
5
+ import { ScalarRender } from ' ./scalar'
6
6
7
- import { filterPaths , registerSchemaPath } from " ./utils" ;
7
+ import { filterPaths , registerSchemaPath } from ' ./utils'
8
8
9
- import type { OpenAPIV3 } from " openapi-types" ;
10
- import type { ReferenceConfiguration } from " ./scalar/types" ;
11
- import type { ElysiaSwaggerConfig } from " ./types" ;
9
+ import type { OpenAPIV3 } from ' openapi-types'
10
+ import type { ReferenceConfiguration } from ' ./scalar/types'
11
+ import type { ElysiaSwaggerConfig } from ' ./types'
12
12
13
13
/**
14
14
* Plugin for [elysia](https://github.com/elysiajs/elysia) that auto-generate Swagger page.
15
15
*
16
16
* @see https://github.com/elysiajs/elysia-swagger
17
17
*/
18
- export const swagger = async < Path extends string = " /swagger" > (
18
+ export const swagger = async < Path extends string = ' /swagger' > (
19
19
{
20
- provider = " scalar" ,
21
- scalarVersion = " latest" ,
22
- scalarCDN = "" ,
20
+ provider = ' scalar' ,
21
+ scalarVersion = ' latest' ,
22
+ scalarCDN = '' ,
23
23
scalarConfig = { } ,
24
24
documentation = { } ,
25
- version = " 5.9.0" ,
25
+ version = ' 5.9.0' ,
26
26
excludeStaticFile = true ,
27
- path = " /swagger" as Path ,
27
+ path = ' /swagger' as Path ,
28
28
exclude = [ ] ,
29
29
swaggerOptions = { } ,
30
30
theme = `https://unpkg.com/swagger-ui-dist@${ version } /swagger-ui.css` ,
31
31
autoDarkMode = true ,
32
- excludeMethods = [ " OPTIONS" ] ,
33
- excludeTags = [ ] ,
32
+ excludeMethods = [ ' OPTIONS' ] ,
33
+ excludeTags = [ ]
34
34
} : ElysiaSwaggerConfig < Path > = {
35
- provider : " scalar" ,
36
- scalarVersion : " latest" ,
37
- scalarCDN : "" ,
35
+ provider : ' scalar' ,
36
+ scalarVersion : ' latest' ,
37
+ scalarCDN : '' ,
38
38
scalarConfig : { } ,
39
39
documentation : { } ,
40
- version : " 5.9.0" ,
40
+ version : ' 5.9.0' ,
41
41
excludeStaticFile : true ,
42
- path : " /swagger" as Path ,
42
+ path : ' /swagger' as Path ,
43
43
exclude : [ ] ,
44
44
swaggerOptions : { } ,
45
45
autoDarkMode : true ,
46
- excludeMethods : [ " OPTIONS" ] ,
47
- excludeTags : [ ] ,
48
- } ,
46
+ excludeMethods : [ ' OPTIONS' ] ,
47
+ excludeTags : [ ]
48
+ }
49
49
) => {
50
- const schema = { } ;
51
- let totalRoutes = 0 ;
50
+ const schema = { }
51
+ let totalRoutes = 0
52
52
53
53
if ( ! version )
54
- version = `https://unpkg.com/swagger-ui-dist@${ version } /swagger-ui.css` ;
54
+ version = `https://unpkg.com/swagger-ui-dist@${ version } /swagger-ui.css`
55
55
56
56
const info = {
57
- title : " Elysia Documentation" ,
58
- description : " Development documentation" ,
59
- version : " 0.0.0" ,
60
- ...documentation . info ,
61
- } ;
57
+ title : ' Elysia Documentation' ,
58
+ description : ' Development documentation' ,
59
+ version : ' 0.0.0' ,
60
+ ...documentation . info
61
+ }
62
62
63
- const relativePath = path . startsWith ( "/" ) ? path . slice ( 1 ) : path ;
63
+ const relativePath = path . startsWith ( '/' ) ? path . slice ( 1 ) : path
64
64
65
- const app = new Elysia ( { name : " @elysiajs/swagger" } ) ;
65
+ const app = new Elysia ( { name : ' @elysiajs/swagger' } )
66
66
67
67
app . get ( path , function documentation ( ) {
68
68
const combinedSwaggerOptions = {
69
69
url : `${ relativePath } /json` ,
70
- dom_id : " #swagger-ui" ,
71
- ...swaggerOptions ,
72
- } ;
70
+ dom_id : ' #swagger-ui' ,
71
+ ...swaggerOptions
72
+ }
73
73
74
74
const stringifiedSwaggerOptions = JSON . stringify (
75
75
combinedSwaggerOptions ,
76
76
( key , value ) => {
77
- if ( typeof value == " function" ) return undefined ;
77
+ if ( typeof value == ' function' ) return undefined
78
78
79
- return value ;
80
- } ,
81
- ) ;
79
+ return value
80
+ }
81
+ )
82
82
83
83
const scalarConfiguration : ReferenceConfiguration = {
84
84
spec : {
85
85
...scalarConfig . spec ,
86
- url : `${ relativePath } /json` ,
86
+ url : `${ relativePath } /json`
87
87
} ,
88
- ...scalarConfig ,
89
- } ;
88
+ ...scalarConfig
89
+ }
90
90
91
91
return new Response (
92
- provider === " swagger-ui"
92
+ provider === ' swagger-ui'
93
93
? SwaggerUIRender (
94
94
info ,
95
95
version ,
96
96
theme ,
97
97
stringifiedSwaggerOptions ,
98
- autoDarkMode ,
98
+ autoDarkMode
99
99
)
100
100
: ScalarRender ( scalarVersion , scalarConfiguration , scalarCDN ) ,
101
101
{
102
102
headers : {
103
- " content-type" : " text/html; charset=utf8" ,
104
- } ,
105
- } ,
106
- ) ;
107
- } ) . get ( path === "/" ? " /json" : `${ path } /json` , function openAPISchema ( ) {
103
+ ' content-type' : ' text/html; charset=utf8'
104
+ }
105
+ }
106
+ )
107
+ } ) . get ( path === '/' ? ' /json' : `${ path } /json` , function openAPISchema ( ) {
108
108
// @ts -expect-error Private property
109
- const routes = app . getGlobalRoutes ( ) as InternalRoute [ ] ;
109
+ const routes = app . getGlobalRoutes ( ) as InternalRoute [ ]
110
110
111
111
if ( routes . length !== totalRoutes ) {
112
- totalRoutes = routes . length ;
112
+ totalRoutes = routes . length
113
113
114
114
routes . forEach ( ( route : InternalRoute ) => {
115
- if ( excludeMethods . includes ( route . method ) ) return ;
115
+ if ( excludeMethods . includes ( route . method ) ) return
116
116
117
117
registerSchemaPath ( {
118
118
schema,
@@ -121,45 +121,45 @@ export const swagger = async <Path extends string = "/swagger">(
121
121
path : route . path ,
122
122
// @ts -ignore
123
123
models : app . definitions ?. type ,
124
- contentType : route . hooks . type ,
125
- } ) ;
126
- } ) ;
124
+ contentType : route . hooks . type
125
+ } )
126
+ } )
127
127
}
128
128
129
129
return {
130
- openapi : " 3.0.3" ,
130
+ openapi : ' 3.0.3' ,
131
131
...{
132
132
...documentation ,
133
133
tags : documentation . tags ?. filter (
134
- ( tag ) => ! excludeTags ?. includes ( tag ?. name ) ,
134
+ ( tag ) => ! excludeTags ?. includes ( tag ?. name )
135
135
) ,
136
136
info : {
137
- title : " Elysia Documentation" ,
138
- description : " Development documentation" ,
139
- version : " 0.0.0" ,
140
- ...documentation . info ,
141
- } ,
137
+ title : ' Elysia Documentation' ,
138
+ description : ' Development documentation' ,
139
+ version : ' 0.0.0' ,
140
+ ...documentation . info
141
+ }
142
142
} ,
143
143
paths : {
144
- ...filterPaths ( schema , {
144
+ ...filterPaths ( schema , relativePath , {
145
145
excludeStaticFile,
146
- exclude : Array . isArray ( exclude ) ? exclude : [ exclude ] ,
146
+ exclude : Array . isArray ( exclude ) ? exclude : [ exclude ]
147
147
} ) ,
148
- ...documentation . paths ,
148
+ ...documentation . paths
149
149
} ,
150
150
components : {
151
151
...documentation . components ,
152
152
schemas : {
153
153
// @ts -ignore
154
154
...app . definitions ?. type ,
155
- ...documentation . components ?. schemas ,
156
- } ,
157
- } ,
158
- } satisfies OpenAPIV3 . Document ;
159
- } ) ;
155
+ ...documentation . components ?. schemas
156
+ }
157
+ }
158
+ } satisfies OpenAPIV3 . Document
159
+ } )
160
160
161
- // This is intentional to prevent deeply nested type
162
- return app ;
163
- } ;
161
+ return app
162
+ }
164
163
165
- export default swagger ;
164
+ export type { ElysiaSwaggerConfig }
165
+ export default swagger
0 commit comments