Skip to main content
Version: Next

RateLimiter

RateLimiter

Rate limiter implementation that enforces request limits per key. Supports configurable request limits and time intervals.

Signature
class RateLimiter {
constructor(maxRequests: number = DEFAULT_MAX_REQUESTS, interval: number = DEFAULT_TIMEOUT, storage: RateLimitStorage = new MemoryRateLimitStorage())
setStorage(storage: RateLimitStorage) => ;
getStorage() => RateLimitStorage;
limit(key: string) => Promise<boolean>;
getRemaining(key: string) => Promise<number>;
getResetTime(key: string) => Promise<number>;
reset(key: string) => Promise<void>;
getConfig() => Omit<RateLimitOptions, 'storage'>;
}

constructor

method
(maxRequests: number = DEFAULT_MAX_REQUESTS, interval: number = DEFAULT_TIMEOUT, storage: RateLimitStorage = new MemoryRateLimitStorage()) => RateLimiter

Creates a new rate limiter instance

setStorage

method
(storage: RateLimitStorage) =>

Sets the storage implementation for the rate limiter

getStorage

method

Gets the storage implementation for the rate limiter

limit

method
(key: string) => Promise<boolean>

Checks if a request is allowed for the given key and increments the counter if allowed

getRemaining

method
(key: string) => Promise<number>

Gets the remaining requests allowed for a given key

getResetTime

method
(key: string) => Promise<number>

Gets the time until the rate limit resets for a given key

reset

method
(key: string) => Promise<void>

Resets the rate limit for a given key

getConfig

method
() => Omit<RateLimitOptions, 'storage'>

Gets the current configuration of the rate limiter