ciscoheat / sveltekit-rate-limiter

A modular rate limiter for SvelteKit. Use in password resets, account registration, etc.
MIT License
216 stars 3 forks source link

Feature request: Expose rate-limit reason #7

Open oscarhermoso opened 7 months ago

oscarhermoso commented 7 months ago

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