From b7c7e5eb67d7b537a4a585b2114b6f75c80da681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Thu, 26 Sep 2024 21:37:32 +0200 Subject: [PATCH] reduce optional chaining usage --- index.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 378fe90..fb3f625 100644 --- a/index.js +++ b/index.js @@ -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) } @@ -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