Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: reduce optional chaining usage #382

Merged
merged 1 commit into from
Sep 26, 2024
Merged
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
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