fix(api): 🏷️ Use context.body for 204 responses

This commit is contained in:
Jesse Wierzbinski 2024-12-30 16:18:28 +01:00
parent 6af6bde12a
commit a7b29d563e
No known key found for this signature in database
8 changed files with 17 additions and 13 deletions

View file

@ -146,7 +146,7 @@ export default apiRoute((app) => {
await role.linkUser(targetUser.id); await role.linkUser(targetUser.id);
return context.text("", 204); return context.body(null, 204);
}); });
app.openapi(routeDelete, async (context) => { app.openapi(routeDelete, async (context) => {
@ -186,6 +186,6 @@ export default apiRoute((app) => {
await role.unlinkUser(targetUser.id); await role.unlinkUser(targetUser.id);
return context.text("", 204); return context.body(null, 204);
}); });
}); });

View file

@ -362,6 +362,6 @@ export default apiRoute((app) => {
await db.delete(Emojis).where(eq(Emojis.id, id)); await db.delete(Emojis).where(eq(Emojis.id, id));
return context.text("", 204); return context.body(null, 204);
}); });
}); });

View file

@ -52,7 +52,7 @@ const routePut = createRoute({
}, },
}, },
responses: { responses: {
204: { 200: {
description: "Media updated", description: "Media updated",
content: { content: {
"application/json": { "application/json": {
@ -147,10 +147,10 @@ export default apiRoute((app) => {
thumbnailUrl, thumbnailUrl,
}); });
return context.json(attachment.toApi(), 204); return context.json(attachment.toApi(), 200);
} }
return context.json(attachment.toApi(), 204); return context.json(attachment.toApi(), 200);
}); });
app.openapi(routeGet, async (context) => { app.openapi(routeGet, async (context) => {

View file

@ -226,7 +226,7 @@ export default apiRoute((app) => {
visible, visible,
}); });
return context.text("", 204); return context.body(null, 204);
}); });
app.openapi(routeDelete, async (context) => { app.openapi(routeDelete, async (context) => {
@ -261,6 +261,6 @@ export default apiRoute((app) => {
await role.delete(); await role.delete();
return context.text("", 204); return context.body(null, 204);
}); });
}); });

View file

@ -343,6 +343,6 @@ export default apiRoute((app) => {
.delete(Filters) .delete(Filters)
.where(and(eq(Filters.userId, user.id), eq(Filters.id, id))); .where(and(eq(Filters.userId, user.id), eq(Filters.id, id)));
return context.text("", 204); return context.body(null, 204);
}); });
}); });

View file

@ -1,6 +1,6 @@
import { apiRoute, applyConfig } from "@/api"; import { apiRoute, applyConfig } from "@/api";
import { createRoute } from "@hono/zod-openapi"; import { createRoute } from "@hono/zod-openapi";
import type { StatusCode } from "hono/utils/http-status"; import type { ContentfulStatusCode, StatusCode } from "hono/utils/http-status";
import { z } from "zod"; import { z } from "zod";
import { config } from "~/packages/config-manager"; import { config } from "~/packages/config-manager";
import { ErrorSchema } from "~/types/api"; import { ErrorSchema } from "~/types/api";
@ -85,7 +85,11 @@ export default apiRoute((app) =>
.get("Content-Disposition") .get("Content-Disposition")
?.match(/filename="(.+)"/)?.[1] || id.split("/").pop(); ?.match(/filename="(.+)"/)?.[1] || id.split("/").pop();
return context.body(media.body, media.status as StatusCode, { if (!media.body) {
return context.body(null, media.status as StatusCode);
}
return context.body(media.body, media.status as ContentfulStatusCode, {
"Content-Type": "Content-Type":
media.headers.get("Content-Type") || "application/octet-stream", media.headers.get("Content-Type") || "application/octet-stream",
"Content-Length": media.headers.get("Content-Length") || "0", "Content-Length": media.headers.get("Content-Length") || "0",

2
app.ts
View file

@ -164,7 +164,7 @@ export const appFactory = async (): Promise<OpenAPIHono<HonoEnv>> => {
applyToHono(app); applyToHono(app);
app.options("*", (context) => { app.options("*", (context) => {
return context.text("", 204); return context.body(null, 204);
}); });
app.all("*", async (context) => { app.all("*", async (context) => {

View file

@ -201,7 +201,7 @@ export default (plugin: PluginType): void => {
.delete(OpenIdAccounts) .delete(OpenIdAccounts)
.where(eq(OpenIdAccounts.id, account.id)); .where(eq(OpenIdAccounts.id, account.id));
return context.text("", 204); return context.body(null, 204);
}, },
); );
}); });