diff --git a/CHANGELOG.md b/CHANGELOG.md index 8acfdc5c53d9..9448326954b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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- diff --git a/packages/core-js/modules/web.url-search-params.js b/packages/core-js/modules/web.url-search-params.js index 8972cb60fa4d..63c3baa417b0 100644 --- a/packages/core-js/modules/web.url-search-params.js +++ b/packages/core-js/modules/web.url-search-params.js @@ -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'; @@ -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 = { diff --git a/tests/tests/web.url-search-params.js b/tests/tests/web.url-search-params.js index b85c375c79f5..64ec9c379e60 100644 --- a/tests/tests/web.url-search-params.js +++ b/tests/tests/web.url-search-params.js @@ -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()); + }); +}