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

Support interceptors #14

Closed
mzabriskie opened this issue Sep 22, 2014 · 4 comments
Closed

Support interceptors #14

mzabriskie opened this issue Sep 22, 2014 · 4 comments

Comments

@mzabriskie
Copy link
Member

See Angular's spec for reference https://docs.angularjs.org/api/ng/service/$http#interceptors

@mzabriskie mzabriskie changed the title Support request interceptors Support interceptors Oct 14, 2014
@kentcdodds
Copy link

Can I help with this? I'm using js-data and I need this working for my authentication to work :-)

@jmdobry
Copy link
Contributor

jmdobry commented Dec 2, 2014

👍

@mzabriskie
Copy link
Member Author

Here's a couple approaches for the API that might work:

// 1) This style follows Angular's $http service
axios.interceptors.push({
  request: function (config) {},
  requestError: function (rejection) {},
  response: function (response) {},
  responseError: function (rejection) {}
});

// 2) This style follows express middleware
axios.interceptors.request.use(function (config) {}, function (rejection) {});
axios.interceptors.response.use(function (config) {}, function (rejection) {});

The first option has the benefit of familiarity for those coming from Angular. That said it is not enough to mimic an API based on familiarity, especially if it's not a good API. As @jmdobry mentioned offline, "pushing stuff into some random array [is] kinda dumb". I agree that arbitrarily pushing some config object onto an array is less than ideal.

The second option allows for simplicity by just passing in a function, and axios knowing if the function is to be used for request, or response. It is a little uglier to only specify a requestError handler for example, since you have to provide a null value for the request handler:

axios.interceptors.request.use(null, function (rejection) {});

A third option could be to do a hybrid of the two. Expose an API that supports the second option, but under the hood it just creates what option one is doing. This would allow for both options to be supported.

@mzabriskie
Copy link
Member Author

Closed with #27

@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

3 participants