isomorphic-fetch icon indicating copy to clipboard operation
isomorphic-fetch copied to clipboard

should we be sniffing for `window` and binding `window.fetch` instead of using just `global` when using npm package?

Open niftylettuce opened this issue 9 years ago • 10 comments

updated title

niftylettuce avatar Nov 29 '15 16:11 niftylettuce

maybe you don't need this - because you defined browser in package.json, right?

niftylettuce avatar Nov 29 '15 17:11 niftylettuce

otherwise, this is how it'd need to be refactored I think?

-var realFetch = require('node-fetch');
+var realFetch = (typeof window === 'undefined')
+  ? require('node-fetch')
+  : require('whatwg-fetch');
+

-if (!global.fetch) {
-       global.fetch = module.exports;
-       global.Response = realFetch.Response;
-       global.Headers = realFetch.Headers;
-       global.Request = realFetch.Request;
+if (typeof window === 'undefined') {
+  if (!window.fetch) {
+    window.fetch = module.exports;
+    window.Response = realFetch.Response;
+    window.Headers = realFetch.Headers;
+    window.Request = realFetch.Request;
+  }
+} else {
+  if (!global.fetch) {
+    global.fetch = module.exports;
+    global.Response = realFetch.Response;
+    global.Headers = realFetch.Headers;
+    global.Request = realFetch.Request;
+  }
 }

niftylettuce avatar Nov 29 '15 17:11 niftylettuce

actually we need to - because if we use jsdomify then it injects window, I will submit PR

// create our DOM instance and expose `global.window`
// so that when we import Frisbee it will use `whatwg-fetch`
// instead of using `node-fetch` as the other test does
jsdomify.create();

import es6promise from 'es6-promise';
es6promise.polyfill();
import 'isomorphic-fetch';
import Frisbee from '../../src/frisbee';

console.log('window.fetch', window.fetch); // <-- returns undefined

niftylettuce avatar Nov 29 '15 18:11 niftylettuce

Don't do the else, just test for global and window separately...

if (typeof window !== 'undefined' && !window.fetch) {
  ...
}
if (typeof blobal !== 'undefined' && !window.fetch) {
  ...
}

tracker1 avatar Nov 29 '15 19:11 tracker1

why are you doing this btw?

module.exports = function(url, options) {
    if (/^\/\//.test(url)) {
        url = 'https:' + url;
    }
    return realFetch.call(this, url, options);
};

why are you forcing https on // paths? the browser auto detects // by checking if https or http is available on them?

niftylettuce avatar Nov 29 '15 19:11 niftylettuce

leave it up to user to put in the proper URL, why are you doing this explicitly? seems like people would eventually file issues reg'd this

niftylettuce avatar Nov 29 '15 19:11 niftylettuce

I agree with @niftylettuce . Push the burden/power of URL building onto the caller.

IMO // is an antipattern anyway, because web documents are not-infrequently served over non-http/https protocols (e.g. file:// in localdev, Apache Cordova, and more). But that doesn't mean you should change it to https.

gobengo avatar Nov 29 '15 19:11 gobengo

I removed this in my PR referenced On Nov 29, 2015 2:28 PM, "Benjamin Goering" [email protected] wrote:

I agree with @niftylettuce https://github.com/niftylettuce . Push the burden of URL building onto the caller.

IMO // is an antipattern anyway, because web documents are not-infrequently served over non-http/https protocols (e.g. file:// in localdev, Apache Cordova, and more). But that doesn't mean you should change it to https.

— Reply to this email directly or view it on GitHub https://github.com/matthew-andrews/isomorphic-fetch/issues/53#issuecomment-160455110 .

niftylettuce avatar Nov 29 '15 19:11 niftylettuce

don't merge yet - I have to fix tests, indentation to use tabs (sigh), and also fix something else

niftylettuce avatar Nov 29 '15 19:11 niftylettuce

OK pull request is ready!

niftylettuce avatar Nov 29 '15 23:11 niftylettuce