nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.91k stars 7.55k forks source link

Dependency Injection for a Config Token. #10634

Closed bokzor closed 1 year ago

bokzor commented 1 year ago

Is there an existing issue for this?

Current behavior

I have a really simple use case that should work (in my opinion) but that doesn't.

I have a Dynamic Module that declares a "Config Token"

And a Service that depends on that token.

It seems the service is not able to resolve that dependency.

@Global()
@Module({})
export class QuoteCommonBrokerModule {
    static register(options: QuoteBrokerModuleOptions) {
        return {
            module: QuoteCommonBrokerModule,
            providers: [
             { provide: KAFKA_CONFIG_TOKEN, useValue: options.kafkaConfig },
               KafkaHealthCheck
            ],
            exports: [KafkaHealthCheck],
        };
    }
}

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-ha7qrt?file=src/app.module.ts

Steps to reproduce

npm i npm start

Expected behavior

Dependency should be accessible in the dependency tree

Package

Other package

No response

NestJS version

No response

Packages versions

[System Information] OS Version : Linux 5.0 NodeJS Version : v16.14.2 NPM Version : 7.17.0

[Nest CLI] Nest CLI Version : 9.0.0

[Nest Platform Information] platform-express version : 9.0.0 schematics version : 9.0.0 testing version : 9.0.0 common version : 9.0.0 core version : 9.0.0 cli version : 9.0.0

Node.js version

No response

In which operating systems have you tested?

Other

As workaround, I created a small service containing the config.

         {
                    provide: KafkaConfigService,
                    useFactory: () => {
                        return new KafkaConfigService(options.kafkaConfig);
                    },
                },
kamilmysliwiec commented 1 year ago

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.