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>
}
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
Returns
Promise<boolean>
Throws
ThrottlerException
|
shouldSkip()
|
protected shouldSkip(_context: ExecutionContext): Promise<boolean>
Parameters
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
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
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
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
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
Returns
Promise<string>
|