Skip to content

Commit

Permalink
Fixing custom config options (axios#2207)
Browse files Browse the repository at this point in the history
- fixes axios#2203
  • Loading branch information
rafaelrenanpacheco authored and felipewmartins committed Sep 7, 2019
1 parent e50a08b commit a11cdf4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
40 changes: 31 additions & 9 deletions lib/core/mergeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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') {
Expand Down
5 changes: 5 additions & 0 deletions test/specs/core/mergeConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

0 comments on commit a11cdf4

Please sign in to comment.