@@ -74,65 +74,62 @@ export async function openApi<
7474 delete ( global as { window : unknown } ) . window ;
7575 }
7676
77- app . locals . openApiSpecification = await new OpenAPIFramework ( { apiDoc : apiSpec } )
78- . initialize ( { visitApi ( ) { } } )
79- . then ( ( docs ) => docs . apiDoc )
80- . catch ( ( error ) => {
81- app . locals . logger . error ( error , 'Failed to parse and load OpenAPI spec' ) ;
82- } ) ;
83-
84- if ( _window ) {
85- ( global as { window : unknown } ) . window = _window ;
86- } ;
77+ try {
78+ app . locals . openApiSpecification = await new OpenAPIFramework ( { apiDoc : apiSpec } )
79+ . initialize ( { visitApi ( ) { } } )
80+ . then ( ( docs ) => docs . apiDoc )
81+ . catch ( ( error ) => {
82+ app . locals . logger . error ( error , 'Failed to parse and load OpenAPI spec' ) ;
83+ } ) ;
8784
88- const defaultOptions : OAPIOpts = {
89- apiSpec : app . locals . openApiSpecification ,
90- ignoreUndocumented : true ,
91- validateRequests : {
92- allowUnknownQueryParameters : true ,
93- coerceTypes : 'array' ,
94- } ,
95- operationHandlers : {
96- basePath,
97- resolver (
98- basePath : string ,
99- route : Parameters < typeof OpenApiValidator . resolvers . defaultResolver > [ 1 ] ,
100- ) {
101- const pathKey = route . openApiRoute . substring ( route . basePath . length ) ;
102- const modulePath = path . join ( basePath , pathKey ) ;
85+ const defaultOptions : OAPIOpts = {
86+ apiSpec : app . locals . openApiSpecification ,
87+ ignoreUndocumented : true ,
88+ validateRequests : {
89+ allowUnknownQueryParameters : true ,
90+ coerceTypes : 'array' ,
91+ } ,
92+ operationHandlers : {
93+ basePath,
94+ resolver (
95+ basePath : string ,
96+ route : Parameters < typeof OpenApiValidator . resolvers . defaultResolver > [ 1 ] ,
97+ ) {
98+ const pathKey = route . openApiRoute . substring ( route . basePath . length ) ;
99+ const modulePath = path . join ( basePath , pathKey ) ;
103100
104- try {
105- const module = modulesByPath [ pathKey ] ;
106- const method = module
107- ? Object . keys ( module ) . find ( ( m ) => m . toUpperCase ( ) === route . method )
108- : undefined ;
109- if ( ! module || ! method ) {
110- throw new Error (
111- `Could not find a [${ route . method } ] function in ${ modulePath } when trying to route [${ route . method } ${ route . expressRoute } ].` ,
101+ try {
102+ const module = modulesByPath [ pathKey ] ;
103+ const method = module
104+ ? Object . keys ( module ) . find ( ( m ) => m . toUpperCase ( ) === route . method )
105+ : undefined ;
106+ if ( ! module || ! method ) {
107+ throw new Error (
108+ `Could not find a [${ route . method } ] function in ${ modulePath } when trying to route [${ route . method } ${ route . expressRoute } ].` ,
109+ ) ;
110+ }
111+ return module [ method ] as RequestHandler ;
112+ } catch ( error ) {
113+ app . locals . logger . error (
114+ {
115+ error : ( error as Error ) . message ,
116+ pathKey,
117+ modulePath : path . relative ( rootDirectory , modulePath ) ,
118+ } ,
119+ 'Failed to load API method handler' ,
112120 ) ;
121+ return notImplementedHandler ;
113122 }
114- return module [ method ] as RequestHandler ;
115- } catch ( error ) {
116- app . locals . logger . error (
117- {
118- error : ( error as Error ) . message ,
119- pathKey,
120- modulePath : path . relative ( rootDirectory , modulePath ) ,
121- } ,
122- 'Failed to load API method handler' ,
123- ) ;
124- return notImplementedHandler ;
125- }
123+ } ,
126124 } ,
127- } ,
128- } ;
125+ } ;
129126
130- const { routing } = app . locals . config ;
131- const combinedOptions = {
132- // In test mode, validate returned swagger responses. This can easily be disabled
133- // by setting validateResponses to false in the config.
134- ...( getNodeEnv ( ) === 'test'
135- ? {
127+ const { routing } = app . locals . config ;
128+ const combinedOptions = {
129+ // In test mode, validate returned swagger responses. This can easily be disabled
130+ // by setting validateResponses to false in the config.
131+ ...( getNodeEnv ( ) === 'test'
132+ ? {
136133 validateResponses : {
137134 onError ( error : Error , body : unknown , req : Request ) {
138135 console . log ( 'Response body fails validation: ' , error ) ;
@@ -142,10 +139,15 @@ export async function openApi<
142139 } ,
143140 } ,
144141 }
145- : { } ) ,
146- ...( typeof routing . openapi === 'object' ? routing . openapi : { } ) ,
147- ...openApiOptions ,
148- } ;
142+ : { } ) ,
143+ ...( typeof routing . openapi === 'object' ? routing . openapi : { } ) ,
144+ ...openApiOptions ,
145+ } ;
149146
150- return OpenApiValidator . middleware ( _ . defaultsDeep ( defaultOptions , combinedOptions ) ) ;
147+ return OpenApiValidator . middleware ( _ . defaultsDeep ( defaultOptions , combinedOptions ) ) ;
148+ } finally {
149+ if ( _window ) {
150+ ( global as { window : unknown } ) . window = _window ;
151+ } ;
152+ }
151153}
0 commit comments