api: remove deprecated statuses & clean up related code

This commit is contained in:
wukko 2024-08-06 20:45:04 +06:00
parent 5948cab4fb
commit f96c1cd13b
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
2 changed files with 14 additions and 33 deletions

View file

@ -52,16 +52,13 @@ export function runAPI(express, app, __dirname) {
legacyHeaders: false, legacyHeaders: false,
keyGenerator: req => generateHmac(getIP(req), ipSalt), keyGenerator: req => generateHmac(getIP(req), ipSalt),
handler: (req, res) => { handler: (req, res) => {
return res.status(429).json({ const { status, body } = createResponse("error", {
status: "error", code: "error.rate_exceeded",
error: { context: {
code: "ErrorRateLimit", limit: env.rateLimitWindow
context: {
limit: env.rateLimitWindow
},
text: "ErrorRateLimit" // temporary backwards compatibility
} }
}); });
return res.status(status).json(body);
} }
}) })
@ -104,13 +101,13 @@ export function runAPI(express, app, __dirname) {
app.use('/', express.json({ limit: 1024 })); app.use('/', express.json({ limit: 1024 }));
app.use('/', (err, _, res, next) => { app.use('/', (err, _, res, next) => {
if (err) { if (err) {
return res.status(400).json({ const { status, body } = createResponse("error", {
status: "error", code: "error.body_invalid",
error: { context: {
code: "error.body.invalid", limit: env.rateLimitWindow
}, }
text: "invalid json body", // temporary backwards compatibility
}); });
return res.status(status).json(body);
} }
next(); next();

View file

@ -24,7 +24,6 @@ const apiRequest = {
export function createResponse(responseType, responseData) { export function createResponse(responseType, responseData) {
const internalError = (code) => { const internalError = (code) => {
let error = code || "Internal Server Error";
return { return {
status: 500, status: 500,
body: { body: {
@ -32,7 +31,6 @@ export function createResponse(responseType, responseData) {
error: { error: {
code: code || "Internal Server Error", code: code || "Internal Server Error",
}, },
text: error, // temporary backwards compatibility
critical: true critical: true
} }
} }
@ -42,14 +40,8 @@ export function createResponse(responseType, responseData) {
let status = 200, let status = 200,
response = {}; response = {};
switch(responseType) { if (responseType === "error") {
case "error": status = 400;
status = 400;
break;
case "rate-limit":
status = 429;
break;
} }
switch (responseType) { switch (responseType) {
@ -58,17 +50,9 @@ export function createResponse(responseType, responseData) {
error: { error: {
code: responseData.code, code: responseData.code,
context: responseData?.context, context: responseData?.context,
}, }
text: responseData.code, // temporary backwards compatibility
} }
break; break;
case "success":
case "rate-limit":
response = {
text: responseData.t,
}
break;
case "redirect": case "redirect":
response = { response = {
url: responseData.u, url: responseData.u,