From d77810a15c78bbbaf2defd4ea3af6db21c8d117f Mon Sep 17 00:00:00 2001 From: David Graham Date: Wed, 30 Mar 2016 12:22:32 -0600 Subject: [PATCH 1/2] Support URLSearchParams POST body https://fetch.spec.whatwg.org/#body-mixin --- .jshintrc | 3 ++- fetch.js | 5 +++++ package.json | 7 ++++--- test/test-worker.html | 1 + test/test.html | 1 + test/test.js | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 45 insertions(+), 4 deletions(-) diff --git a/.jshintrc b/.jshintrc index ef71670d..c451b546 100644 --- a/.jshintrc +++ b/.jshintrc @@ -19,6 +19,7 @@ "browser": true, "worker": true, "globals": { - "JSON": false + "JSON": false, + "URLSearchParams": false } } diff --git a/fetch.js b/fetch.js index f3412dc4..b507458a 100644 --- a/fetch.js +++ b/fetch.js @@ -6,6 +6,7 @@ } var support = { + searchParams: 'URLSearchParams' in self, iterable: 'Symbol' in self && 'iterator' in Symbol, blob: 'FileReader' in self && 'Blob' in self && (function() { try { @@ -193,6 +194,8 @@ this._bodyBlob = body } else if (support.formData && FormData.prototype.isPrototypeOf(body)) { this._bodyFormData = body + } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) { + this._bodyText = body.toString() } else if (!body) { this._bodyText = '' } else if (support.arrayBuffer && ArrayBuffer.prototype.isPrototypeOf(body)) { @@ -207,6 +210,8 @@ this.headers.set('content-type', 'text/plain;charset=UTF-8') } else if (this._bodyBlob && this._bodyBlob.type) { this.headers.set('content-type', this._bodyBlob.type) + } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) { + this.headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8') } } } diff --git a/package.json b/package.json index c26ed986..f473e523 100644 --- a/package.json +++ b/package.json @@ -14,11 +14,12 @@ "bower": "1.3.8", "chai": "1.10.0", "jshint": "2.8.0", - "mocha-phantomjs": "3.5.2", "mocha": "2.1.0", - "phantomjs": "1.9.19" + "mocha-phantomjs": "3.5.2", + "phantomjs": "1.9.19", + "url-search-params": "0.5.0" }, - "files" : [ + "files": [ "LICENSE", "fetch.js" ] diff --git a/test/test-worker.html b/test/test-worker.html index 57f4a51c..fa0d4886 100644 --- a/test/test-worker.html +++ b/test/test-worker.html @@ -7,6 +7,7 @@
+