refactor: ♻️ Use native Hono return functions instead of custom ones

This commit is contained in:
Jesse Wierzbinski 2024-08-19 21:03:59 +02:00 committed by April John
parent 1a8e063f87
commit a601bc856e
105 changed files with 639 additions and 581 deletions

View file

@ -1,4 +1,3 @@
import { errorResponse } from "@/response";
import { createMiddleware } from "@hono/hono/factory";
import { config } from "~/packages/config-manager";
@ -8,7 +7,7 @@ export const agentBans = createMiddleware(async (context, next) => {
for (const agent of config.http.banned_user_agents) {
if (new RegExp(agent).test(ua)) {
return errorResponse("Forbidden", 403);
return context.json({ error: "Forbidden" }, 403);
}
}

View file

@ -1,4 +1,3 @@
import { errorResponse } from "@/response";
import { createMiddleware } from "@hono/hono/factory";
export const boundaryCheck = createMiddleware(async (context, next) => {
@ -7,8 +6,10 @@ export const boundaryCheck = createMiddleware(async (context, next) => {
if (contentType?.includes("multipart/form-data")) {
if (!contentType.includes("boundary")) {
return errorResponse(
"You are sending a request with a multipart/form-data content type but without a boundary. Please include a boundary in the Content-Type header. For more information, visit https://stackoverflow.com/questions/3508338/what-is-the-boundary-in-multipart-form-data",
return context.json(
{
error: "You are sending a request with a multipart/form-data content type but without a boundary. Please include a boundary in the Content-Type header. For more information, visit https://stackoverflow.com/questions/3508338/what-is-the-boundary-in-multipart-form-data",
},
400,
);
}

View file

@ -1,4 +1,3 @@
import { errorResponse } from "@/response";
import { sentry } from "@/sentry";
import { createMiddleware } from "@hono/hono/factory";
import { getLogger } from "@logtape/logtape";
@ -19,7 +18,7 @@ export const ipBans = createMiddleware(async (context, next) => {
for (const ip of config.http.banned_ips) {
try {
if (matches(ip, requestIp?.address)) {
return errorResponse("Forbidden", 403);
return context.json({ error: "Forbidden" }, 403);
}
} catch (e) {
const logger = getLogger("server");
@ -28,8 +27,8 @@ export const ipBans = createMiddleware(async (context, next) => {
logger.error`${e}`;
sentry?.captureException(e);
return errorResponse(
`A server error occured: ${(e as Error).message}`,
return context.json(
{ error: `A server error occured: ${(e as Error).message}` },
500,
);
}

View file

@ -1,4 +1,3 @@
import { errorResponse } from "@/response";
import { createMiddleware } from "@hono/hono/factory";
import { config } from "~/packages/config-manager";
@ -7,8 +6,10 @@ export const urlCheck = createMiddleware(async (context, next) => {
const baseUrl = new URL(config.http.base_url);
if (new URL(context.req.url).origin !== baseUrl.origin) {
return errorResponse(
`Request URL ${context.req.url} does not match base URL ${baseUrl.origin}`,
return context.json(
{
error: `Request URL ${context.req.url} does not match base URL ${baseUrl.origin}`,
},
400,
);
}