@@ -76,7 +76,7 @@ export class OpenAPI {
7676
7777 public route ( ) {
7878 const recordOpenapi = container . resolve ( RecordOpenApi )
79- return new Elysia ( { prefix : "/api/bases/:baseName/tables/:tableName" } )
79+ return new Elysia ( )
8080 . onAfterResponse ( ( ctx ) => {
8181 const requestId = executionContext . getStore ( ) ?. requestId
8282 this . logger . info (
@@ -91,7 +91,7 @@ export class OpenAPI {
9191 )
9292 } )
9393 . get (
94- "/" ,
94+ "/api/bases/:baseName/tables/:tableName " ,
9595 async ( ctx ) => {
9696 const spec = await this . getSpec ( ctx . params . baseName , ctx . params . tableName )
9797
@@ -128,52 +128,56 @@ export class OpenAPI {
128128 } ,
129129 } ,
130130 )
131- . guard ( {
132- beforeHandle : async ( context ) => {
133- const apiToken =
134- context . headers [ API_TOKEN_HEADER_NAME ] ?? context . headers [ API_TOKEN_HEADER_NAME . toLowerCase ( ) ]
135-
136- this . logger . debug ( { apiToken } , "Checking Authorization token in openapi" )
137-
138- if ( apiToken ) {
139- const userId = await this . apiTokenService . verify ( apiToken )
140- if ( userId . isSome ( ) ) {
141- const user = ( await this . userService . findOneById ( userId . unwrap ( ) ) ) . unwrap ( )
142- const space = await this . spaceService . setSpaceContext ( setContextValue , { apiToken } )
143- await this . spaceMemberService . setSpaceMemberContext ( setContextValue , space . id . value , user . id )
144-
145- return
146- }
147- } else {
148- const userId = getCurrentUserId ( )
149-
150- if ( userId ) {
151- return
152- } else {
153- this . logger . error ( "No api token found in openapi" )
154- }
155- }
156-
157- // throw 401 openapi error
158- context . set . status = 401
159- throw new Error ( "Unauthorized" )
160- } ,
161- } )
162- . get (
163- "/openapi.json" ,
164- async ( ctx ) => {
165- const spec = await this . getSpec ( ctx . params . baseName , ctx . params . tableName )
166- return spec
167- } ,
168- {
169- params : t . Object ( { baseName : t . String ( ) , tableName : t . String ( ) } ) ,
170- detail : {
171- tags : [ "Doc" ] ,
172- summary : "Get OpenAPI documentation json spec for a table" ,
173- description : "Get OpenAPI documentation json spec for a table" ,
174- } ,
175- } ,
131+ . group ( "/openapi/bases/:baseName/tables/:tableName" , ( app ) =>
132+ app
133+
134+ . guard ( {
135+ beforeHandle : async ( context ) => {
136+ const apiToken =
137+ context . headers [ API_TOKEN_HEADER_NAME ] ?? context . headers [ API_TOKEN_HEADER_NAME . toLowerCase ( ) ]
138+
139+ this . logger . debug ( { apiToken } , "Checking Authorization token in openapi" )
140+
141+ if ( apiToken ) {
142+ const userId = await this . apiTokenService . verify ( apiToken )
143+ if ( userId . isSome ( ) ) {
144+ const user = ( await this . userService . findOneById ( userId . unwrap ( ) ) ) . unwrap ( )
145+ const space = await this . spaceService . setSpaceContext ( setContextValue , { apiToken } )
146+ await this . spaceMemberService . setSpaceMemberContext ( setContextValue , space . id . value , user . id )
147+
148+ return
149+ }
150+ } else {
151+ const userId = getCurrentUserId ( )
152+
153+ if ( userId ) {
154+ return
155+ } else {
156+ this . logger . error ( "No api token found in openapi" )
157+ }
158+ }
159+
160+ // throw 401 openapi error
161+ context . set . status = 401
162+ throw new Error ( "Unauthorized" )
163+ } ,
164+ } )
165+ . get (
166+ "/openapi.json" ,
167+ async ( ctx ) => {
168+ const spec = await this . getSpec ( ctx . params . baseName , ctx . params . tableName )
169+ return spec
170+ } ,
171+ {
172+ params : t . Object ( { baseName : t . String ( ) , tableName : t . String ( ) } ) ,
173+ detail : {
174+ tags : [ "Doc" ] ,
175+ summary : "Get OpenAPI documentation json spec for a table" ,
176+ description : "Get OpenAPI documentation json spec for a table" ,
177+ } ,
178+ } ,
179+ )
180+ . use ( recordOpenapi . route ( ) ) ,
176181 )
177- . use ( recordOpenapi . route ( ) )
178182 }
179183}
0 commit comments