@@ -50,7 +50,8 @@ export const openapi = <
50
50
swagger,
51
51
scalar,
52
52
references,
53
- mapJsonSchema
53
+ mapJsonSchema,
54
+ embedSpec
54
55
} : ElysiaOpenAPIConfig < Enabled , Path > = { } ) => {
55
56
if ( ! enabled ) return new Elysia ( { name : '@elysiajs/openapi' } )
56
57
@@ -66,6 +67,38 @@ export const openapi = <
66
67
let totalRoutes = 0
67
68
let cachedSchema : OpenAPIV3 . Document | undefined
68
69
70
+ const toFullSchema = ( {
71
+ paths,
72
+ components : { schemas }
73
+ } : ReturnType < typeof toOpenAPISchema > ) : OpenAPIV3 . Document => {
74
+ return ( cachedSchema = {
75
+ openapi : '3.0.3' ,
76
+ ...documentation ,
77
+ tags : ! exclude ?. tags
78
+ ? documentation . tags
79
+ : documentation . tags ?. filter (
80
+ ( tag ) => ! exclude . tags ?. includes ( tag . name )
81
+ ) ,
82
+ info : {
83
+ title : 'Elysia Documentation' ,
84
+ description : 'Development documentation' ,
85
+ version : '0.0.0' ,
86
+ ...documentation . info
87
+ } ,
88
+ paths : {
89
+ ...paths ,
90
+ ...documentation . paths
91
+ } ,
92
+ components : {
93
+ ...documentation . components ,
94
+ schemas : {
95
+ ...schemas ,
96
+ ...( documentation . components ?. schemas as any )
97
+ }
98
+ }
99
+ } )
100
+ }
101
+
69
102
const app = new Elysia ( { name : '@elysiajs/openapi' } )
70
103
. use ( ( app ) => {
71
104
if ( provider === null ) return app
@@ -80,64 +113,58 @@ export const openapi = <
80
113
autoDarkMode : true ,
81
114
...swagger
82
115
} )
83
- : ScalarRender ( info , {
84
- url : relativePath ,
85
- version : 'latest' ,
86
- cdn : `https://cdn.jsdelivr.net/npm/@scalar/api-reference@${ scalar ?. version ?? 'latest' } /dist/browser/standalone.min.js` ,
87
- ...( scalar as ApiReferenceConfiguration ) ,
88
- _integration : 'elysiajs'
89
- } ) ,
116
+ : ScalarRender (
117
+ info ,
118
+ {
119
+ url : relativePath ,
120
+ version : 'latest' ,
121
+ cdn : `https://cdn.jsdelivr.net/npm/@scalar/api-reference@${ scalar ?. version ?? 'latest' } /dist/browser/standalone.min.js` ,
122
+ ...( scalar as ApiReferenceConfiguration ) ,
123
+ _integration : 'elysiajs'
124
+ } ,
125
+ embedSpec
126
+ ? JSON . stringify (
127
+ totalRoutes === app . routes . length
128
+ ? cachedSchema
129
+ : toFullSchema (
130
+ toOpenAPISchema (
131
+ app ,
132
+ exclude ,
133
+ references ,
134
+ mapJsonSchema
135
+ )
136
+ )
137
+ )
138
+ : undefined
139
+ ) ,
90
140
{
91
141
headers : {
92
142
'content-type' : 'text/html; charset=utf8'
93
143
}
94
144
}
95
145
)
96
146
97
- return app . get ( path , isCloudflareWorker ( ) ? page : page ( ) , {
98
- detail : {
99
- hide : true
147
+ return app . get (
148
+ path ,
149
+ embedSpec || isCloudflareWorker ( ) ? page : page ( ) ,
150
+ {
151
+ detail : {
152
+ hide : true
153
+ }
100
154
}
101
- } )
155
+ )
102
156
} )
103
157
. get (
104
158
specPath ,
105
- function openAPISchema ( ) {
106
- if ( totalRoutes === app . routes . length ) return cachedSchema
159
+ function openAPISchema ( ) : OpenAPIV3 . Document {
160
+ if ( totalRoutes === app . routes . length && cachedSchema )
161
+ return cachedSchema
107
162
108
163
totalRoutes = app . routes . length
109
164
110
- const {
111
- paths,
112
- components : { schemas }
113
- } = toOpenAPISchema ( app , exclude , references , mapJsonSchema )
114
-
115
- return ( cachedSchema = {
116
- openapi : '3.0.3' ,
117
- ...documentation ,
118
- tags : ! exclude ?. tags
119
- ? documentation . tags
120
- : documentation . tags ?. filter (
121
- ( tag ) => ! exclude . tags ?. includes ( tag . name )
122
- ) ,
123
- info : {
124
- title : 'Elysia Documentation' ,
125
- description : 'Development documentation' ,
126
- version : '0.0.0' ,
127
- ...documentation . info
128
- } ,
129
- paths : {
130
- ...paths ,
131
- ...documentation . paths
132
- } ,
133
- components : {
134
- ...documentation . components ,
135
- schemas : {
136
- ...schemas ,
137
- ...( documentation . components ?. schemas as any )
138
- }
139
- }
140
- } satisfies OpenAPIV3 . Document )
165
+ return toFullSchema (
166
+ toOpenAPISchema ( app , exclude , references , mapJsonSchema )
167
+ )
141
168
} ,
142
169
{
143
170
error ( { error } ) {
0 commit comments