From a873702c336c7ecce87c506d81c146db9f7516d0 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 9 Dec 2020 14:44:40 +0100 Subject: [PATCH] feat: universal + isomorphic builds --- README.md | 8 ++++---- package.json | 8 ++------ src/browser.ts | 7 ------- src/index.ts | 17 +++++++++++++++++ 4 files changed, 23 insertions(+), 17 deletions(-) delete mode 100644 src/browser.ts create mode 100644 src/index.ts diff --git a/README.md b/README.md index 1f787ed8..76be06d4 100644 --- a/README.md +++ b/README.md @@ -22,13 +22,13 @@ yarn add ohmyfetch Import: ```js -// Browser / Workers -import { $fetch } from 'ohmyfetch/browser' +// Universal (requires global.fetch) +import { $fetch } from 'ohmyfetch' -// NodeJS +// NodeJS / Isomorphic import { $fetch } from 'ohmyfetch/node' -// NodeJS (commonjs) +// NodeJS / Isomorphic (CommonJS) const { $fetch } = require('ohmyfetch/node') ``` diff --git a/package.json b/package.json index 049ac059..b6907a2e 100644 --- a/package.json +++ b/package.json @@ -9,14 +9,10 @@ "node": { "import": "./node.mjs", "require": "./node.js" - }, - "browser": { - "import": "./browser.mjs", - "require": "./browser.js" } }, - "main": "./dist/node.js", - "module": "./dist/node.mjs", + "main": "./dist/index.js", + "module": "./dist/index.mjs", "types": "./dist/index.d.ts", "files": [ "dist" diff --git a/src/browser.ts b/src/browser.ts deleted file mode 100644 index 391eb620..00000000 --- a/src/browser.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { createFetch } from './base' - -export * from './base' - -export const $fetch = createFetch({ - fetch: globalThis.fetch -}) diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 00000000..bb6d1ef4 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,17 @@ +import { createFetch } from './base' + +export * from './base' + +// ref: https://github.com/tc39/proposal-global +const getGlobal = function () { + 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') +} + +export const $fetch = createFetch({ + fetch: getGlobal().fetch || (() => { + return Promise.reject(new Error('[ohmyfetch] global.fetch is not supported!')) + }) +})