api/core: extract rate limit response to function

This commit is contained in:
dumbmoron 2024-10-04 16:58:15 +00:00
parent 44f7e4f76c
commit 034f7ebe4a
No known key found for this signature in database

View file

@ -57,6 +57,16 @@ export const runAPI = (express, app, __dirname) => {
git, 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({ const apiLimiter = rateLimit({
windowMs: env.rateLimitWindow * 1000, windowMs: env.rateLimitWindow * 1000,
max: env.rateLimitMax, max: env.rateLimitMax,
@ -68,15 +78,7 @@ export const runAPI = (express, app, __dirname) => {
} }
return generateHmac(getIP(req), ipSalt); return generateHmac(getIP(req), ipSalt);
}, },
handler: (req, res) => { handler: handleRateExceeded
const { status, body } = createResponse("error", {
code: "error.api.rate_exceeded",
context: {
limit: env.rateLimitWindow
}
});
return res.status(status).json(body);
}
}) })
const apiLimiterStream = rateLimit({ const apiLimiterStream = rateLimit({