Forráskód Böngészése

@uppy/transloadit: pass fields to transloadit (#3228)

* @uppy/transloadit: pass fields to transloadit

Co-authored-by: Merlijn Vos <Murderlon@users.noreply.github.com>
Fixes: https://github.com/transloadit/uppy/issues/3189

* inline `normalizeAssemblyOptions`

Co-authored-by: Merlijn Vos <Murderlon@users.noreply.github.com>
Antoine du Hamel 3 éve
szülő
commit
8fbabe4223

+ 7 - 14
packages/@uppy/transloadit/src/AssemblyOptions.js

@@ -24,19 +24,6 @@ function validateParams (params) {
   }
 }
 
-/**
- * Normalize Uppy-specific Assembly option features to a Transloadit-
- * compatible object.
- */
-function normalizeAssemblyOptions (file, assemblyOptions) {
-  // eslint-disable-next-line no-param-reassign
-  assemblyOptions.fields = Array.isArray(assemblyOptions.fields)
-    ? Object.fromEntries(assemblyOptions.fields.map((fieldName) => [fieldName, file.meta[fieldName]]))
-    : {}
-
-  return assemblyOptions
-}
-
 /**
  * Combine Assemblies with the same options into a single Assembly for all the
  * relevant files.
@@ -78,9 +65,15 @@ class AssemblyOptions {
     const options = this.opts
 
     const assemblyOptions = await options.getAssemblyOptions(file, options)
+    if (Array.isArray(assemblyOptions.fields)) {
+      assemblyOptions.fields = Object.fromEntries(
+        assemblyOptions.fields.map((fieldName) => [fieldName, file.meta[fieldName]])
+      )
+    } else if (assemblyOptions.fields == null) {
+      assemblyOptions.fields = {}
+    }
 
     validateParams(assemblyOptions.params)
-    normalizeAssemblyOptions(file, assemblyOptions)
 
     return {
       fileIDs: [file.id],

+ 8 - 12
test/endtoend/transloadit2/main.js

@@ -33,24 +33,20 @@ function initUppyTransloadit (transloaditKey) {
             auth: { key: transloaditKey },
             template_id: 'uppyTransloadit',
           },
+          fields: {
+            message: 'test',
+          },
         }
       },
       waitForEncoding: true,
     })
 
-  uppyTransloadit.on('transloadit:result', (stepName, result) => {
+  uppyTransloadit.on('transloadit:result', (_, __, assembly) => {
     // use transloadit encoding result here.
-    console.log('Result here ====>', stepName, result)
-    console.log('Cropped image url is here ====>', result.url)
-
-    const img = new Image()
-    img.onload = function onload () {
-      const result = document.createElement('div')
-      result.setAttribute('id', 'uppy-result')
-      result.textContent = 'ok'
-      document.body.appendChild(result)
-    }
-    img.src = result.url
+    const result = document.createElement('div')
+    result.setAttribute('id', 'uppy-result')
+    result.textContent = assembly.fields.message === 'test' ? 'ok' : 'fail'
+    document.body.appendChild(result)
   })
 }