logaretm / villus

🏎 A tiny and fast GraphQL client for Vue.js
https://villus.dev
MIT License
791 stars 31 forks source link

In Service Worker: Could not resolve a fetch() method #160

Closed bravik closed 2 years ago

bravik commented 2 years ago

Tried to use villus to make a query in a service worker:

villus.esm.js:236 Uncaught Error: Could not resolve a fetch() method, you should provide one.
    at fetch (villus.esm.js:236:15)
    at defaultPlugins (villus.esm.js:337:49)
    at index.ts:22:12

villus.esm.js:

function fetch(opts) {
    const fetch = (opts === null || opts === void 0 ? void 0 : opts.fetch) || resolveGlobalFetch();
    if (!fetch) {
        throw new Error('Could not resolve a fetch() method, you should provide one.');
    }

Maybe self should be also checked here for workers:

  function resolveGlobalFetch() {
      if (typeof window !== 'undefined' && 'fetch' in window && window.fetch) {
          return window.fetch.bind(window);
      }
      if (typeof global !== 'undefined' && 'fetch' in global) {
          return global.fetch;
      }
      return undefined;
  }
logaretm commented 2 years ago

closed by #161