UNPKG

@mastra/core

Version:

Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.

38 lines (36 loc) 1.23 kB
'use strict'; // 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"); } exports.fetchWithRetry = fetchWithRetry; //# sourceMappingURL=chunk-PAMAGEYY.cjs.map //# sourceMappingURL=chunk-PAMAGEYY.cjs.map