sveltekit-rate-limiter icon indicating copy to clipboard operation
sveltekit-rate-limiter copied to clipboard

Feature request: Expose rate-limit reason

Open oscarhermoso opened this issue 1 year ago • 0 comments

For debugging/logging/testing, it would be valuable to know the reason for being rate limited.

For example:

const status = await limiter.check(event);
  if (status.limited) {
    console.warn(
      'Rate limit activated:',
      event.getClientAddress(),
      status.limitedBy // <-- new, expose which limiter blocked the request
    );
    let response = new Response(
      `You are being rate limited. Please try after ${status.retryAfter} seconds.`,
      {
        status: 429,
        headers: { 'Retry-After': status.retryAfter.toString() }
      }
    );
    return response;
  }

Proposed limitedBy property would have a type signature of something like this:

type LimitedBy = "IP" | "IPUA" | "cookies" | `plugins[${number}]` | undefined

oscarhermoso avatar Mar 23 '24 01:03 oscarhermoso