ThrottlerGuard

  
class ThrottlerGuard implements CanActivate {
  protected headerPrefix: 'X-RateLimit'
  protected errorMessage: throttlerMessage
  protected options: ThrottlerModuleOptions
  protected storageService: ThrottlerStorage
  protected reflector: Reflector
  canActivate(context: ExecutionContext): Promise<boolean>
  protected handleRequest(context: ExecutionContext, limit: number, ttl: number): Promise<boolean>
  protected getTracker(req: Record<string, any>): string
  protected getRequestResponse(context: ExecutionContext): {...}
  protected generateKey(context: ExecutionContext, suffix: string): string
  protected throwThrottlingException(context: ExecutionContext): void
}

Properties

Property Description
protected headerPrefix: 'X-RateLimit'
protected errorMessage: throttlerMessage
protected options: ThrottlerModuleOptions Read-only.
protected storageService: ThrottlerStorage Read-only.
protected reflector: Reflector Read-only.

Methods

canActivate()

Throttle requests against their TTL limit and whether to allow or deny it. Based on the context type different handlers will be called.


canActivate(context: ExecutionContext): Promise<boolean>

Parameters

Option Type Description
context ExecutionContext

Returns

Promise<boolean>

Throws

ThrottlerException

handleRequest()

Throttles incoming HTTP requests. All the outgoing requests will contain RFC-compatible RateLimit headers.


protected handleRequest(context: ExecutionContext, limit: number, ttl: number): Promise<boolean>

Parameters

Option Type Description
context ExecutionContext
limit number
ttl number

Returns

Promise<boolean>

Throws

ThrottlerException

getTracker()


protected getTracker(req: Record<string, any>): string

Parameters

Option Type Description
req Record

Returns

string

getRequestResponse()


protected getRequestResponse(context: ExecutionContext): {
    req: Record<string, any>;
    res: Record<string, any>;
}

Parameters

Option Type Description
context ExecutionContext

Returns

{ req: Record<string, any>; res: Record<string, any>; }

generateKey()

Generate a hashed key that will be used as a storage key. The key will always be a combination of the current context and IP.


protected generateKey(context: ExecutionContext, suffix: string): string

Parameters

Option Type Description
context ExecutionContext
suffix string

Returns

string

throwThrottlingException()

Throws an exception for the event that the rate limit has been exceeded.


protected throwThrottlingException(context: ExecutionContext): void

Parameters

Option Type Description
context ExecutionContext

Returns

void

Throws

ThrottlerException

The context parameter allows to access the context when overwriting the method.