Skip to content

Commit

Permalink
fetch fixed to require XMLHttpRequest if not avaliable
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Enchev committed Jan 11, 2016
1 parent 3713097 commit 51145b7
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions fetch/fetch.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
(function () {
'use strict';

var self = exports;
exports.XMLHttpRequest = global.XMLHttpRequest;
exports.FormData = global.FormData;

if (self.fetch) {
return
if (!exports.XMLHttpRequest) {
var xhr = require("xhr");
exports.XMLHttpRequest = xhr.XMLHttpRequest;
exports.FormData = xhr.FormData;
}

function normalizeName(name) {
Expand Down Expand Up @@ -110,15 +113,15 @@
}

var support = {
blob: 'FileReader' in global && 'Blob' in global && (function () {
blob: 'FileReader' in exports && 'Blob' in exports && (function () {
try {
new Blob();
return true
} catch (e) {
return false
}
})(),
formData: 'FormData' in global
formData: 'FormData' in exports
}

function Body() {
Expand All @@ -131,7 +134,7 @@
this._bodyText = body
} else if (support.blob && Blob.prototype.isPrototypeOf(body)) {
this._bodyBlob = body
} else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
} else if (support.formData && exports.FormData.prototype.isPrototypeOf(body)) {
this._bodyFormData = body
} else if (!body) {
this._bodyText = ''
Expand Down Expand Up @@ -219,7 +222,7 @@
}

function decode(body) {
var form = new FormData()
var form = new exports.FormData()
body.trim().split('&').forEach(function (bytes) {
if (bytes) {
var split = bytes.split('=')
Expand Down Expand Up @@ -262,11 +265,11 @@

Body.call(Response.prototype)

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

self.fetch = function (input, init) {
exports.fetch = function (input, init) {
// TODO: Request constructor should accept input, init
var request
if (Request.prototype.isPrototypeOf(input) && !init) {
Expand All @@ -276,7 +279,7 @@
}

return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest()
var xhr = new exports.XMLHttpRequest()

function responseURL() {
if ('responseURL' in xhr) {
Expand Down Expand Up @@ -328,6 +331,6 @@
xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit)
})
}
self.fetch.polyfill = true
exports.fetch.polyfill = true

})();

0 comments on commit 51145b7

Please sign in to comment.