瀏覽代碼

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

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Amr Bashir 3 年之前
父節點
當前提交
50c63900c7
共有 3 個文件被更改,包括 9 次插入1 次删除
  1. 5 0
      .changes/api-fetch-empty-reponse.md
  2. 0 0
      core/tauri/scripts/bundle.js
  3. 4 1
      tooling/api/src/http.ts

+ 5 - 0
.changes/api-fetch-empty-reponse.md

@@ -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 it is too large
+ 0 - 0
core/tauri/scripts/bundle.js


+ 4 - 1
tooling/api/src/http.ts

@@ -203,7 +203,10 @@ class Client {
           // @ts-expect-error
           response.data = JSON.parse(response.data as string)
         } 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(
               `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.`

Some files were not shown because too many files changed in this diff