From 034f7ebe4a23c63d2d44eec90fe7d674c8c0eeb7 Mon Sep 17 00:00:00 2001 From: dumbmoron Date: Fri, 4 Oct 2024 16:58:15 +0000 Subject: [PATCH] api/core: extract rate limit response to function --- api/src/core/api.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/api/src/core/api.js b/api/src/core/api.js index 78d4359e..4ed1524d 100644 --- a/api/src/core/api.js +++ b/api/src/core/api.js @@ -57,6 +57,16 @@ export const runAPI = (express, app, __dirname) => { git, }) + const handleRateExceeded = (_, res) => { + const { status, body } = createResponse("error", { + code: "error.api.rate_exceeded", + context: { + limit: env.rateLimitWindow + } + }); + return res.status(status).json(body); + }; + const apiLimiter = rateLimit({ windowMs: env.rateLimitWindow * 1000, max: env.rateLimitMax, @@ -68,15 +78,7 @@ export const runAPI = (express, app, __dirname) => { } return generateHmac(getIP(req), ipSalt); }, - handler: (req, res) => { - const { status, body } = createResponse("error", { - code: "error.api.rate_exceeded", - context: { - limit: env.rateLimitWindow - } - }); - return res.status(status).json(body); - } + handler: handleRateExceeded }) const apiLimiterStream = rateLimit({