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) )