near-protocol-rewards
Version:
A transparent, metric-based rewards system for NEAR projects
34 lines (33 loc) • 1.03 kB
JavaScript
;
/**
* Retry utility with exponential backoff
* Handles transient failures in API calls
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.retry = void 0;
const defaultShouldRetry = (error) => {
// Retry on network errors and rate limits
if (!error.response)
return true;
const status = error.response.status;
return status === 429 || (status >= 500 && status < 600);
};
async function retry(fn, options) {
const { maxRetries, retryDelay, shouldRetry = defaultShouldRetry } = options;
let lastError;
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
return await fn();
}
catch (error) {
lastError = error;
if (attempt === maxRetries || !shouldRetry(error)) {
throw error;
}
const delay = retryDelay(attempt);
await new Promise((resolve) => setTimeout(resolve, delay));
}
}
throw lastError;
}
exports.retry = retry;