aboutsummaryrefslogtreecommitdiffstats
path: root/packages/shared/ratelimiting.ts
blob: 3b22310b6932f6f7f76c826ced57fdc5654a95d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { PluginManager, PluginType } from "./plugins";

export interface RateLimitConfig {
  name: string;
  windowMs: number;
  maxRequests: number;
}

export interface RateLimitResult {
  allowed: boolean;
  resetInSeconds?: number;
}

export interface RateLimitClient {
  /**
   * Check if a request should be allowed based on rate limiting rules
   * @param config Rate limit configuration
   * @param key Unique rate limiting key (e.g., "ip:127.0.0.1:path:/api/v1")
   * @returns Result indicating if the request is allowed and reset time if not
   */
  checkRateLimit(config: RateLimitConfig, key: string): RateLimitResult;

  /**
   * Reset rate limit for a specific key
   * @param config Rate limit configuration
   * @param key Unique rate limiting key
   */
  reset(config: RateLimitConfig, key: string): void;

  /**
   * Clear all rate limit entries
   */
  clear(): void;
}

export async function getRateLimitClient(): Promise<RateLimitClient | null> {
  return PluginManager.getClient(PluginType.RateLimit);
}