@push.rocks/webrequest
Version:
Modern, fetch-compatible web request library with intelligent HTTP caching, retry strategies, and fault tolerance.
66 lines (65 loc) • 1.94 kB
TypeScript
/**
* Cache strategy implementations
*/
import type { TCacheStrategy } from '../webrequest.types.js';
import { CacheStore } from './cache.store.js';
export interface IStrategyContext {
request: Request;
cacheKey: string;
cacheStore: CacheStore;
fetchFn: (request: Request) => Promise<Response>;
logging?: boolean;
}
export interface IStrategyResult {
response: Response;
fromCache: boolean;
revalidated: boolean;
}
/**
* Base strategy handler interface
*/
export interface ICacheStrategyHandler {
execute(context: IStrategyContext): Promise<IStrategyResult>;
}
/**
* Network-First Strategy
* Try network first, fallback to cache on failure
*/
export declare class NetworkFirstStrategy implements ICacheStrategyHandler {
execute(context: IStrategyContext): Promise<IStrategyResult>;
private cacheResponse;
}
/**
* Cache-First Strategy
* Check cache first, fetch if miss or stale
*/
export declare class CacheFirstStrategy implements ICacheStrategyHandler {
execute(context: IStrategyContext): Promise<IStrategyResult>;
private revalidate;
}
/**
* Stale-While-Revalidate Strategy
* Return cache immediately, update in background
*/
export declare class StaleWhileRevalidateStrategy implements ICacheStrategyHandler {
execute(context: IStrategyContext): Promise<IStrategyResult>;
private revalidateInBackground;
}
/**
* Network-Only Strategy
* Never use cache
*/
export declare class NetworkOnlyStrategy implements ICacheStrategyHandler {
execute(context: IStrategyContext): Promise<IStrategyResult>;
}
/**
* Cache-Only Strategy
* Only use cache, fail if miss
*/
export declare class CacheOnlyStrategy implements ICacheStrategyHandler {
execute(context: IStrategyContext): Promise<IStrategyResult>;
}
/**
* Get strategy handler for a given strategy type
*/
export declare function getStrategyHandler(strategy: TCacheStrategy): ICacheStrategyHandler;