All files / src/platform/gateway/services api-rate-limit-policy.service.ts

0% Statements 0/15
0% Branches 0/2
0% Functions 0/3
0% Lines 0/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 39                                                                             
import { Injectable, Logger } from '@nestjs/common';
 
export interface RateLimitPolicy {
  key: string;
  limit: number; // requests per window
  ttl: number; // window in seconds
  scope: 'ip' | 'user' | 'tenant' | 'global';
}
 
@Injectable()
export class ApiRateLimitPolicyService {
  private readonly logger = new Logger(ApiRateLimitPolicyService.name);
  private readonly policies = new Map<string, RateLimitPolicy>();
 
  constructor() {
    // Register default platform-wide policies
    this.register({ key: 'default', limit: 300, ttl: 60, scope: 'ip' });
    this.register({ key: 'auth', limit: 10, ttl: 60, scope: 'ip' });
    this.register({ key: 'upload', limit: 20, ttl: 60, scope: 'user' });
    this.register({ key: 'ai-execute', limit: 30, ttl: 60, scope: 'tenant' });
    this.register({
      key: 'integration-webhook',
      limit: 200,
      ttl: 60,
      scope: 'ip',
    });
    this.register({ key: 'export', limit: 5, ttl: 60, scope: 'user' });
    this.register({ key: 'public-api', limit: 60, ttl: 60, scope: 'ip' });
  }
 
  register(policy: RateLimitPolicy): void {
    this.policies.set(policy.key, policy);
  }
 
  get(key: string): RateLimitPolicy | undefined {
    return this.policies.get(key) ?? this.policies.get('default');
  }
}