Skip to content

Commit

Permalink
fix work of Request with polyfilled URLSearchParams, close #965
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jul 26, 2021
1 parent 8666d47 commit 379c629
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@
- `%TypedArray%.prototype.filterReject` replaces `%TypedArray%.prototype.filterOut`
- Work with symbols made stricter: some missed before case of methods that should throw error on symbols now works as they should
- Handling `@@toPrimitive` in some cases of `ToPrimitive` internal logic made stricter
- Fixed work of `Request` with polyfilled `URLSearchParams`, [#965](https://github.com/zloirock/core-js/issues/965)
- Fixed some cases of typed arrays subclassing logic
- Fixed a minor bug related to string conversion in `RegExp#exec`
- Fixed `Date.prototype.getYear` feature detection and compat data for IE8-
Expand Down
65 changes: 41 additions & 24 deletions packages/core-js/modules/web.url-search-params.js
Expand Up @@ -22,7 +22,9 @@ var getIterator = require('../internals/get-iterator');
var getIteratorMethod = require('../internals/get-iterator-method');
var wellKnownSymbol = require('../internals/well-known-symbol');

var $fetch = getBuiltIn('fetch');
var nativeFetch = getBuiltIn('fetch');
var NativeRequest = getBuiltIn('Request');
var RequestPrototype = NativeRequest && NativeRequest.prototype;
var Headers = getBuiltIn('Headers');
var ITERATOR = wellKnownSymbol('iterator');
var URL_SEARCH_PARAMS = 'URLSearchParams';
Expand Down Expand Up @@ -319,30 +321,45 @@ $({ global: true, forced: !USE_NATIVE_URL }, {

// Wrap `fetch` for correct work with polyfilled `URLSearchParams`
// https://github.com/zloirock/core-js/issues/674
if (!USE_NATIVE_URL && typeof $fetch == 'function' && typeof Headers == 'function') {
$({ global: true, enumerable: true, forced: true }, {
fetch: function fetch(input /* , init */) {
var args = [input];
var init, body, headers;
if (arguments.length > 1) {
init = arguments[1];
if (isObject(init)) {
body = init.body;
if (classof(body) === URL_SEARCH_PARAMS) {
headers = init.headers ? new Headers(init.headers) : new Headers();
if (!headers.has('content-type')) {
headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
}
init = create(init, {
body: createPropertyDescriptor(0, String(body)),
headers: createPropertyDescriptor(0, headers)
});
}
if (!USE_NATIVE_URL && typeof Headers == 'function') {
var wrapRequestOptions = function (init) {
if (!isObject(init)) {
var body = init.body;
var headers;
if (classof(body) === URL_SEARCH_PARAMS) {
headers = init.headers ? new Headers(init.headers) : new Headers();
if (!headers.has('content-type')) {
headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
}
args.push(init);
} return $fetch.apply(this, args);
}
});
return create(init, {
body: createPropertyDescriptor(0, String(body)),
headers: createPropertyDescriptor(0, headers)
});
}
} return init;
};

if (typeof nativeFetch == 'function') {
$({ global: true, enumerable: true, forced: true }, {
fetch: function fetch(input /* , init */) {
return nativeFetch.call(this, input, arguments.length > 1 ? wrapRequestOptions(arguments[1]) : {});
}
});
}

if (typeof NativeRequest == 'function') {
var RequestConstructor = function Request(input /* , init */) {
anInstance(this, RequestConstructor, 'Request');
return new NativeRequest(input, arguments.length > 1 ? wrapRequestOptions(arguments[1]) : {});
};

RequestPrototype.constructor = RequestConstructor;
RequestConstructor.prototype = RequestPrototype;

$({ global: true, forced: true }, {
Request: RequestConstructor
});
}
}

module.exports = {
Expand Down
8 changes: 8 additions & 0 deletions tests/tests/web.url-search-params.js
Expand Up @@ -844,3 +844,11 @@ QUnit.test('URLSearchParams#@@toStringTag', assert => {
const params = new URLSearchParams('a=b');
assert.same(({}).toString.call(params), '[object URLSearchParams]');
});

if (typeof Request === 'function') {
QUnit.test('URLSearchParams with Request', assert => {
new Request('#', { body: new URLSearchParams({ foo: 'baz' }), method: 'POST' }).text().then(text => {
assert.same(text, 'foo=baz');
}).then(assert.async());
});
}

0 comments on commit 379c629

Please sign in to comment.