Skip to content
This repository was archived by the owner on Nov 23, 2022. It is now read-only.

Commit ec295bb

Browse files
authored
Merge pull request #61 from evle/master
feat: #60
2 parents c4c3592 + 1c53acf commit ec295bb

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

lib/rate-limiter.interceptor.ts

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -179,34 +179,21 @@ export class RateLimiterInterceptor implements NestInterceptor {
179179
}
180180

181181
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+
194187
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())
196191
}
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 {
210197
throw new HttpException(this.spesificOptions?.errorMessage || this.options.errorMessage, HttpStatus.TOO_MANY_REQUESTS)
211198
}
212199
}

lib/rate-limiter.interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Provider } from '@nestjs/common'
22
import { ModuleMetadata, Type } from '@nestjs/common/interfaces'
3+
import { RateLimiterRes } from 'rate-limiter-flexible'
34

45
export interface RateLimiterOptions {
56
for?: 'Express' | 'Fastify' | 'Microservice' | 'ExpressGraphql' | 'FastifyGraphql'
@@ -26,6 +27,7 @@ export interface RateLimiterOptions {
2627
indexKeyPrefix?: {}
2728
maxQueueSize?: number
2829
errorMessage?: string
30+
createErrorBody?: (rateLimiterResponse: RateLimiterRes) => {}
2931
}
3032

3133
export interface RateLimiterOptionsFactory {

0 commit comments

Comments
 (0)