ThrottlerGuard

  
class ThrottlerGuard implements CanActivate {
  protected headerPrefix: 'X-RateLimit'
  protected errorMessage: throttlerMessage
  protected throttlers: Array<ThrottlerOptions>
  protected commonOptions: Pick<ThrottlerOptions, 'skipIf' | 'ignoreUserAgents' | 'getTracker' | 'generateKey'>
  protected options: ThrottlerModuleOptions
  protected storageService: ThrottlerStorage
  protected reflector: Reflector
  onModuleInit()
  canActivate(context: ExecutionContext): Promise<boolean>
  protected shouldSkip(_context: ExecutionContext): Promise<boolean>
  protected handleRequest(requestProps: ThrottlerRequest): Promise<boolean>
  protected getTracker(req: Record<string, any>): Promise<string>
  protected getRequestResponse(context: ExecutionContext): {...}
  protected generateKey(context: ExecutionContext, suffix: string, name: string): string
  protected throwThrottlingException(context: ExecutionContext, throttlerLimitDetail: ThrottlerLimitDetail): Promise<void>
  protected getErrorMessage(context: ExecutionContext, throttlerLimitDetail: ThrottlerLimitDetail): Promise<string>
}

Properties

Property Description
protected headerPrefix: 'X-RateLimit'
protected errorMessage: throttlerMessage
protected throttlers: Array<ThrottlerOptions>
protected commonOptions: Pick<ThrottlerOptions, 'skipIf' | 'ignoreUserAgents' | 'getTracker' | 'generateKey'>
protected options: ThrottlerModuleOptions Read-only.
protected storageService: ThrottlerStorage Read-only.
protected reflector: Reflector Read-only.

Methods

onModuleInit()


onModuleInit()

Parameters

There are no parameters.

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

shouldSkip()


protected shouldSkip(_context: ExecutionContext): Promise<boolean>

Parameters

Option Type Description
_context ExecutionContext

Returns

Promise<boolean>

handleRequest()

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


protected handleRequest(requestProps: ThrottlerRequest): Promise<boolean>

Parameters

Option Type Description
requestProps ThrottlerRequest

Returns

Promise<boolean>

Throws

ThrottlerException

getTracker()


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

Parameters

Option Type Description
req Record

Returns

Promise<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, name: string): string

Parameters

Option Type Description
context ExecutionContext
suffix string
name string

Returns

string

throwThrottlingException()

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


protected throwThrottlingException(context: ExecutionContext, throttlerLimitDetail: ThrottlerLimitDetail): Promise<void>

Parameters

Option Type Description
context ExecutionContext
throttlerLimitDetail ThrottlerLimitDetail

Returns

Promise<void>

Throws

ThrottlerException

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

getErrorMessage()


protected getErrorMessage(context: ExecutionContext, throttlerLimitDetail: ThrottlerLimitDetail): Promise<string>

Parameters

Option Type Description
context ExecutionContext
throttlerLimitDetail ThrottlerLimitDetail

Returns

Promise<string>