mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
Merge pull request #1346 from NativeScript/fetch-fix
fetch fixed to require XMLHttpRequest if not avaliable
This commit is contained in:
@ -1,10 +1,13 @@
|
|||||||
(function () {
|
(function () {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var self = exports;
|
exports.XMLHttpRequest = global.XMLHttpRequest;
|
||||||
|
exports.FormData = global.FormData;
|
||||||
|
|
||||||
if (self.fetch) {
|
if (!exports.XMLHttpRequest) {
|
||||||
return
|
var xhr = require("xhr");
|
||||||
|
exports.XMLHttpRequest = xhr.XMLHttpRequest;
|
||||||
|
exports.FormData = xhr.FormData;
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeName(name) {
|
function normalizeName(name) {
|
||||||
@ -110,7 +113,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
var support = {
|
var support = {
|
||||||
blob: 'FileReader' in global && 'Blob' in global && (function () {
|
blob: 'FileReader' in exports && 'Blob' in exports && (function () {
|
||||||
try {
|
try {
|
||||||
new Blob();
|
new Blob();
|
||||||
return true
|
return true
|
||||||
@ -118,7 +121,7 @@
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
})(),
|
})(),
|
||||||
formData: 'FormData' in global
|
formData: 'FormData' in exports
|
||||||
}
|
}
|
||||||
|
|
||||||
function Body() {
|
function Body() {
|
||||||
@ -131,7 +134,7 @@
|
|||||||
this._bodyText = body
|
this._bodyText = body
|
||||||
} else if (support.blob && Blob.prototype.isPrototypeOf(body)) {
|
} else if (support.blob && Blob.prototype.isPrototypeOf(body)) {
|
||||||
this._bodyBlob = body
|
this._bodyBlob = body
|
||||||
} else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
|
} else if (support.formData && exports.FormData.prototype.isPrototypeOf(body)) {
|
||||||
this._bodyFormData = body
|
this._bodyFormData = body
|
||||||
} else if (!body) {
|
} else if (!body) {
|
||||||
this._bodyText = ''
|
this._bodyText = ''
|
||||||
@ -219,7 +222,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function decode(body) {
|
function decode(body) {
|
||||||
var form = new FormData()
|
var form = new exports.FormData()
|
||||||
body.trim().split('&').forEach(function (bytes) {
|
body.trim().split('&').forEach(function (bytes) {
|
||||||
if (bytes) {
|
if (bytes) {
|
||||||
var split = bytes.split('=')
|
var split = bytes.split('=')
|
||||||
@ -262,11 +265,11 @@
|
|||||||
|
|
||||||
Body.call(Response.prototype)
|
Body.call(Response.prototype)
|
||||||
|
|
||||||
self.Headers = Headers;
|
exports.Headers = Headers;
|
||||||
self.Request = Request;
|
exports.Request = Request;
|
||||||
self.Response = Response;
|
exports.Response = Response;
|
||||||
|
|
||||||
self.fetch = function (input, init) {
|
exports.fetch = function (input, init) {
|
||||||
// TODO: Request constructor should accept input, init
|
// TODO: Request constructor should accept input, init
|
||||||
var request
|
var request
|
||||||
if (Request.prototype.isPrototypeOf(input) && !init) {
|
if (Request.prototype.isPrototypeOf(input) && !init) {
|
||||||
@ -276,7 +279,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
var xhr = new XMLHttpRequest()
|
var xhr = new exports.XMLHttpRequest()
|
||||||
|
|
||||||
function responseURL() {
|
function responseURL() {
|
||||||
if ('responseURL' in xhr) {
|
if ('responseURL' in xhr) {
|
||||||
@ -328,6 +331,6 @@
|
|||||||
xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit)
|
xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
self.fetch.polyfill = true
|
exports.fetch.polyfill = true
|
||||||
|
|
||||||
})();
|
})();
|
Reference in New Issue
Block a user