2023-01-02 23:25:21 +00:00
|
|
|
export type BodyData = Record<string, string | File>
|
2022-09-20 13:01:03 +00:00
|
|
|
|
2023-08-05 09:10:12 +00:00
|
|
|
export const parseBody = async <T extends BodyData = BodyData>(
|
|
|
|
r: Request | Response
|
|
|
|
): Promise<T> => {
|
2023-01-02 23:25:21 +00:00
|
|
|
let body: BodyData = {}
|
2022-08-27 02:38:48 +00:00
|
|
|
const contentType = r.headers.get('Content-Type')
|
|
|
|
if (
|
|
|
|
contentType &&
|
|
|
|
(contentType.startsWith('multipart/form-data') ||
|
2023-06-21 13:51:59 +00:00
|
|
|
contentType.startsWith('application/x-www-form-urlencoded'))
|
2022-08-27 02:38:48 +00:00
|
|
|
) {
|
2023-01-02 23:25:21 +00:00
|
|
|
const form: BodyData = {}
|
2022-11-22 22:27:42 +00:00
|
|
|
;(await r.formData()).forEach((value, key) => {
|
|
|
|
form[key] = value
|
|
|
|
})
|
|
|
|
body = form
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
2023-08-05 09:10:12 +00:00
|
|
|
return body as T
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|