mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
committed by
Felipe Martins
parent
e50a08b2c3
commit
a11cdf4683
+31
-9
@@ -15,13 +15,23 @@ module.exports = function mergeConfig(config1, config2) {
|
||||
config2 = config2 || {};
|
||||
var config = {};
|
||||
|
||||
utils.forEach(['url', 'method', 'params', 'data'], function valueFromConfig2(prop) {
|
||||
var valueFromConfig2Keys = ['url', 'method', 'params', 'data'];
|
||||
var mergeDeepPropertiesKeys = ['headers', 'auth', 'proxy'];
|
||||
var defaultToConfig2Keys = [
|
||||
'baseURL', 'url', 'transformRequest', 'transformResponse', 'paramsSerializer',
|
||||
'timeout', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
|
||||
'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress',
|
||||
'maxContentLength', 'validateStatus', 'maxRedirects', 'httpAgent',
|
||||
'httpsAgent', 'cancelToken', 'socketPath'
|
||||
];
|
||||
|
||||
utils.forEach(valueFromConfig2Keys, function valueFromConfig2(prop) {
|
||||
if (typeof config2[prop] !== 'undefined') {
|
||||
config[prop] = config2[prop];
|
||||
}
|
||||
});
|
||||
|
||||
utils.forEach(['headers', 'auth', 'proxy'], function mergeDeepProperties(prop) {
|
||||
utils.forEach(mergeDeepPropertiesKeys, function mergeDeepProperties(prop) {
|
||||
if (utils.isObject(config2[prop])) {
|
||||
config[prop] = utils.deepMerge(config1[prop], config2[prop]);
|
||||
} else if (typeof config2[prop] !== 'undefined') {
|
||||
@@ -33,13 +43,25 @@ module.exports = function mergeConfig(config1, config2) {
|
||||
}
|
||||
});
|
||||
|
||||
utils.forEach([
|
||||
'baseURL', 'url', 'transformRequest', 'transformResponse', 'paramsSerializer',
|
||||
'timeout', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
|
||||
'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress', 'maxContentLength',
|
||||
'validateStatus', 'maxRedirects', 'httpAgent', 'httpsAgent', 'cancelToken',
|
||||
'socketPath'
|
||||
], function defaultToConfig2(prop) {
|
||||
utils.forEach(defaultToConfig2Keys, function defaultToConfig2(prop) {
|
||||
if (typeof config2[prop] !== 'undefined') {
|
||||
config[prop] = config2[prop];
|
||||
} else if (typeof config1[prop] !== 'undefined') {
|
||||
config[prop] = config1[prop];
|
||||
}
|
||||
});
|
||||
|
||||
var axiosKeys = valueFromConfig2Keys
|
||||
.concat(mergeDeepPropertiesKeys)
|
||||
.concat(defaultToConfig2Keys);
|
||||
|
||||
var otherKeys = Object
|
||||
.keys(config2)
|
||||
.filter(function filterAxiosKeys(key) {
|
||||
return axiosKeys.indexOf(key) === -1;
|
||||
});
|
||||
|
||||
utils.forEach(otherKeys, function otherKeysDefaultToConfig2(prop) {
|
||||
if (typeof config2[prop] !== 'undefined') {
|
||||
config[prop] = config2[prop];
|
||||
} else if (typeof config1[prop] !== 'undefined') {
|
||||
|
||||
@@ -64,4 +64,9 @@ describe('core::mergeConfig', function() {
|
||||
var merged = mergeConfig(defaults, { timeout: 123 });
|
||||
expect(merged.timeout).toEqual(123);
|
||||
});
|
||||
|
||||
it('should allow setting custom options', function() {
|
||||
var merged = mergeConfig(defaults, { foo: 'bar' });
|
||||
expect(merged.foo).toEqual('bar');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user