npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

fetch-with-proxy3

v1.0.4

Published

fetch-with-proxy3 is a utility function built on top of the Axios library, designed to make HTTP(S) requests with proxy support. When a request encounters issues like network errors or timeouts, it uses a specified proxy to attempt the operation. This fun

Readme

fetch-with-proxy3

npm version License: MIT TypeScript

A robust, TypeScript-first HTTP client built on top of Axios with comprehensive proxy support and automatic retry logic. Designed for reliable network operations in unstable environments.

Features

  • 🔄 Automatic Retry Logic: Configurable retry attempts with customizable delays
  • 🌐 Comprehensive Proxy Support: HTTP, HTTPS, SOCKS4, and SOCKS5 proxies
  • 🔒 Type-Safe: Full TypeScript support with comprehensive type definitions
  • Modern: Uses AbortController for request cancellation (no deprecated APIs)
  • 🛡️ Reliable: Built-in error handling and timeout management
  • 📦 Zero Config: Works out of the box with sensible defaults
  • 🎯 Flexible: Highly configurable for various use cases

Installation

npm install fetch-with-proxy3
yarn add fetch-with-proxy3
pnpm add fetch-with-proxy3

Quick Start

Basic Usage (No Proxy)

import { fetchWithProxy } from 'fetch-with-proxy3';

const response = await fetchWithProxy('https://api.example.com/data', {
  method: 'GET',
  headers: {
    'Authorization': 'Bearer your-token'
  }
});

if (response.ok) {
  console.log('Success:', response.data);
} else {
  console.error('Error:', response.error);
}

With HTTP/HTTPS Proxy

import { fetchWithProxy, PROXY_PROTOCOL } from 'fetch-with-proxy3';

const proxies = [{
  host: 'proxy.example.com',
  port: 8080,
  protocol: PROXY_PROTOCOL.http,
  username: 'your-username', // optional
  password: 'your-password'  // optional
}];

const response = await fetchWithProxy(
  'https://api.example.com/data',
  {
    method: 'POST',
    data: { key: 'value' },
    headers: { 'Content-Type': 'application/json' }
  },
  proxies,
  3,     // retry attempts
  1500,  // delay between retries (ms)
  30000  // timeout (ms)
);

With SOCKS Proxy

import { fetchWithProxy, PROXY_PROTOCOL } from 'fetch-with-proxy3';

const socksProxy = [{
  host: 'socks-proxy.example.com',
  port: 1080,
  protocol: PROXY_PROTOCOL.socks5,
  username: 'user',
  password: 'pass'
}];

const response = await fetchWithProxy(
  'https://api.example.com/secure-endpoint',
  { method: 'GET' },
  socksProxy
);

Multiple Proxies (Failover)

import { fetchWithProxy, PROXY_PROTOCOL } from 'fetch-with-proxy3';

const proxies = [
  {
    host: 'primary-proxy.com',
    port: 8080,
    protocol: PROXY_PROTOCOL.http
  },
  {
    host: 'backup-proxy.com',
    port: 3128,
    protocol: PROXY_PROTOCOL.https
  },
  {
    host: 'socks-proxy.com',
    port: 1080,
    protocol: PROXY_PROTOCOL.socks5
  }
];

// Will try each proxy in order until one succeeds
const response = await fetchWithProxy(
  'https://api.example.com/data',
  { method: 'GET' },
  proxies
);

API Reference

fetchWithProxy

function fetchWithProxy<T = any, D = any>(
  url: string,
  options?: AxiosRequestConfig<D>,
  proxies?: Proxy[],
  attempts?: number,
  delay?: number,
  timeout?: number
): Promise<AxiosResponse<T> & { ok: boolean; error?: Error }>

Parameters

| Parameter | Type | Default | Description | |-----------|------|---------|-------------| | url | string | - | The URL to request | | options | AxiosRequestConfig<D> | {} | Axios request configuration | | proxies | Proxy[] | [] | Array of proxy configurations | | attempts | number | 3 | Number of retry attempts | | delay | number | 1500 | Delay between retries (milliseconds) | | timeout | number | 30000 | Request timeout (milliseconds) |

Returns

Promise resolving to an enhanced Axios response with:

  • ok: Boolean indicating success (status 200-299)
  • error: Error object if request failed
  • All standard Axios response properties

fetchWithRetry

function fetchWithRetry<T = any, D = any>(
  url: string,
  options?: AxiosRequestConfig<D>,
  attempts?: number,
  delay?: number,
  timeout?: number
): Promise<AxiosResponse<T> & { ok: boolean; error?: Error }>

Direct retry functionality without proxy support.

Proxy Configuration

interface Proxy {
  host: string;
  port: number;
  protocol: PROXY_PROTOCOL;
  username?: string;
  password?: string;
}

enum PROXY_PROTOCOL {
  http = "http",
  https = "https",
  socks4 = "socks4",
  socks5 = "socks5",
  unknown = "unknown"
}

Error Handling

The library provides comprehensive error handling:

const response = await fetchWithProxy('https://api.example.com/data');

if (response.ok) {
  // Success - status code 200-299
  console.log(response.data);
} else {
  // Handle different error scenarios
  if (response.status === 404) {
    console.log('Resource not found');
  } else if (response.error) {
    console.error('Request failed:', response.error.message);
  }
}

Advanced Configuration

Custom Timeout and Retries

const response = await fetchWithProxy(
  'https://slow-api.example.com/data',
  { method: 'GET' },
  [], // no proxies
  5,     // 5 retry attempts
  2000,  // 2 second delay between retries
  60000  // 60 second timeout
);

Request Interceptors

Since this library is built on Axios, you can use Axios interceptors:

import axios from 'axios';

// Add request interceptor
axios.interceptors.request.use(config => {
  config.headers['X-Custom-Header'] = 'value';
  return config;
});

// Then use fetchWithProxy normally
const response = await fetchWithProxy('https://api.example.com/data');

TypeScript Support

Full TypeScript support with generic types:

interface ApiResponse {
  id: number;
  name: string;
  email: string;
}

interface RequestPayload {
  name: string;
  email: string;
}

const response = await fetchWithProxy<ApiResponse, RequestPayload>(
  'https://api.example.com/users',
  {
    method: 'POST',
    data: {
      name: 'John Doe',
      email: '[email protected]'
    }
  }
);

// response.data is now typed as ApiResponse
console.log(response.data.name); // TypeScript knows this is a string

Testing

The library includes comprehensive tests. Run them with:

npm test

For coverage report:

npm run test:coverage

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

v1.0.4

  • Updated lint-staged to latest version (16.2.1)
  • Applied consistent code formatting with double quotes
  • Maintained 100% type safety and test coverage
  • Zero security vulnerabilities
  • Professional code quality assurance

v1.0.3

  • Updated all dependencies to latest versions
  • Replaced deprecated CancelToken with AbortController
  • Improved TypeScript type safety
  • Enhanced error handling
  • Added comprehensive test suite
  • Updated documentation

v1.0.2

  • Initial stable release with proxy support

Support


Made with ❤️ by Dmitrii Selikhov