Skip to content

Commit d8b4ca0

Browse files
fix(headers): added support for setting header names that overlap with class methods; (#5831)
1 parent 3f53eb6 commit d8b4ca0

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ module.exports = {
1010
"sourceType": "module"
1111
},
1212
"rules": {
13+
"no-cond-assign": 0
1314
}
1415
}

lib/core/AxiosHeaders.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,17 @@ class AxiosHeaders {
282282

283283
AxiosHeaders.accessor(['Content-Type', 'Content-Length', 'Accept', 'Accept-Encoding', 'User-Agent', 'Authorization']);
284284

285-
utils.freezeMethods(AxiosHeaders.prototype);
285+
// reserved names hotfix
286+
utils.reduceDescriptors(AxiosHeaders.prototype, ({value}, key) => {
287+
let mapped = key[0].toUpperCase() + key.slice(1); // map `set` => `Set`
288+
return {
289+
get: () => value,
290+
set(headerValue) {
291+
this[mapped] = headerValue;
292+
}
293+
}
294+
});
295+
286296
utils.freezeMethods(AxiosHeaders);
287297

288298
export default AxiosHeaders;

lib/utils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,9 @@ const reduceDescriptors = (obj, reducer) => {
540540
const reducedDescriptors = {};
541541

542542
forEach(descriptors, (descriptor, name) => {
543-
if (reducer(descriptor, name, obj) !== false) {
544-
reducedDescriptors[name] = descriptor;
543+
let ret;
544+
if ((ret = reducer(descriptor, name, obj)) !== false) {
545+
reducedDescriptors[name] = ret || descriptor;
545546
}
546547
});
547548

test/unit/core/AxiosHeaders.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ describe('AxiosHeaders', function () {
7676
})
7777
});
7878

79+
it('should support uppercase name mapping for names overlapped by class methods', () => {
80+
const headers = new AxiosHeaders({
81+
set: 'foo'
82+
});
83+
84+
headers.set('get', 'bar');
85+
86+
assert.strictEqual(headers.get('Set'), 'foo');
87+
assert.strictEqual(headers.get('Get'), 'bar');
88+
});
89+
7990
describe('get', function () {
8091
describe('filter', function() {
8192
it('should support RegExp', function () {

0 commit comments

Comments
 (0)