Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

native-fetch

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

native-fetch - npm Package Compare versions

Comparing version 3.0.0 to 4.0.0

dist/index.min.js

16

dist/src/index.d.ts
declare const _exports: {
default: typeof fetch;
fetch: typeof fetch;
Headers: {
new (init?: Headers | string[][] | Record<string, string> | undefined): Headers;
new (init?: HeadersInit | undefined): Headers;
prototype: Headers;

@@ -12,14 +12,14 @@ };

Response: {
new (body?: string | Blob | ArrayBufferView | ArrayBuffer | FormData | URLSearchParams | ReadableStream<Uint8Array> | null | undefined, init?: ResponseInit | undefined): Response;
new (body?: BodyInit | null | undefined, init?: ResponseInit | undefined): Response;
prototype: Response;
error(): Response;
redirect(url: string, status?: number | undefined): Response;
redirect(url: string | URL, status?: number | undefined): Response;
};
} | {
default: typeof import("node-fetch").default;
Headers: typeof import("node-fetch").Headers;
Request: typeof import("node-fetch").Request;
Response: typeof import("node-fetch").Response;
fetch: typeof import("undici").fetch;
Headers: typeof import("undici").Headers;
Request: typeof import("undici").Request;
Response: typeof import("undici").Response;
};
export = _exports;
//# sourceMappingURL=index.d.ts.map
{
"name": "native-fetch",
"version": "3.0.0",
"description": "Returns native fetch if available or the node-fetch module if not",
"version": "4.0.0",
"description": "Returns native fetch if available or the undici module if not",
"main": "src/index.js",

@@ -14,3 +14,3 @@ "types": "dist/src/index.d.ts",

"lint": "aegir lint && aegir ts -p check",
"prepare": "aegir build --no-bundle",
"build": "aegir build --no-bundle",
"release": "aegir release --docs false",

@@ -27,8 +27,7 @@ "release-minor": "aegir release --type minor --docs false",

"peerDependencies": {
"node-fetch": "*"
"undici": "*"
},
"devDependencies": {
"@types/node-fetch": "^2.5.8",
"aegir": "^30.3.0",
"node-fetch": "^2.6.0"
"aegir": "^36.0.0",
"undici": "^4.10.0"
},

@@ -35,0 +34,0 @@ "contributors": [

# native-fetch
> Returns native fetch/Request/Headers if available or the node-fetch module if not
> Returns native fetch/Request/Headers if available or the `undici` module if not
An (almost) drop-in replacement for the `node-fetch` module that returns the native fetch if available or the polyfill if not.
An (almost) drop-in replacement for the `undici` module that returns the native fetch if available or the polyfill if not.

@@ -13,8 +13,16 @@ ### Why?

### Why Undici and not node-fetch?
[node-fetch](https://www.npmjs.com/package/node-fetch) is the OG fetch implementation for node, but it uses [Node.js streams](https://nodejs.org/api/stream.html) instead of [WHATWG streams](https://streams.spec.whatwg.org/). This means the APIs are not the same which leads to all sorts of weird shenanigans with types.
[undici](https://www.npmjs.com/package/undici) is the new kid on the block and uses WHATWG streams so all of the APIs now live in glorious harmony.
If you are trying to write polymorphic code with strong typing this is a big deal.
## Install
You must install a version of `node-fetch` [alongside this module](https://docs.npmjs.com/files/package.json#peerdependencies) to be used if a native implementation is not available.
You must install a version of `undici` [alongside this module](https://docs.npmjs.com/files/package.json#peerdependencies) to be used if a native implementation is not available.
```console
$ npm install --save native-fetch node-fetch
$ npm install --save native-fetch undici
```

@@ -25,3 +33,3 @@

```javascript
const { default: fetch } = require('native-fetch')
const { fetch } = require('native-fetch')

@@ -28,0 +36,0 @@ fetch('https://github.com/')

'use strict'
// @ts-expect-error globalThis.fetch is defined according to the env types but it's not in node
if (globalThis.fetch && globalThis.Headers && globalThis.Request && globalThis.Response) {
module.exports = {
default: globalThis.fetch,
fetch: globalThis.fetch,
Headers: globalThis.Headers,

@@ -12,7 +13,7 @@ Request: globalThis.Request,

module.exports = {
default: require('node-fetch').default,
Headers: require('node-fetch').Headers,
Request: require('node-fetch').Request,
Response: require('node-fetch').Response
fetch: require('undici').fetch,
Headers: require('undici').Headers,
Request: require('undici').Request,
Response: require('undici').Response
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc