action-debian-package/node_modules/axios/lib/core/mergeConfig.js

88 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-03-26 14:37:35 +00:00
'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 = {};
2020-11-12 15:37:43 +00:00
var valueFromConfig2Keys = ['url', 'method', 'data'];
var mergeDeepPropertiesKeys = ['headers', 'auth', 'proxy', 'params'];
2020-03-26 14:37:35 +00:00
var defaultToConfig2Keys = [
2020-11-12 15:37:43 +00:00
'baseURL', 'transformRequest', 'transformResponse', 'paramsSerializer',
'timeout', 'timeoutMessage', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress', 'decompress',
'maxContentLength', 'maxBodyLength', 'maxRedirects', 'transport', 'httpAgent',
'httpsAgent', 'cancelToken', 'socketPath', 'responseEncoding'
2020-03-26 14:37:35 +00:00
];
2020-11-12 15:37:43 +00:00
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]);
}
}
2020-03-26 14:37:35 +00:00
utils.forEach(valueFromConfig2Keys, function valueFromConfig2(prop) {
2020-11-12 15:37:43 +00:00
if (!utils.isUndefined(config2[prop])) {
config[prop] = getMergedValue(undefined, config2[prop]);
2020-03-26 14:37:35 +00:00
}
});
2020-11-12 15:37:43 +00:00
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]);
2020-03-26 14:37:35 +00:00
}
});
2020-11-12 15:37:43 +00:00
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]);
2020-03-26 14:37:35 +00:00
}
});
var axiosKeys = valueFromConfig2Keys
.concat(mergeDeepPropertiesKeys)
2020-11-12 15:37:43 +00:00
.concat(defaultToConfig2Keys)
.concat(directMergeKeys);
2020-03-26 14:37:35 +00:00
var otherKeys = Object
2020-11-12 15:37:43 +00:00
.keys(config1)
.concat(Object.keys(config2))
2020-03-26 14:37:35 +00:00
.filter(function filterAxiosKeys(key) {
return axiosKeys.indexOf(key) === -1;
});
2020-11-12 15:37:43 +00:00
utils.forEach(otherKeys, mergeDeepProperties);
2020-03-26 14:37:35 +00:00
return config;
};