Skip to content

Commit

Permalink
reduce optional chaining usage
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Sep 26, 2024
1 parent 126ea3f commit b7c7e5e
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,18 @@ async function fastifyRateLimit (fastify, settings) {
}

fastify.addHook('onRoute', (routeOptions) => {
if (routeOptions.config?.rateLimit !== undefined) {
if (routeOptions.config?.rateLimit != null) {
if (typeof routeOptions.config.rateLimit === 'object') {
const newPluginComponent = Object.create(pluginComponent)
const mergedRateLimitParams = mergeParams(globalParams, routeOptions.config.rateLimit, { routeInfo: routeOptions })
newPluginComponent.store = pluginComponent.store.child(mergedRateLimitParams)

if (routeOptions?.config?.rateLimit?.groupId) {
const groupId = routeOptions.config.rateLimit.groupId
if (typeof groupId === 'string') {
addRouteRateHook(pluginComponent, globalParams, routeOptions)
} else {
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)
}
Expand Down Expand Up @@ -219,7 +218,7 @@ function rateLimitRequestHandler (pluginComponent, params) {

// Retrieve the key from the generator (the global one or the one defined in the endpoint)
let key = await params.keyGenerator(req)
const groupId = req?.routeOptions?.config?.rateLimit?.groupId
const groupId = req.routeOptions.config?.rateLimit?.groupId

if (groupId) {
key += groupId
Expand Down

0 comments on commit b7c7e5e

Please sign in to comment.