Skip to content

Commit

Permalink
docs(rate-limiting): update WsThrottlerGuard example for v5
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyTseng committed Oct 24, 2023
1 parent 7f393a9 commit 1c4fbc1
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions content/security/rate-limiting.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class AppModule {}

#### Customization

There may be a time where you want to bind the guard to a controller or globally, but want to disable rate limiting for one or more of your endpoints. For that, you can use the `@SkipThrottle()` decorator, to negate the throttler for an entire class or a single route. The `@SkipThrottle()` decorator can also take in an object of string keys with boolean values for if there is a case where you want to exclude _most_ of a controller, but not every route, and configure it per throttler set if you have more than one. If you do not pass an object, the default is to use `{{ '{' }} default: true {{ '}' }}`
There may be a time where you want to bind the guard to a controller or globally, but want to disable rate limiting for one or more of your endpoints. For that, you can use the `@SkipThrottle()` decorator, to negate the throttler for an entire class or a single route. The `@SkipThrottle()` decorator can also take in an object of string keys with boolean values for if there is a case where you want to exclude _most_ of a controller, but not every route, and configure it per throttler set if you have more than one. If you do not pass an object, the default is to use `{{ '{' }} default: true {{ '}' }}`

```typescript
@SkipThrottle()
Expand Down Expand Up @@ -132,10 +132,10 @@ This module can work with websockets, but it requires some class extension. You
```typescript
@Injectable()
export class WsThrottlerGuard extends ThrottlerGuard {
async handleRequest(context: ExecutionContext, limit: number, ttl: number): Promise<boolean> {
async handleRequest(context: ExecutionContext, limit: number, ttl: number, throttler: ThrottlerOptions): Promise<boolean> {
const client = context.switchToWs().getClient();
const ip = client._socket.remoteAddress;
const key = this.generateKey(context, ip);
const key = this.generateKey(context, ip, throttler.name);
const { totalHits } = await this.storageService.increment(key, ttl);

if (totalHits > limit) {
Expand Down Expand Up @@ -231,10 +231,12 @@ One approach would be to use a factory function:
ThrottlerModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ([{
ttl: config.get('THROTTLE_TTL'),
limit: config.get('THROTTLE_LIMIT'),
}]),
useFactory: (config: ConfigService) => [
{
ttl: config.get('THROTTLE_TTL'),
limit: config.get('THROTTLE_LIMIT'),
},
],
}),
],
})
Expand Down

0 comments on commit 1c4fbc1

Please sign in to comment.