11import type { MiddlewareConsumer , Provider , Type } from '@nestjs/common' ;
22import { APP_FILTER , APP_GUARD , APP_PIPE } from '@nestjs/core' ;
33import { ThrottlerGuard , ThrottlerModule } from '@nestjs/throttler' ;
4+ import type { ThrottlerModuleOptions } from '@nestjs/throttler' ;
45import type { Simplify } from 'type-fest' ;
56import type { z } from 'zod/v4' ;
67
@@ -24,6 +25,7 @@ export type CreateAppOptions = Simplify<
2425 envSchema : z . ZodType < UserTypes . Env > ;
2526 imports ?: ( ConditionalImport | ImportedModule ) [ ] ;
2627 providers ?: Provider [ ] ;
28+ throttler ?: ThrottlerModuleOptions ;
2729 }
2830> ;
2931
@@ -39,9 +41,26 @@ export class AppFactory {
3941 controllers,
4042 envConfig,
4143 imports : userImports = [ ] ,
42- providers : userProviders = [ ]
44+ providers : userProviders = [ ] ,
45+ throttler = [
46+ {
47+ limit : 25 ,
48+ name : 'short' ,
49+ ttl : 1000
50+ } ,
51+ {
52+ limit : 100 ,
53+ name : 'medium' ,
54+ ttl : 10_000
55+ } ,
56+ {
57+ limit : 250 ,
58+ name : 'long' ,
59+ ttl : 60_000
60+ }
61+ ]
4362 } : Simplify <
44- Pick < CreateAppOptions , 'configureMiddleware' | 'controllers' | 'imports' | 'providers' > & {
63+ Pick < CreateAppOptions , 'configureMiddleware' | 'controllers' | 'imports' | 'providers' | 'throttler' > & {
4564 envConfig : UserTypes . Env ;
4665 }
4766 > ) : DynamicAppModule {
@@ -65,34 +84,7 @@ export class AppFactory {
6584 }
6685
6786 if ( envConfig . THROTTLER_ENABLED ) {
68- coreImports . push (
69- ThrottlerModule . forRoot ( [
70- {
71- limit : 25 ,
72- name : 'short' ,
73- ttl : 1000
74- } ,
75- {
76- limit : 100 ,
77- name : 'medium' ,
78- ttl : 10_000
79- } ,
80- {
81- limit : 250 ,
82- name : 'long' ,
83- ttl : 60_000
84- } ,
85- {
86- limit : envConfig . LOGIN_REQUEST_TROTTLER_LIMIT ?? 50 ,
87- name : 'login' ,
88- skipIf : ( context ) : boolean => {
89- const handler = context . getHandler ( ) ;
90- return handler . name !== 'login' ;
91- } ,
92- ttl : envConfig . LOGIN_REQUEST_TROTTLER_TTL ?? 60_000
93- }
94- ] )
95- ) ;
87+ coreImports . push ( ThrottlerModule . forRoot ( throttler ) ) ;
9688 coreProviders . push ( {
9789 provide : APP_GUARD ,
9890 useClass : ThrottlerGuard
0 commit comments