Hello, i have been strugling with this case.
I'm using some NPM package, and i would like to have some kind of wrapper. To do that, i created a provider, and i intend to use it with dependency injection. So that means, importing it as a Module in some .module.ts file, and use it in my .service.ts file.
But this package first needs to be initialized by passing it some tokens, API Key, or whatever. So i though about populating the class example here :
https://docs.nestjs.com/fundamentals/custom-providers#class-providers-useclass
With some constructor, and i added methods like this :
import Package from "package"
@Module({
providers: [{ provide: "customProvider", useClass: CustomProvider }]
exports: ["customProvider"]
})
export default class CustomProvider {
public constructor(private readonly package: Package) {
package = new Package({
api_key: ""
token: ""
})
}
public methodWrapped() {
// some process to be abstracted so that the service just have to call this function to work with the NPM package
}
}
So that in my service (where it will be injected), i could call the provider like this :
export class ExampleService() {
constructor(@Inject(CustomProvider) private readonly customProvider: CustomProvider) {}
function function1() {
// other things occuring before
this.customProvider.methodWrapped()
// and other things also occuring after this customProvider is called
}
}
But it doesn't seems to work. When importing the custom provider in my module i get the following error :
Error: Nest can't resolve dependencies of the CustomProvider (?). Please make sure that the argument a at index [0] is available in the CustomProvider context.
I think the error is occuring because it is somehow trying to inject the package inside CustomProvider but i'm not sure.
I'm also not sure that providers is what i need here, but i don't know any other way to make another class / file / whatever dependency injectable.
Any help appreciated !
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Progamming), FP (Functional Programming), and FRP (Functional Reactive Programming).