-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor mergeConfig without utils.deepMerge (#2844) #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: pr_023_before
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,56 +18,70 @@ module.exports = function mergeConfig(config1, config2) { | |
| var valueFromConfig2Keys = ['url', 'method', 'data']; | ||
| var mergeDeepPropertiesKeys = ['headers', 'auth', 'proxy', 'params']; | ||
| var defaultToConfig2Keys = [ | ||
| 'baseURL', 'url', 'transformRequest', 'transformResponse', 'paramsSerializer', | ||
| 'timeout', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName', | ||
| 'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress', | ||
| 'maxContentLength', 'maxBodyLength', 'validateStatus', 'maxRedirects', 'httpAgent', | ||
| 'baseURL', 'transformRequest', 'transformResponse', 'paramsSerializer', | ||
| 'timeout', 'timeoutMessage', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName', | ||
| 'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress', 'decompress', | ||
| 'maxContentLength', 'maxBodyLength', 'maxRedirects', 'transport', 'httpAgent', | ||
| 'httpsAgent', 'cancelToken', 'socketPath', 'responseEncoding' | ||
| ]; | ||
| var directMergeKeys = ['validateStatus']; | ||
|
|
||
| function getMergedValue(target, source) { | ||
| if (utils.isPlainObject(target) && utils.isPlainObject(source)) { | ||
| return utils.merge(target, source); | ||
| } else if (utils.isPlainObject(source)) { | ||
| return utils.merge({}, source); | ||
| } else if (utils.isArray(source)) { | ||
| return source.slice(); | ||
| } | ||
| return source; | ||
| } | ||
|
|
||
| function mergeDeepProperties(prop) { | ||
| if (!utils.isUndefined(config2[prop])) { | ||
| config[prop] = getMergedValue(config1[prop], config2[prop]); | ||
| } else if (!utils.isUndefined(config1[prop])) { | ||
| config[prop] = getMergedValue(undefined, config1[prop]); | ||
| } | ||
| } | ||
|
|
||
| utils.forEach(valueFromConfig2Keys, function valueFromConfig2(prop) { | ||
| if (typeof config2[prop] !== 'undefined') { | ||
| config[prop] = config2[prop]; | ||
| if (!utils.isUndefined(config2[prop])) { | ||
| config[prop] = getMergedValue(undefined, config2[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') { | ||
| 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(mergeDeepPropertiesKeys, mergeDeepProperties); | ||
|
|
||
| utils.forEach(defaultToConfig2Keys, function defaultToConfig2(prop) { | ||
| if (!utils.isUndefined(config2[prop])) { | ||
| config[prop] = getMergedValue(undefined, config2[prop]); | ||
| } else if (!utils.isUndefined(config1[prop])) { | ||
| config[prop] = getMergedValue(undefined, config1[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]; | ||
| utils.forEach(directMergeKeys, function merge(prop) { | ||
| if (prop in config2) { | ||
| config[prop] = getMergedValue(config1[prop], config2[prop]); | ||
| } else if (prop in config1) { | ||
| config[prop] = getMergedValue(undefined, config1[prop]); | ||
| } | ||
| }); | ||
|
|
||
| var axiosKeys = valueFromConfig2Keys | ||
| .concat(mergeDeepPropertiesKeys) | ||
| .concat(defaultToConfig2Keys); | ||
| .concat(defaultToConfig2Keys) | ||
| .concat(directMergeKeys); | ||
|
|
||
| var otherKeys = Object | ||
| .keys(config2) | ||
| .keys(config1) | ||
| .concat(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') { | ||
| config[prop] = config1[prop]; | ||
| } | ||
| }); | ||
| utils.forEach(otherKeys, mergeDeepProperties); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate keys cause redundant processing in otherKeys loopLow Severity
|
||
|
|
||
| return config; | ||
| }; | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Null-valued headers no longer removed before sending requests
High Severity
The code that filtered out headers with
nullvalues was removed fromdispatchRequest.js. Previously, setting a header tonullwould remove it from the request (useful for overriding default headers). Now,null-valued headers are passed toXMLHttpRequest.setRequestHeader(), which stringifiesnullto the string"null"and sends it in the request. This breaks the documented pattern of usingnullto unset default headers and will cause unexpected header values in production requests.