Skip to content

Commit

Permalink
Support Request being passed to fetch()
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed May 4, 2015
1 parent f5dc22e commit ab1ac93
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
12 changes: 9 additions & 3 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
return head
}

Request.prototype.fetch = function() {
Request.prototype._fetch = function() {
var self = this

return new Promise(function(resolve, reject) {
Expand Down Expand Up @@ -325,8 +325,14 @@
self.Request = Request;
self.Response = Response;

self.fetch = function (url, options) {
return new Request(url, options).fetch()
self.fetch = function (input, init) {
var request
if (Request.prototype.isPrototypeOf(input)) {
request = input
} else {
request = new Request(input, init)
}
return request._fetch()
}
self.fetch.polyfill = true
})();
16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ suite('Request', function() {
})
})

test('fetch request', function() {
var request = new Request('/request', {
headers: {
'Accept': 'application/json',
'X-Test': '42'
}
})

return fetch(request).then(function(response) {
return response.json()
}).then(function(json) {
assert.equal(json.headers['accept'], 'application/json')
assert.equal(json.headers['x-test'], '42')
})
})

test('construct with url', function() {
var request = new Request('https://fetch.spec.whatwg.org/')
assert.equal(request.url, 'https://fetch.spec.whatwg.org/')
Expand Down

0 comments on commit ab1ac93

Please sign in to comment.