fetch-enhanced

fetch wrapper with support for automatic HTTP proxy, timeout and accessible agent options

BSD-2-Clause 61 个版本
安装
npm install fetch-enhanced
yarn add fetch-enhanced
pnpm add fetch-enhanced
bun add fetch-enhanced
README

fetch-enhanced

fetch-enhanced wraps a provided fetch-like function like undici or node-fetch and adds:

  • HTTP Proxy discovery from standard environment variables
  • HTTP Request Timeout support
  • Accessible agent/dispatcher options

Usage

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 or Dispatcher 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

TimeoutError

Error class that can be used for err instanceof TimeoutError:

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.

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, distributed under BSD licence

版本列表
13.0.0 2026-05-07
12.3.2 2026-02-14
12.3.1 2025-09-14
12.3.0 2024-07-16
12.2.0 2024-07-11
12.1.0 2024-05-17
12.0.2 2024-03-16
12.0.1 2024-01-15
12.0.0 2023-10-11
11.1.2 2023-07-08
11.1.1 2023-01-24
11.1.0 2022-12-21
11.0.3 2022-12-07
11.0.2 2022-12-07
11.0.1 2022-12-07
11.0.0 2022-11-27
10.1.1 2022-11-20
10.1.0 2022-11-03
10.0.3 2022-10-18
10.0.2 2022-09-20
10.0.1 2022-09-15
10.0.0 2022-09-14
9.0.1 2022-09-14
9.0.0 2022-09-14
8.1.1 2022-04-14
8.1.0 2022-04-11
8.0.2 2021-11-29
8.0.1 2021-11-29
8.0.0 2021-10-16
7.0.1 2021-09-09
7.0.0 2021-06-10
6.0.0 2021-04-06
5.0.2 2021-03-26
5.0.1 2020-10-29
5.0.0 2020-10-09
4.2.2 2020-10-06
4.2.1 2020-10-05
4.2.0 2020-10-05
4.1.2 2020-10-05
4.1.1 2020-10-05
4.1.0 2020-10-05
4.0.5 2020-10-04
4.0.4 2020-09-27
4.0.3 2020-09-27
4.0.2 2020-09-20
4.0.1 2020-09-20
4.0.0 2020-09-20
3.0.1 2020-09-20
3.0.0 2020-09-18
2.3.1 2020-09-18
2.3.0 2020-09-18
2.2.2 2020-09-18
2.2.1 2020-09-18
2.2.0 2020-09-18
2.1.0 2020-09-18
2.0.0 2020-09-18
1.0.4 2020-09-16
1.0.3 2020-09-16
1.0.2 2020-09-16
1.0.1 2020-09-16
1.0.0 2020-09-16