forked from axios/axios
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request axios#1395 from codeclown/instance-options
Fixing axios#385 - Keep defaults local to instance
- Loading branch information
Showing
9 changed files
with
252 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
var utils = require('../utils'); | ||
|
||
/** | ||
* Config-specific merge-function which creates a new config-object | ||
* by merging two configuration objects together. | ||
* | ||
* @param {Object} config1 | ||
* @param {Object} config2 | ||
* @returns {Object} New object resulting from merging config2 to config1 | ||
*/ | ||
module.exports = function mergeConfig(config1, config2) { | ||
// eslint-disable-next-line no-param-reassign | ||
config2 = config2 || {}; | ||
var config = {}; | ||
|
||
utils.forEach(['url', 'method', 'params', 'data'], function valueFromConfig2(prop) { | ||
if (typeof config2[prop] !== 'undefined') { | ||
config[prop] = config2[prop]; | ||
} | ||
}); | ||
|
||
utils.forEach(['headers', 'auth', 'proxy'], function mergeDeepProperties(prop) { | ||
if (utils.isObject(config2[prop])) { | ||
config[prop] = utils.deepMerge(config1[prop], config2[prop]); | ||
} else if (typeof config2[prop] !== 'undefined') { | ||
config[prop] = config2[prop]; | ||
} else if (utils.isObject(config1[prop])) { | ||
config[prop] = utils.deepMerge(config1[prop]); | ||
} else if (typeof config1[prop] !== 'undefined') { | ||
config[prop] = config1[prop]; | ||
} | ||
}); | ||
|
||
utils.forEach([ | ||
'baseURL', 'transformRequest', 'transformResponse', 'paramsSerializer', | ||
'timeout', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName', | ||
'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress', 'maxContentLength', | ||
'validateStatus', 'maxRedirects', 'httpAgent', 'httpsAgent', 'cancelToken', | ||
'socketPath' | ||
], function defaultToConfig2(prop) { | ||
if (typeof config2[prop] !== 'undefined') { | ||
config[prop] = config2[prop]; | ||
} else if (typeof config1[prop] !== 'undefined') { | ||
config[prop] = config1[prop]; | ||
} | ||
}); | ||
|
||
return config; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
var defaults = require('../../../lib/defaults'); | ||
var mergeConfig = require('../../../lib/core/mergeConfig'); | ||
|
||
describe('core::mergeConfig', function() { | ||
it('should accept undefined for second argument', function() { | ||
expect(mergeConfig(defaults, undefined)).toEqual(defaults); | ||
}); | ||
|
||
it('should accept an object for second argument', function() { | ||
expect(mergeConfig(defaults, {})).toEqual(defaults); | ||
}); | ||
|
||
it('should not leave references', function() { | ||
var merged = mergeConfig(defaults, {}); | ||
expect(merged).not.toBe(defaults); | ||
expect(merged.headers).not.toBe(defaults.headers); | ||
}); | ||
|
||
it('should allow setting request options', function() { | ||
var config = { | ||
url: '__sample url__', | ||
method: '__sample method__', | ||
params: '__sample params__', | ||
data: { foo: true } | ||
}; | ||
var merged = mergeConfig(defaults, config); | ||
expect(merged.url).toEqual(config.url); | ||
expect(merged.method).toEqual(config.method); | ||
expect(merged.params).toEqual(config.params); | ||
expect(merged.data).toEqual(config.data); | ||
}); | ||
|
||
it('should not inherit request options', function() { | ||
var localDefaults = { | ||
url: '__sample url__', | ||
method: '__sample method__', | ||
params: '__sample params__', | ||
data: { foo: true } | ||
}; | ||
var merged = mergeConfig(localDefaults, {}); | ||
expect(merged.url).toEqual(undefined); | ||
expect(merged.method).toEqual(undefined); | ||
expect(merged.params).toEqual(undefined); | ||
expect(merged.data).toEqual(undefined); | ||
}); | ||
|
||
it('should merge auth, headers, proxy with defaults', function() { | ||
expect(mergeConfig({ auth: undefined }, { auth: { user: 'foo', pass: 'test' } })).toEqual({ | ||
auth: { user: 'foo', pass: 'test' } | ||
}); | ||
expect(mergeConfig({ auth: { user: 'foo', pass: 'test' } }, { auth: { pass: 'foobar' } })).toEqual({ | ||
auth: { user: 'foo', pass: 'foobar' } | ||
}); | ||
}); | ||
|
||
it('should overwrite auth, headers, proxy with a non-object value', function() { | ||
expect(mergeConfig({ auth: { user: 'foo', pass: 'test' } }, { auth: false })).toEqual({ | ||
auth: false | ||
}); | ||
expect(mergeConfig({ auth: { user: 'foo', pass: 'test' } }, { auth: null })).toEqual({ | ||
auth: null | ||
}); | ||
}); | ||
|
||
it('should allow setting other options', function() { | ||
var merged = mergeConfig(defaults, { timeout: 123 }); | ||
expect(merged.timeout).toEqual(123); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
var deepMerge = require('../../../lib/utils').deepMerge; | ||
|
||
describe('utils::deepMerge', function () { | ||
it('should be immutable', function () { | ||
var a = {}; | ||
var b = {foo: 123}; | ||
var c = {bar: 456}; | ||
|
||
deepMerge(a, b, c); | ||
|
||
expect(typeof a.foo).toEqual('undefined'); | ||
expect(typeof a.bar).toEqual('undefined'); | ||
expect(typeof b.bar).toEqual('undefined'); | ||
expect(typeof c.foo).toEqual('undefined'); | ||
}); | ||
|
||
it('should deepMerge properties', function () { | ||
var a = {foo: 123}; | ||
var b = {bar: 456}; | ||
var c = {foo: 789}; | ||
var d = deepMerge(a, b, c); | ||
|
||
expect(d.foo).toEqual(789); | ||
expect(d.bar).toEqual(456); | ||
}); | ||
|
||
it('should deepMerge recursively', function () { | ||
var a = {foo: {bar: 123}}; | ||
var b = {foo: {baz: 456}, bar: {qux: 789}}; | ||
|
||
expect(deepMerge(a, b)).toEqual({ | ||
foo: { | ||
bar: 123, | ||
baz: 456 | ||
}, | ||
bar: { | ||
qux: 789 | ||
} | ||
}); | ||
}); | ||
|
||
it('should remove all references from nested objects', function () { | ||
var a = {foo: {bar: 123}}; | ||
var b = {}; | ||
var d = deepMerge(a, b); | ||
|
||
expect(d).toEqual({ | ||
foo: { | ||
bar: 123 | ||
} | ||
}); | ||
|
||
expect(d.foo).not.toBe(a.foo); | ||
}); | ||
|
||
it('handles null and undefined arguments', function () { | ||
expect(deepMerge(undefined, undefined)).toEqual({}); | ||
expect(deepMerge(undefined, {foo: 123})).toEqual({foo: 123}); | ||
expect(deepMerge({foo: 123}, undefined)).toEqual({foo: 123}); | ||
|
||
expect(deepMerge(null, null)).toEqual({}); | ||
expect(deepMerge(null, {foo: 123})).toEqual({foo: 123}); | ||
expect(deepMerge({foo: 123}, null)).toEqual({foo: 123}); | ||
}); | ||
}); | ||
|