Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Axios Interceptor #2443

Closed
vagostep opened this issue Jun 24, 2019 · 5 comments
Closed

Axios Interceptor #2443

vagostep opened this issue Jun 24, 2019 · 5 comments
Labels
needs clarification needs triage This issue has not been looked into

Comments

@vagostep
Copy link

vagostep commented Jun 24, 2019

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
@vagostep vagostep added the needs triage This issue has not been looked into label Jun 24, 2019
@kamilmysliwiec
Copy link
Member

Could you please provide a minimal repository which reproduces your issue?

@kamilmysliwiec kamilmysliwiec added needs clarification and removed needs triage This issue has not been looked into labels Jun 27, 2019
@vagostep
Copy link
Author

vagostep commented 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
Copy link
Member

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.

@vagostep
Copy link
Author

thanks!!! You're the best

@BrunnerLivio BrunnerLivio added the needs triage This issue has not been looked into label Aug 8, 2019
@lock
Copy link

lock bot commented 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.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs clarification needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

3 participants