node-fetch-native
Advanced tools
Comparing version 1.5.1 to 1.6.0
{ | ||
"name": "node-fetch-native", | ||
"version": "1.5.1", | ||
"version": "1.6.0", | ||
"description": "better fetch for Node.js. Works on any JavaScript runtime!", | ||
@@ -85,7 +85,7 @@ "repository": "unjs/node-fetch-native", | ||
"import": { | ||
"types": "./lib/proxy.d.mts", | ||
"types": "./lib/proxy.d.ts", | ||
"default": "./dist/proxy.cjs" | ||
}, | ||
"require": { | ||
"types": "./lib/proxy.d.cts", | ||
"types": "./lib/proxy.d.ts", | ||
"default": "./dist/proxy.cjs" | ||
@@ -96,7 +96,7 @@ } | ||
"import": { | ||
"types": "./lib/proxy.d.mts", | ||
"types": "./lib/proxy.d.ts", | ||
"default": "./dist/proxy-stub.mjs" | ||
}, | ||
"require": { | ||
"types": "./lib/proxy.d.cts", | ||
"types": "./lib/proxy.d.ts", | ||
"default": "./dist/proxy-stub.cjs" | ||
@@ -103,0 +103,0 @@ } |
@@ -1,1 +0,1 @@ | ||
export * from "./lib/proxy.mjs"; | ||
export * from "./lib/proxy"; |
@@ -89,3 +89,3 @@ # node-fetch-native | ||
- Set `FORCE_NODE_FETCH` environment variable before starting the application. | ||
- Set the `FORCE_NODE_FETCH` environment variable before starting the application. | ||
- Import from `node-fetch-native/node` | ||
@@ -97,3 +97,3 @@ | ||
**Note:** I don't recommend this if you are authoring a library! Please prefer explicit methods. | ||
**Note:** I don't recommend this if you are authoring a library! Please prefer the explicit methods. | ||
@@ -114,30 +114,50 @@ ```js | ||
This package bundles a compact and simple proxy supported for both Node.js versions without native fetch using [HTTP Agent](https://github.com/TooTallNate/proxy-agents/tree/main/packages/proxy-agent) and versions with native fetch using [Undici Proxy Agent](https://undici.nodejs.org/#/docs/api/ProxyAgent). | ||
This package bundles a compact and simple proxy-supported solution for both Node.js versions without native fetch using [HTTP Agent](https://github.com/TooTallNate/proxy-agents/tree/main/packages/proxy-agent) and versions with native fetch using [Undici Proxy Agent](https://undici.nodejs.org/#/docs/api/ProxyAgent). | ||
**Usage:** | ||
By default, `https_proxy`, `http_proxy`, `HTTPS_PROXY`, and `HTTP_PROXY` environment variables will be checked and used (in order) for the proxy and if not any of them are set, the proxy will be disabled. You can override it using the `url` option passed to `createFetch` and `createProxy` utils. | ||
By default, `no_proxy` and `NO_PROXY` environment variables will be checked and used for the (comma-separated) list of hosts to ignore the proxy for. You can override it using the `noProxy` option passed to `createFetch` and `createProxy` utils. The entries starting with a dot will be used to check the domain and also any subdomain. | ||
> [!NOTE] | ||
> Using export conditions, this utility adds proxy support for Node.js and for other runtimes, it will simply return native fetch. | ||
> [!IMPORTANT] | ||
> Proxy support is under development. Check [unjs/node-fetch-native#107](https://github.com/unjs/node-fetch-native/issues/107) for the roadmap and contributing! | ||
### `fetch` with proxy support | ||
You can simply import `{ fetch }` from `node-fetch-native/proxy` with a preconfigured `fetch` function that has proxy support. | ||
```ts | ||
import { fetch } from "node-fetch-native"; // or use global fetch | ||
import { createProxy } from "node-fetch-native/proxy"; | ||
import { fetch } from "node-fetch-native/proxy"; | ||
// Uses HTTPS_PROXY or HTTP_PROXY environment variables | ||
const proxy = createProxy(); | ||
console.log(await fetch("https://icanhazip.com").then((r) => r.text()); | ||
``` | ||
// const proxy = createProxy({ url: "http://localhost:8080" }); | ||
### `createFetch` utility | ||
await fetch("https://google.com", { | ||
...proxy, | ||
}); | ||
You can use the `createFetch` utility to instantiate a `fetch` instance with custom proxy options. | ||
```ts | ||
import { createFetch } from "node-fetch-native/proxy"; | ||
const fetch = createFetch({ url: "http://localhost:9080" }); | ||
console.log(await fetch("https://icanhazip.com").then((r) => r.text()); | ||
``` | ||
`createProxy` returns an object with `agent` for older Node.js versions and `dispatcher` keys for newer Node.js versions with Undici and native fetch. | ||
### `createProxy` utility | ||
If no `url` option is provided, `HTTPS_PROXY` or `HTTP_PROXY` (or lowercase) environment variables will be used, and if they also are not set, both `agent` and `dispatcher` values will be undefined. | ||
`createProxy` returns an object with `agent` and `dispatcher` keys that can be passed as fetch options. | ||
> [!NOTE] | ||
> Using export conditions, this utility works in Node.js and for other runtimes, it will simply return a stubbed version as most of the other runtimes now support HTTP proxy out of the box! | ||
```ts | ||
import { fetch } from "node-fetch-native"; | ||
import { createProxy } from "node-fetch-native/proxy"; | ||
> [!NOTE] | ||
> Proxy support is under development. Check [unjs/node-fetch-native#107](https://github.com/unjs/node-fetch-native/issues/107) for the roadmap and contributing! | ||
const proxy = createProxy(); | ||
// const proxy = createProxy({ url: "http://localhost:8080" }); | ||
console.log(await fetch("https://icanhazip.com", { ...proxy }).then((r) => r.text()); | ||
``` | ||
## Alias to `node-fetch` | ||
@@ -190,6 +210,4 @@ | ||
Made with 💛 | ||
Made with 💛 Published under the [MIT](./LICENSE) license. | ||
[node-fetch is published under the MIT license](https://github.com/node-fetch/node-fetch/blob/main/LICENSE.md) | ||
<!-- Badges --> | ||
@@ -196,0 +214,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
718378
2801
223
33
25
17