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

Is that possible to add different Interceptors for different request? #108

Closed
CrisLi opened this issue Sep 14, 2015 · 4 comments
Closed

Comments

@CrisLi
Copy link

CrisLi commented Sep 14, 2015

Right now, the axios only support add/remove Interceptors to the global axios instance.

For example:

var axios = require('axios');
axios.interceptors.request.use(function (config) {

  var token = auth.getToken();

  if (token) {
    config.headers['authorization'] = 'Bearer ' + token;
  }

  config.headers['X-Requested-With'] = 'XMLHttpRequest';
  config.headers['Expires'] = '-1';
  config.headers['Cache-Control'] = "no-cache,no-store,must-revalidate,max-age=-1,private";

  if (ie && config.method == 'get') {
    config.url = buildUrl(config.url, { timestamp: Date.now().toString() });
  }

  return config;

}, function (err) {

  return Promise.reject(err);
});
var axios = require('axios');
// I don't need the Interceptor in below request
    axios
      .post( 'login', body)
      .then(function(response) {
        self.setToken(response.data.token)
        return true;
      })
      .catch(function (error) {
        if (error.message) {
          throw new Error(error.message);
        } else {
          throw new Error("System error, please try again later.");
        }
      });

Right now, I have to add another method to filter out some requests base on the url.

@deyhle
Copy link

deyhle commented Sep 15, 2015

It is indeed really bad to add the interceptors to the global object. Makes it impossible to use two different node modules that both require axios with their own interceptors.

@CrisLi
Copy link
Author

CrisLi commented Sep 16, 2015

@deyhle I am agree to use different node modules. But node will cache the module when you require it at first time. So when I add interceptors to one of them, all axios will have the interceptors. I have check the source code, the axios does not use factory pattern to exports its instance.

This was referenced Oct 3, 2015
@mzabriskie
Copy link
Member

You can now create instances of axios using axios.create which allows you to create interceptors per instance.

axios.interceptors.request.use((config) => {
  config.foo = 123;
});

const instance = axios.create();
instance.interceptors.request.use((config) => {
  config.foo = 456;
});

axios.get('/foo'); // config.foo === 123 -> true
instance.get('/foo'); // config.foo === 456 -> true

@trikiomar712
Copy link

hi everyone is there any posiibility to create an interceptor in electron js project ?

@axios axios locked and limited conversation to collaborators May 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants