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
Yusuke Wada 1eb49b8d96
refactor: support new @cloudflare/worker-types (#673)
* refactor: support new `@cloudflare/worker-types`

* do not attach crypto to global

* denoify ignore serve-static for cloudlfare

* ignore the utility for cloudflare

* import types
2022-11-23 07:27:42 +09:00

21 lines
606 B
TypeScript

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')
) {
const form: Record<string, string | File> = {}
;(await r.formData()).forEach((value, key) => {
form[key] = value
})
body = form
}
return body as BodyType
}