Skip to content

Commit

Permalink
fix: losting request header (axios#4858)
Browse files Browse the repository at this point in the history
  • Loading branch information
WillianAgostini committed Jul 20, 2022
1 parent 665db73 commit 8d3a3cb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/core/mergeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module.exports = function mergeConfig(config1, config2) {
function getMergedValue(target, source) {
if (utils.isPlainObject(target) && utils.isPlainObject(source)) {
return utils.merge(target, source);
} else if (utils.isEmptyObject(source)) {
return utils.merge({}, target);
} else if (utils.isPlainObject(source)) {
return utils.merge({}, source);
} else if (utils.isArray(source)) {
Expand Down
11 changes: 11 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ function isPlainObject(val) {
return prototype === null || prototype === Object.prototype;
}

/**
* Determine if a value is a empty Object
*
* @param {Object} val The value to test
* @return {boolean} True if value is a empty Object, otherwise false
*/
function isEmptyObject(val) {
return val && Object.keys(val).length === 0 && Object.getPrototypeOf(val) === Object.prototype;
}

/**
* Determine if a value is a Date
*
Expand Down Expand Up @@ -483,6 +493,7 @@ module.exports = {
isNumber: isNumber,
isObject: isObject,
isPlainObject: isPlainObject,
isEmptyObject: isEmptyObject,
isUndefined: isUndefined,
isDate: isDate,
isFile: isFile,
Expand Down
16 changes: 16 additions & 0 deletions test/specs/core/mergeConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,22 @@ describe('core::mergeConfig', function() {
.toEqual({validateStatus: {a: 1, b: 2, c: 2}});
});

it('should merge if config1 is class object', function() {
class Header {};
var validateStatus = new Header();
validateStatus["X-Foo"] = "bar";
var merged = mergeConfig({validateStatus: validateStatus}, {validateStatus: {}});
expect(JSON.stringify(merged.validateStatus)).toBe(JSON.stringify(validateStatus));
});

it('should merge if config2 is class object', function() {
class Header {};
var validateStatus = new Header();
validateStatus["X-Foo"] = "bar";
var merged = mergeConfig({validateStatus: {}}, {validateStatus: validateStatus});
expect(JSON.stringify(merged.validateStatus)).toBe(JSON.stringify(validateStatus));
});

it('should clone config2 if is plain object', function() {
var config1 = {validateStatus: [1, 2, 3]};
var config2 = {validateStatus: {a: 1, b: 2}};
Expand Down

0 comments on commit 8d3a3cb

Please sign in to comment.