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

userData in axios request objects #1890

Closed
Flyrell opened this issue Nov 23, 2018 · 3 comments
Closed

userData in axios request objects #1890

Flyrell opened this issue Nov 23, 2018 · 3 comments

Comments

@Flyrell
Copy link

Flyrell commented Nov 23, 2018

Hi. I've just published axios-auth-refresh yesterday and I have some more ideas in mind that might be implemented in the future releases of my package. I'd like to ask you if there's a possibility to add some userData to request objects (like for example in Three.js - Object3D)?

If there's not, wouldn't it be good to implement something similar?

There's lots of use cases for it: calls differentiation, dynamic request's params binding, etc.

Take a look at this example:

axios.interceptors.request.use(req => {
  if (req.userData.token) {
    return req.headers['Authorization'] = req.userData.token;
  }
  return req;
});

const exampleToken = localStorage.get('exampleToken');
axios.get('https://www.example.com/restricted/area', {
  userData: {
    token: exampleToken
  }
}).then(/* ... */).catch(/* ... */);

const apiToken = localStorage.get('apiToken');
axios.get('https://www.api.com/restricted/area', {
  userData: {
    token: apiToken
  }
}).then(/* ... */).catch(/* ... */);

I'd be happy to contribute if anyone feels like it might be nice to have this possibility onboard.

@sbimochan
Copy link

Yeah. I'm up.

@Kamahl19
Copy link

Why would you need to create a userData field? You can just easily pass any data into config and consume it in both request & response interceptors. When using typescript, you can also extend the AxiosRequestConfig interface like this

declare module 'axios' {
  interface AxiosRequestConfig {
    apiCallId?: string;
  }
}

@chinesedfan
Copy link
Collaborator

I think what you need is custom configs. It is last supported in 0.18. And #2207 has fixed it again.

@axios axios locked and limited conversation to collaborators May 22, 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