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 5cbd89c3ae
fix(validator): cache arrayBuffer to use after validation (#1393)
* wip

* fix(validator): cache `arrayBuffer` to use after validation

* denoify

* refactor

* denoify

* make it `bufferToFormData()` in utils/buffer.ts
2023-09-03 14:43:01 +09:00

27 lines
681 B
TypeScript

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