2
0
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:
Dmitriy Mozgovoy
2022-11-07 21:11:42 +02:00
committed by GitHub
parent c0a723ab6c
commit ab77a40e1c
16 changed files with 321 additions and 159 deletions
+36 -8
View File
@@ -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
};