nestjs/throttler

Docs on WS guard is not updated

TrejGun opened this issue · 2 comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

old docs says

@Injectable()
export class WsThrottlerGuard extends ThrottlerGuard {
  async handleRequest(context: ExecutionContext, limit: number, ttl: number): Promise<boolean> {
    const client = context.switchToWs().getClient();
    const ip = client._socket.remoteAddress;
    const key = this.generateKey(context, ip);
    const { totalHits } = await this.storageService.increment(key, ttl);

    if (totalHits > limit) {
      throw new ThrottlerException();
    }

    return true;
  }
}

but in fact generateKey expects more parameters

image

Minimum reproduction code

https://github.com/nestjs/throttler

Steps to reproduce

No response

Expected behavior

doc is aligned with code

Package version

5

NestJS version

No response

Node.js version

No response

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

looks like it is little bit more complicated than I thought

@Injectable()
export class WsThrottlerGuard extends ThrottlerGuard {
  async handleRequest(
    context: ExecutionContext,
    limit: number,
    ttl: number,
    throttler: ThrottlerOptions,
  ): Promise<boolean> {
    const { req } = this.getRequestResponse(context);
    const tracker = await this.getTracker(req);
    const key = this.generateKey(context, tracker, throttler.name || "default");
    const { totalHits } = await this.storageService.increment(key, ttl);

    if (totalHits > limit) {
      void this.throwThrottlingException();
    }

    return true;
  }

  protected throwThrottlingException(): Promise<void> {
    throw new ThrottlerException("tooManyRequests");
  }
}

btw there is one more small typo

image