Workaround lack of global context in react-native (#545)

This commit is contained in:
David Frank 2018-11-13 12:43:27 +08:00 committed by GitHub
parent ecd3d52c55
commit d1ca2dfbb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 5 deletions

View File

@ -5,6 +5,10 @@ Changelog
# 2.x release
## master
- Fix: update `browser.js` to support react-native, where `self` isn't available.
## v2.2.1
- Fix: `compress` flag shouldn't overwrite existing `Accept-Encoding` header.

View File

@ -1,10 +1,23 @@
"use strict";
module.exports = exports = self.fetch;
// ref: https://github.com/tc39/proposal-global
var getGlobal = function () {
// the only reliable means to get the global object is
// `Function('return this')()`
// However, this causes CSP violations in Chrome apps.
if (typeof self !== 'undefined') { return self; }
if (typeof window !== 'undefined') { return window; }
if (typeof global !== 'undefined') { return global; }
throw new Error('unable to locate global object');
}
var global = getGlobal();
module.exports = exports = global.fetch;
// Needed for TypeScript and Webpack.
exports.default = self.fetch.bind(self);
exports.default = global.fetch.bind(global);
exports.Headers = self.Headers;
exports.Request = self.Request;
exports.Response = self.Response;
exports.Headers = global.Headers;
exports.Request = global.Request;
exports.Response = global.Response;