Documentation / @zerotal/core / index / ThrottleMiddleware
Class: ThrottleMiddleware
Defined in: middleware/ThrottleMiddleware.ts:68
Rate-limiting middleware using an in-memory sliding window counter.
Returns 429 Too Many Requests with Retry-After and X-RateLimit-* headers when a client exceeds the configured threshold within the time window.
Example
// Global: 120 req / min
app.use([ThrottleMiddleware.with({ maxAttempts: 120, windowSeconds: 60 })]);
// Per-route: 5 login attempts / min
Router.post('/login', AuthController, 'login', [
ThrottleMiddleware.with({ maxAttempts: 5, windowSeconds: 60 }),
]);
// By authenticated user ID
ThrottleMiddleware.with({
maxAttempts: 1000,
windowSeconds: 3600,
keyResolver: (ctx) => String(ctx.user?.id ?? _clientIp(ctx.request)),
})
Extends
Constructors
Constructor
new ThrottleMiddleware(
options?):ThrottleMiddleware
Defined in: middleware/ThrottleMiddleware.ts:74
Parameters
options?
Partial<ThrottleOptions> = {}
Returns
ThrottleMiddleware
Overrides
Properties
options
protectedoptions:ThrottleOptions
Defined in: middleware/ThrottleMiddleware.ts:69
Subclasses must declare this with their default option values. TypeScript enforces this at compile time — forgetting it is a type error.
Overrides
Methods
with()
staticwith<T,Opts>(this,options): () =>InstanceType<T>
Defined in: middleware/BaseMiddleware.ts:48
Returns a zero-arg subclass with the given options deep-merged on top of the subclass defaults, usable directly in app.use([...]).
Type Parameters
T
T extends (...args) => BaseMiddleware<any>
Opts
Opts = T extends (...args) => BaseMiddleware<U> ? U : object
Parameters
this
T
options
Partial<Opts>
Returns
() => InstanceType<T>
Inherited from
afterResponse()?
optionalafterResponse(ctx):Promise<void>
Defined in: middleware/BaseMiddleware.ts:70
Parameters
ctx
Returns
Promise<void>
Inherited from
onError()?
optionalonError(ctx,error):Promise<void>
Defined in: middleware/BaseMiddleware.ts:71
Parameters
ctx
error
Error
Returns
Promise<void>
Inherited from
handle()
handle(
http,next):Promise<void|Response>
Defined in: middleware/ThrottleMiddleware.ts:102
Parameters
http
next
Returns
Promise<void | Response>
Overrides
reset()
reset():
void
Defined in: middleware/ThrottleMiddleware.ts:170
Reset all counters — useful in tests.
Returns
void
resetKey()
resetKey(
ctx):void
Defined in: middleware/ThrottleMiddleware.ts:179
Reset the counter for a single key derived from the given context.
Used by RateLimiter.resetFor() to clear a specific actor's bucket
(e.g. after a successful login clears failed-attempt counters).
Parameters
ctx
Returns
void