Is there a special technique for successfully implementing `useValue` dependency injection in Nest.js interceptors? I have a dynamic module that looks similar to this:
@Module({})
export class SomeFeatureModule {
static register({
perRequestParams,
...clientOptions
}: ModuleOptions): DynamicModule {
const provider = new SomeClientProvider(clientOptions);
return {
module: SomeFeatureModule,
providers: [
{
provide: SomeClientProvider,
useValue: provider,
},
{
provide: SomeInterceptor,
useValue: new SomeInterceptor(provider, perRequestParams),
},
],
exports: [SomeClientProvider, SomeInterceptor],
};
}
}
...where the `SomeInterceptor` class is defined as follows:
@Injectable()
export class SomeInterceptor implements NestInterceptor {
constructor(
private readonly someClientProvider: SomeClientProvider,
private readonly perRequestParams: (
context: ExecutionContext,
) => EvaluationCriteria | Promise<EvaluationCriteria>,
) {}
async intercept(
execContext: ExecutionContext,
next: CallHandler<any>,
): Promise<Observable<any>> {
const params = await this.perRequestParams(execContext);
return this.someClientProvider.injectLocalStorageData(params, () => next.handle());
}
}
...but when I attempt to utilize the interceptor on my app's controller using:
@UseInterceptors(SomeInterceptor)
...I encounter the error:
Error: Nest can't resolve dependencies of the SomeInterceptor (SomeClientProvider, ?). Please ensure that the argument Function at index [1] is available in the AppModule context.
I am specifically importing `SomeFeatureModule.register(...)` within my `AppModule`:
@Module({})
export class AppModule {
static register(env: Environment): DynamicModule {
// ...
return {
module: AppModule,
imports: [
SomeFeatureModule.register({
...clientConfig,
async perRequestParams(ctx) {
// ...
},
}),
],
// ...
};
}
}
Why is the dependency injection system attempting to resolve the constructor parameters for `SomeInterceptor` even though I'm already manually providing one?
Note that removing `@Injectable()` eliminates the startup error, but then the interceptor's constructor is invoked without any arguments, resulting in an issue.