mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
fix(api): 🐛 Add JSON support to every form that doesn't have a file parameter
This commit is contained in:
parent
9f0eab03f2
commit
4713d0f93f
|
|
@ -1,4 +1,4 @@
|
||||||
import { applyConfig, auth, handleZodError } from "@api";
|
import { applyConfig, auth, handleZodError, jsonOrForm } from "@api";
|
||||||
import { zValidator } from "@hono/zod-validator";
|
import { zValidator } from "@hono/zod-validator";
|
||||||
import { errorResponse, jsonResponse } from "@response";
|
import { errorResponse, jsonResponse } from "@response";
|
||||||
import { and, eq } from "drizzle-orm";
|
import { and, eq } from "drizzle-orm";
|
||||||
|
|
@ -33,6 +33,7 @@ export default (app: Hono) =>
|
||||||
app.on(
|
app.on(
|
||||||
meta.allowedMethods,
|
meta.allowedMethods,
|
||||||
meta.route,
|
meta.route,
|
||||||
|
jsonOrForm(),
|
||||||
zValidator("param", schemas.param, handleZodError),
|
zValidator("param", schemas.param, handleZodError),
|
||||||
zValidator("form", schemas.form, handleZodError),
|
zValidator("form", schemas.form, handleZodError),
|
||||||
auth(meta.auth),
|
auth(meta.auth),
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { applyConfig, auth, handleZodError, qs } from "@api";
|
import { applyConfig, auth, handleZodError, jsonOrForm, qs } from "@api";
|
||||||
import { zValidator } from "@hono/zod-validator";
|
import { zValidator } from "@hono/zod-validator";
|
||||||
import { errorResponse, jsonResponse } from "@response";
|
import { errorResponse, jsonResponse } from "@response";
|
||||||
import { and, eq, inArray } from "drizzle-orm";
|
import { and, eq, inArray } from "drizzle-orm";
|
||||||
|
|
@ -70,7 +70,7 @@ export default (app: Hono) =>
|
||||||
app.on(
|
app.on(
|
||||||
meta.allowedMethods,
|
meta.allowedMethods,
|
||||||
meta.route,
|
meta.route,
|
||||||
qs(),
|
jsonOrForm(),
|
||||||
zValidator("param", schemas.param, handleZodError),
|
zValidator("param", schemas.param, handleZodError),
|
||||||
zValidator("form", schemas.form, handleZodError),
|
zValidator("form", schemas.form, handleZodError),
|
||||||
auth(meta.auth),
|
auth(meta.auth),
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { applyConfig, auth, handleZodError, qs } from "@api";
|
import { applyConfig, auth, handleZodError, jsonOrForm, qs } from "@api";
|
||||||
import { zValidator } from "@hono/zod-validator";
|
import { zValidator } from "@hono/zod-validator";
|
||||||
import { errorResponse, jsonResponse } from "@response";
|
import { errorResponse, jsonResponse } from "@response";
|
||||||
import type { Hono } from "hono";
|
import type { Hono } from "hono";
|
||||||
|
|
@ -61,7 +61,7 @@ export default (app: Hono) =>
|
||||||
app.on(
|
app.on(
|
||||||
meta.allowedMethods,
|
meta.allowedMethods,
|
||||||
meta.route,
|
meta.route,
|
||||||
qs(),
|
jsonOrForm(),
|
||||||
zValidator("form", schemas.form, handleZodError),
|
zValidator("form", schemas.form, handleZodError),
|
||||||
auth(meta.auth),
|
auth(meta.auth),
|
||||||
async (context) => {
|
async (context) => {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { randomBytes } from "node:crypto";
|
import { randomBytes } from "node:crypto";
|
||||||
import { applyConfig, handleZodError } from "@api";
|
import { applyConfig, handleZodError, jsonOrForm } from "@api";
|
||||||
import { zValidator } from "@hono/zod-validator";
|
import { zValidator } from "@hono/zod-validator";
|
||||||
import { response } from "@response";
|
import { response } from "@response";
|
||||||
import type { Hono } from "hono";
|
import type { Hono } from "hono";
|
||||||
|
|
@ -76,6 +76,7 @@ export default (app: Hono) =>
|
||||||
app.on(
|
app.on(
|
||||||
meta.allowedMethods,
|
meta.allowedMethods,
|
||||||
meta.route,
|
meta.route,
|
||||||
|
jsonOrForm(),
|
||||||
zValidator("query", schemas.query, handleZodError),
|
zValidator("query", schemas.query, handleZodError),
|
||||||
zValidator("form", schemas.form, handleZodError),
|
zValidator("form", schemas.form, handleZodError),
|
||||||
async (context) => {
|
async (context) => {
|
||||||
|
|
|
||||||
|
|
@ -207,12 +207,16 @@ export const jsonOrForm = () => {
|
||||||
context.req.parseBody = async <T extends BodyData = BodyData>() =>
|
context.req.parseBody = async <T extends BodyData = BodyData>() =>
|
||||||
(await context.req.json()) as T;
|
(await context.req.json()) as T;
|
||||||
context.req.bodyCache.formData = await context.req.json();
|
context.req.bodyCache.formData = await context.req.json();
|
||||||
|
context.req.formData = async () =>
|
||||||
|
context.req.bodyCache.formData as FormData;
|
||||||
} else if (contentType?.includes("application/x-www-form-urlencoded")) {
|
} else if (contentType?.includes("application/x-www-form-urlencoded")) {
|
||||||
const parsed = parse(await context.req.text(), {
|
const parsed = parse(await context.req.text(), {
|
||||||
parseArrays: true,
|
parseArrays: true,
|
||||||
interpretNumericEntities: true,
|
interpretNumericEntities: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
context.req.parseBody = <T extends BodyData = BodyData>() =>
|
||||||
|
Promise.resolve(parsed as T);
|
||||||
// @ts-ignore Very bad hack
|
// @ts-ignore Very bad hack
|
||||||
context.req.formData = () => Promise.resolve(parsed);
|
context.req.formData = () => Promise.resolve(parsed);
|
||||||
// @ts-ignore I'm so sorry for this
|
// @ts-ignore I'm so sorry for this
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue