@adonisjs/limiter
Version:
Rate limiting package for AdonisJS framework
63 lines (62 loc) • 2.31 kB
TypeScript
import type { HttpContext } from '@adonisjs/core/http';
import { LimiterResponse } from './response.js';
import type { LimiterManager } from './limiter_manager.js';
import { type ThrottleException } from './errors.js';
import type { LimiterConsumptionOptions, LimiterManagerStoreFactory } from './types.js';
/**
* HttpLimiter is a special type of limiter instance created specifically
* for HTTP requests. It exposes a single method to throttle the request
* using the request ip address or the pre-defined unique key.
*/
export declare class HttpLimiter<KnownStores extends Record<string, LimiterManagerStoreFactory>> {
#private;
constructor(manager: LimiterManager<KnownStores>, options?: LimiterConsumptionOptions);
/**
* Specify the store you want to use during
* the request
*/
store(store: keyof KnownStores): this;
/**
* Specify the number of requests to allow
*/
allowRequests(requests: number): this;
/**
* Specify the duration in seconds or a time expression
* for which the requests to allow.
*
* For example: allowRequests(10).every('1 minute')
*/
every(duration: number | string): this;
/**
* Specify a custom unique key to identify the user.
* Defaults to: request.ip()
*/
usingKey(key: string | number): this;
/**
* Register a callback function to modify the ThrottleException.
*/
limitExceeded(callback: (error: ThrottleException) => void): this;
/**
* Define the block duration. The key will be blocked for the
* specified duration after all the requests have been
* exhausted
*/
blockFor(duration: number | string): this;
/**
* JSON representation of the HTTP limiter
*/
toJSON(): {
inMemoryBlockDuration?: number | string | undefined;
inMemoryBlockOnConsumed?: number | undefined;
requests?: number | undefined;
duration?: string | number | undefined;
blockDuration?: number | string | undefined;
store: keyof KnownStores | undefined;
};
/**
* Throttle request using the pre-defined options. Returns
* LimiterResponse when request is allowed or throws
* an exception.
*/
throttle(prefix: string, ctx: HttpContext): Promise<LimiterResponse>;
}