Timeout and Retries
Configure automatic retries and request timeouts
Timeout
You can set the request timeout in milliseconds.
import { createFetchClient } from "@zayne-labs/callapi";
const callBackendApi = createFetchClient({
baseURL: "http://localhost:3000",
timeout: 5000,
});
const result = await callBackendApi("/api/users", {
timeout: 10000,
});
Auto Retry
You can set the retry attempts and interval in milliseconds.
const result = await callApi("/api/users", {
// Retry up to 3 times
retryAttempts: 3,
});
Advanced Retry Options
CallApi provides flexible retry mechanisms with both linear and exponential backoff strategies. You can customize the retry behavior to suit your specific needs.
Basic Retry with only number of attempts
const result = await callApi("/api/users", {
// Retry up to 3 times
retryAttempts: 3,
});
Linear Retry Strategy
Waits a fixed amount of time between retries:
const result = await callApi("/api/users", {
retryStrategy: "linear",
retryAttempts: 3,
retryDelay: 1000,
});
Exponential Retry Strategy
Increases the delay between retries exponentially:
const result = await callApi("/api/users", {
retryStrategy: "exponential",
retryAttempts: 5, // Retry up to 5 times
retryDelay: 1000, // Start with 1 second delay
retryMaxDelay: 10000, // Cap the delay at 10 seconds, so requests would go out after 1s then 2s, 4s, 8s, 10s
});
RetryMethods and RetryStatusCodes
You can customize when to retry a request with the retryMethods
and retryStatusCodes
options.
-
Retry Methods: This option allows you to specify which HTTP methods should be retried. By default, this option is set to
["GET", "POST"]
.This implies that onlyGET
andPOST
requests will be retried by default. -
Retry Status Codes: This option allows you to specify which HTTP status codes should be retried. There are no default values for this option.
const result = await callApi("/api/users", {
retryStrategy: "linear",
retryAttempts: 3,
retryDelay: 1000,
retryMethods: ["GET", "POST"],
retryStatusCodes: [409, 425, 429, 500, 502, 503, 504],
});
Custom condition
Use retryCondition
to implement custom retry logic:
const result = await callApi("/api/users", {
retryStrategy: "linear",
retryAttempts: 3,
retryCondition: ({ error, response }) => {
return response?.status === 429; // Only retry on rate limit errors
},
});
onRetry callback
Listen to retry attempts using the onRetry
hook:
const result = await callApi("/todos/1", {
retryAttempts: 3,
onRetry: (response) => {
console.log(`Retrying request.`);
},
});
Types
Timeout
Prop | Type | Default |
---|---|---|
timeout? | number | - |
Retry
Prop | Type | Default |
---|---|---|
~retryAttemptCount? | number | - |
retry? | InnerRetryOptions<TErrorData> | - |
retryAttempts? | number | 0 |
retryCondition? | RetryCondition<TErrorData> | - |
retryDelay? | number | ((currentAttemptCount: number) => number) | 1000 |
retryMaxDelay? | number | 10000 |
retryMethods? | ("CONNECT" | "DELETE" | "GET" | "HEAD" | "OPTIONS" | "PATCH" | "POST" | "PUT" | "TRACE" | AnyString)[] | ["GET", "POST"] |
retryStatusCodes? | (AnyNumber | 408 | 409 | 425 | 429 | 500 | 502 | 503 | 504)[] | - |
retryStrategy? | "exponential" | "linear" | "linear" |