forked from Shared/esg
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import { Module } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
|
|
import { APP_GUARD } from '@nestjs/core';
|
|
import { UsersModule } from '../users/users.module';
|
|
import { UserRateLimitService } from './user-rate-limit.service';
|
|
import { UserRateLimitGuard } from './user-rate-limit.guard';
|
|
|
|
/**
|
|
* Global rate limiting via @nestjs/throttler plus per-user limits for inquiry routes.
|
|
*/
|
|
@Module({
|
|
imports: [
|
|
UsersModule,
|
|
ThrottlerModule.forRootAsync({
|
|
inject: [ConfigService],
|
|
useFactory: (config: ConfigService) => [
|
|
{
|
|
ttl: config.get<number>('throttle.ttl', 60) * 1000,
|
|
limit: config.get<number>('throttle.limit', 100),
|
|
// Keep the guard registered so route-level metadata remains intact,
|
|
// but allow local batch tests to disable enforcement explicitly.
|
|
skipIf: () => !config.get<boolean>('throttle.enabled', true),
|
|
},
|
|
],
|
|
}),
|
|
],
|
|
providers: [
|
|
UserRateLimitService,
|
|
UserRateLimitGuard,
|
|
{
|
|
provide: APP_GUARD,
|
|
useClass: ThrottlerGuard,
|
|
},
|
|
],
|
|
/** Re-export UsersModule so importers (e.g. InquiryModule) can resolve UserRateLimitGuard deps. */
|
|
exports: [UserRateLimitService, UserRateLimitGuard, UsersModule],
|
|
})
|
|
export class RateLimitModule {}
|