
Product
Introducing Reports: An Extensible Reporting Framework for Socket Data
Explore exportable charts for vulnerabilities, dependencies, and usage with Reports, Socket’s new extensible reporting framework.
fetch-with-retry3
Advanced tools
fetch-with-retry3 is a utility function built on top of the Axios library, designed to make HTTP(S) requests with automatic retry logic. When a request fails due to issues like network errors, timeouts, or specific status codes, it retries the operation a
A robust, TypeScript-first HTTP client built on top of Axios with automatic retry logic and proxy support. Designed to handle unreliable network conditions with configurable retry strategies, timeouts, and comprehensive error handling.
npm install fetch-with-retry3
# or
yarn add fetch-with-retry3
# or
pnpm add fetch-with-retry3
import { fetchWithRetry } from 'fetch-with-retry3';
// Simple GET request
const response = await fetchWithRetry('https://api.example.com/users');
if (response.ok) {
console.log('Success:', response.data);
} else {
console.error('Error:', response.error);
}
import { fetchWithRetry } from 'fetch-with-retry3';
const response = await fetchWithRetry(
'https://api.example.com/data',
{
method: 'POST',
data: { name: 'John Doe', email: 'john@example.com' },
headers: { 'Content-Type': 'application/json' }
},
5, // 5 retry attempts
2000, // 2 second delay between retries
30000 // 30 second timeout
);
function fetchWithRetry<T = any, D = any>(
url: string,
options?: AxiosRequestConfig<D>,
attempts?: number,
delay?: number,
timeout?: number
): Promise<AxiosResponse<T> & { ok: boolean; error?: Error }>
| Parameter | Type | Default | Description |
|---|---|---|---|
url | string | - | The request URL |
options | AxiosRequestConfig<D> | {} | Axios request configuration |
attempts | number | 3 | Number of retry attempts |
delay | number | 1500 | Delay between retries (ms) |
timeout | number | 30000 | Request timeout (ms) |
Returns a Promise that resolves to an enhanced AxiosResponse with:
ok: Boolean indicating if the request was successful (2xx status)error: Error object if the request failed after all retriesfunction 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 }>
type Proxy = {
host: string;
port: number;
protocol: PROXY_PROTOCOL;
username?: string;
password?: string;
};
enum PROXY_PROTOCOL {
http = "http",
https = "https",
socks4 = "socks4",
socks5 = "socks5"
}
import { fetchWithRetry } from 'fetch-with-retry3';
const getUserData = async (userId: string) => {
const response = await fetchWithRetry(`https://api.example.com/users/${userId}`, {
method: 'GET',
headers: {
'Authorization': 'Bearer your-token-here'
}
});
if (response.ok) {
return response.data;
} else {
throw new Error(`Failed to fetch user: ${response.error?.message}`);
}
};
import { fetchWithRetry } from 'fetch-with-retry3';
const createUser = async (userData: any) => {
const response = await fetchWithRetry(
'https://api.example.com/users',
{
method: 'POST',
data: userData,
headers: { 'Content-Type': 'application/json' }
},
3, // Retry up to 3 times
1000 // Wait 1 second between retries
);
return response;
};
import { fetchWithProxy, PROXY_PROTOCOL } from 'fetch-with-retry3';
const proxies = [
{
host: 'proxy1.example.com',
port: 8080,
protocol: PROXY_PROTOCOL.http,
username: 'user1',
password: 'pass1'
},
{
host: 'proxy2.example.com',
port: 1080,
protocol: PROXY_PROTOCOL.socks5,
username: 'user2',
password: 'pass2'
}
];
const response = await fetchWithProxy(
'https://api.example.com/data',
{ method: 'GET' },
proxies,
3, // attempts
1500, // delay
30000 // timeout
);
import { fetchWithRetry } from 'fetch-with-retry3';
const controller = new AbortController();
// Cancel request after 5 seconds
setTimeout(() => controller.abort(), 5000);
const response = await fetchWithRetry(
'https://api.example.com/slow-endpoint',
{
method: 'GET',
signal: controller.signal
}
);
The library automatically handles various error scenarios:
ok: false)const response = await fetchWithRetry('https://api.example.com/data');
if (!response.ok) {
if (response.status === 404) {
console.log('Resource not found');
} else if (response.error) {
console.error('Request failed:', response.error.message);
}
}
Run the test suite:
npm test
Run tests with coverage:
npm run test:coverage
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
Built with:
FAQs
fetch-with-retry3 is a utility function built on top of the Axios library, designed to make HTTP(S) requests with automatic retry logic. When a request fails due to issues like network errors, timeouts, or specific status codes, it retries the operation a
We found that fetch-with-retry3 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Explore exportable charts for vulnerabilities, dependencies, and usage with Reports, Socket’s new extensible reporting framework.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.