@@ -40,6 +40,12 @@ export interface ExternalHandlerMetadata {
4040 ) => ( ParamProperties & { metatype ?: any } ) [ ] ;
4141}
4242
43+ export interface ExternalContextOptions {
44+ guards ?: boolean ;
45+ interceptors ?: boolean ;
46+ filters ?: boolean ;
47+ }
48+
4349export class ExternalContextCreator {
4450 private readonly contextUtils = new ContextUtils ( ) ;
4551 private readonly externalErrorProxy = new ExternalErrorProxy ( ) ;
@@ -98,6 +104,11 @@ export class ExternalContextCreator {
98104 paramsFactory ?: ParamsFactory ,
99105 contextId = STATIC_CONTEXT ,
100106 inquirerId ?: string ,
107+ options : ExternalContextOptions = {
108+ interceptors : true ,
109+ guards : true ,
110+ filters : true ,
111+ } ,
101112 ) {
102113 const module = this . findContextModuleName ( instance . constructor ) ;
103114 const { argsLength, paramtypes, getParamsMetadata } = this . getMetadata < T > (
@@ -106,42 +117,45 @@ export class ExternalContextCreator {
106117 metadataKey ,
107118 paramsFactory ,
108119 ) ;
109-
110120 const pipes = this . pipesContextCreator . create (
111121 instance ,
112122 callback ,
113123 module ,
114124 contextId ,
115125 inquirerId ,
116126 ) ;
117-
118127 const guards = this . guardsContextCreator . create (
119128 instance ,
120129 callback ,
121130 module ,
122131 contextId ,
123132 inquirerId ,
124133 ) ;
125- const interceptors = this . interceptorsContextCreator . create (
126- instance ,
127- callback ,
128- module ,
129- contextId ,
130- inquirerId ,
131- ) ;
132134 const exceptionFilter = this . filtersContextCreator . create (
133135 instance ,
134136 callback ,
135137 module ,
136138 contextId ,
137139 inquirerId ,
138140 ) ;
141+ const interceptors = options . interceptors
142+ ? this . interceptorsContextCreator . create (
143+ instance ,
144+ callback ,
145+ module ,
146+ contextId ,
147+ inquirerId ,
148+ )
149+ : [ ] ;
139150
140151 const paramsMetadata = getParamsMetadata ( module , contextId , inquirerId ) ;
141152 const paramsOptions = paramsMetadata
142153 ? this . contextUtils . mergeParamsMetatypes ( paramsMetadata , paramtypes )
143154 : [ ] ;
144155
156+ const fnCanActivate = options . guards
157+ ? this . createGuardsFn ( guards , instance , callback )
158+ : null ;
145159 const fnApplyPipes = this . createPipesFn ( pipes , paramsOptions ) ;
146160 const handler = ( initialArgs : any [ ] , ...args : any [ ] ) => async ( ) => {
147161 if ( fnApplyPipes ) {
@@ -153,15 +167,8 @@ export class ExternalContextCreator {
153167
154168 const target = async ( ...args : any [ ] ) => {
155169 const initialArgs = this . contextUtils . createNullArray ( argsLength ) ;
156- const canActivate = await this . guardsConsumer . tryActivate (
157- guards ,
158- args ,
159- instance ,
160- callback ,
161- ) ;
162- if ( ! canActivate ) {
163- throw new ForbiddenException ( FORBIDDEN_MESSAGE ) ;
164- }
170+ fnCanActivate && ( await fnCanActivate ( args ) ) ;
171+
165172 const result = await this . interceptorsConsumer . intercept (
166173 interceptors ,
167174 args ,
@@ -171,7 +178,9 @@ export class ExternalContextCreator {
171178 ) ;
172179 return this . transformToResult ( result ) ;
173180 } ;
174- return this . externalErrorProxy . createProxy ( target , exceptionFilter ) ;
181+ return options . filters
182+ ? this . externalErrorProxy . createProxy ( target , exceptionFilter )
183+ : target ;
175184 }
176185
177186 public getMetadata < T > (
@@ -328,4 +337,23 @@ export class ExternalContextCreator {
328337 }
329338 return resultOrDeffered ;
330339 }
340+
341+ public createGuardsFn (
342+ guards : any [ ] ,
343+ instance : Controller ,
344+ callback : ( ...args : any [ ] ) => any ,
345+ ) : Function | null {
346+ const canActivateFn = async ( args : any [ ] ) => {
347+ const canActivate = await this . guardsConsumer . tryActivate (
348+ guards ,
349+ args ,
350+ instance ,
351+ callback ,
352+ ) ;
353+ if ( ! canActivate ) {
354+ throw new ForbiddenException ( FORBIDDEN_MESSAGE ) ;
355+ }
356+ } ;
357+ return guards . length ? canActivateFn : null ;
358+ }
331359}
0 commit comments