I'm in the process of migrating an existing Express application to NestJS. Currently, I have a configuration file where I define multiple axios instances for each microservice:
export const writeModelApi = axios.create({
baseURL: getWriteModelApiUrl(),
});
export const readModelApi = axios.create({
baseURL: getReadModelApiUrl(),
});
export const configApi = axios.create({
baseURL: getConfigApiUrl(),
});
function addCamelizeInterceptors(api: any) {
// Interceptor logic
}
addCamelizeInterceptors(taskingApi);
addCamelizeInterceptors(readModelApi);
addCamelizeInterceptors(configApi);
I've been thinking about how to replicate this functionality using shared modules in NestJS. Currently, I have set up the following:
- ReadModelModule
@Module({
imports: [
HttpModule.register({
baseURL: getReadModelApiUrl(),
}),
],
providers: [ReadModelService],
exports: [ReadModelService],
})
export class ReadModelModule implements OnModuleInit {
constructor(@Inject() private httpService: ReadModelService) {}
public onModuleInit() {
addCamelizeInterceptors(this.httpService.axiosRef);
}
}
- ReadModelService
@Injectable()
export class ReadModelService extends HttpService {}
However, Nest is throwing an error message that reads:
[ExceptionHandler] Nest can't resolve dependencies of the ReadModelModule (?). Please make sure that the argument dependency at index [0] is available in the ReadModelModule context.
Potential solutions:
- If dependency is a provider, is it part of the current ReadModelModule?
- If dependency is exported from a separate @Module, is that module imported within ReadModelModule?
@Module({
imports: [ /* the Module containing dependency */ ]
})
I'm struggling with resolving this issue and would appreciate any guidance or help. Thank you!