Fix incorrect body data handling
This commit is contained in:
@@ -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,
|
status: response.status,
|
||||||
statusText: response.statusText,
|
statusText: response.statusText,
|
||||||
headers: headers,
|
headers: headers,
|
||||||
|
|||||||
Reference in New Issue
Block a user