Fix incorrect body data handling

This commit is contained in:
GW_MC
2025-12-19 21:16:04 +08:00
parent a0a9584a4d
commit 85e8668e34

View File

@@ -65,7 +65,34 @@ function axiosResponseToFetchResponse(response: AxiosResponse): Response {
}
});
return new Response(response.data, {
// Normalize Axios response.data to a Fetch-compatible BodyInit
let body: BodyInit | null = null;
const data = response.data;
if (data == null) {
body = null;
} else if (
typeof data === 'string' ||
data instanceof Blob ||
data instanceof ArrayBuffer ||
ArrayBuffer.isView(data) ||
data instanceof FormData ||
data instanceof URLSearchParams
) {
body = data as BodyInit;
} else {
try {
body = JSON.stringify(data);
if (!headers.has('content-type')) {
headers.set('content-type', 'application/json;charset=utf-8');
}
} catch {
console.warn('Failed to stringify response data as JSON, falling back to string conversion.');
body = String(data);
}
}
return new Response(body, {
status: response.status,
statusText: response.statusText,
headers: headers,