11import { ForbiddenException , ParamData } from '@nestjs/common' ;
22import { CUSTOM_ROUTE_AGRS_METADATA } from '@nestjs/common/constants' ;
3- import { Controller , PipeTransform } from '@nestjs/common/interfaces' ;
3+ import {
4+ ContextType ,
5+ Controller ,
6+ PipeTransform ,
7+ } from '@nestjs/common/interfaces' ;
48import { isEmpty , isFunction } from '@nestjs/common/utils/shared.utils' ;
9+ import { ExternalExceptionFilterContext } from '../exceptions/external-exception-filter-context' ;
510import { FORBIDDEN_MESSAGE } from '../guards/constants' ;
611import { GuardsConsumer } from '../guards/guards-consumer' ;
712import { GuardsContextCreator } from '../guards/guards-context-creator' ;
13+ import { STATIC_CONTEXT } from '../injector/constants' ;
814import { NestContainer } from '../injector/container' ;
915import { Module } from '../injector/module' ;
1016import { ModulesContainer } from '../injector/modules-container' ;
1117import { InterceptorsConsumer } from '../interceptors/interceptors-consumer' ;
1218import { InterceptorsContextCreator } from '../interceptors/interceptors-context-creator' ;
1319import { PipesConsumer } from '../pipes/pipes-consumer' ;
1420import { PipesContextCreator } from '../pipes/pipes-context-creator' ;
15- import { ExternalExceptionFilterContext } from '../exceptions/external-exception-filter-context' ;
16- import { STATIC_CONTEXT } from '../injector/constants' ;
1721import { ContextUtils , ParamProperties } from './context-utils' ;
1822import { ExternalErrorProxy } from './external-proxy' ;
1923import { HandlerMetadataStorage } from './handler-metadata-storage' ;
@@ -80,7 +84,10 @@ export class ExternalContextCreator {
8084 ) ;
8185 }
8286
83- public create < T extends ParamsMetadata = ParamsMetadata > (
87+ public create <
88+ TParamsMetadata extends ParamsMetadata = ParamsMetadata ,
89+ TContext extends ContextType = ContextType
90+ > (
8491 instance : Controller ,
8592 callback : ( ...args : any [ ] ) => any ,
8693 methodName : string ,
@@ -93,14 +100,12 @@ export class ExternalContextCreator {
93100 guards : true ,
94101 filters : true ,
95102 } ,
103+ contextType : TContext = 'http' as TContext ,
96104 ) {
97105 const module = this . getContextModuleName ( instance . constructor ) ;
98- const { argsLength, paramtypes, getParamsMetadata } = this . getMetadata < T > (
99- instance ,
100- methodName ,
101- metadataKey ,
102- paramsFactory ,
103- ) ;
106+ const { argsLength, paramtypes, getParamsMetadata } = this . getMetadata <
107+ TParamsMetadata
108+ > ( instance , methodName , metadataKey , paramsFactory ) ;
104109 const pipes = this . pipesContextCreator . create (
105110 instance ,
106111 callback ,
@@ -138,7 +143,7 @@ export class ExternalContextCreator {
138143 : [ ] ;
139144
140145 const fnCanActivate = options . guards
141- ? this . createGuardsFn ( guards , instance , callback )
146+ ? this . createGuardsFn ( guards , instance , callback , contextType )
142147 : null ;
143148 const fnApplyPipes = this . createPipesFn ( pipes , paramsOptions ) ;
144149 const handler = ( initialArgs : any [ ] , ...args : any [ ] ) => async ( ) => {
@@ -159,6 +164,7 @@ export class ExternalContextCreator {
159164 instance ,
160165 callback ,
161166 handler ( initialArgs , ...args ) ,
167+ contextType ,
162168 ) ;
163169 return this . transformToResult ( result ) ;
164170 } ;
@@ -316,17 +322,19 @@ export class ExternalContextCreator {
316322 return resultOrDeffered ;
317323 }
318324
319- public createGuardsFn (
325+ public createGuardsFn < TContext extends ContextType = ContextType > (
320326 guards : any [ ] ,
321327 instance : Controller ,
322328 callback : ( ...args : any [ ] ) => any ,
329+ contextType ?: TContext ,
323330 ) : Function | null {
324331 const canActivateFn = async ( args : any [ ] ) => {
325- const canActivate = await this . guardsConsumer . tryActivate (
332+ const canActivate = await this . guardsConsumer . tryActivate < TContext > (
326333 guards ,
327334 args ,
328335 instance ,
329336 callback ,
337+ contextType ,
330338 ) ;
331339 if ( ! canActivate ) {
332340 throw new ForbiddenException ( FORBIDDEN_MESSAGE ) ;
0 commit comments