- 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.
		
			
				
	
	
		
			68 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			68 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| /**
 | |
|  * Retry backoff strategies
 | |
|  */
 | |
| 
 | |
| import type { TBackoffStrategy } from '../webrequest.types.js';
 | |
| 
 | |
| export interface IBackoffCalculator {
 | |
|   calculate(attempt: number, initialDelay: number, maxDelay: number): number;
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Exponential backoff strategy
 | |
|  * Delay increases exponentially: initialDelay * 2^attempt
 | |
|  */
 | |
| export class ExponentialBackoff implements IBackoffCalculator {
 | |
|   calculate(attempt: number, initialDelay: number, maxDelay: number): number {
 | |
|     const delay = initialDelay * Math.pow(2, attempt - 1);
 | |
|     return Math.min(delay, maxDelay);
 | |
|   }
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Linear backoff strategy
 | |
|  * Delay increases linearly: initialDelay * attempt
 | |
|  */
 | |
| export class LinearBackoff implements IBackoffCalculator {
 | |
|   calculate(attempt: number, initialDelay: number, maxDelay: number): number {
 | |
|     const delay = initialDelay * attempt;
 | |
|     return Math.min(delay, maxDelay);
 | |
|   }
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Constant backoff strategy
 | |
|  * Delay stays constant: initialDelay
 | |
|  */
 | |
| export class ConstantBackoff implements IBackoffCalculator {
 | |
|   calculate(attempt: number, initialDelay: number, maxDelay: number): number {
 | |
|     return Math.min(initialDelay, maxDelay);
 | |
|   }
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Get backoff calculator for a given strategy
 | |
|  */
 | |
| export function getBackoffCalculator(
 | |
|   strategy: TBackoffStrategy,
 | |
| ): IBackoffCalculator {
 | |
|   switch (strategy) {
 | |
|     case 'exponential':
 | |
|       return new ExponentialBackoff();
 | |
|     case 'linear':
 | |
|       return new LinearBackoff();
 | |
|     case 'constant':
 | |
|       return new ConstantBackoff();
 | |
|     default:
 | |
|       return new ExponentialBackoff();
 | |
|   }
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Add jitter to delay to prevent thundering herd
 | |
|  */
 | |
| export function addJitter(delay: number, jitterFactor: number = 0.1): number {
 | |
|   const jitter = delay * jitterFactor * Math.random();
 | |
|   return delay + jitter;
 | |
| }
 |