Skip to content

Commit

Permalink
feat: polyfill support
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed May 11, 2022
1 parent 78eff16 commit fb27159
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
19 changes: 18 additions & 1 deletion README.md
Expand Up @@ -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

Expand All @@ -49,7 +50,7 @@ import fetch from 'node-fetch-native'
const fetch = require('node-fetch-native')
```

Other exports:
More named exports:

```js
// ESM
Expand All @@ -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`.
Expand Down
3 changes: 2 additions & 1 deletion build.config.ts
Expand Up @@ -8,6 +8,7 @@ export default defineBuildConfig({
},
entries: [
'src/index',
'src/native'
'src/native',
'src/polyfill'
]
})
Empty file added lib/empty.mjs
Empty file.
10 changes: 8 additions & 2 deletions 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": {
".": {
Expand All @@ -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"
}
Expand Down
17 changes: 17 additions & 0 deletions 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

0 comments on commit fb27159

Please sign in to comment.