mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
fix(headers): added support for setting header names that overlap with class methods; (#5831)
This commit is contained in:
@@ -10,5 +10,6 @@ module.exports = {
|
|||||||
"sourceType": "module"
|
"sourceType": "module"
|
||||||
},
|
},
|
||||||
"rules": {
|
"rules": {
|
||||||
|
"no-cond-assign": 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -282,7 +282,17 @@ class AxiosHeaders {
|
|||||||
|
|
||||||
AxiosHeaders.accessor(['Content-Type', 'Content-Length', 'Accept', 'Accept-Encoding', 'User-Agent', 'Authorization']);
|
AxiosHeaders.accessor(['Content-Type', 'Content-Length', 'Accept', 'Accept-Encoding', 'User-Agent', 'Authorization']);
|
||||||
|
|
||||||
utils.freezeMethods(AxiosHeaders.prototype);
|
// reserved names hotfix
|
||||||
|
utils.reduceDescriptors(AxiosHeaders.prototype, ({value}, key) => {
|
||||||
|
let mapped = key[0].toUpperCase() + key.slice(1); // map `set` => `Set`
|
||||||
|
return {
|
||||||
|
get: () => value,
|
||||||
|
set(headerValue) {
|
||||||
|
this[mapped] = headerValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
utils.freezeMethods(AxiosHeaders);
|
utils.freezeMethods(AxiosHeaders);
|
||||||
|
|
||||||
export default AxiosHeaders;
|
export default AxiosHeaders;
|
||||||
|
|||||||
+3
-2
@@ -540,8 +540,9 @@ const reduceDescriptors = (obj, reducer) => {
|
|||||||
const reducedDescriptors = {};
|
const reducedDescriptors = {};
|
||||||
|
|
||||||
forEach(descriptors, (descriptor, name) => {
|
forEach(descriptors, (descriptor, name) => {
|
||||||
if (reducer(descriptor, name, obj) !== false) {
|
let ret;
|
||||||
reducedDescriptors[name] = descriptor;
|
if ((ret = reducer(descriptor, name, obj)) !== false) {
|
||||||
|
reducedDescriptors[name] = ret || descriptor;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -76,6 +76,17 @@ describe('AxiosHeaders', function () {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should support uppercase name mapping for names overlapped by class methods', () => {
|
||||||
|
const headers = new AxiosHeaders({
|
||||||
|
set: 'foo'
|
||||||
|
});
|
||||||
|
|
||||||
|
headers.set('get', 'bar');
|
||||||
|
|
||||||
|
assert.strictEqual(headers.get('Set'), 'foo');
|
||||||
|
assert.strictEqual(headers.get('Get'), 'bar');
|
||||||
|
});
|
||||||
|
|
||||||
describe('get', function () {
|
describe('get', function () {
|
||||||
describe('filter', function() {
|
describe('filter', function() {
|
||||||
it('should support RegExp', function () {
|
it('should support RegExp', function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user