UNPKG

@mastra/core

Version:
69 lines (68 loc) 2.4 kB
//#region src/utils/fetchWithRetry.ts const defaultShouldRetryResponse = () => true; /** * Performs a fetch request with automatic retries using exponential backoff. * Network failures are always retried. Non-OK responses are retried unless * `shouldRetryResponse` returns false. */ 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 = /* @__PURE__ */ 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 || /* @__PURE__ */ new Error("Request failed after multiple retry attempts"); } //#endregion //#region src/utils/deep-equal.ts /** * Deep equality comparison for comparing two values. * Handles primitives, arrays, objects, and Date instances. */ function deepEqual(a, b) { if (a === b) return true; if (a == null || b == null) return a === b; if (typeof a !== typeof b) return false; if (Array.isArray(a) && Array.isArray(b)) { if (a.length !== b.length) return false; return a.every((item, index) => deepEqual(item, b[index])); } if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime(); if (typeof a === "object" && typeof b === "object") { const aObj = a; const bObj = b; const aKeys = Object.keys(aObj); const bKeys = Object.keys(bObj); if (aKeys.length !== bKeys.length) return false; return aKeys.every((key) => Object.prototype.hasOwnProperty.call(bObj, key) && deepEqual(aObj[key], bObj[key])); } return false; } //#endregion Object.defineProperty(exports, "deepEqual", { enumerable: true, get: function() { return deepEqual; } }); Object.defineProperty(exports, "fetchWithRetry", { enumerable: true, get: function() { return fetchWithRetry; } }); //# sourceMappingURL=deep-equal-BvQBG8wE.cjs.map