Hi,
Normally when you "provide" a class using Nest, the DI system automatically injects the correct constructor args, ie:
@Injectable()
class MyProvider {
constructor(private myOtherProvider MyOtherProvider)
}
@Module({
providers: [MyProvider],
import: [MyOtherModule],
export: [MyProvider]
})
class MyModule{}
I'm wondering if there's a way to dynamically swap the implementation class being used for a provider without having to also manually inject and pass every dependency of that class.
For example, I know you can do something like
@Module({
providers: [
{
provide: MyProvider,
useFactory: (myOtherProvider: MyOtherProvider) => {
// make some decision about what provider to use here
if (useNewProvider) {
return new MyNewProvider(myOtherProvider)
}
return new MyProvider(myOtherProvider)
},
inject: [MyOtherProvider],
}
]
})
but that requires knowing that the underlying provider requires MyOtherProvider and making sure to inject it and pass it in inside useFactory
because of that it doesnt seem possible to build a general-purpose utility (think a feature flagging evaluator) that could swap between two service implementations (as long as they have the same signature)
any way to accomplish this?