whatwg-fetch
Advanced tools
Comparing version 0.6.1 to 0.7.0
{ | ||
"name": "fetch", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"main": "fetch.js", | ||
@@ -5,0 +5,0 @@ "devDependencies": { |
102
fetch.js
@@ -95,10 +95,13 @@ (function() { | ||
var blobSupport = 'FileReader' in self && 'Blob' in self && (function() { | ||
try { | ||
new Blob(); | ||
return true | ||
} catch(e) { | ||
return false | ||
} | ||
})(); | ||
var support = { | ||
blob: 'FileReader' in self && 'Blob' in self && (function() { | ||
try { | ||
new Blob(); | ||
return true | ||
} catch(e) { | ||
return false | ||
} | ||
})(), | ||
formData: 'FormData' in self | ||
} | ||
@@ -108,6 +111,31 @@ function Body() { | ||
if (blobSupport) { | ||
if (support.blob) { | ||
this._initBody = function(body) { | ||
this._bodyInit = body | ||
if (typeof body === 'string') { | ||
this._bodyText = body | ||
} else if (support.blob && Blob.prototype.isPrototypeOf(body)) { | ||
this._bodyBlob = body | ||
} else if (support.formData && FormData.prototype.isPrototypeOf(body)) { | ||
this._bodyFormData = body | ||
} else if (!body) { | ||
this._bodyText = '' | ||
} else { | ||
throw new Error('unsupported BodyInit type') | ||
} | ||
} | ||
this.blob = function() { | ||
var rejected = consumed(this) | ||
return rejected ? rejected : Promise.resolve(this._bodyBlob) | ||
if (rejected) { | ||
return rejected | ||
} | ||
if (this._bodyBlob) { | ||
return Promise.resolve(this._bodyBlob) | ||
} else if (this._bodyFormData) { | ||
throw new Error('could not read FormData body as blob') | ||
} else { | ||
return Promise.resolve(new Blob([this._bodyText])) | ||
} | ||
} | ||
@@ -120,5 +148,29 @@ | ||
this.text = function() { | ||
return this.blob().then(readBlobAsText) | ||
var rejected = consumed(this) | ||
if (rejected) { | ||
return rejected | ||
} | ||
if (this._bodyBlob) { | ||
return readBlobAsText(this._bodyBlob) | ||
} else if (this._bodyFormData) { | ||
throw new Error('could not read FormData body as text') | ||
} else { | ||
return Promise.resolve(this._bodyText) | ||
} | ||
} | ||
} else { | ||
this._initBody = function(body) { | ||
this._bodyInit = body | ||
if (typeof body === 'string') { | ||
this._bodyText = body | ||
} else if (support.formData && FormData.prototype.isPrototypeOf(body)) { | ||
this._bodyFormData = body | ||
} else if (!body) { | ||
this._bodyText = '' | ||
} else { | ||
throw new Error('unsupported BodyInit type') | ||
} | ||
} | ||
this.text = function() { | ||
@@ -130,3 +182,3 @@ var rejected = consumed(this) | ||
if ('FormData' in self) { | ||
if (support.formData) { | ||
this.formData = function() { | ||
@@ -155,3 +207,3 @@ return this.text().then(decode) | ||
this.url = url | ||
this._body = options.body | ||
this.credentials = options.credentials || 'omit' | ||
@@ -162,2 +214,7 @@ this.headers = new Headers(options.headers) | ||
this.referrer = null | ||
if ((this.method === 'GET' || this.method === 'HEAD') && options.body) { | ||
throw new TypeError('Body not allowed for GET or HEAD requests') | ||
} | ||
this._initBody(options.body) | ||
} | ||
@@ -195,2 +252,5 @@ | ||
var xhr = new XMLHttpRequest() | ||
if (self.credentials === 'cors') { | ||
xhr.withCredentials = true; | ||
} | ||
@@ -230,4 +290,4 @@ function responseURL() { | ||
xhr.open(self.method, self.url) | ||
if ('responseType' in xhr && blobSupport) { | ||
xhr.open(self.method, self.url, true) | ||
if ('responseType' in xhr && support.blob) { | ||
xhr.responseType = 'blob' | ||
@@ -242,3 +302,3 @@ } | ||
xhr.send((self._body === undefined) ? null : self._body) | ||
xhr.send(typeof self._bodyInit === 'undefined' ? null : self._bodyInit) | ||
}) | ||
@@ -254,11 +314,3 @@ } | ||
if (blobSupport) { | ||
if (typeof bodyInit === 'string') { | ||
this._bodyBlob = new Blob([bodyInit]) | ||
} else { | ||
this._bodyBlob = bodyInit | ||
} | ||
} else { | ||
this._bodyText = bodyInit | ||
} | ||
this._initBody(bodyInit) | ||
this.type = 'default' | ||
@@ -265,0 +317,0 @@ this.url = null |
{ | ||
"name": "whatwg-fetch", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"main": "fetch.js", | ||
"repository": "github/fetch", | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/github/fetch/blob/master/LICENSE" | ||
} | ||
], | ||
"devDependencies": { | ||
@@ -6,0 +13,0 @@ "bower": "1.3.8", |
@@ -21,3 +21,3 @@ # window.fetch polyfill | ||
This can be also be installed with `npm`. | ||
This can also be installed with `npm`. | ||
@@ -24,0 +24,0 @@ ```sh |
@@ -101,3 +101,3 @@ #!/usr/bin/env node | ||
var params = querystring.parse(url.parse(req.url).query); | ||
if (params.value && params.value) { | ||
if (params.name && params.value) { | ||
setCookie = [params.name, params.value].join('='); | ||
@@ -104,0 +104,0 @@ } |
102
test/test.js
@@ -70,2 +70,42 @@ function readBlobAsText(blob) { | ||
}) | ||
test('construct with url', function() { | ||
var request = new Request('https://fetch.spec.whatwg.org/') | ||
assert.equal(request.url, 'https://fetch.spec.whatwg.org/') | ||
}) | ||
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract | ||
suite('BodyInit extract', function() { | ||
;(Request.prototype.blob ? suite : suite.skip)('type Blob', function() { | ||
test('consume as blob', function() { | ||
var request = new Request(null, {method: 'POST', body: new Blob(['hello'])}) | ||
return request.blob().then(readBlobAsText).then(function(text) { | ||
assert.equal(text, 'hello') | ||
}) | ||
}) | ||
test('consume as text', function() { | ||
var request = new Request(null, {method: 'POST', body: new Blob(['hello'])}) | ||
return request.text().then(function(text) { | ||
assert.equal(text, 'hello') | ||
}) | ||
}) | ||
}) | ||
suite('type USVString', function() { | ||
test('consume as text', function() { | ||
var request = new Request(null, {method: 'POST', body: 'hello'}) | ||
return request.text().then(function(text) { | ||
assert.equal(text, 'hello') | ||
}) | ||
}) | ||
;(Request.prototype.blob ? test : test.skip)('consume as blob', function() { | ||
var request = new Request(null, {method: 'POST', body: 'hello'}) | ||
return request.blob().then(readBlobAsText).then(function(text) { | ||
assert.equal(text, 'hello') | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
@@ -80,3 +120,3 @@ | ||
var response = new Response(new Blob(['hello'])) | ||
response.blob().then(readBlobAsText).then(function(text) { | ||
return response.blob().then(readBlobAsText).then(function(text) { | ||
assert.equal(text, 'hello') | ||
@@ -88,3 +128,3 @@ }) | ||
var response = new Response(new Blob(['hello'])) | ||
response.text().then(function(text) { | ||
return response.text().then(function(text) { | ||
assert.equal(text, 'hello') | ||
@@ -98,3 +138,3 @@ }) | ||
var response = new Response('hello') | ||
response.text().then(function(text) { | ||
return response.text().then(function(text) { | ||
assert.equal(text, 'hello') | ||
@@ -106,3 +146,3 @@ }) | ||
var response = new Response('hello') | ||
response.blob().then(readBlobAsText).then(function(text) { | ||
return response.blob().then(readBlobAsText).then(function(text) { | ||
assert.equal(text, 'hello') | ||
@@ -327,3 +367,3 @@ }) | ||
assert.equal(response.bodyUsed, false) | ||
response.json() | ||
response.text() | ||
assert.equal(response.bodyUsed, true) | ||
@@ -343,3 +383,2 @@ return response.text() | ||
method: 'get', | ||
body: 'name=Hubot' | ||
}).then(function(response) { | ||
@@ -352,3 +391,22 @@ return response.json() | ||
}) | ||
test('supports HTTP POST', function() { | ||
test('GET with body throws TypeError', function() { | ||
assert.throw(function() { | ||
new Request('', { | ||
method: 'get', | ||
body: 'invalid' | ||
}) | ||
}, TypeError) | ||
}) | ||
test('HEAD with body throws TypeError', function() { | ||
assert.throw(function() { | ||
new Request('', { | ||
method: 'head', | ||
body: 'invalid' | ||
}) | ||
}, TypeError) | ||
}) | ||
test('supports HTTP POST', function() { | ||
return fetch('/request', { | ||
@@ -462,2 +520,6 @@ method: 'post', | ||
setup(function() { | ||
return fetch('/cookie?name=foo&value=reset', {credentials: 'same-origin'}); | ||
}) | ||
;(omitSupported ? suite : suite.skip)('omit', function() { | ||
@@ -469,4 +531,24 @@ test('request credentials defaults to omit', function() { | ||
test('does not accept cookies with implicit omit credentials', function() { | ||
return fetch('/cookie?name=foo&value=bar').then(function() { | ||
return fetch('/cookie?name=foo', {credentials: 'same-origin'}); | ||
}).then(function(response) { | ||
return response.text() | ||
}).then(function(data) { | ||
assert.equal(data, 'reset') | ||
}) | ||
}) | ||
test('does not accept cookies with omit credentials', function() { | ||
return fetch('/cookie?name=foo&value=bar', {credentials: 'omit'}).then(function() { | ||
return fetch('/cookie?name=foo', {credentials: 'same-origin'}); | ||
}).then(function(response) { | ||
return response.text() | ||
}).then(function(data) { | ||
assert.equal(data, 'reset') | ||
}) | ||
}) | ||
test('does not send cookies with implicit omit credentials', function() { | ||
return fetch('/cookie?name=foo&value=bar').then(function() { | ||
return fetch('/cookie?name=foo&value=bar', {credentials: 'same-origin'}).then(function() { | ||
return fetch('/cookie?name=foo'); | ||
@@ -498,3 +580,3 @@ }).then(function(response) { | ||
test('send cookies with same-origin credentials', function() { | ||
return fetch('/cookie?name=foo&value=bar').then(function() { | ||
return fetch('/cookie?name=foo&value=bar', {credentials: 'same-origin'}).then(function() { | ||
return fetch('/cookie?name=foo', {credentials: 'same-origin'}) | ||
@@ -511,3 +593,3 @@ }).then(function(response) { | ||
test('send cookies with include credentials', function() { | ||
return fetch('/cookie?name=foo&value=bar').then(function() { | ||
return fetch('/cookie?name=foo&value=bar', {credentials: 'include'}).then(function() { | ||
return fetch('/cookie?name=foo', {credentials: 'include'}) | ||
@@ -514,0 +596,0 @@ }).then(function(response) { |
Sorry, the diff of this file is not supported yet
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 repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
43001
990
48