https://github.com/silverwind/fetch-enhanced
fetch wrapper with support for automatic HTTP proxy and timeout
https://github.com/silverwind/fetch-enhanced
Last synced: about 1 month ago
JSON representation
fetch wrapper with support for automatic HTTP proxy and timeout
- Host: GitHub
- URL: https://github.com/silverwind/fetch-enhanced
- Owner: silverwind
- Created: 2020-09-16T20:22:48.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-07-18T11:25:43.000Z (almost 2 years ago)
- Last Synced: 2025-04-20T13:37:18.096Z (about 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 1.51 MB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# fetch-enhanced
[](https://www.npmjs.org/package/fetch-enhanced) [](https://www.npmjs.org/package/fetch-enhanced) [](https://packagephobia.com/result?p=fetch-enhanced)
`fetch-enhanced` wraps a provided `fetch`-like function like [undici](https://nodejs.org/dist/latest-v18.x/docs/api/globals.html#fetch) or [node-fetch](https://github.com/node-fetch/node-fetch) and adds:
- HTTP Proxy discovery from standard environment variables
- HTTP Request Timeout support
- Accessible [agent/dispatcher](https://nodejs.org/api/https.html#https_new_agent_options) [options](https://nodejs.org/api/http.html#http_new_agent_options)
## Usage
```js
import {fetch as undiciFetch} from "undici";
import fetchEnhanced from "fetch-enhanced";
const fetch = fetchEnhanced(undiciFetch, {undici: true});
await fetch("https://example.com");
```
## API
### fetchEnhanced(fetchImplementation, opts)
- fetchImplementation: *Function* A `fetch`-like module that takes `(url, opts)` and a `agent` (like `node-fetch`) or `dispatcher` (like `undici`) option.
- `opts` *Object* Required.
- `agentCacheSize`: *number* Size of the agent cache. Default: `512`.
- `undici`: *boolean* Whether the fetch implementation is undici. Required.
Returns: A wrapped `fetch` function.
### fetch(url, [opts])
- `opts` *Object*
- `timeout`: *number* Request timeout in milliseconds. Default: 0 (meaning no timeout).
- `noProxy`: *boolean* Explicitely disable any proxy server use. Default: false.
- `agent`: *http.Agent* Custom HTTP agent. When specified, proxy discovery will no longer work.
- `agentOpts`: *object* [Agent](https://nodejs.org/api/https.html#https_new_agent_options) or [Dispatcher](https://github.com/nodejs/undici/blob/main/docs/api/ProxyAgent.md#parameter-proxyagentoptions) [options](https://nodejs.org/api/http.html#http_new_agent_options).Default: `{maxSockets: 64, keepAlive: false}`
- `agentOpts.noProxy`: *boolean* Do not use proxy in any case. Default: `false`.
- Any valid `fetch` module option, like for [`node-fetch`](https://github.com/node-fetch/node-fetch#options)
### TimeoutError
Error class that can be used for `err instanceof TimeoutError`:
```js
import {TimeoutError} from "fetch-enhanced";
try {
await fetch("https://example.com", {timeout: 0});
} catch (err) {
console.log(err instanceof TimeoutError);
// => true
}
```
### fetch.clearCache()
Clear the agent cache and destroys all cached agents. This is generally only neccessary when the proxy environment variables are expected to change during runtime.
```js
process.env.HTTPS_PROXY = "https://proxy1.dev";
await fetch("https://example.com");
fetch.clearCache();
process.env.HTTPS_PROXY = "https://proxy2.dev";
await fetch("https://example.com");
```
© [silverwind](https://github.com/silverwind), distributed under BSD licence