Skip to content

Commit

Permalink
fix(api.js): fix http.fetch throwing error if response body is empty,
Browse files Browse the repository at this point in the history
closes #2831
  • Loading branch information
amrbashir committed Dec 1, 2021
1 parent b4fcb36 commit ae2bca8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/api-fetch-empty-reponse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"api": patch
---

Fix `http.fetch` throwing error if the repsone is successfult but the body is empty.
6 changes: 5 additions & 1 deletion tooling/api/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ class Client {
// @ts-expect-error
response.data = JSON.parse(response.data as string)
} catch (e) {
if (response.ok) {
// @ts-expect-error
if (response.ok && (response.data as string) === '') {
// @ts-expect-error
response.data = {}
} else {
throw Error(
`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.`
Expand Down

0 comments on commit ae2bca8

Please sign in to comment.