@@ -41,6 +41,7 @@ const routeScore = (route: RouteRegisterInformation) =>
4141export interface RouteRegisterInformation {
4242 route : GiuseppeRoute ;
4343 ctrl : Function ;
44+ instance : object ;
4445 segments : number ;
4546 wildcards : number ;
4647 urlParams : number ;
@@ -98,11 +99,11 @@ export class Giuseppe {
9899 protected routes : { [ id : string ] : RouteRegisterInformation } = { } ;
99100 protected _server : Server | undefined ;
100101
101- protected _returnTypes : ReturnType < any > [ ] | null ;
102- protected _pluginController : ControllerDefinitionConstructor [ ] | null ;
103- protected _pluginRoutes : RouteDefinitionConstructor [ ] | null ;
104- protected _pluginRouteModificators : RouteModificatorConstructor [ ] | null ;
105- protected _pluginParameters : ParameterDefinitionConstructor [ ] | null ;
102+ protected _returnTypes : ReturnType < any > [ ] | null = null ;
103+ protected _pluginController : ControllerDefinitionConstructor [ ] | null = null ;
104+ protected _pluginRoutes : RouteDefinitionConstructor [ ] | null = null ;
105+ protected _pluginRouteModificators : RouteModificatorConstructor [ ] | null = null ;
106+ protected _pluginParameters : ParameterDefinitionConstructor [ ] | null = null ;
106107
107108 /**
108109 * List of registered {@link ReturnType}.
@@ -344,6 +345,8 @@ export class Giuseppe {
344345 ctrlRoutes = ctrlRoutes . concat ( modifiedRoutes ) ;
345346 }
346347
348+ const ctrlInstance = new ( ctrl . ctrlTarget as { new ( ...args : any [ ] ) : any ; } ) ( ) ;
349+
347350 for ( const route of ctrlRoutes ) {
348351 if ( this . routes [ route . id ] ) {
349352 throw new DuplicateRouteError ( route ) ;
@@ -354,6 +357,7 @@ export class Giuseppe {
354357 wildcards : route . url . split ( '*' ) . length - 1 ,
355358 urlParams : route . url . split ( '/' ) . filter ( s => s . indexOf ( ':' ) >= 0 ) . length ,
356359 ctrl : ctrl . ctrlTarget ,
360+ instance : ctrlInstance ,
357361 } ;
358362 }
359363 }
@@ -363,7 +367,7 @@ export class Giuseppe {
363367 Object . keys ( this . routes )
364368 . map ( k => this . routes [ k ] )
365369 . sort ( ( a , b ) => routeScore ( b ) - routeScore ( a ) )
366- . forEach ( r => this . router [ HttpMethod [ r . route . method ] ] (
370+ . forEach ( r => ( this . router as any ) [ HttpMethod [ r . route . method ] ] (
367371 `/${ r . route . url } ` ,
368372 ...r . route . middlewares ,
369373 this . createRouteWrapper ( r ) ,
@@ -385,7 +389,6 @@ export class Giuseppe {
385389 const meta = new ControllerMetadata ( routeInfo . ctrl . prototype ) ;
386390 const params = meta . parameters ( routeInfo . route . name ) ;
387391 const returnTypeHandler = new ReturnTypeHandler ( this . returnTypes ) ;
388- const ctrlInstance = new ( routeInfo as any ) . ctrl ( ) ;
389392
390393 return async ( req : express . Request , res : express . Response ) => {
391394 const paramValues : any [ ] = [ ] ;
@@ -395,7 +398,7 @@ export class Giuseppe {
395398 paramValues [ param . index ] = param . getValue ( req , res ) ;
396399 }
397400
398- let result = routeInfo . route . function . apply ( ctrlInstance , paramValues ) ;
401+ let result = routeInfo . route . function . apply ( routeInfo . instance , paramValues ) ;
399402
400403 if ( result instanceof Promise ) {
401404 result = await result ;
@@ -407,7 +410,7 @@ export class Giuseppe {
407410
408411 returnTypeHandler . handleValue ( result , res ) ;
409412 } catch ( e ) {
410- meta . errorHandler ( ) . handleError ( ctrlInstance , req , res , e ) ;
413+ meta . errorHandler ( ) . handleError ( routeInfo . instance , req , res , e ) ;
411414 }
412415 } ;
413416 }
0 commit comments