Ver código fonte

@uppy/xhr-upload: fix getResponseData regression (#4964)

Merlijn Vos 1 ano atrás
pai
commit
b46652db08
1 arquivos alterados com 11 adições e 5 exclusões
  1. 11 5
      packages/@uppy/xhr-upload/src/index.ts

+ 11 - 5
packages/@uppy/xhr-upload/src/index.ts

@@ -59,10 +59,7 @@ export interface XhrUploadOpts<M extends Meta, B extends Body>
     body: string,
     xhr: XMLHttpRequest,
   ) => boolean
-  getResponseData?: (
-    body: string,
-    xhr: XMLHttpRequest,
-  ) => NonNullable<UppyFile<M, B>['response']>['body']
+  getResponseData?: (body: string, xhr: XMLHttpRequest) => B
   getResponseError?: (body: string, xhr: XMLHttpRequest) => Error | NetworkError
   allowedMetaFields?: string[] | null
   bundle?: boolean
@@ -117,7 +114,16 @@ const defaultOptions = {
   withCredentials: false,
   responseType: '',
   getResponseData(responseText) {
-    return JSON.parse(responseText)
+    let parsedResponse = {}
+    try {
+      parsedResponse = JSON.parse(responseText)
+    } catch {
+      // ignore
+    }
+    // We don't have access to the B (Body) generic here
+    // so we have to cast it to any. The user facing types
+    // remain correct, this is only to please the merging of default options.
+    return parsedResponse as any
   },
   getResponseError(_, response) {
     let error = new Error('Upload error')