UNPKG

@adonisjs/limiter

Version:

Rate limiting package for AdonisJS framework

56 lines (55 loc) 2.08 kB
import type { HttpContext } from '@adonisjs/core/http'; import type { MiddlewareFn } from '@adonisjs/core/types/http'; import { Limiter } from './limiter.js'; import { HttpLimiter } from './http_limiter.js'; import type { LimiterConsumptionOptions, LimiterManagerStoreFactory } from './types.js'; /** * Limiter manager is used to manage multiple rate limiters * using different storage providers. * * Also, you can create limiter instances with runtime options * for "requests", "duration", and "blockDuration". */ export declare class LimiterManager<KnownStores extends Record<string, LimiterManagerStoreFactory>> { #private; config: { default: keyof KnownStores; stores: KnownStores; }; constructor(config: { default: keyof KnownStores; stores: KnownStores; }); /** * Creates a unique key for a limiter instance. Since, we allow creating * limiters with runtime options for "requests", "duration" and "blockDuration". * The limiterKey is used to identify a limiter instance. */ protected makeLimiterKey(store: keyof KnownStores, options: LimiterConsumptionOptions): string; /** * Make a limiter instance for a given store and with * runtime options. * * Caches instances forever for the lifecycle of the process. */ use(options: LimiterConsumptionOptions): Limiter; use<K extends keyof KnownStores>(store: K, options: LimiterConsumptionOptions): Limiter; /** * Clear stored data with the stores */ clear(stores?: Extract<keyof KnownStores, string>[]): Promise<void>; /** * Creates HTTP limiter instance */ allowRequests(requests: number): HttpLimiter<KnownStores>; /** * A shorthand method that returns null to disable * rate limiting */ noLimit(): null; /** * Define a named HTTP middleware to apply rate * limits on specific routes */ define(name: string, builder: (ctx: HttpContext) => HttpLimiter<any> | null | Promise<HttpLimiter<any>> | Promise<null>): MiddlewareFn; }