@@ -139,6 +139,10 @@ export class OpenAPIBackend<D extends Document = Document> {
139
139
'400' ,
140
140
'validationFail' ,
141
141
'unauthorizedHandler' ,
142
+ 'preRoutingHandler' ,
143
+ 'postRoutingHandler' ,
144
+ 'postSecurityHandler' ,
145
+ 'preOperationHandler' ,
142
146
'postResponseHandler' ,
143
147
] ;
144
148
@@ -310,10 +314,22 @@ export class OpenAPIBackend<D extends Document = Document> {
310
314
// parse request
311
315
context . request = this . router . parseRequest ( req ) ;
312
316
317
+ // preRoutingHandler
318
+ const preRoutingHandler = this . handlers [ 'preRoutingHandler' ] ;
319
+ if ( preRoutingHandler ) {
320
+ await preRoutingHandler ( context as Context < D > , ...handlerArgs ) ;
321
+ }
322
+
313
323
// match operation (routing)
314
324
try {
315
325
context . operation = this . router . matchOperation ( req , true ) ;
316
326
} catch ( err ) {
327
+ // postRoutingHandler on routing failure
328
+ const postRoutingHandler = this . handlers [ 'postRoutingHandler' ] ;
329
+ if ( postRoutingHandler ) {
330
+ await postRoutingHandler ( context as Context < D > , ...handlerArgs ) ;
331
+ }
332
+
317
333
let handler = this . handlers [ '404' ] || this . handlers [ 'notFound' ] ;
318
334
if ( err instanceof Error && err . message . startsWith ( '405' ) ) {
319
335
// 405 method not allowed
@@ -330,6 +346,12 @@ export class OpenAPIBackend<D extends Document = Document> {
330
346
// parse request again now with matched operation
331
347
context . request = this . router . parseRequest ( req , context . operation ) ;
332
348
349
+ // postRoutingHandler on routing success
350
+ const postRoutingHandler = this . handlers [ 'postRoutingHandler' ] ;
351
+ if ( postRoutingHandler ) {
352
+ await postRoutingHandler ( context as Context < D > , ...handlerArgs ) ;
353
+ }
354
+
333
355
// get security requirements for the matched operation
334
356
// global requirements are already included in the router
335
357
const securityRequirements = context . operation . security || [ ] ;
@@ -398,6 +420,12 @@ export class OpenAPIBackend<D extends Document = Document> {
398
420
...securityHandlerResults ,
399
421
} ;
400
422
423
+ // postSecurityHandler
424
+ const postSecurityHandler = this . handlers [ 'postSecurityHandler' ] ;
425
+ if ( postSecurityHandler ) {
426
+ await postSecurityHandler ( context as Context < D > , ...handlerArgs ) ;
427
+ }
428
+
401
429
// call unauthorizedHandler handler if auth fails
402
430
if ( ! authorized && securityRequirements . length > 0 ) {
403
431
const unauthorizedHandler = this . handlers [ 'unauthorizedHandler' ] ;
@@ -430,9 +458,15 @@ export class OpenAPIBackend<D extends Document = Document> {
430
458
}
431
459
}
432
460
461
+ // preOperationHandler – runs just before the operation handler
462
+ const preOperationHandler = this . handlers [ 'preOperationHandler' ] ;
463
+ if ( preOperationHandler ) {
464
+ await preOperationHandler ( context as Context < D > , ...handlerArgs ) ;
465
+ }
466
+
433
467
// get operation handler
434
- const routeHandler = this . handlers [ operationId ] ;
435
- if ( ! routeHandler ) {
468
+ const operationHandler = this . handlers [ operationId ] ;
469
+ if ( ! operationHandler ) {
436
470
// 501 not implemented
437
471
const notImplementedHandler = this . handlers [ '501' ] || this . handlers [ 'notImplemented' ] ;
438
472
if ( ! notImplementedHandler ) {
@@ -442,7 +476,7 @@ export class OpenAPIBackend<D extends Document = Document> {
442
476
}
443
477
444
478
// handle route
445
- return routeHandler ( context as Context < D > , ...handlerArgs ) ;
479
+ return operationHandler ( context as Context < D > , ...handlerArgs ) ;
446
480
} ) . bind ( this ) ( ) ;
447
481
448
482
// post response handler
0 commit comments