1. How many Requests Allowed
  2. Window (TTl) - over what period
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ItemsModule } from './items/items.module';
import { CacheModule } from '@nestjs/cache-manager';
import { Throttle, ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
import { APP_GUARD } from '@nestjs/core';

@Module({
  imports: [
    CacheModule.register({
      isGlobal: true,
      rrl: 10000,
    }),
    ThrottlerModule.forRoot([
      {
        ttl: 60000,
        limit: 5,
      },
    ]),
    ItemsModule,
  ],
  controllers: [AppController],
  providers: [
    {
      provide: APP_GUARD,
      useClass: ThrottlerGuard,
    },
    AppService,
  ],
})
export class AppModule {}

image.png

import {
  Body,
  Controller,
  Get,
  Param,
  ParseIntPipe,
  Post,
  UseInterceptors,
} from '@nestjs/common';
import { ItemsService } from './items.service';
import { CacheInterceptor } from '@nestjs/cache-manager';
import { Throttle } from '@nestjs/throttler';

@Controller('items')
export class ItemsController {
  constructor(private readonly itemsService: ItemsService) {}

  @UseInterceptors(CacheInterceptor)
  @Get()
  findAll() {
    return this.itemsService.findAll();
  }

  @Throttle({ default: { limit: 2, ttl: 10000 } }) // 2 per 10 seconds now
  @Get(':id')
  findOne(@Param('id', ParseIntPipe) id: number) {
    return this.itemsService.findOne(id);
  }

  @Post()
  addItem(@Body('name') name: string) {
    return this.itemsService.addItem(name);
  }
}

output - 429

image.png