mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
fix(headers): allow iterable objects to be a data source for the set method; (#6873)
This commit is contained in:
@@ -100,9 +100,12 @@ class AxiosHeaders {
|
|||||||
setHeaders(header, valueOrRewrite)
|
setHeaders(header, valueOrRewrite)
|
||||||
} else if(utils.isString(header) && (header = header.trim()) && !isValidHeaderName(header)) {
|
} else if(utils.isString(header) && (header = header.trim()) && !isValidHeaderName(header)) {
|
||||||
setHeaders(parseHeaders(header), valueOrRewrite);
|
setHeaders(parseHeaders(header), valueOrRewrite);
|
||||||
} else if (utils.isHeaders(header)) {
|
} else if (utils.isObject(header) && utils.isIterable(header)) {
|
||||||
for (const [key, value] of header.entries()) {
|
for (const entry of header) {
|
||||||
setHeader(value, key, rewrite);
|
if (!utils.isArray(entry)) {
|
||||||
|
throw TypeError('Object iterator must return a key-value pair');
|
||||||
|
}
|
||||||
|
setHeader(entry[1], entry[0], rewrite);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
header != null && setHeader(valueOrRewrite, header, rewrite);
|
header != null && setHeader(valueOrRewrite, header, rewrite);
|
||||||
|
|||||||
+12
-6
@@ -6,6 +6,7 @@ import bind from './helpers/bind.js';
|
|||||||
|
|
||||||
const {toString} = Object.prototype;
|
const {toString} = Object.prototype;
|
||||||
const {getPrototypeOf} = Object;
|
const {getPrototypeOf} = Object;
|
||||||
|
const {iterator, toStringTag} = Symbol;
|
||||||
|
|
||||||
const kindOf = (cache => thing => {
|
const kindOf = (cache => thing => {
|
||||||
const str = toString.call(thing);
|
const str = toString.call(thing);
|
||||||
@@ -132,7 +133,7 @@ const isPlainObject = (val) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const prototype = getPrototypeOf(val);
|
const prototype = getPrototypeOf(val);
|
||||||
return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in val) && !(Symbol.iterator in val);
|
return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(toStringTag in val) && !(iterator in val);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -483,13 +484,13 @@ const isTypedArray = (TypedArray => {
|
|||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
const forEachEntry = (obj, fn) => {
|
const forEachEntry = (obj, fn) => {
|
||||||
const generator = obj && obj[Symbol.iterator];
|
const generator = obj && obj[iterator];
|
||||||
|
|
||||||
const iterator = generator.call(obj);
|
const _iterator = generator.call(obj);
|
||||||
|
|
||||||
let result;
|
let result;
|
||||||
|
|
||||||
while ((result = iterator.next()) && !result.done) {
|
while ((result = _iterator.next()) && !result.done) {
|
||||||
const pair = result.value;
|
const pair = result.value;
|
||||||
fn.call(obj, pair[0], pair[1]);
|
fn.call(obj, pair[0], pair[1]);
|
||||||
}
|
}
|
||||||
@@ -610,7 +611,7 @@ const toFiniteNumber = (value, defaultValue) => {
|
|||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
function isSpecCompliantForm(thing) {
|
function isSpecCompliantForm(thing) {
|
||||||
return !!(thing && isFunction(thing.append) && thing[Symbol.toStringTag] === 'FormData' && thing[Symbol.iterator]);
|
return !!(thing && isFunction(thing.append) && thing[toStringTag] === 'FormData' && thing[iterator]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const toJSONObject = (obj) => {
|
const toJSONObject = (obj) => {
|
||||||
@@ -679,6 +680,10 @@ const asap = typeof queueMicrotask !== 'undefined' ?
|
|||||||
|
|
||||||
// *********************
|
// *********************
|
||||||
|
|
||||||
|
|
||||||
|
const isIterable = (thing) => thing != null && isFunction(thing[iterator]);
|
||||||
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
isArray,
|
isArray,
|
||||||
isArrayBuffer,
|
isArrayBuffer,
|
||||||
@@ -734,5 +739,6 @@ export default {
|
|||||||
isAsyncFn,
|
isAsyncFn,
|
||||||
isThenable,
|
isThenable,
|
||||||
setImmediate: _setImmediate,
|
setImmediate: _setImmediate,
|
||||||
asap
|
asap,
|
||||||
|
isIterable
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -73,7 +73,15 @@ describe('AxiosHeaders', function () {
|
|||||||
headers.set('foo', 'value2', true);
|
headers.set('foo', 'value2', true);
|
||||||
|
|
||||||
assert.strictEqual(headers.get('foo'), 'value2');
|
assert.strictEqual(headers.get('foo'), 'value2');
|
||||||
})
|
});
|
||||||
|
|
||||||
|
it('should support iterables as a key-value source object', function () {
|
||||||
|
const headers = new AxiosHeaders();
|
||||||
|
|
||||||
|
headers.set(new Map([['x', '123']]));
|
||||||
|
|
||||||
|
assert.strictEqual(headers.get('x'), '123');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support uppercase name mapping for names overlapped by class methods', () => {
|
it('should support uppercase name mapping for names overlapped by class methods', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user