Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add possibility to specify extra providers #762

Merged
merged 2 commits into from
Apr 26, 2022

Conversation

Gamote
Copy link
Contributor

@Gamote Gamote commented Apr 7, 2022

Useful when the consumers wants to use a provider in the useFactory method. Inspired by Nest's HttpModule (code).
More info on the issue: https://stackoverflow.com/a/66164772/10878244

Usage example:

import ...

@Global()
@Module({})
export class AppMailerModule {
  public static registerAsync(options: AppMailerAsyncOptions): DynamicModule {
    const configProvider = this.createConfigProvider(options);

    return {
      module: AppMailerModule,
      imports: [
        MailerModule.forRootAsync({
          imports: options.imports || [],
          inject: [APP_MAILER_OPTIONS],
          extraProviders: [configProvider], // <-- Specify extra providers
          useFactory: async (consumerOptions: AppMailerOptions) => {
            return {
              ...consumerOptions.mailer,
              // <-- Now we can use it to add default options for example
            };
          },
        }),
        ...(options.imports || []),
      ],
      providers: [configProvider, AppMailerService],
      exports: [AppMailerService],
    };
  }
}

@Gamote
Copy link
Contributor Author

Gamote commented Apr 20, 2022

@cdiaz @juandav Is there anything I can do to make it easier for you to review this PR?

@cdiaz cdiaz merged commit 680c67f into nest-modules:master Apr 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants