@@ -179,34 +179,21 @@ export class RateLimiterInterceptor implements NestInterceptor {
179
179
}
180
180
181
181
private async responseHandler ( response : any , key : any , rateLimiter : RateLimiterAbstract , points : number , pointsConsumed : number ) {
182
- if ( this . options . for === 'Fastify' || this . options . for === 'FastifyGraphql' ) {
183
- try {
184
- if ( this . spesificOptions ?. queueEnabled || this . options . queueEnabled ) await this . queueLimiter . removeTokens ( 1 )
185
- else {
186
- const rateLimiterResponse : RateLimiterRes = await rateLimiter . consume ( key , pointsConsumed )
187
-
188
- response . header ( 'Retry-After' , Math . ceil ( rateLimiterResponse . msBeforeNext / 1000 ) )
189
- response . header ( 'X-RateLimit-Limit' , points )
190
- response . header ( 'X-Retry-Remaining' , rateLimiterResponse . remainingPoints )
191
- response . header ( 'X-Retry-Reset' , new Date ( Date . now ( ) + rateLimiterResponse . msBeforeNext ) . toUTCString ( ) )
192
- }
193
- } catch ( rateLimiterResponse ) {
182
+ try {
183
+ if ( this . spesificOptions ?. queueEnabled || this . options . queueEnabled ) await this . queueLimiter . removeTokens ( 1 )
184
+ else {
185
+ const rateLimiterResponse : RateLimiterRes = await rateLimiter . consume ( key , pointsConsumed )
186
+
194
187
response . header ( 'Retry-After' , Math . ceil ( rateLimiterResponse . msBeforeNext / 1000 ) )
195
- throw new HttpException ( this . spesificOptions ?. errorMessage || this . options . errorMessage , HttpStatus . TOO_MANY_REQUESTS )
188
+ response . header ( 'X-RateLimit-Limit' , points )
189
+ response . header ( 'X-Retry-Remaining' , rateLimiterResponse . remainingPoints )
190
+ response . header ( 'X-Retry-Reset' , new Date ( Date . now ( ) + rateLimiterResponse . msBeforeNext ) . toUTCString ( ) )
196
191
}
197
- } else {
198
- try {
199
- if ( this . spesificOptions ?. queueEnabled || this . options . queueEnabled ) await this . queueLimiter . removeTokens ( 1 )
200
- else {
201
- const rateLimiterResponse : RateLimiterRes = await rateLimiter . consume ( key , pointsConsumed )
202
-
203
- response . set ( 'Retry-After' , Math . ceil ( rateLimiterResponse . msBeforeNext / 1000 ) )
204
- response . set ( 'X-RateLimit-Limit' , points )
205
- response . set ( 'X-Retry-Remaining' , rateLimiterResponse . remainingPoints )
206
- response . set ( 'X-Retry-Reset' , new Date ( Date . now ( ) + rateLimiterResponse . msBeforeNext ) . toUTCString ( ) )
207
- }
208
- } catch ( rateLimiterResponse ) {
209
- response . set ( 'Retry-After' , Math . ceil ( rateLimiterResponse . msBeforeNext / 1000 ) )
192
+ } catch ( rateLimiterResponse ) {
193
+ response . header ( 'Retry-After' , Math . ceil ( rateLimiterResponse . msBeforeNext / 1000 ) )
194
+ if ( typeof this . options . createErrorBody === 'function' ) {
195
+ throw new HttpException ( this . options . createErrorBody ( rateLimiterResponse ) , HttpStatus . TOO_MANY_REQUESTS )
196
+ } else {
210
197
throw new HttpException ( this . spesificOptions ?. errorMessage || this . options . errorMessage , HttpStatus . TOO_MANY_REQUESTS )
211
198
}
212
199
}
0 commit comments