Axios interceptor not working. I believe that my services are receiving another instance of httpService.
I initialize axios interceptor in AppModule onModuleInit lifecycle. But when in a service i use the httpService, this interceptor function not execute.
I use the Graphql Module for this project.
It should be noted that I have just updated the core of nest. Before that, is working well!
https://github.com/vagostep/interceptor-demo
export class AppModule implements OnModuleInit {
constructor(private httpService: HttpService) { }
public onModuleInit() {
this.httpService.axiosRef.interceptors.request.use((req) => {
console.log('request', req);
return req;
})
this.httpService.axiosRef.interceptors.response.use((response) => {
console.log('response', response);
return response;
});
}
}
export class UsersService {
constructor(private readonly httpService: HttpService) {}
public getUser(document: string): Promise<User> {
return new Promise<User>( (resolve, reject) => {
this.httpService.get<User>('http://jsonplaceholder.typicode.com/users/1')
.subscribe((resp) => resolve(resp.data), (err) => reject(err));
});
}
}
Intercept Axios request and responses
Nest version: 6.3.2
For Tooling issues:
- Node version: 10.15.3
- Platform: Windows
Could you please provide a minimal repository which reproduces your issue?
@kamilmysliwiec yes! https://github.com/vagostep/interceptor-demo
using Graphql (localhost:3000/graphql) see how when call de resolver, the console log inside interceptor (app.module) not showing
It's not an issue. Your UsersModule is using a different instance of HttpService than this one used inside the AppModule. You should move your interceptors registration logic to the UsersModule OR reuse the same instance of HttpService (create a SharedModule for instance). It was working before because we had an issue with module injection scopes which is solved now.
thanks!!! You're the best
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
It's not an issue. Your
UsersModuleis using a different instance ofHttpServicethan this one used inside theAppModule. You should move your interceptors registration logic to theUsersModuleOR reuse the same instance ofHttpService(create aSharedModulefor instance). It was working before because we had an issue with module injection scopes which is solved now.