Skip to content

Commit aeafcd2

Browse files
fix: configure rate limit max points (#112)
1 parent aa8569c commit aeafcd2

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/middlewares/rateLimit.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ export class RateLimitMiddleware<S extends Record<string, any>, C extends RateLi
1919
return !req.headers.authorization;
2020
}
2121

22-
async consume(res: Response, next: NextFunction, { resource, isPublic }: { resource: string; isPublic?: boolean }): Promise<void> {
23-
const maxPoints = isPublic ? this.config.RATE_LIMITER_POINTS_PUBLIC : this.config.RATE_LIMITER_POINTS_PRIVATE;
24-
const rateLimiterClient = this.services.rateLimitClient[isPublic ? 'public' : 'private'];
22+
async consume(res: Response, next: NextFunction, options: { resource: string; isPublic?: boolean; maxPoints?: number }): Promise<void> {
23+
const maxPoints = options.maxPoints ?? options.isPublic ? this.config.RATE_LIMITER_POINTS_PUBLIC : this.config.RATE_LIMITER_POINTS_PRIVATE;
24+
const rateLimiterClient = this.services.rateLimitClient[options.isPublic ? 'public' : 'private'];
2525

2626
try {
27-
const rateLimiterRes = await rateLimiterClient.consume(resource!);
27+
const rateLimiterRes = await rateLimiterClient.consume(options.resource!);
2828

2929
RateLimitMiddleware.setHeaders(res, rateLimiterRes, maxPoints);
3030
} catch (rateLimiterRes) {
3131
res.setHeader('Retry-After', Math.floor(rateLimiterRes.msBeforeNext / 1000));
3232

3333
RateLimitMiddleware.setHeaders(res, rateLimiterRes, maxPoints);
3434

35-
throw new VError('Too Many Request', VError.HTTP_STATUS.TOO_MANY_REQUESTS);
35+
throw new VError('Too Many Requests', VError.HTTP_STATUS.TOO_MANY_REQUESTS);
3636
}
3737

3838
return next();

0 commit comments

Comments
 (0)