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