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
606 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> = {}
;(await r.formData()).forEach((value, key) => {
form[key] = value
})
body = form
2022-07-02 06:09:45 +00:00
}
return body as BodyType
2022-07-02 06:09:45 +00:00
}