From 8ffe89889a75f41089c669d77efd3e8cb14e7e9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Sat, 16 Nov 2024 16:03:10 +0100 Subject: [PATCH] fix: apply merged params in all cases --- index.js | 14 +++++--------- test/group-rate-limit.test.js | 4 ++-- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 80d81c2..7d14ec5 100644 --- a/index.js +++ b/index.js @@ -145,15 +145,7 @@ async function fastifyRateLimit (fastify, settings) { const mergedRateLimitParams = mergeParams(globalParams, routeOptions.config.rateLimit, { routeInfo: routeOptions }) newPluginComponent.store = pluginComponent.store.child(mergedRateLimitParams) - if (routeOptions.config.rateLimit.groupId) { - if (typeof routeOptions.config.rateLimit.groupId !== 'string') { - throw new Error('groupId must be a string') - } - - addRouteRateHook(pluginComponent, globalParams, routeOptions) - } else { - addRouteRateHook(newPluginComponent, mergedRateLimitParams, routeOptions) - } + addRouteRateHook(newPluginComponent, mergedRateLimitParams, routeOptions) } else if (routeOptions.config.rateLimit !== false) { throw new Error('Unknown value for route rate-limit configuration') } @@ -187,6 +179,10 @@ function mergeParams (...params) { result.ban = -1 } + if (result.groupId !== undefined && typeof result.groupId !== 'string') { + throw new Error('groupId must be a string') + } + return result } diff --git a/test/group-rate-limit.test.js b/test/group-rate-limit.test.js index 038c767..8156639 100644 --- a/test/group-rate-limit.test.js +++ b/test/group-rate-limit.test.js @@ -140,7 +140,7 @@ test('With multiple routes and custom groupId', async (t) => { config: { rateLimit: { max: 2, - timeWindow: 500, + timeWindow: 1000, groupId: 'group2' } } @@ -203,7 +203,7 @@ test('With multiple routes and custom groupId', async (t) => { { statusCode: 429, error: 'Too Many Requests', - message: 'Rate limit exceeded, retry in 500 ms' + message: 'Rate limit exceeded, retry in 1 second' }, JSON.parse(res.payload) )