0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 11:51:01 +01:00
hono/deno_dist/utils/body.ts
Yusuke Wada 85b54920f9
feat(parseBody): allow passing generics to parseBody() (#1289)
* feat(parseBody): allow passing generics to `parseBody()`

* denoify
2023-08-05 18:10:12 +09:00

21 lines
546 B
TypeScript

export type BodyData = Record<string, string | File>
export const parseBody = async <T extends BodyData = BodyData>(
r: Request | Response
): Promise<T> => {
let body: BodyData = {}
const contentType = r.headers.get('Content-Type')
if (
contentType &&
(contentType.startsWith('multipart/form-data') ||
contentType.startsWith('application/x-www-form-urlencoded'))
) {
const form: BodyData = {}
;(await r.formData()).forEach((value, key) => {
form[key] = value
})
body = form
}
return body as T
}