Skip to content

Commit

Permalink
fix: apply merged params in all cases
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Nov 16, 2024
1 parent 1f517d8 commit 8ffe898
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
14 changes: 5 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions test/group-rate-limit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ test('With multiple routes and custom groupId', async (t) => {
config: {
rateLimit: {
max: 2,
timeWindow: 500,
timeWindow: 1000,
groupId: 'group2'
}
}
Expand Down Expand Up @@ -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)
)
Expand Down

0 comments on commit 8ffe898

Please sign in to comment.