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

Comments

@dylancwood
Copy link

commented Jan 31, 2016

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

This comment has been minimized.

Copy link
Member

commented Apr 2, 2016

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

This comment has been minimized.

Copy link

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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.