mirror of
https://github.com/honojs/hono.git
synced 2024-12-01 11:51:01 +01:00
5cbd89c3ae
* wip * fix(validator): cache `arrayBuffer` to use after validation * denoify * refactor * denoify * make it `bufferToFormData()` in utils/buffer.ts
27 lines
681 B
TypeScript
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
|
|
}
|