export interface RateLimitContext { ip?: string; method: string; path: string; headers: Record; query?: any; body?: any; user?: any; route?: string; raw: any; } export interface RateLimiterConfig { window?: number; max?: number; key?: (ctx: RateLimitContext) => string; skip?: (ctx: RateLimitContext) => boolean; prefix?: string; algorithm?: AlgorithmType; mode?: 'accurate' | 'fast'; failStrategy?: 'fail-open' | 'fail-closed' | 'fallback-to-memory'; store?: 'redis' | 'memory' | 'hybrid' | any; redis?: any; policies?: RateLimitRule[]; default?: RateLimitDefaultConfig | RateLimitRule; hooks?: RateLimiterHooks; plugins?: RateLimiterPlugin[]; layers?: any[]; message?: any | ((ctx: RateLimitContext) => any); } export interface RateLimitResult { allowed: boolean; limit: number; remaining: number; reset: number; totalHits: number; } export interface RateLimiterPlugin { name?: string; beforeRequest?: (ctx: RateLimitContext) => Promise | void; beforeLimit?: (ctx: RateLimitContext, key: string) => Promise | void; afterLimit?: (ctx: RateLimitContext, result: RateLimitResult) => Promise | void; onError?: (error: unknown, ctx: RateLimitContext) => void; } export interface RateLimitDefaultConfig { window: number | ((ctx: RateLimitContext) => number); max: number | ((ctx: RateLimitContext) => number); algorithm?: AlgorithmType; burst?: number; mode?: 'accurate' | 'fast'; } export interface RateLimitRule { path?: string | RegExp; prefix?: string; method?: string; window?: number | ((ctx: RateLimitContext) => number); max?: number | ((ctx: RateLimitContext) => number); options?: { window?: number | ((ctx: RateLimitContext) => number); max?: number | ((ctx: RateLimitContext) => number); }; match?: (ctx: RateLimitContext) => boolean; algorithm?: AlgorithmType; burst?: number; mode?: 'accurate' | 'fast'; adaptive?: AdaptiveConfig; } export interface AdaptiveConfig { enabled: boolean; strategy?: 'auto-scale'; rules?: { spikeThreshold: number; reduceLimitBy: number; recoveryTime: number; }; } export type AlgorithmType = 'sliding-window' | 'token-bucket' | 'fixed-window'; export interface RateLimitInfo { key: string; limit: number; remaining: number; reset: number; totalHits: number; } export interface RateLimiterHooks { onRequest?: (ctx: RateLimitContext) => void; onLimit?: (ctx: RateLimitContext, info: RateLimitInfo) => void; onError?: (error: unknown) => void; } export interface MultiLayerRateLimitResult { allowed: boolean; limit?: number; remaining?: number; reset?: number; layers?: RateLimitResult[]; }