From fb271590eaeeecbcaadc11e3a999df0830ec42f4 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 11 May 2022 22:48:17 +0200 Subject: [PATCH] feat: polyfill support --- README.md | 19 ++++++++++++++++++- build.config.ts | 3 ++- lib/empty.mjs | 0 package.json | 10 ++++++++-- src/polyfill.ts | 17 +++++++++++++++++ 5 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 lib/empty.mjs create mode 100644 src/polyfill.ts diff --git a/README.md b/README.md index 73876f5..160ccd2 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ A redistribution of [node-fetch v3](https://github.com/node-fetch/node-fetch) fo ✅ Use native version if imported without `node` condition using [conditional exports](https://nodejs.org/api/packages.html#packages_conditional_exports) with **zero bundle overhead** +✅ Polyfill support for Node.js ## Usage @@ -49,7 +50,7 @@ import fetch from 'node-fetch-native' const fetch = require('node-fetch-native') ``` -Other exports: +More named exports: ```js // ESM @@ -59,6 +60,22 @@ import { fetch, Blob, FormData, Headers, Request, Response } from 'node-fetch-na const { fetch, Blob, FormData, Headers, Request, Response } = require('node-fetch-native') ``` +## Polyfill support + +Using the polyfill method, we can once ensure global fetch is available in the environment and all files. Natives are always preferred. + +**Note:** I don't recommand this if you are authoring a library! Please prefer explicit methods. + +```js +// ESM +import 'node-fetch-native/polyfill' + +// CJS +require('node-fetch-native/polyfill') + +// You can now use fetch() without any import! +``` + ## Alias to `node-fetch` Using this method, you can ensure all project dependencies and usages of `node-fetch` can benefit from improved `node-fetch-native` and won't conflict between `node-fetch@2` and `node-fetch@3`. diff --git a/build.config.ts b/build.config.ts index 329d0a9..7dee359 100644 --- a/build.config.ts +++ b/build.config.ts @@ -8,6 +8,7 @@ export default defineBuildConfig({ }, entries: [ 'src/index', - 'src/native' + 'src/native', + 'src/polyfill' ] }) diff --git a/lib/empty.mjs b/lib/empty.mjs new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json index 5f525e2..d673b74 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,9 @@ { "name": "node-fetch-native", "version": "0.1.1", - "description": "", + "description": "A better redistribution of `node-fetch`", "repository": "unjs/node-fetch-native", "license": "MIT", - "sideEffects": false, "type": "module", "exports": { ".": { @@ -14,6 +13,13 @@ }, "import": "./dist/native.mjs" }, + "./polyfill": { + "node": { + "require": "./dist/polyfill.cjs", + "import": "./dist/polyfill.mjs" + }, + "import": "./lib/empty.mjs" + }, "./src/index.js": { "import": "./dist/index.mjs" } diff --git a/src/polyfill.ts b/src/polyfill.ts new file mode 100644 index 0000000..bf0497f --- /dev/null +++ b/src/polyfill.ts @@ -0,0 +1,17 @@ +import _fetch, { + Blob as _Blob, + File as _File, + FormData as _FormData, + Headers as _Headers, + Request as _Request, + Response as _Response +} from 'node-fetch' + +globalThis.fetch = globalThis.fetch || _fetch as unknown as typeof globalThis.fetch + +globalThis.Blob = globalThis.Blob || _Blob +globalThis.File = globalThis.File || _File +globalThis.FormData = globalThis.FormData || _FormData +globalThis.Headers = globalThis.Headers || _Headers +globalThis.Request = globalThis.Request || _Request as unknown as typeof globalThis.Request +globalThis.Response = globalThis.Response || _Response as unknown as typeof globalThis.Response