UNPKG

@mastra/core

Version:

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

1 lines 2.79 kB
{"version":3,"sources":["../src/utils/fetchWithRetry.ts"],"names":[],"mappings":";;;AAIA,IAAM,6BAA6B,MAAM,IAAA;AAOzC,eAAsB,cAAA,CACpB,KACA,OAAA,GAAuB,IACvB,UAAA,GAAqB,CAAA,EACrB,YAAA,GAAsC,EAAC,EACpB;AACnB,EAAA,IAAI,UAAA,GAAa,CAAA;AACjB,EAAA,IAAI,SAAA,GAA0B,IAAA;AAC9B,EAAA,MAAM,mBAAA,GAAsB,aAAa,mBAAA,IAAuB,0BAAA;AAEhE,EAAA,OAAO,aAAa,UAAA,EAAY;AAC9B,IAAA,IAAI,QAAA;AAEJ,IAAA,IAAI;AACF,MAAA,QAAA,GAAW,MAAM,KAAA,CAAM,GAAA,EAAK,OAAO,CAAA;AAAA,IACrC,SAAS,KAAA,EAAO;AACd,MAAA,SAAA,GAAY,iBAAiB,KAAA,GAAQ,KAAA,GAAQ,IAAI,KAAA,CAAM,MAAA,CAAO,KAAK,CAAC,CAAA;AAAA,IACtE;AAEA,IAAA,IAAI,QAAA,EAAU;AACZ,MAAA,IAAI,CAAC,SAAS,EAAA,EAAI;AAChB,QAAA,SAAA,GAAY,IAAI,MAAM,CAAA,4BAAA,EAA+B,QAAA,CAAS,MAAM,CAAA,CAAA,EAAI,QAAA,CAAS,UAAU,CAAA,CAAE,CAAA;AAE7F,QAAA,IAAI,CAAC,mBAAA,CAAoB,QAAQ,CAAA,EAAG;AAClC,UAAA,MAAM,SAAA;AAAA,QACR;AAAA,MACF,CAAA,MAAO;AACL,QAAA,OAAO,QAAA;AAAA,MACT;AAAA,IACF;AAEA,IAAA,UAAA,EAAA;AAEA,IAAA,IAAI,cAAc,UAAA,EAAY;AAC5B,MAAA;AAAA,IACF;AAEA,IAAA,MAAM,KAAA,GAAQ,KAAK,GAAA,CAAI,GAAA,GAAO,KAAK,GAAA,CAAI,CAAA,EAAG,UAAU,CAAA,EAAG,GAAK,CAAA;AAC5D,IAAA,MAAM,IAAI,OAAA,CAAQ,CAAA,OAAA,KAAW,UAAA,CAAW,OAAA,EAAS,KAAK,CAAC,CAAA;AAAA,EACzD;AAEA,EAAA,MAAM,SAAA,IAAa,IAAI,KAAA,CAAM,8CAA8C,CAAA;AAC7E","file":"chunk-PAMAGEYY.cjs","sourcesContent":["export interface FetchWithRetryOptions {\n shouldRetryResponse?: (response: Response) => boolean;\n}\n\nconst defaultShouldRetryResponse = () => true;\n\n/**\n * Performs a fetch request with automatic retries using exponential backoff.\n * Network failures are always retried. Non-OK responses are retried unless\n * `shouldRetryResponse` returns false.\n */\nexport async function fetchWithRetry(\n url: string,\n options: RequestInit = {},\n maxRetries: number = 3,\n retryOptions: FetchWithRetryOptions = {},\n): Promise<Response> {\n let retryCount = 0;\n let lastError: Error | null = null;\n const shouldRetryResponse = retryOptions.shouldRetryResponse ?? defaultShouldRetryResponse;\n\n while (retryCount < maxRetries) {\n let response: Response | undefined;\n\n try {\n response = await fetch(url, options);\n } catch (error) {\n lastError = error instanceof Error ? error : new Error(String(error));\n }\n\n if (response) {\n if (!response.ok) {\n lastError = new Error(`Request failed with status: ${response.status} ${response.statusText}`);\n\n if (!shouldRetryResponse(response)) {\n throw lastError;\n }\n } else {\n return response;\n }\n }\n\n retryCount++;\n\n if (retryCount >= maxRetries) {\n break;\n }\n\n const delay = Math.min(1000 * Math.pow(2, retryCount), 10000);\n await new Promise(resolve => setTimeout(resolve, delay));\n }\n\n throw lastError || new Error('Request failed after multiple retry attempts');\n}\n"]}