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

Commit 498a829

Browse files
committed
provide customResponseSchema feature
1 parent 2035b9d commit 498a829

File tree

4 files changed

+7
-4
lines changed

4 files changed

+7
-4
lines changed

lib/default-options.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ describe('defaultRateLimiterOptions', () => {
2929
expect(Object.keys(defaultRateLimiterOptions.indexKeyPrefix).length).toBe(0)
3030
expect(defaultRateLimiterOptions.maxQueueSize).toBe(100)
3131
expect(defaultRateLimiterOptions.errorMessage).toBe('Rate limit exceeded')
32+
expect(defaultRateLimiterOptions.customResponseSchema).toBeUndefined()
3233
})
3334
})

lib/default-options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ export const defaultRateLimiterOptions: RateLimiterOptions = {
2424
execEvenlyMinDelayMs: undefined,
2525
indexKeyPrefix: {},
2626
maxQueueSize: 100,
27-
errorMessage: 'Rate limit exceeded'
27+
errorMessage: 'Rate limit exceeded',
28+
customResponseSchema: undefined
2829
}

lib/rate-limiter.interceptor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,9 @@ export class RateLimiterInterceptor implements NestInterceptor {
191191
}
192192
} catch (rateLimiterResponse) {
193193
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)
194+
if (typeof this.spesificOptions?.customResponseSchema === 'function' || typeof this.options.customResponseSchema === 'function') {
195+
var errorBody = this.spesificOptions?.customResponseSchema || this.options.customResponseSchema;
196+
throw new HttpException(errorBody(rateLimiterResponse), HttpStatus.TOO_MANY_REQUESTS)
196197
} else {
197198
throw new HttpException(this.spesificOptions?.errorMessage || this.options.errorMessage, HttpStatus.TOO_MANY_REQUESTS)
198199
}

lib/rate-limiter.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface RateLimiterOptions {
2727
indexKeyPrefix?: {}
2828
maxQueueSize?: number
2929
errorMessage?: string
30-
createErrorBody?: (rateLimiterResponse: RateLimiterRes) => {}
30+
customResponseSchema?: (rateLimiterResponse: RateLimiterRes) => {}
3131
}
3232

3333
export interface RateLimiterOptionsFactory {

0 commit comments

Comments
 (0)