
//#region src/internal/fetch-utils.ts
/**
* Fetch with timeout using AbortController
* @param url - The URL to fetch
* @param options - Standard fetch options
* @param timeoutMs - Timeout in milliseconds (default: 30000)
* @returns Promise<Response>
* @throws Error if request times out
*/
async function fetchWithTimeout(url, options = {}, timeoutMs = 3e4) {
	const controller = new AbortController();
	const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
	try {
		const response = await fetch(url, {
			...options,
			signal: controller.signal
		});
		return response;
	} catch (error) {
		if (error instanceof Error && error.name === "AbortError") throw new Error(`Request timeout after ${timeoutMs}ms`);
		throw error;
	} finally {
		clearTimeout(timeoutId);
	}
}
/**
* Retry a function with exponential backoff
* @param fn - The async function to retry
* @param options - Retry options
* @param options.maxRetries - Maximum number of retries (default: 3)
* @param options.initialDelay - Initial delay in milliseconds (default: 1000)
* @param options.shouldRetry - Predicate to determine if error should be retried (default: always retry)
* @returns Promise with the function result
* @throws The last error if all retries are exhausted
*/
async function retryWithBackoff(fn, { maxRetries = 3, initialDelay = 1e3, shouldRetry = (error) => true } = {}) {
	let lastError;
	let delay = initialDelay;
	for (let attempt = 0; attempt <= maxRetries; attempt++) try {
		return await fn();
	} catch (error) {
		lastError = error;
		if (attempt === maxRetries || !shouldRetry(error)) throw error;
		await new Promise((resolve) => setTimeout(resolve, delay));
		delay = Math.min(delay * 2, 3e4);
	}
	throw lastError;
}

//#endregion
Object.defineProperty(exports, 'fetchWithTimeout', {
  enumerable: true,
  get: function () {
    return fetchWithTimeout;
  }
});
Object.defineProperty(exports, 'retryWithBackoff', {
  enumerable: true,
  get: function () {
    return retryWithBackoff;
  }
});
//# sourceMappingURL=fetch-utils-Cp3P-Ai3.cjs.map