Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(headers): added support for setting header names that overlap wit…
…h class methods; (#5831)
  • Loading branch information
DigitalBrainJS committed Aug 25, 2023
1 parent 3f53eb6 commit d8b4ca0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Expand Up @@ -10,5 +10,6 @@ module.exports = {
"sourceType": "module"
},
"rules": {
"no-cond-assign": 0
}
}
12 changes: 11 additions & 1 deletion lib/core/AxiosHeaders.js
Expand Up @@ -282,7 +282,17 @@ class AxiosHeaders {

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);

export default AxiosHeaders;
5 changes: 3 additions & 2 deletions lib/utils.js
Expand Up @@ -540,8 +540,9 @@ const reduceDescriptors = (obj, reducer) => {
const reducedDescriptors = {};

forEach(descriptors, (descriptor, name) => {
if (reducer(descriptor, name, obj) !== false) {
reducedDescriptors[name] = descriptor;
let ret;
if ((ret = reducer(descriptor, name, obj)) !== false) {
reducedDescriptors[name] = ret || descriptor;
}
});

Expand Down
11 changes: 11 additions & 0 deletions test/unit/core/AxiosHeaders.js
Expand Up @@ -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('filter', function() {
it('should support RegExp', function () {
Expand Down

0 comments on commit d8b4ca0

Please sign in to comment.