Nest: Axios Interceptor

Created on 25 Jun 2019  路  5Comments  路  Source: nestjs/nest

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
needs clarification needs triage

Most helpful comment

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.

All 5 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mishelashala picture mishelashala  路  3Comments

thohoh picture thohoh  路  3Comments

VRspace4 picture VRspace4  路  3Comments

menme95 picture menme95  路  3Comments

cdiaz picture cdiaz  路  3Comments