mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
fix(build): 🐛 Add bodyOrJson middleware to server/api/v1/apps
This commit is contained in:
parent
959dd27ad6
commit
516bfb72e7
|
|
@ -1,5 +1,5 @@
|
|||
import { randomBytes } from "node:crypto";
|
||||
import { applyConfig, handleZodError } from "@api";
|
||||
import { applyConfig, handleZodError, jsonOrForm } from "@api";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { jsonResponse } from "@response";
|
||||
import type { Hono } from "hono";
|
||||
|
|
@ -32,6 +32,7 @@ export default (app: Hono) =>
|
|||
app.on(
|
||||
meta.allowedMethods,
|
||||
meta.route,
|
||||
jsonOrForm(),
|
||||
zValidator("form", schemas.form, handleZodError),
|
||||
async (context) => {
|
||||
const { client_name, redirect_uris, scopes, website } =
|
||||
|
|
|
|||
|
|
@ -34,7 +34,15 @@ describe("POST /api/v1/apps/", () => {
|
|||
const response = await sendTestRequest(
|
||||
new Request(new URL("/api/v1/apps", config.http.base_url), {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
client_name: "Test Application",
|
||||
website: "https://example.com",
|
||||
redirect_uris: "https://example.com",
|
||||
scopes: "read write",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
|
|
|
|||
34
utils/api.ts
34
utils/api.ts
|
|
@ -152,3 +152,37 @@ export const qsQuery = () => {
|
|||
await next();
|
||||
});
|
||||
};
|
||||
|
||||
// Fill in queries, formData and json
|
||||
export const jsonOrForm = () => {
|
||||
return createMiddleware(async (context, next) => {
|
||||
const contentType = context.req.header("content-type");
|
||||
|
||||
if (contentType?.includes("application/json")) {
|
||||
context.req.parseBody = async <T extends BodyData = BodyData>() =>
|
||||
(await context.req.json()) as T;
|
||||
context.req.bodyCache.formData = await context.req.json();
|
||||
} else if (contentType?.includes("application/x-www-form-urlencoded")) {
|
||||
const parsed = parse(await context.req.text(), {
|
||||
parseArrays: true,
|
||||
interpretNumericEntities: true,
|
||||
});
|
||||
|
||||
// @ts-ignore Very bad hack
|
||||
context.req.formData = () => Promise.resolve(parsed);
|
||||
// @ts-ignore I'm so sorry for this
|
||||
context.req.bodyCache.formData = parsed;
|
||||
} else {
|
||||
const parsed = parse(await context.req.text(), {
|
||||
parseArrays: true,
|
||||
interpretNumericEntities: true,
|
||||
});
|
||||
|
||||
// @ts-ignore Very bad hack
|
||||
context.req.formData = () => Promise.resolve(parsed);
|
||||
// @ts-ignore I'm so sorry for this
|
||||
context.req.bodyCache.formData = parsed;
|
||||
}
|
||||
await next();
|
||||
});
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue