Skip to content

Commit

Permalink
feat: add AbortController
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed May 11, 2022
1 parent fb27159 commit 9942b41
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -15,7 +15,7 @@ A redistribution of [node-fetch v3](https://github.com/node-fetch/node-fetch) fo

**Features:**

✅ Prefer to **native globals** when available (`fetch`, `Blob`, `File`, `FormData`, `Headers`, `Request`, and `Response`) (See Node.js [experimental fetch](https://nodejs.org/dist/latest-v17.x/docs/api/cli.html#--experimental-fetch))
✅ Prefer to **native globals** when available (See Node.js [experimental fetch](https://nodejs.org/dist/latest-v17.x/docs/api/cli.html#--experimental-fetch))

✅ Compact build and less install size with **zero dependencies** [![][packagephobia-s-src]][packagephobia-s-href] <sup>vs</sup> [![][packagephobia-s-alt-src]][packagephobia-s-alt-href]

Expand Down Expand Up @@ -54,10 +54,10 @@ More named exports:

```js
// ESM
import { fetch, Blob, FormData, Headers, Request, Response } from 'node-fetch-native'
import { fetch, Blob, FormData, Headers, Request, Response, AbortController } from 'node-fetch-native'

// CommonJS
const { fetch, Blob, FormData, Headers, Request, Response } = require('node-fetch-native')
const { fetch, Blob, FormData, Headers, Request, Response, AbortController } = require('node-fetch-native')
```

## Polyfill support
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -40,6 +40,7 @@
},
"devDependencies": {
"@nuxtjs/eslint-config-typescript": "latest",
"abort-controller": "^3.0.0",
"c8": "latest",
"eslint": "latest",
"node-fetch": "^3.2.4",
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/index.ts
Expand Up @@ -7,6 +7,8 @@ import _fetch, {
Response as _Response
} from 'node-fetch'

import _AbortController from 'abort-controller'

export const fetch = globalThis.fetch || _fetch
export default fetch

Expand All @@ -16,6 +18,7 @@ export const FormData = globalThis.FormData || _FormData
export const Headers = globalThis.Headers || _Headers
export const Request = globalThis.Request || _Request
export const Response = globalThis.Response || _Response
export const AbortController = globalThis.AbortController || _AbortController

export {
AbortError,
Expand Down
1 change: 1 addition & 0 deletions src/native.ts
Expand Up @@ -4,6 +4,7 @@ export const FormData = globalThis.FormData
export const Headers = globalThis.Headers
export const Request = globalThis.Request
export const Response = globalThis.Response
export const AbortController = globalThis.AbortController

export const fetch = globalThis.fetch || (() => { throw new Error('global fetch is not available!') })
export default fetch
3 changes: 3 additions & 0 deletions src/polyfill.ts
Expand Up @@ -7,6 +7,8 @@ import _fetch, {
Response as _Response
} from 'node-fetch'

import _AbortController from 'abort-controller'

globalThis.fetch = globalThis.fetch || _fetch as unknown as typeof globalThis.fetch

globalThis.Blob = globalThis.Blob || _Blob
Expand All @@ -15,3 +17,4 @@ 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
globalThis.AbortController = globalThis.AbortController || _AbortController as unknown as typeof globalThis.AbortController
2 changes: 1 addition & 1 deletion test/index.test.ts
Expand Up @@ -9,7 +9,7 @@ import defaultESM from '../dist/index.mjs'
const require = createRequire(import.meta.url)
const libCJS = require('../lib/index.cjs')

const expectedExports = ['fetch', 'Blob', 'FormData', 'Headers', 'Request', 'Response']
const expectedExports = ['fetch', 'Blob', 'FormData', 'Headers', 'Request', 'Response', 'AbortController']

const suites = [
{ name: 'cjs', defaultExport: libCJS, exports: libCJS },
Expand Down

0 comments on commit 9942b41

Please sign in to comment.