mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
fix(build): 🐛 Add bodyOrJson middleware to server/api/v1/apps
This commit is contained in:
parent
959dd27ad6
commit
516bfb72e7
3 changed files with 45 additions and 2 deletions
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…
Add table
Add a link
Reference in a new issue