mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
Fixing #385 - Keep defaults local to instance
This commit is contained in:
+1
-1
@@ -32,7 +32,7 @@ axios.Axios = Axios;
|
||||
|
||||
// Factory for creating new instances
|
||||
axios.create = function create(instanceConfig) {
|
||||
return createInstance(utils.merge(defaults, instanceConfig));
|
||||
return createInstance(utils.mergeConfig(axios.defaults, instanceConfig || {}));
|
||||
};
|
||||
|
||||
// Expose Cancel & CancelToken
|
||||
|
||||
+9
-5
@@ -1,6 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
var defaults = require('./../defaults');
|
||||
var utils = require('./../utils');
|
||||
var InterceptorManager = require('./InterceptorManager');
|
||||
var dispatchRequest = require('./dispatchRequest');
|
||||
@@ -24,15 +23,20 @@ function Axios(instanceConfig) {
|
||||
* @param {Object} config The config specific for this request (merged with this.defaults)
|
||||
*/
|
||||
Axios.prototype.request = function request(config) {
|
||||
config = config || {};
|
||||
|
||||
/*eslint no-param-reassign:0*/
|
||||
// Allow for axios('example/url'[, config]) a la fetch API
|
||||
if (typeof config === 'string') {
|
||||
config = utils.merge({
|
||||
url: arguments[0]
|
||||
}, arguments[1]);
|
||||
config = arguments[1] || {};
|
||||
config.url = arguments[0];
|
||||
}
|
||||
|
||||
config = utils.merge(defaults, this.defaults, { method: 'get' }, config);
|
||||
if (!config.method) {
|
||||
config.method = 'get';
|
||||
}
|
||||
|
||||
config = utils.mergeConfig(this.defaults, config);
|
||||
config.method = config.method.toLowerCase();
|
||||
|
||||
// Hook up interceptors middleware
|
||||
|
||||
+47
-1
@@ -279,6 +279,51 @@ function extend(a, b, thisArg) {
|
||||
return a;
|
||||
}
|
||||
|
||||
function mergeConfig(defaults, instanceConfig) {
|
||||
var config = {};
|
||||
forEach(['url', 'method', 'params', 'data'], function(prop) {
|
||||
config[prop] = instanceConfig[prop];
|
||||
});
|
||||
forEach(['headers', 'auth', 'proxy'], function(prop) {
|
||||
if (!isUndefined(instanceConfig[prop])) {
|
||||
if (!isObject(instanceConfig[prop])) {
|
||||
config[prop] = instanceConfig[prop];
|
||||
} else {
|
||||
config[prop] = merge(
|
||||
defaults[prop],
|
||||
instanceConfig[prop]
|
||||
);
|
||||
}
|
||||
} else if (!isUndefined(defaults[prop])) {
|
||||
config[prop] = JSON.parse(JSON.stringify(defaults[prop]));
|
||||
}
|
||||
});
|
||||
var remainingProperties = [
|
||||
'baseURL',
|
||||
'transformRequest',
|
||||
'transformResponse',
|
||||
'paramsSerializer',
|
||||
'timeout',
|
||||
'withCredentials',
|
||||
'adapter',
|
||||
'responseType',
|
||||
'xsrfCookieName',
|
||||
'xsrfHeaderName',
|
||||
'onUploadProgress',
|
||||
'onDownloadProgress',
|
||||
'maxContentLength',
|
||||
'validateStatus',
|
||||
'maxRedirects',
|
||||
'httpAgent',
|
||||
'httpsAgent',
|
||||
'cancelToken'
|
||||
];
|
||||
forEach(remainingProperties, function(prop) {
|
||||
config[prop] = !isUndefined(instanceConfig[prop]) ? instanceConfig[prop] : defaults[prop];
|
||||
});
|
||||
return config;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
isArray: isArray,
|
||||
isArrayBuffer: isArrayBuffer,
|
||||
@@ -299,5 +344,6 @@ module.exports = {
|
||||
forEach: forEach,
|
||||
merge: merge,
|
||||
extend: extend,
|
||||
trim: trim
|
||||
trim: trim,
|
||||
mergeConfig: mergeConfig
|
||||
};
|
||||
|
||||
@@ -148,14 +148,14 @@ describe('defaults', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('should be used by custom instance if set after instance created', function (done) {
|
||||
it('should not 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');
|
||||
expect(request.url).toBe('/foo');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -81,4 +81,32 @@ describe('options', function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should change only the baseURL of the specified instance', function() {
|
||||
var instance1 = axios.create();
|
||||
var instance2 = axios.create();
|
||||
|
||||
instance1.defaults.baseURL = 'http://instance1.example.com/';
|
||||
|
||||
expect(instance2.defaults.baseURL).not.toBe('http://instance1.example.com/');
|
||||
});
|
||||
|
||||
it('should change only the headers of the specified instance', function() {
|
||||
var instance1 = axios.create();
|
||||
var instance2 = axios.create();
|
||||
|
||||
instance1.defaults.headers.common.Authorization = 'faketoken';
|
||||
instance2.defaults.headers.common.Authorization = 'differentfaketoken';
|
||||
|
||||
instance1.defaults.headers.common['Content-Type'] = 'application/xml';
|
||||
instance2.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
|
||||
expect(axios.defaults.headers.common.Authorization).toBe(undefined);
|
||||
expect(instance1.defaults.headers.common.Authorization).toBe('faketoken');
|
||||
expect(instance2.defaults.headers.common.Authorization).toBe('differentfaketoken');
|
||||
|
||||
expect(axios.defaults.headers.common['Content-Type']).toBe(undefined);
|
||||
expect(instance1.defaults.headers.common['Content-Type']).toBe('application/xml');
|
||||
expect(instance2.defaults.headers.common['Content-Type']).toBe('application/x-www-form-urlencoded');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user