Hello, I'm trying to make sense of the forRoot and forFeature approach for creating dynamic modules.
I've found very good explanations online but not too many code examples.
If I understand well (probably not) importing a module with forRoot from the App module would allow me to share providers from the forRoot module with the forFeature one.
Is that correct?
I'm trying to follow an example from this post
but I cannot make it work, here you have a reproduction link: https://stackblitz.com/~/github.com/dami2/nestjs-for-root-for-feature
The module created from forRoot exports a the baseUrlProvider that should be accessible by the module created from forFeature
@Module({})
export class ApiWrapperModule {
static forRoot(options: BaseApiConfig): DynamicModule {
const baseUrlProvider: Provider = {
provide: 'BASE_API_URL',
useValue: options.baseUrl,
};
return {
module: ApiWrapperModule,
providers: [baseUrlProvider],
exports: [baseUrlProvider],
};
}
static forFeature(options: EndpointConfig): DynamicModule {
const featureConfigProvider: Provider = {
provide: 'API_ENDPOINT',
useFactory: (baseApiUrl: string) => {
return `${baseApiUrl}/${options.endpoint}`;
},
inject: ['BASE_API_URL'],
};
return {
module: ApiWrapperModule,
providers: [featureConfigProvider],
exports: [featureConfigProvider],
};
}
}
but it seems this is not happening as I get the classic dependency not found error
what I'm missing here?
thanks in advance