@stacksjs/router
Version:
The Stacks framework router.
36 lines (35 loc) • 1.48 kB
TypeScript
/**
* Check + consume a rate-limit slot for the current scope.
*
* @example
* ```ts
* await rateLimit('create-post', 10).per('hour')
* await rateLimit('login-attempts', 5, { identity: email }).per('minute')
* await rateLimit('expensive-job', 3).over(900) // custom 15-minute ttl
* ```
*/
export declare function rateLimit(key: string, max: number, options?: { identity?: string }): {
/** Run with a string period name (`'minute'`, `'hour'`, …). */
per: (period: Period) => Promise<void>
/** Run with a numeric ttl in seconds. */
over: (ttlSeconds: number) => Promise<void>
};
/**
* Read the current bucket state without consuming a slot. Useful for
* "you have N attempts remaining" hints in dashboards and pre-flight
* checks. Returns `null` if the limiter's storage doesn't expose
* `getCount` (the default memory storage does; redis storage may not).
*/
export declare function rateLimitStatus(key: string, max: number, windowSeconds: number, options?: { identity?: string }): Promise<{ count: number, limit: number, remaining: number } | null>;
/**
* Drop the bucket for the given key (e.g. after a successful login,
* the failed-attempt counter should reset).
*/
export declare function clearRateLimit(key: string, max: number, windowSeconds: number, options?: { identity?: string }): Promise<void>;
declare const PERIOD_SECONDS: {
second: 1;
minute: 60;
hour: 3600;
day: unknown
};
declare type Period = keyof typeof PERIOD_SECONDS;