0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 10:51:01 +00:00
hono/deno_dist/utils/body.ts

21 lines
624 B
TypeScript
Raw Normal View History

export type BodyData = Record<string, string | number | boolean | File>
export async function parseBody<BodyType extends BodyData>(
r: Request | Response
): Promise<BodyType> {
let body: Record<string, string | File> = {}
const contentType = r.headers.get('Content-Type')
if (
contentType &&
(contentType.startsWith('multipart/form-data') ||
contentType === 'application/x-www-form-urlencoded')
) {
2022-07-02 06:09:45 +00:00
const form: Record<string, string | File> = {}
body = [...(await r.formData())].reduce((acc, cur) => {
2022-07-02 06:09:45 +00:00
acc[cur[0]] = cur[1]
return acc
}, form)
}
return body as BodyType
2022-07-02 06:09:45 +00:00
}