Skip to content

Axios Interceptor #2443

Closed
Closed
@vagostep

Description

@vagostep

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

Activity

kamilmysliwiec

kamilmysliwiec commented on Jun 27, 2019

@kamilmysliwiec
Member

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

vagostep

vagostep commented on Jun 27, 2019

@vagostep
Author

@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

kamilmysliwiec commented on Jun 28, 2019

@kamilmysliwiec
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

vagostep commented on Jun 28, 2019

@vagostep
Author

thanks!!! You're the best

lock

lock commented on Nov 6, 2019

@lock

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.

locked as resolved and limited conversation to collaborators on Nov 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @BrunnerLivio@vagostep@kamilmysliwiec

        Issue actions

          Axios Interceptor · Issue #2443 · nestjs/nest