mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Fixing bug with custom intances and global defaults
This commit is contained in:
+4
-3
@@ -3,6 +3,7 @@
|
|||||||
var utils = require('./utils');
|
var utils = require('./utils');
|
||||||
var bind = require('./helpers/bind');
|
var bind = require('./helpers/bind');
|
||||||
var Axios = require('./core/Axios');
|
var Axios = require('./core/Axios');
|
||||||
|
var defaults = require('./defaults');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an instance of Axios
|
* Create an instance of Axios
|
||||||
@@ -24,14 +25,14 @@ function createInstance(defaultConfig) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create the default instance to be exported
|
// Create the default instance to be exported
|
||||||
var axios = createInstance();
|
var axios = createInstance(defaults);
|
||||||
|
|
||||||
// Expose Axios class to allow class inheritance
|
// Expose Axios class to allow class inheritance
|
||||||
axios.Axios = Axios;
|
axios.Axios = Axios;
|
||||||
|
|
||||||
// Factory for creating new instances
|
// Factory for creating new instances
|
||||||
axios.create = function create(defaultConfig) {
|
axios.create = function create(instanceConfig) {
|
||||||
return createInstance(defaultConfig);
|
return createInstance(utils.merge(defaults, instanceConfig));
|
||||||
};
|
};
|
||||||
|
|
||||||
// Expose Cancel & CancelToken
|
// Expose Cancel & CancelToken
|
||||||
|
|||||||
+3
-3
@@ -10,10 +10,10 @@ var combineURLs = require('./../helpers/combineURLs');
|
|||||||
/**
|
/**
|
||||||
* Create a new instance of Axios
|
* Create a new instance of Axios
|
||||||
*
|
*
|
||||||
* @param {Object} defaultConfig The default config for the instance
|
* @param {Object} instanceConfig The default config for the instance
|
||||||
*/
|
*/
|
||||||
function Axios(defaultConfig) {
|
function Axios(instanceConfig) {
|
||||||
this.defaults = utils.merge(defaults, defaultConfig);
|
this.defaults = instanceConfig;
|
||||||
this.interceptors = {
|
this.interceptors = {
|
||||||
request: new InterceptorManager(),
|
request: new InterceptorManager(),
|
||||||
response: new InterceptorManager()
|
response: new InterceptorManager()
|
||||||
|
|||||||
@@ -136,4 +136,27 @@ describe('defaults', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should be used by custom instance if set before instance created', function (done) {
|
||||||
|
axios.defaults.baseURL = 'http://example.org/';
|
||||||
|
var instance = axios.create();
|
||||||
|
|
||||||
|
instance.get('/foo');
|
||||||
|
|
||||||
|
getAjaxRequest().then(function (request) {
|
||||||
|
expect(request.url).toBe('http://example.org/foo');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be used by custom instance if set after instance created', function (done) {
|
||||||
|
var instance = axios.create();
|
||||||
|
axios.defaults.baseURL = 'http://example.org/';
|
||||||
|
|
||||||
|
instance.get('/foo');
|
||||||
|
|
||||||
|
getAjaxRequest().then(function (request) {
|
||||||
|
expect(request.url).toBe('http://example.org/foo');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user