Skip to content

Commit

Permalink
Remove private Request._fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed May 4, 2015
1 parent ab1ac93 commit 60f902d
Showing 1 changed file with 35 additions and 39 deletions.
74 changes: 35 additions & 39 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,41 @@
return head
}

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

function Response(bodyInit, options) {
if (!options) {
options = {}
}

this._initBody(bodyInit)
this.type = 'default'
this.url = null
this.status = options.status
this.ok = this.status >= 200 && this.status < 300
this.statusText = options.statusText
this.headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers)
this.url = options.url || ''
}

Body.call(Response.prototype)

self.Headers = Headers;
self.Request = Request;
self.Response = Response;

self.fetch = function(input, init) {
// TODO: Request constructor should accept input, init
var request
if (Request.prototype.isPrototypeOf(input) && !init) {
request = input
} else {
request = new Request(input, init)
}

return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest()
if (self.credentials === 'cors') {
if (request.credentials === 'cors') {
xhr.withCredentials = true;
}

Expand Down Expand Up @@ -286,53 +315,20 @@
reject(new TypeError('Network request failed'))
}

xhr.open(self.method, self.url, true)
xhr.open(request.method, request.url, true)

if ('responseType' in xhr && support.blob) {
xhr.responseType = 'blob'
}

self.headers.forEach(function(name, values) {
request.headers.forEach(function(name, values) {
values.forEach(function(value) {
xhr.setRequestHeader(name, value)
})
})

xhr.send(typeof self._bodyInit === 'undefined' ? null : self._bodyInit)
xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit)
})
}

Body.call(Request.prototype)

function Response(bodyInit, options) {
if (!options) {
options = {}
}

this._initBody(bodyInit)
this.type = 'default'
this.url = null
this.status = options.status
this.ok = this.status >= 200 && this.status < 300
this.statusText = options.statusText
this.headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers)
this.url = options.url || ''
}

Body.call(Response.prototype)

self.Headers = Headers;
self.Request = Request;
self.Response = Response;

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
})();

0 comments on commit 60f902d

Please sign in to comment.