import { Module } from '@nestjs/common'; import { ConfigModule, ConfigService } from '@nestjs/config'; import { ThrottlerModule } from '@nestjs/throttler'; import { ThrottlerStorageRedisService } from '@nest-lab/throttler-storage-redis'; @Module({ imports: [ ThrottlerModule.forRootAsync({ imports: [ConfigModule], inject: [ConfigService], useFactory: (configService: ConfigService) => { // Use in-memory storage with very high limits for test environment const isTest = process.env.NODE_ENV === 'test' || process.env.JEST_WORKER_ID !== undefined; if (isTest) { return { throttlers: [ { name: 'default', ttl: 60000, limit: 1000000, // Effectively disable throttling in tests }, { name: 'auth', ttl: 60000, limit: 1000000, // Effectively disable throttling in tests }, ], // No storage specified = uses in-memory storage }; } const host = configService.get('redis.host') || 'localhost'; const port = Number(configService.get('redis.port') || 6379); const password = configService.get('redis.password'); const db = configService.get('redis.db'); const globalTtl = configService.get('throttle.global.ttl'); const globalLimit = configService.get('throttle.global.limit'); const authTtl = configService.get('throttle.auth.ttl'); const authLimit = configService.get('throttle.auth.limit'); return { throttlers: [ { name: 'default', ttl: globalTtl, limit: globalLimit, }, { name: 'auth', ttl: authTtl, limit: authLimit, }, ], storage: new ThrottlerStorageRedisService({ host, port, password, db, }), }; }, }), ], }) export class AppThrottleModule { }