@@ -156,6 +156,7 @@ type HandlerMethod<
156
156
input : z . output <
157
157
ExtractSchemaFromSchemaDef < NonNullable < InputSchema > >
158
158
> ;
159
+ rawInput : unknown ;
159
160
} ) & {
160
161
response : <
161
162
Status extends OutputMapKey < OutputSchema > ,
@@ -444,12 +445,13 @@ export function endpoint<
444
445
}
445
446
} ;
446
447
447
- // invoke method
448
- ( cls . prototype as any ) . invoke = async function ( rawInput : any ) {
449
- const handlerParams : Record < string | symbol , any > = {
450
- response,
451
- schemas : { } ,
452
- } ;
448
+ const response = ( s : number , b : any ) => new EndpointResponse ( s , b ) ;
449
+
450
+ const commonHandlerLogic = async function (
451
+ this : any ,
452
+ handlerParams : Record < string | symbol , any > ,
453
+ rawInput : any ,
454
+ ) {
453
455
if ( inject ) {
454
456
for ( const key of Object . keys ( inject ) ) {
455
457
handlerParams [ key ] = this [ key ] ;
@@ -463,10 +465,25 @@ export function endpoint<
463
465
throw new ZodValidationException ( parsed . error ) ;
464
466
}
465
467
handlerParams . input = parsed . data ;
468
+ handlerParams . rawInput = rawInput ;
466
469
handlerParams . schemas . input = schema ;
467
470
}
468
471
// eslint-disable-next-line @typescript-eslint/await-thenable
469
472
const result : any = await handler ( handlerParams as any ) ;
473
+ return result ;
474
+ } ;
475
+
476
+ // invoke method
477
+ ( cls . prototype as any ) . invoke = async function ( rawInput : any ) {
478
+ const handlerParams : Record < string | symbol , any > = {
479
+ response,
480
+ schemas : { } ,
481
+ } ;
482
+ const result = await commonHandlerLogic . call (
483
+ this ,
484
+ handlerParams ,
485
+ rawInput ,
486
+ ) ;
470
487
if ( result instanceof EndpointResponse ) {
471
488
validateOutput ( result ) ;
472
489
return result ;
@@ -478,7 +495,6 @@ export function endpoint<
478
495
} ;
479
496
480
497
// handler method
481
- const response = ( s : number , b : any ) => new EndpointResponse ( s , b ) ;
482
498
( cls . prototype as any ) . handler = async function ( ...params : any [ ] ) {
483
499
const injectedMethodParams : Record < string | symbol , any > =
484
500
Object . fromEntries (
@@ -491,28 +507,17 @@ export function endpoint<
491
507
response,
492
508
schemas : { } ,
493
509
} ;
494
- if ( inject ) {
495
- for ( const key of Object . keys ( inject ) ) {
496
- handlerParams [ key ] = this [ key ] ;
497
- }
498
- }
499
510
if ( injectMethod ) {
500
511
for ( const key of Object . keys ( injectMethod ) ) {
501
512
handlerParams [ key ] = injectedMethodParams [ key ] ;
502
513
}
503
514
}
504
- if ( input ) {
505
- const schema : ZodSchema =
506
- input instanceof SchemaDef ? input . schema : input ;
507
- const parsed = schema . safeParse ( injectedMethodParams [ inputKey ] ) ;
508
- if ( parsed . error ) {
509
- throw new ZodValidationException ( parsed . error ) ;
510
- }
511
- handlerParams . input = parsed . data ;
512
- handlerParams . schemas . input = schema ;
513
- }
514
- // eslint-disable-next-line @typescript-eslint/await-thenable
515
- const result : any = await handler ( handlerParams as any ) ;
515
+ const rawInput = injectedMethodParams [ inputKey ] ;
516
+ const result = await commonHandlerLogic . call (
517
+ this ,
518
+ handlerParams ,
519
+ rawInput ,
520
+ ) ;
516
521
let endpointResponse : EndpointResponse ;
517
522
if ( result instanceof EndpointResponse ) {
518
523
endpointResponse = result ;
0 commit comments