@@ -50,7 +50,8 @@ export const openapi = <
5050 swagger,
5151 scalar,
5252 references,
53- mapJsonSchema
53+ mapJsonSchema,
54+ embedSpec
5455} : ElysiaOpenAPIConfig < Enabled , Path > = { } ) => {
5556 if ( ! enabled ) return new Elysia ( { name : '@elysiajs/openapi' } )
5657
@@ -66,6 +67,38 @@ export const openapi = <
6667 let totalRoutes = 0
6768 let cachedSchema : OpenAPIV3 . Document | undefined
6869
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+
69102 const app = new Elysia ( { name : '@elysiajs/openapi' } )
70103 . use ( ( app ) => {
71104 if ( provider === null ) return app
@@ -80,64 +113,58 @@ export const openapi = <
80113 autoDarkMode : true ,
81114 ...swagger
82115 } )
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+ ) ,
90140 {
91141 headers : {
92142 'content-type' : 'text/html; charset=utf8'
93143 }
94144 }
95145 )
96146
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+ }
100154 }
101- } )
155+ )
102156 } )
103157 . get (
104158 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
107162
108163 totalRoutes = app . routes . length
109164
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+ )
141168 } ,
142169 {
143170 error ( { error } ) {
0 commit comments