0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-30 01:56:18 +01:00
hono/deno_dist/utils/body.ts
Yusuke Wada f254fdc846
feat(req): cache body content (#1333)
* feat(req): cache body content

* denoify

* use destruction

* denoify
2023-08-18 16:25:48 +09:00

23 lines
599 B
TypeScript

import type { HonoRequest } from '../request.ts'
export type BodyData = Record<string, string | File>
export const parseBody = async <T extends BodyData = BodyData>(
r: HonoRequest | Request
): 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
}