Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
Inject service into NestJS guard

We use ThrottlerGuard in our NestJS application from @nestjs/throttler package to rate limit connections and it's implemented like this:

@Injectable()
export class RateLimiter extends ThrottlerGuard {
  RATE_LIMIT_EXCEEDED = 'Rate limit exceeded.';

  async handleRequest(context: ExecutionContext, limit: number, ttl: number) {
    const wsData = context.switchToWs().getData();
    const metadata: MetadataObject = wsData.internalRepr;
    const clientTokenMetadata = metadata.get(TOKEN_NAME) as MetadataValue[];
    const clientToken = clientTokenMetadata[0].toString();
    const tokensArray = await this.storageService.getRecord(clientToken);

    if (tokensArray.length >= limit) {
      throw new RpcException({
        code: Status.UNAVAILABLE,
        message: this.RATE_LIMIT_EXCEEDED,
      });
    }

    await this.storageService.addRecord(clientToken, ttl);

    return true;
  }
}

Now, I need to inject another service, let's say ConfigService with the usage of constructor, but because it is an extended class, I need to call super(); method which required three more original arguments inherited from ThrottlerGuard, but they are not exported from the package. Is there any other way to make it work besides the method with DI in constructor?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

What you can do, instead of extending ThrottleGuard is injecting it in your constructor and call canActivate() in handleRequest(). Benefit when not extending you don'

I haven't tested this but you can try the following:

import {ExecutionContext, Injectable} from "@nestjs/common";
import {ConfigService} from "@nestjs/config";
import {InjectThrottlerStorage, ThrottlerGuard, ThrottlerStorage} from '@nestjs/throttler'

@Injectable()
export class RateLimiter {

  constructor(
        private throttle: ThrottlerGuard,
        private configService: ConfigService,
        @InjectThrottlerStorage() private storage: ThrottlerStorage) {
  }

  async handleRequest(context: ExecutionContext, limit: number, ttl: number) {
    // TODO something with ConfigService
    return this.throttle.canActivate(context);
  }
}

ThrottleGuard source code

about 4 years ago · Juan Pablo Isaza Report

0

read the docs: Property-based injection

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!