UNPKG

@adonisjs/limiter

Version:

Rate limiting package for AdonisJS framework

118 lines (117 loc) 3.46 kB
import { Exception } from '@adonisjs/core/exceptions'; import type { HttpContext } from '@adonisjs/core/http'; import type { LimiterResponse } from './response.ts'; /** * Throttle exception is raised when the user has exceeded * the number of requests allowed during a given duration. * * The exception automatically sets appropriate HTTP headers and * formats the response based on the request's Accept header. */ export declare class ThrottleException extends Exception { response: LimiterResponse; message: string; status: number; code: string; /** * Error identifier to lookup translation message */ identifier: string; /** * The response headers to set when converting exception * to response */ headers?: { [name: string]: any; }; /** * Translation identifier to use for creating throttle * response. */ translation?: { identifier: string; data?: Record<string, any>; }; constructor(response: LimiterResponse, options?: ErrorOptions & { code?: string; status?: number; }); /** * Returns the default rate limit headers that will be sent in the HTTP response. * Includes limit information, remaining requests, retry-after time, and reset time. */ getDefaultHeaders(): { [K: string]: any; }; /** * Returns the message to be sent in the HTTP response. * Supports i18n translations when the i18n package is available. * * @param ctx - The HTTP context */ getResponseMessage(ctx: HttpContext): string; /** * Updates the default error message. * * @param message - The new error message * * @example * ```ts * throw new ThrottleException(response) * .setMessage('Rate limit exceeded. Please try again later.') * ``` */ setMessage(message: string): this; /** * Updates the default error status code. * * @param status - The HTTP status code * * @example * ```ts * throw new ThrottleException(response) * .setStatus(503) * ``` */ setStatus(status: number): this; /** * Defines custom response headers. This will replace the default headers. * * @param headers - Custom headers to set * * @example * ```ts * throw new ThrottleException(response) * .setHeaders({ * 'X-Custom-Header': 'value', * 'Retry-After': 60 * }) * ``` */ setHeaders(headers: { [name: string]: any; }): this; /** * Defines the i18n translation identifier for the throttle response message. * * @param identifier - The translation key * @param data - Optional translation data * * @example * ```ts * throw new ThrottleException(response) * .t('errors.rate_limit_exceeded', { minutes: 5 }) * ``` */ t(identifier: string, data?: Record<string, any>): this; /** * Converts the throttle exception to an HTTP response. * Automatically sets appropriate headers and formats the response * based on the Accept header (HTML, JSON, or JSON:API). * * @param error - The throttle exception instance * @param ctx - The HTTP context */ handle(error: ThrottleException, ctx: HttpContext): Promise<void>; } export declare const E_TOO_MANY_REQUESTS: typeof ThrottleException;