From f96c1cd13bc9981bac9e20ff19f56de8c6db61d5 Mon Sep 17 00:00:00 2001 From: wukko Date: Tue, 6 Aug 2024 20:45:04 +0600 Subject: [PATCH] api: remove deprecated statuses & clean up related code --- api/src/core/api.js | 25 +++++++++++-------------- api/src/processing/request.js | 22 +++------------------- 2 files changed, 14 insertions(+), 33 deletions(-) diff --git a/api/src/core/api.js b/api/src/core/api.js index fee4007d..1de90033 100644 --- a/api/src/core/api.js +++ b/api/src/core/api.js @@ -52,16 +52,13 @@ export function runAPI(express, app, __dirname) { legacyHeaders: false, keyGenerator: req => generateHmac(getIP(req), ipSalt), handler: (req, res) => { - return res.status(429).json({ - status: "error", - error: { - code: "ErrorRateLimit", - context: { - limit: env.rateLimitWindow - }, - text: "ErrorRateLimit" // temporary backwards compatibility + const { status, body } = createResponse("error", { + code: "error.rate_exceeded", + context: { + limit: env.rateLimitWindow } }); + return res.status(status).json(body); } }) @@ -104,13 +101,13 @@ export function runAPI(express, app, __dirname) { app.use('/', express.json({ limit: 1024 })); app.use('/', (err, _, res, next) => { if (err) { - return res.status(400).json({ - status: "error", - error: { - code: "error.body.invalid", - }, - text: "invalid json body", // temporary backwards compatibility + const { status, body } = createResponse("error", { + code: "error.body_invalid", + context: { + limit: env.rateLimitWindow + } }); + return res.status(status).json(body); } next(); diff --git a/api/src/processing/request.js b/api/src/processing/request.js index e90d17d2..22e34969 100644 --- a/api/src/processing/request.js +++ b/api/src/processing/request.js @@ -24,7 +24,6 @@ const apiRequest = { export function createResponse(responseType, responseData) { const internalError = (code) => { - let error = code || "Internal Server Error"; return { status: 500, body: { @@ -32,7 +31,6 @@ export function createResponse(responseType, responseData) { error: { code: code || "Internal Server Error", }, - text: error, // temporary backwards compatibility critical: true } } @@ -42,14 +40,8 @@ export function createResponse(responseType, responseData) { let status = 200, response = {}; - switch(responseType) { - case "error": - status = 400; - break; - - case "rate-limit": - status = 429; - break; + if (responseType === "error") { + status = 400; } switch (responseType) { @@ -58,17 +50,9 @@ export function createResponse(responseType, responseData) { error: { code: responseData.code, context: responseData?.context, - }, - text: responseData.code, // temporary backwards compatibility + } } break; - case "success": - case "rate-limit": - response = { - text: responseData.t, - } - break; - case "redirect": response = { url: responseData.u,