whatwg-fetch
Advanced tools
Comparing version 0.11.0 to 1.0.0
96
fetch.js
@@ -8,2 +8,17 @@ (function(self) { | ||
var support = { | ||
searchParams: 'URLSearchParams' in self, | ||
iterable: 'Symbol' in self && 'iterator' in Symbol, | ||
blob: 'FileReader' in self && 'Blob' in self && (function() { | ||
try { | ||
new Blob() | ||
return true | ||
} catch(e) { | ||
return false | ||
} | ||
})(), | ||
formData: 'FormData' in self, | ||
arrayBuffer: 'ArrayBuffer' in self | ||
} | ||
function normalizeName(name) { | ||
@@ -26,2 +41,20 @@ if (typeof name !== 'string') { | ||
// Build a destructive iterator for the value list | ||
function iteratorFor(items) { | ||
var iterator = { | ||
next: function() { | ||
var value = items.shift() | ||
return {done: value === undefined, value: value} | ||
} | ||
} | ||
if (support.iterable) { | ||
iterator[Symbol.iterator] = function() { | ||
return iterator | ||
} | ||
} | ||
return iterator | ||
} | ||
function Headers(headers) { | ||
@@ -82,2 +115,24 @@ this.map = {} | ||
Headers.prototype.keys = function() { | ||
var items = [] | ||
this.forEach(function(value, name) { items.push(name) }) | ||
return iteratorFor(items) | ||
} | ||
Headers.prototype.values = function() { | ||
var items = [] | ||
this.forEach(function(value) { items.push(value) }) | ||
return iteratorFor(items) | ||
} | ||
Headers.prototype.entries = function() { | ||
var items = [] | ||
this.forEach(function(value, name) { items.push([name, value]) }) | ||
return iteratorFor(items) | ||
} | ||
if (support.iterable) { | ||
Headers.prototype[Symbol.iterator] = Headers.prototype.entries | ||
} | ||
function consumed(body) { | ||
@@ -113,19 +168,5 @@ if (body.bodyUsed) { | ||
var support = { | ||
blob: 'FileReader' in self && 'Blob' in self && (function() { | ||
try { | ||
new Blob(); | ||
return true | ||
} catch(e) { | ||
return false | ||
} | ||
})(), | ||
formData: 'FormData' in self, | ||
arrayBuffer: 'ArrayBuffer' in self | ||
} | ||
function Body() { | ||
this.bodyUsed = false | ||
this._initBody = function(body) { | ||
@@ -139,2 +180,4 @@ this._bodyInit = body | ||
this._bodyFormData = body | ||
} else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) { | ||
this._bodyText = body.toString() | ||
} else if (!body) { | ||
@@ -154,2 +197,4 @@ this._bodyText = '' | ||
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') | ||
} | ||
@@ -276,3 +321,3 @@ } | ||
var head = new Headers() | ||
var pairs = xhr.getAllResponseHeaders().trim().split('\n') | ||
var pairs = (xhr.getAllResponseHeaders() || '').trim().split('\n') | ||
pairs.forEach(function(header) { | ||
@@ -330,5 +375,5 @@ var split = header.trim().split(':') | ||
self.Headers = Headers; | ||
self.Request = Request; | ||
self.Response = Response; | ||
self.Headers = Headers | ||
self.Request = Request | ||
self.Response = Response | ||
@@ -356,13 +401,8 @@ self.fetch = function(input, init) { | ||
return; | ||
return | ||
} | ||
xhr.onload = function() { | ||
var status = (xhr.status === 1223) ? 204 : xhr.status | ||
if (status < 100 || status > 599) { | ||
reject(new TypeError('Network request failed')) | ||
return | ||
} | ||
var options = { | ||
status: status, | ||
status: xhr.status, | ||
statusText: xhr.statusText, | ||
@@ -372,3 +412,3 @@ headers: headers(xhr), | ||
} | ||
var body = 'response' in xhr ? xhr.response : xhr.responseText; | ||
var body = 'response' in xhr ? xhr.response : xhr.responseText | ||
resolve(new Response(body, options)) | ||
@@ -381,2 +421,6 @@ } | ||
xhr.ontimeout = function() { | ||
reject(new TypeError('Network request failed')) | ||
} | ||
xhr.open(request.method, request.url, true) | ||
@@ -383,0 +427,0 @@ |
{ | ||
"name": "whatwg-fetch", | ||
"description": "A window.fetch polyfill.", | ||
"version": "0.11.0", | ||
"version": "1.0.0", | ||
"main": "fetch.js", | ||
"repository": "github/fetch", | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/github/fetch/blob/master/LICENSE" | ||
} | ||
], | ||
"license": "MIT", | ||
"devDependencies": { | ||
@@ -17,7 +12,7 @@ "bower": "1.3.8", | ||
"jshint": "2.8.0", | ||
"mocha-phantomjs": "3.5.2", | ||
"mocha": "2.1.0", | ||
"phantomjs": "1.9.19" | ||
"mocha-phantomjs-core": "2.0.1", | ||
"url-search-params": "0.5.0" | ||
}, | ||
"files" : [ | ||
"files": [ | ||
"LICENSE", | ||
@@ -24,0 +19,0 @@ "fetch.js" |
@@ -34,2 +34,9 @@ # window.fetch polyfill | ||
For babel and es2015+, make sure to import the file: | ||
```javascript | ||
import 'whatwg-fetch'; | ||
fetch(...); | ||
``` | ||
## Usage | ||
@@ -81,3 +88,3 @@ | ||
fetch('/users', { | ||
method: 'post', | ||
method: 'POST', | ||
body: new FormData(form) | ||
@@ -91,3 +98,3 @@ }) | ||
fetch('/users', { | ||
method: 'post', | ||
method: 'POST', | ||
headers: { | ||
@@ -114,3 +121,3 @@ 'Accept': 'application/json', | ||
fetch('/avatars', { | ||
method: 'post', | ||
method: 'POST', | ||
body: data | ||
@@ -218,2 +225,2 @@ }) | ||
--- | --- | --- | --- | --- | | ||
Latest ✔ | Latest ✔ | 9+ ✔ | Latest ✔ | 6.1+ ✔ | | ||
Latest ✔ | Latest ✔ | 10+ ✔ | Latest ✔ | 6.1+ ✔ | |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
18987
366
1
222