@@ -83,7 +83,11 @@ export class RouterExecutionContext {
8383 fnHandleResponse,
8484 paramtypes,
8585 getParamsMetadata,
86+ httpStatusCode,
87+ responseHeaders,
88+ hasCustomHeaders,
8689 } = this . getMetadata ( instance , callback , methodName , module , requestMethod ) ;
90+
8791 const paramsOptions = this . contextUtils . mergeParamsMetatypes (
8892 getParamsMetadata ( module , contextId , inquirerId ) ,
8993 paramtypes ,
@@ -131,6 +135,10 @@ export class RouterExecutionContext {
131135 const args = this . contextUtils . createNullArray ( argsLength ) ;
132136 fnCanActivate && ( await fnCanActivate ( [ req , res ] ) ) ;
133137
138+ this . responseController . setStatus ( res , httpStatusCode ) ;
139+ hasCustomHeaders &&
140+ this . responseController . setHeaders ( res , responseHeaders ) ;
141+
134142 const result = await this . interceptorsConsumer . intercept (
135143 interceptors ,
136144 [ req , res ] ,
@@ -165,7 +173,6 @@ export class RouterExecutionContext {
165173 instance ,
166174 methodName ,
167175 ) ;
168- const httpCode = this . reflectHttpStatusCode ( callback ) ;
169176 const getParamsMetadata = (
170177 moduleKey : string ,
171178 contextId = STATIC_CONTEXT ,
@@ -184,20 +191,28 @@ export class RouterExecutionContext {
184191 ( { type } ) =>
185192 type === RouteParamtypes . RESPONSE || type === RouteParamtypes . NEXT ,
186193 ) ;
187- const httpStatusCode = httpCode
188- ? httpCode
189- : this . responseController . getStatusByMethod ( requestMethod ) ;
190194
191195 const fnHandleResponse = this . createHandleResponseFn (
192196 callback ,
193197 isResponseHandled ,
194- httpStatusCode ,
195198 ) ;
199+
200+ const httpCode = this . reflectHttpStatusCode ( callback ) ;
201+ const httpStatusCode = httpCode
202+ ? httpCode
203+ : this . responseController . getStatusByMethod ( requestMethod ) ;
204+
205+ const responseHeaders = this . reflectResponseHeaders ( callback ) ;
206+ const hasCustomHeaders = ! isEmpty ( responseHeaders ) ;
207+
196208 const handlerMetadata : HandlerMetadata = {
197209 argsLength,
198210 fnHandleResponse,
199211 paramtypes,
200212 getParamsMetadata,
213+ httpStatusCode,
214+ hasCustomHeaders,
215+ responseHeaders,
201216 } ;
202217 this . handlerMetadataStorage . set ( instance , methodName , handlerMetadata ) ;
203218 return handlerMetadata ;
@@ -342,23 +357,16 @@ export class RouterExecutionContext {
342357 public createHandleResponseFn (
343358 callback : ( ...args : any [ ] ) => any ,
344359 isResponseHandled : boolean ,
345- httpStatusCode : number ,
360+ httpStatusCode ? : number ,
346361 ) {
347362 const renderTemplate = this . reflectRenderTemplate ( callback ) ;
348- const responseHeaders = this . reflectResponseHeaders ( callback ) ;
349- const hasCustomHeaders = ! isEmpty ( responseHeaders ) ;
350363
351364 if ( renderTemplate ) {
352365 return async < TResult , TResponse > ( result : TResult , res : TResponse ) => {
353- hasCustomHeaders &&
354- this . responseController . setHeaders ( res , responseHeaders ) ;
355366 await this . responseController . render ( result , res , renderTemplate ) ;
356367 } ;
357368 }
358369 return async < TResult , TResponse > ( result : TResult , res : TResponse ) => {
359- hasCustomHeaders &&
360- this . responseController . setHeaders ( res , responseHeaders ) ;
361-
362370 result = await this . responseController . transformToResult ( result ) ;
363371 ! isResponseHandled &&
364372 ( await this . responseController . apply ( result , res , httpStatusCode ) ) ;
0 commit comments