@@ -29,7 +29,6 @@ export class RateLimiterInterceptor implements NestInterceptor {
29
29
this . spesificOptions = null
30
30
this . spesificOptions = options
31
31
32
-
33
32
const limiterOptions : RateLimiterOptions = {
34
33
...this . options ,
35
34
...options
@@ -39,7 +38,8 @@ export class RateLimiterInterceptor implements NestInterceptor {
39
38
40
39
let rateLimiter : RateLimiterAbstract = this . rateLimiters . get ( libraryArguments . keyPrefix )
41
40
42
- if ( libraryArguments . execEvenlyMinDelayMs === undefined ) libraryArguments . execEvenlyMinDelayMs = ( this . options . duration * 1000 ) / this . options . points
41
+ if ( libraryArguments . execEvenlyMinDelayMs === undefined )
42
+ libraryArguments . execEvenlyMinDelayMs = ( this . options . duration * 1000 ) / this . options . points
43
43
44
44
if ( ! rateLimiter ) {
45
45
switch ( this . spesificOptions ?. type || this . options . type ) {
@@ -56,7 +56,7 @@ export class RateLimiterInterceptor implements NestInterceptor {
56
56
Logger . log ( `Rate Limiter started with ${ limiterOptions . keyPrefix } key prefix` , 'RateLimiterMemcache' )
57
57
break
58
58
case 'Postgres' :
59
- if ( libraryArguments . storeType === undefined ) libraryArguments . storeType = this . options . storeClient . constructor . name
59
+ if ( libraryArguments . storeType === undefined ) libraryArguments . storeType = this . options . storeClient . constructor . name
60
60
61
61
libraryArguments . tableName = this . spesificOptions ?. tableName || this . options . tableName
62
62
if ( libraryArguments . tableName === undefined ) {
@@ -78,7 +78,7 @@ export class RateLimiterInterceptor implements NestInterceptor {
78
78
Logger . log ( `Rate Limiter started with ${ limiterOptions . keyPrefix } key prefix` , 'RateLimiterPostgres' )
79
79
break
80
80
case 'MySQL' :
81
- if ( libraryArguments . storeType === undefined ) libraryArguments . storeType = this . options . storeClient . constructor . name
81
+ if ( libraryArguments . storeType === undefined ) libraryArguments . storeType = this . options . storeClient . constructor . name
82
82
83
83
libraryArguments . tableName = this . spesificOptions ?. tableName || this . options . tableName
84
84
if ( libraryArguments . tableName === undefined ) {
@@ -100,7 +100,7 @@ export class RateLimiterInterceptor implements NestInterceptor {
100
100
Logger . log ( `Rate Limiter started with ${ limiterOptions . keyPrefix } key prefix` , 'RateLimiterMySQL' )
101
101
break
102
102
case 'Mongo' :
103
- if ( libraryArguments . storeType === undefined ) libraryArguments . storeType = this . options . storeClient . constructor . name
103
+ if ( libraryArguments . storeType === undefined ) libraryArguments . storeType = this . options . storeClient . constructor . name
104
104
105
105
libraryArguments . tableName = this . spesificOptions ?. tableName || this . options . tableName
106
106
if ( libraryArguments . tableName === undefined ) {
@@ -190,15 +190,18 @@ export class RateLimiterInterceptor implements NestInterceptor {
190
190
response . header ( 'X-Retry-Remaining' , rateLimiterResponse . remainingPoints )
191
191
response . header ( 'X-Retry-Reset' , new Date ( Date . now ( ) + rateLimiterResponse . msBeforeNext ) . toUTCString ( ) )
192
192
}
193
- return next . handle ( )
194
193
} catch ( rateLimiterResponse ) {
195
194
response . header ( 'Retry-After' , Math . ceil ( rateLimiterResponse . msBeforeNext / 1000 ) )
196
- response . code ( 429 ) . header ( 'Content-Type' , 'application/json; charset=utf-8' ) . send ( {
197
- statusCode : HttpStatus . TOO_MANY_REQUESTS ,
198
- error : 'Too Many Requests' ,
199
- message : this . spesificOptions ?. errorMessage || this . options . errorMessage
200
- } )
195
+ response
196
+ . code ( 429 )
197
+ . header ( 'Content-Type' , 'application/json; charset=utf-8' )
198
+ . send ( {
199
+ statusCode : HttpStatus . TOO_MANY_REQUESTS ,
200
+ error : 'Too Many Requests' ,
201
+ message : this . spesificOptions ?. errorMessage || this . options . errorMessage
202
+ } )
201
203
}
204
+ return next . handle ( )
202
205
} else {
203
206
try {
204
207
if ( this . spesificOptions ?. queueEnabled || this . options . queueEnabled ) await this . queueLimiter . removeTokens ( 1 )
@@ -210,7 +213,6 @@ export class RateLimiterInterceptor implements NestInterceptor {
210
213
response . set ( 'X-Retry-Remaining' , rateLimiterResponse . remainingPoints )
211
214
response . set ( 'X-Retry-Reset' , new Date ( Date . now ( ) + rateLimiterResponse . msBeforeNext ) . toUTCString ( ) )
212
215
}
213
- return next . handle ( )
214
216
} catch ( rateLimiterResponse ) {
215
217
response . set ( 'Retry-After' , Math . ceil ( rateLimiterResponse . msBeforeNext / 1000 ) )
216
218
response . status ( 429 ) . json ( {
@@ -219,6 +221,7 @@ export class RateLimiterInterceptor implements NestInterceptor {
219
221
message : this . spesificOptions ?. errorMessage || this . options . errorMessage
220
222
} )
221
223
}
224
+ return next . handle ( )
222
225
}
223
226
}
224
227
}
0 commit comments