fix(api.js): fix http.fetch throwing error if response body is empty, closes #2831 (#3008)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
Amr Bashir 2021-12-09 04:39:42 +02:00 committed by GitHub
parent c664e9dbe0
commit 50c63900c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 2 deletions

View File

@ -0,0 +1,5 @@
---
"api": patch
---
Fix `http.fetch` throwing error if the response is successful but the body is empty.

File diff suppressed because one or more lines are too long

View File

@ -203,7 +203,10 @@ class Client {
// @ts-expect-error // @ts-expect-error
response.data = JSON.parse(response.data as string) response.data = JSON.parse(response.data as string)
} catch (e) { } catch (e) {
if (response.ok) { if (response.ok && (response.data as unknown as string) === '') {
// @ts-expect-error
response.data = {}
} else if (response.ok) {
throw Error( throw Error(
`Failed to parse response \`${response.data}\` as JSON: ${e}; `Failed to parse response \`${response.data}\` as JSON: ${e};
try setting the \`responseType\` option to \`ResponseType.Text\` or \`ResponseType.Binary\` if the API does not return a JSON response.` try setting the \`responseType\` option to \`ResponseType.Text\` or \`ResponseType.Binary\` if the API does not return a JSON response.`