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

Usage no npm install needed!

<script type="module">
  import fetchEnhanced from 'https://cdn.skypack.dev/fetch-enhanced';



fetch-enhanced wraps a provided fetch-like module like node-fetch and adds:

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


npm i fetch-enhanced node-fetch
import nodeFetch from "node-fetch";
import fetchEnhanced from "fetch-enhanced";

const fetch = fetchEnhanced(nodeFetch);
await fetch("https://example.com");


fetchEnhanced(fetchImplementation, [opts])

  • fetchImplementation: Function A fetch-like module that takes (url, opts) and a agent option.
  • opts Object
    • agentCacheSize: number Size of the agent cache. Default: 512.

Returns: A wrapped fetch function.

fetch(url, [opts])

  • opts Object
    • timeout: number Request timeout in milliseconds. Default: 0 (meaning no timeout).
    • agent: http.Agent Custom HTTP agent. When specified, proxy discovery will no longer work.
    • agentOpts: object Agent options. Default: {maxSockets: 64, keepAlive: false}
    • Any valid fetch module option, like for node-fetch


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


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");
process.env.HTTPS_PROXY = "https://proxy2.dev";
await fetch("https://example.com");

© silverwind, distributed under BSD licence