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

17 lines
529 B
TypeScript

export async function parseBody(r: Request | Response): Promise<Record<string, string | File>> {
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')
) {
const form: Record<string, string | File> = {}
body = [...(await r.formData())].reduce((acc, cur) => {
acc[cur[0]] = cur[1]
return acc
}, form)
}
return body
}