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

axios.create() does not export the same API as axios #217

Closed
dylancwood opened this issue Jan 31, 2016 · 2 comments
Closed

axios.create() does not export the same API as axios #217

dylancwood opened this issue Jan 31, 2016 · 2 comments

Comments

@dylancwood
Copy link

I am using axios.create() to create new instances of axios and thereby avoid adding interceptors to the global axios object. Firstly, let me say that it is awesome that you've exposed an API that allows this! Unfortunately, I had to spend a bit too much time discovering that axios.create() does not return a function, like require('axios') does.

This means that I cannot use the following way of invoking my axios instance:

var axios = require('axios');
var instance = axios.create({ foo: 123 });

instance({ //this needs to be `instance.request` in order for it to work
  url: '...',
})
  .then(handler)
  .catch(errHandler);

I am fine using instance.request instead of just instance, but it is inconsistent with the API exposed by the default axios object.

Solutions:

  1. Modify axios.create to return a bound function call to Axios.prototype.request() instead of the plain axios instance object. This is the same thing that is exported by axios.js.
  2. If you're not up for doing that, maybe we can just document this behavior in the README under axios.create.

I would love to implement either solution: just let me know which you prefer (if any).

@nickuraltsev
Copy link
Contributor

I don't like the idea of creating an instance of something and then using that instance as a function, but I would be happy to merge a PR that updates the README. Thank you!

@szujak
Copy link

szujak commented Apr 27, 2016

when i do this

// config/axios.js
import axios from 'axios';

import ApiHelper from '../helper/ApiHelper';
import InterceptorHelper from './../helper/InterceptorHelper';

export const interceptorHelper = new InterceptorHelper();
export const apiClient = axios.create({
   baseURL: ApiHelper.getUrl()
});

and then in another file i try to

import { apiClient, interceptorHelper } from './axios';

const interceptor = apiClient.interceptors.request.use((config) => {
        console.log(config);
});

then i don't get this console.log and interceptor variable is 0

solution for now:

// config/axios.js
import axios from 'axios';

import ApiHelper from '../helper/ApiHelper';
import InterceptorHelper from './../helper/InterceptorHelper';

axios.defaults.baseURL = ApiHelper.getUrl();

export const interceptorHelper = new InterceptorHelper();
export const apiClient = axios;

@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