@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
36 lines (35 loc) • 1.2 kB
JavaScript
// src/utils/fetchWithRetry.ts
var defaultShouldRetryResponse = () => true;
async function fetchWithRetry(url, options = {}, maxRetries = 3, retryOptions = {}) {
let retryCount = 0;
let lastError = null;
const shouldRetryResponse = retryOptions.shouldRetryResponse ?? defaultShouldRetryResponse;
while (retryCount < maxRetries) {
let response;
try {
response = await fetch(url, options);
} catch (error) {
lastError = error instanceof Error ? error : new Error(String(error));
}
if (response) {
if (!response.ok) {
lastError = new Error(`Request failed with status: ${response.status} ${response.statusText}`);
if (!shouldRetryResponse(response)) {
throw lastError;
}
} else {
return response;
}
}
retryCount++;
if (retryCount >= maxRetries) {
break;
}
const delay = Math.min(1e3 * Math.pow(2, retryCount), 1e4);
await new Promise((resolve) => setTimeout(resolve, delay));
}
throw lastError || new Error("Request failed after multiple retry attempts");
}
export { fetchWithRetry };
//# sourceMappingURL=chunk-RBJJ4Y4N.js.map
//# sourceMappingURL=chunk-RBJJ4Y4N.js.map