diff --git a/src/Utils/request/request.ts b/src/Utils/request/request.ts index a2adad1bd45..72df402ef1a 100644 --- a/src/Utils/request/request.ts +++ b/src/Utils/request/request.ts @@ -38,30 +38,13 @@ export default async function request( try { const res = await fetch(url, options); + const data = await getResponseBody(res); - if (!res.ok) { - const data: Record = await res.json(); - result = { - res, - data: undefined, - error: data, - }; - } else if ( - res.headers.get("content-type")?.includes("application/json") - ) { - const data: TData = await res.json(); - result = { - res, - data, - error: undefined, - }; - } else { - result = { - res, - data: undefined, - error: undefined, - }; - } + result = { + res, + data: res.ok ? data : undefined, + error: res.ok ? undefined : (data as Record), + }; onResponse?.(result); handleResponse(result, silent); @@ -78,3 +61,21 @@ export default async function request( ); return result; } + +const getResponseBody = async (res: Response): Promise => { + if (!(res.headers.get("content-length") !== "0")) { + return null as TData; + } + + const isJson = res.headers.get("content-type")?.includes("application/json"); + + if (!isJson) { + return (await res.text()) as TData; + } + + try { + return await res.json(); + } catch { + return (await res.text()) as TData; + } +};