diff --git a/lib/default-options.spec.ts b/lib/default-options.spec.ts index c59906b..8287937 100644 --- a/lib/default-options.spec.ts +++ b/lib/default-options.spec.ts @@ -29,5 +29,6 @@ describe('defaultRateLimiterOptions', () => { expect(Object.keys(defaultRateLimiterOptions.indexKeyPrefix).length).toBe(0) expect(defaultRateLimiterOptions.maxQueueSize).toBe(100) expect(defaultRateLimiterOptions.errorMessage).toBe('Rate limit exceeded') + expect(defaultRateLimiterOptions.customResponseSchema).toBeUndefined() }) }) diff --git a/lib/default-options.ts b/lib/default-options.ts index 4023959..a4edfdc 100644 --- a/lib/default-options.ts +++ b/lib/default-options.ts @@ -24,5 +24,6 @@ export const defaultRateLimiterOptions: RateLimiterOptions = { execEvenlyMinDelayMs: undefined, indexKeyPrefix: {}, maxQueueSize: 100, - errorMessage: 'Rate limit exceeded' + errorMessage: 'Rate limit exceeded', + customResponseSchema: undefined } diff --git a/lib/rate-limiter.interceptor.ts b/lib/rate-limiter.interceptor.ts index 102c552..9e31c9c 100644 --- a/lib/rate-limiter.interceptor.ts +++ b/lib/rate-limiter.interceptor.ts @@ -191,8 +191,9 @@ export class RateLimiterInterceptor implements NestInterceptor { } } catch (rateLimiterResponse) { response.header('Retry-After', Math.ceil(rateLimiterResponse.msBeforeNext / 1000)) - if (typeof this.options.createErrorBody === 'function') { - throw new HttpException(this.options.createErrorBody(rateLimiterResponse), HttpStatus.TOO_MANY_REQUESTS) + if (typeof this.spesificOptions?.customResponseSchema === 'function' || typeof this.options.customResponseSchema === 'function') { + var errorBody = this.spesificOptions?.customResponseSchema || this.options.customResponseSchema; + throw new HttpException(errorBody(rateLimiterResponse), HttpStatus.TOO_MANY_REQUESTS) } else { throw new HttpException(this.spesificOptions?.errorMessage || this.options.errorMessage, HttpStatus.TOO_MANY_REQUESTS) } diff --git a/lib/rate-limiter.interface.ts b/lib/rate-limiter.interface.ts index 02629e4..e61b866 100644 --- a/lib/rate-limiter.interface.ts +++ b/lib/rate-limiter.interface.ts @@ -27,7 +27,7 @@ export interface RateLimiterOptions { indexKeyPrefix?: {} maxQueueSize?: number errorMessage?: string - createErrorBody?: (rateLimiterResponse: RateLimiterRes) => {} + customResponseSchema?: (rateLimiterResponse: RateLimiterRes) => {} } export interface RateLimiterOptionsFactory {