feat: Implement comprehensive web request handling with caching, retry, and interceptors
- Added cache strategies: NetworkFirst, CacheFirst, StaleWhileRevalidate, NetworkOnly, and CacheOnly.
- Introduced InterceptorManager for managing request, response, and error interceptors.
- Developed RetryManager for handling request retries with customizable backoff strategies.
- Implemented RequestDeduplicator to prevent simultaneous identical requests.
- Created timeout utilities for handling request timeouts.
- Enhanced WebrequestClient to support global interceptors, caching, and retry logic.
- Added convenience methods for common HTTP methods (GET, POST, PUT, DELETE) with JSON handling.
- Established a fetch-compatible webrequest function for seamless integration.
- Defined core type structures for caching, retry options, interceptors, and web request configurations.
2025-10-20 09:59:24 +00:00
|
|
|
/**
|
|
|
|
|
* Timeout handling utilities
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import * as plugins from '../webrequest.plugins.js';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create an AbortController with timeout
|
|
|
|
|
*/
|
|
|
|
|
export function createTimeoutController(timeoutMs: number): {
|
|
|
|
|
controller: AbortController;
|
|
|
|
|
cleanup: () => void;
|
|
|
|
|
} {
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
|
|
2026-02-11 17:20:34 +00:00
|
|
|
// Use Timeout directly so we can cancel it when the fetch completes
|
|
|
|
|
const timeout = new plugins.smartdelay.Timeout(timeoutMs, null);
|
|
|
|
|
timeout.promise.then(() => {
|
|
|
|
|
controller.abort();
|
|
|
|
|
});
|
feat: Implement comprehensive web request handling with caching, retry, and interceptors
- Added cache strategies: NetworkFirst, CacheFirst, StaleWhileRevalidate, NetworkOnly, and CacheOnly.
- Introduced InterceptorManager for managing request, response, and error interceptors.
- Developed RetryManager for handling request retries with customizable backoff strategies.
- Implemented RequestDeduplicator to prevent simultaneous identical requests.
- Created timeout utilities for handling request timeouts.
- Enhanced WebrequestClient to support global interceptors, caching, and retry logic.
- Added convenience methods for common HTTP methods (GET, POST, PUT, DELETE) with JSON handling.
- Established a fetch-compatible webrequest function for seamless integration.
- Defined core type structures for caching, retry options, interceptors, and web request configurations.
2025-10-20 09:59:24 +00:00
|
|
|
|
2026-02-11 17:20:34 +00:00
|
|
|
// Cleanup function to cancel the timeout timer
|
feat: Implement comprehensive web request handling with caching, retry, and interceptors
- Added cache strategies: NetworkFirst, CacheFirst, StaleWhileRevalidate, NetworkOnly, and CacheOnly.
- Introduced InterceptorManager for managing request, response, and error interceptors.
- Developed RetryManager for handling request retries with customizable backoff strategies.
- Implemented RequestDeduplicator to prevent simultaneous identical requests.
- Created timeout utilities for handling request timeouts.
- Enhanced WebrequestClient to support global interceptors, caching, and retry logic.
- Added convenience methods for common HTTP methods (GET, POST, PUT, DELETE) with JSON handling.
- Established a fetch-compatible webrequest function for seamless integration.
- Defined core type structures for caching, retry options, interceptors, and web request configurations.
2025-10-20 09:59:24 +00:00
|
|
|
const cleanup = () => {
|
2026-02-11 17:20:34 +00:00
|
|
|
timeout.cancel();
|
feat: Implement comprehensive web request handling with caching, retry, and interceptors
- Added cache strategies: NetworkFirst, CacheFirst, StaleWhileRevalidate, NetworkOnly, and CacheOnly.
- Introduced InterceptorManager for managing request, response, and error interceptors.
- Developed RetryManager for handling request retries with customizable backoff strategies.
- Implemented RequestDeduplicator to prevent simultaneous identical requests.
- Created timeout utilities for handling request timeouts.
- Enhanced WebrequestClient to support global interceptors, caching, and retry logic.
- Added convenience methods for common HTTP methods (GET, POST, PUT, DELETE) with JSON handling.
- Established a fetch-compatible webrequest function for seamless integration.
- Defined core type structures for caching, retry options, interceptors, and web request configurations.
2025-10-20 09:59:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return { controller, cleanup };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute a fetch with timeout
|
|
|
|
|
*/
|
|
|
|
|
export async function fetchWithTimeout(
|
|
|
|
|
url: string,
|
|
|
|
|
init: RequestInit,
|
|
|
|
|
timeoutMs: number,
|
|
|
|
|
): Promise<Response> {
|
|
|
|
|
const { controller, cleanup } = createTimeoutController(timeoutMs);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(url, {
|
|
|
|
|
...init,
|
|
|
|
|
signal: controller.signal,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cleanup();
|
|
|
|
|
return response;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
|
|
// Re-throw with more informative error if it's a timeout
|
|
|
|
|
if (error instanceof Error && error.name === 'AbortError') {
|
|
|
|
|
throw new Error(`Request timeout after ${timeoutMs}ms: ${url}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|