CICCIOSGAMINO / openweather-apis

Simple APIs for OpenWeatherMap.org basic services
GNU General Public License v3.0
44 stars 27 forks source link

Support for browser (and react-native) #32

Closed namnm closed 3 years ago

namnm commented 4 years ago

Instead of using http/https package, we can use browser friendly package like fetch, axios, request... All those packages should work with Node and have ReactNative bindings.

CICCIOSGAMINO commented 3 years ago

I'm thinking about for the v5 ! I'm actually checking to use fetch API that is not Node.js supported ... so do you have idea for simple API that can works in both environment (Node.js and Browser) without changes ??

namnm commented 3 years ago

@CICCIOSGAMINO Axios is the way https://github.com/axios/axios

CICCIOSGAMINO commented 3 years ago

I'm thinking at a function like this to init the right fetch .... with the use of node-fetch when in Node.js ...


  async initFetch () {
    const isBrowser = new Function('try {return this===window;}catch(e){ return false;}')
    const isNode = new Function('try {return this===global;}catch(e){return false;}')
    // tests if global scope is binded to window
    if (isBrowser()) {
      console.log('Running under browser')
    }
    if (isNode()) {
      console.log('running under node.js')
      nodeFetch = await import('node-fetch')
      fetch = nodeFetch.default
    }
  }```
namnm commented 3 years ago

Yes this one can be a minimal set of changes, fetch is supported by default in browser and react-native.

CICCIOSGAMINO commented 3 years ago

Supported in v5.0.0-pre