Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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