@@ -24,6 +24,10 @@ import { FORBIDDEN_MESSAGE } from '../guards/constants';
2424import { GuardsConsumer } from '../guards/guards-consumer' ;
2525import { GuardsContextCreator } from '../guards/guards-context-creator' ;
2626import { ContextUtils } from '../helpers/context-utils' ;
27+ import {
28+ HandlerMetadata ,
29+ HandlerMetadataStorage ,
30+ } from '../helpers/handler-metadata-storage' ;
2731import { STATIC_CONTEXT } from '../injector/constants' ;
2832import { InterceptorsConsumer } from '../interceptors/interceptors-consumer' ;
2933import { InterceptorsContextCreator } from '../interceptors/interceptors-context-creator' ;
@@ -48,6 +52,7 @@ export interface ParamProperties {
4852}
4953
5054export class RouterExecutionContext {
55+ private readonly handlerMetadataStorage = new HandlerMetadataStorage ( ) ;
5156 private readonly contextUtils = new ContextUtils ( ) ;
5257 private readonly responseController : RouterResponseController ;
5358
@@ -72,23 +77,18 @@ export class RouterExecutionContext {
7277 requestMethod : RequestMethod ,
7378 contextId = STATIC_CONTEXT ,
7479 ) {
75- const metadata =
76- this . contextUtils . reflectCallbackMetadata (
77- instance ,
78- methodName ,
79- ROUTE_ARGS_METADATA ,
80- ) || { } ;
81- const keys = Object . keys ( metadata ) ;
82- const argsLength = this . contextUtils . getArgumentsLength ( keys , metadata ) ;
83- const pipes = this . pipesContextCreator . create (
80+ const { argsLength, paramsOptions, fnHandleResponse } = this . getMetadata (
8481 instance ,
8582 callback ,
83+ methodName ,
8684 module ,
87- contextId ,
85+ requestMethod ,
8886 ) ;
89- const paramtypes = this . contextUtils . reflectCallbackParamtypes (
87+ const pipes = this . pipesContextCreator . create (
9088 instance ,
91- methodName ,
89+ callback ,
90+ module ,
91+ contextId ,
9292 ) ;
9393 const guards = this . guardsContextCreator . create (
9494 instance ,
@@ -102,27 +102,10 @@ export class RouterExecutionContext {
102102 module ,
103103 contextId ,
104104 ) ;
105- const httpCode = this . reflectHttpStatusCode ( callback ) ;
106- const paramsMetadata = this . exchangeKeysForValues ( keys , metadata , module ) ;
107- const isResponseHandled = paramsMetadata . some (
108- ( { type } ) =>
109- type === RouteParamtypes . RESPONSE || type === RouteParamtypes . NEXT ,
110- ) ;
111- const paramsOptions = this . contextUtils . mergeParamsMetatypes (
112- paramsMetadata ,
113- paramtypes ,
114- ) ;
115- const httpStatusCode = httpCode
116- ? httpCode
117- : this . responseController . getStatusByMethod ( requestMethod ) ;
118105
119106 const fnCanActivate = this . createGuardsFn ( guards , instance , callback ) ;
120107 const fnApplyPipes = this . createPipesFn ( pipes , paramsOptions ) ;
121- const fnHandleResponse = this . createHandleResponseFn (
122- callback ,
123- isResponseHandled ,
124- httpStatusCode ,
125- ) ;
108+
126109 const handler = < TRequest , TResponse > (
127110 args : any [ ] ,
128111 req : TRequest ,
@@ -152,6 +135,57 @@ export class RouterExecutionContext {
152135 } ;
153136 }
154137
138+ public getMetadata (
139+ instance : Controller ,
140+ callback : ( ...args : any [ ] ) => any ,
141+ methodName : string ,
142+ module : string ,
143+ requestMethod : RequestMethod ,
144+ ) : HandlerMetadata {
145+ const cacheMetadata = this . handlerMetadataStorage . get ( instance , methodName ) ;
146+ if ( cacheMetadata ) {
147+ return cacheMetadata ;
148+ }
149+ const metadata =
150+ this . contextUtils . reflectCallbackMetadata (
151+ instance ,
152+ methodName ,
153+ ROUTE_ARGS_METADATA ,
154+ ) || { } ;
155+ const keys = Object . keys ( metadata ) ;
156+ const argsLength = this . contextUtils . getArgumentsLength ( keys , metadata ) ;
157+ const paramtypes = this . contextUtils . reflectCallbackParamtypes (
158+ instance ,
159+ methodName ,
160+ ) ;
161+ const httpCode = this . reflectHttpStatusCode ( callback ) ;
162+ const paramsMetadata = this . exchangeKeysForValues ( keys , metadata , module ) ;
163+ const isResponseHandled = paramsMetadata . some (
164+ ( { type } ) =>
165+ type === RouteParamtypes . RESPONSE || type === RouteParamtypes . NEXT ,
166+ ) ;
167+ const paramsOptions = this . contextUtils . mergeParamsMetatypes (
168+ paramsMetadata ,
169+ paramtypes ,
170+ ) ;
171+ const httpStatusCode = httpCode
172+ ? httpCode
173+ : this . responseController . getStatusByMethod ( requestMethod ) ;
174+
175+ const fnHandleResponse = this . createHandleResponseFn (
176+ callback ,
177+ isResponseHandled ,
178+ httpStatusCode ,
179+ ) ;
180+ const handlerMetadata : HandlerMetadata = {
181+ argsLength,
182+ paramsOptions,
183+ fnHandleResponse,
184+ } ;
185+ this . handlerMetadataStorage . set ( instance , methodName , handlerMetadata ) ;
186+ return handlerMetadata ;
187+ }
188+
155189 public reflectHttpStatusCode ( callback : ( ...args : any [ ] ) => any ) : number {
156190 return Reflect . getMetadata ( HTTP_CODE_METADATA , callback ) ;
157191 }
0 commit comments