mirror of
https://github.com/tenrok/axios.git
synced 2026-06-08 17:22:34 +03:00
Fixed & Imporoved AxiosHeaders class (#5224)
* Refactored AxiosHeaders class; * Added support for instances of AxiosHeaders as a value for the headers option; Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
+36
-8
@@ -228,7 +228,7 @@ const trim = (str) => str.trim ?
|
||||
* @param {Function} fn The callback to invoke for each item
|
||||
*
|
||||
* @param {Boolean} [allOwnKeys = false]
|
||||
* @returns {void}
|
||||
* @returns {any}
|
||||
*/
|
||||
function forEach(obj, fn, {allOwnKeys = false} = {}) {
|
||||
// Don't bother if no value provided
|
||||
@@ -263,6 +263,24 @@ function forEach(obj, fn, {allOwnKeys = false} = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
function findKey(obj, key) {
|
||||
key = key.toLowerCase();
|
||||
const keys = Object.keys(obj);
|
||||
let i = keys.length;
|
||||
let _key;
|
||||
while (i-- > 0) {
|
||||
_key = keys[i];
|
||||
if (key === _key.toLowerCase()) {
|
||||
return _key;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const _global = typeof self === "undefined" ? typeof global === "undefined" ? this : global : self;
|
||||
|
||||
const isContextDefined = (context) => !isUndefined(context) && context !== _global;
|
||||
|
||||
/**
|
||||
* Accepts varargs expecting each argument to be an object, then
|
||||
* immutably merges the properties of each object and returns result.
|
||||
@@ -282,16 +300,18 @@ function forEach(obj, fn, {allOwnKeys = false} = {}) {
|
||||
* @returns {Object} Result of all merge properties
|
||||
*/
|
||||
function merge(/* obj1, obj2, obj3, ... */) {
|
||||
const {caseless} = isContextDefined(this) && this || {};
|
||||
const result = {};
|
||||
const assignValue = (val, key) => {
|
||||
if (isPlainObject(result[key]) && isPlainObject(val)) {
|
||||
result[key] = merge(result[key], val);
|
||||
const targetKey = caseless && findKey(result, key) || key;
|
||||
if (isPlainObject(result[targetKey]) && isPlainObject(val)) {
|
||||
result[targetKey] = merge(result[targetKey], val);
|
||||
} else if (isPlainObject(val)) {
|
||||
result[key] = merge({}, val);
|
||||
result[targetKey] = merge({}, val);
|
||||
} else if (isArray(val)) {
|
||||
result[key] = val.slice();
|
||||
result[targetKey] = val.slice();
|
||||
} else {
|
||||
result[key] = val;
|
||||
result[targetKey] = val;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -527,6 +547,11 @@ const reduceDescriptors = (obj, reducer) => {
|
||||
|
||||
const freezeMethods = (obj) => {
|
||||
reduceDescriptors(obj, (descriptor, name) => {
|
||||
// skip restricted props in strict mode
|
||||
if (isFunction(obj) && ['arguments', 'caller', 'callee'].indexOf(name) !== -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const value = obj[name];
|
||||
|
||||
if (!isFunction(value)) return;
|
||||
@@ -540,7 +565,7 @@ const freezeMethods = (obj) => {
|
||||
|
||||
if (!descriptor.set) {
|
||||
descriptor.set = () => {
|
||||
throw Error('Can not read-only method \'' + name + '\'');
|
||||
throw Error('Can not rewrite read-only method \'' + name + '\'');
|
||||
};
|
||||
}
|
||||
});
|
||||
@@ -609,5 +634,8 @@ export default {
|
||||
toObjectSet,
|
||||
toCamelCase,
|
||||
noop,
|
||||
toFiniteNumber
|
||||
toFiniteNumber,
|
||||
findKey,
|
||||
global: _global,
|
||||
isContextDefined
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user