@@ -75,40 +75,22 @@ export class RouterExecutionContext {
7575 type === RouteParamtypes . RESPONSE || type === RouteParamtypes . NEXT ,
7676 ) ;
7777 const paramsOptions = this . mergeParamsMetatypes ( paramsMetadata , paramtypes ) ;
78+ const httpStatusCode = httpCode
79+ ? httpCode
80+ : this . responseController . getStatusByMethod ( requestMethod ) ;
81+
82+ const fnCanActivate = this . createGuardsFn ( guards , instance , callback ) ;
83+ const fnApplyPipes = this . createPipesFn ( pipes , paramsOptions ) ;
84+ const fnHandleResponse = this . createHandleResponseFn (
85+ isResponseHandled ,
86+ httpStatusCode ,
87+ ) ;
7888
7989 return async ( req , res , next ) => {
8090 const args = this . createNullArray ( argsLength ) ;
81- const canActivate = await this . guardsConsumer . tryActivate (
82- guards ,
83- req ,
84- instance ,
85- callback ,
86- ) ;
87- if ( ! canActivate ) {
88- throw new HttpException ( FORBIDDEN_MESSAGE , HttpStatus . FORBIDDEN ) ;
89- }
90-
91- await Promise . all (
92- paramsOptions . map ( async param => {
93- const {
94- index,
95- extractValue,
96- type,
97- data,
98- metatype,
99- pipes : paramPipes ,
100- } = param ;
101- const value = extractValue ( req , res , next ) ;
91+ await fnCanActivate ( req ) ;
92+ await fnApplyPipes ( args , req , res , next ) ;
10293
103- args [ index ] = await this . getParamValue (
104- value ,
105- { metatype, type, data } ,
106- pipes . concat (
107- this . pipesContextCreator . createConcreteContext ( paramPipes ) ,
108- ) ,
109- ) ;
110- } ) ,
111- ) ;
11294 const handler = ( ) => callback . apply ( instance , args ) ;
11395 const result = await this . interceptorsConsumer . intercept (
11496 interceptors ,
@@ -117,9 +99,7 @@ export class RouterExecutionContext {
11799 callback ,
118100 handler ,
119101 ) ;
120- return ! isResponseHandled
121- ? this . responseController . apply ( result , res , requestMethod , httpCode )
122- : undefined ;
102+ fnHandleResponse ( result , res ) ;
123103 } ;
124104 }
125105
@@ -215,4 +195,63 @@ export class RouterExecutionContext {
215195 }
216196 return Promise . resolve ( value ) ;
217197 }
198+
199+ public createGuardsFn (
200+ guards : any [ ] ,
201+ instance : Controller ,
202+ callback : ( ...args ) => any ,
203+ ) {
204+ const canActivateFn = async req => {
205+ const canActivate = await this . guardsConsumer . tryActivate (
206+ guards ,
207+ req ,
208+ instance ,
209+ callback ,
210+ ) ;
211+ if ( ! canActivate ) {
212+ throw new HttpException ( FORBIDDEN_MESSAGE , HttpStatus . FORBIDDEN ) ;
213+ }
214+ } ;
215+ return guards . length ? canActivateFn : async req => undefined ;
216+ }
217+
218+ public createPipesFn (
219+ pipes : any [ ] ,
220+ paramsOptions : ( ParamProperties & { metatype ?: any } ) [ ] ,
221+ ) {
222+ const pipesFn = async ( args , req , res , next ) => {
223+ await Promise . all (
224+ paramsOptions . map ( async param => {
225+ const {
226+ index,
227+ extractValue,
228+ type,
229+ data,
230+ metatype,
231+ pipes : paramPipes ,
232+ } = param ;
233+ const value = extractValue ( req , res , next ) ;
234+
235+ args [ index ] = await this . getParamValue (
236+ value ,
237+ { metatype, type, data } ,
238+ pipes . concat (
239+ this . pipesContextCreator . createConcreteContext ( paramPipes ) ,
240+ ) ,
241+ ) ;
242+ } ) ,
243+ ) ;
244+ } ;
245+ return paramsOptions . length ? pipesFn : async ( ...args ) => undefined ;
246+ }
247+
248+ public createHandleResponseFn (
249+ isResponseHandled : boolean ,
250+ httpStatusCode : number ,
251+ ) {
252+ return ! isResponseHandled
253+ ? ( result , res ) =>
254+ this . responseController . apply ( result , res , httpStatusCode )
255+ : ( ...args ) => undefined ;
256+ }
218257}
0 commit comments