Duplicated interceptors
matiasgarcia opened this issue · comments
Is there an existing issue for this?
- I have searched the existing issues
Current behavior
- I have an HttpClient service that "wraps" HttpService by initializing axiosRef with some interceptors to add some logging.
- This HttpClientService is imported by several modules that have to do API requests or have their own client.
The result of this is that a single request is logged as many dependencies on HttpClient I have.
How can I avoid this duplication?
Minimum reproduction code
https://gist.github.com/matiasgarcia/53d2aa20acecec511b285efd86525279
Steps to reproduce
No response
Expected behavior
Single interceptor added
Package version
0.1.0
NestJS version
9.0.11
Node.js version
v16.17.0
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
No response
if you want to use the same instance of that service everywhere, don't import the service multiple times. Instead, import it only once in some module, export it from there, and then import this new module multiple times whenever you need to use the HttpClientService
provider
btw another way to enhance HttpService
is by doing class HttpClientService extends HttpService
.
@micalevisk sorry but I am not following you
Gien the gist from above, and these modules:
@Module({
imports: [HttpClientModule, ConfigurationModule],
providers: [CryptocambiosService],
exports: [CryptocambiosService],
})
export class CryptocambiosModule {}
import { Module } from '@nestjs/common';
import { ApexService } from './apex.service';
import { ConfigurationModule } from '../configuration/configuration.module';
import { HttpClientModule } from '../http-client/http-client.module';
@Module({
imports: [HttpClientModule, ConfigurationModule],
providers: [ApexService],
exports: [ApexService],
})
export class ApexModule {}
(Both services receive HttpClientService injected in the constructor)
This is not achieving what you mentioned?
if you want to use the same instance of that service everywhere, don't import the service multiple times. Instead, import it only once in some module, export it from there, and then import this new module multiple times whenever you need to use the HttpClientService provider
oh I thought you were importing the service multiple times, not the HttpClientModule
module.
Check out if HttpClientService
's constructor
has been invoked multiple times.
Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.