Browse Source

improvements for #4922 (#4960)

* prevent "any" which disables typechecking

* make body optional in response (because it is)

* add comment
Mikael Finstad 1 năm trước cách đây
mục cha
commit
8dba0272ab

+ 8 - 2
packages/@uppy/companion-client/src/RequestClient.ts

@@ -479,7 +479,12 @@ export default class RequestClient<M extends Meta, B extends Body> {
                             break
                           }
                           case 'success': {
-                            // payload.response exists for xhr-upload but not for tus/transloadit
+                            // payload.response is sent from companion for xhr-upload (aka uploadMultipart in companion) and
+                            // s3 multipart (aka uploadS3Multipart)
+                            // but not for tus/transloadit (aka uploadTus)
+                            // responseText is a string which may or may not be in JSON format
+                            // this means that an upload destination of xhr or s3 multipart MUST respond with valid JSON
+                            // to companion, or the JSON.parse will crash
                             const text = payload.response?.responseText
 
                             this.uppy.emit(
@@ -488,7 +493,8 @@ export default class RequestClient<M extends Meta, B extends Body> {
                               {
                                 uploadURL: payload.url,
                                 status: payload.response?.status ?? 200,
-                                body: text ? JSON.parse(text) : undefined,
+                                body:
+                                  text ? (JSON.parse(text) as B) : undefined,
                               },
                             )
                             socketAbortController?.abort?.()

+ 1 - 1
packages/@uppy/utils/src/UppyFile.ts

@@ -35,7 +35,7 @@ export interface UppyFile<M extends Meta, B extends Body> {
   type: string
   uploadURL?: string
   response?: {
-    body: B
+    body?: B
     status: number
     bytesUploaded?: number
     uploadURL?: string

+ 3 - 1
packages/@uppy/xhr-upload/src/index.ts

@@ -352,7 +352,9 @@ export default class XHRUpload<
 
         if (opts.validateStatus(xhr.status, xhr.responseText, xhr)) {
           const body = opts.getResponseData(xhr.responseText, xhr)
-          const uploadURL = body[opts.responseUrlFieldName] as string
+          const uploadURL = body?.[opts.responseUrlFieldName] as
+            | string
+            | undefined
 
           const uploadResp = {
             status: xhr.status,