Closed
Description
Bug Report
Axios interceptor not working. I believe that my services are receiving another instance of httpService.
Current behavior
I initialize axios interceptor in AppModule onModuleInit lifecycle. But when in a service i use the httpService, this interceptor function not execute.
Another important info
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!
Input Code
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));
});
}
}
Expected behavior
Intercept Axios request and responses
Environment
Nest version: 6.3.2
For Tooling issues:
- Node version: 10.15.3
- Platform: Windows
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
kamilmysliwiec commentedon Jun 27, 2019
Could you please provide a minimal repository which reproduces your issue?
vagostep commentedon Jun 27, 2019
@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
kamilmysliwiec commentedon Jun 28, 2019
It's not an issue. Your
UsersModule
is using a different instance ofHttpService
than this one used inside theAppModule
. You should move your interceptors registration logic to theUsersModule
OR reuse the same instance ofHttpService
(create aSharedModule
for instance). It was working before because we had an issue with module injection scopes which is solved now.vagostep commentedon Jun 28, 2019
thanks!!! You're the best
lock commentedon Nov 6, 2019
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.