Procházet zdrojové kódy

@uppy/transloadit: fix `assemblyOptions` option (#4316)

Antoine du Hamel před 2 roky
rodič
revize
2ef662140b

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

@@ -59,12 +59,16 @@ async function getAssemblyOptions (file, options) {
 }
 
 function getFields (file, assemblyOptions) {
-  if (Array.isArray(assemblyOptions.fields)) {
+  const { fields } = assemblyOptions
+  if (fields == null) {
+    return {}
+  }
+  if (Array.isArray(fields)) {
     return Object.fromEntries(
-      assemblyOptions.fields.map((fieldName) => [fieldName, file.meta[fieldName]]),
+      fields.map((fieldName) => [fieldName, file.meta[fieldName]]),
     )
   }
-  return {}
+  return fields
 }
 
 /**

+ 15 - 21
packages/@uppy/transloadit/src/index.js

@@ -11,14 +11,6 @@ import AssemblyWatcher from './AssemblyWatcher.js'
 import locale from './locale.js'
 import packageJson from '../package.json'
 
-function defaultGetAssemblyOptions (file, options) {
-  return {
-    params: options.params,
-    signature: options.signature,
-    fields: options.fields,
-  }
-}
-
 const sendErrorToConsole = originalErr => err => {
   const error = new ErrorWithCause('Failed to send error to the client', { cause: err })
   // eslint-disable-next-line no-console
@@ -65,29 +57,31 @@ export default class Transloadit extends BasePlugin {
       /** @deprecated use `assemblyOptions` instead */
       params: null,
       /** @deprecated use `assemblyOptions` instead */
-      fields: {},
+      fields: null,
       /** @deprecated use `assemblyOptions` instead */
-      getAssemblyOptions: defaultGetAssemblyOptions,
+      getAssemblyOptions: null,
       limit: 20,
       retryDelays: [7_000, 10_000, 15_000, 20_000],
     }
 
     this.opts = { ...defaultOptions, ...opts }
-    // TODO: move this into `defaultOptions` once we remove the deprecated options
-    this.opts.assemblyOptions = opts.assemblyOptions ?? this.opts.getAssemblyOptions
-    this.#rateLimitedQueue = new RateLimitedQueue(this.opts.limit)
 
-    this.i18nInit()
+    // TODO: remove this fallback in the next major
+    this.opts.assemblyOptions ??= this.opts.getAssemblyOptions ?? {
+      params: this.opts.params,
+      signature: this.opts.signature,
+      fields: this.opts.fields,
+    }
 
-    const hasCustomAssemblyOptions = this.opts.assemblyOptions !== defaultOptions.assemblyOptions
-    if (this.opts.params) {
-      validateParams(this.opts.params)
-    } else if (!hasCustomAssemblyOptions) {
-      // Throw the same error that we'd throw if the `params` returned from a
-      // `getAssemblyOptions()` function is null.
-      validateParams(null)
+    // TODO: remove this check in the next major (validating params when creating the assembly should be enough)
+    if (opts?.params != null && opts.getAssemblyOptions == null && opts.assemblyOptions == null) {
+      validateParams(this.opts.assemblyOptions.params)
     }
 
+    this.#rateLimitedQueue = new RateLimitedQueue(this.opts.limit)
+
+    this.i18nInit()
+
     this.client = new Client({
       service: this.opts.service,
       client: this.#getClientVersion(),

+ 2 - 2
packages/@uppy/transloadit/types/index.d.ts

@@ -97,7 +97,7 @@ interface AssemblyParameters {
 
 interface AssemblyOptions {
   params?: AssemblyParameters
-  fields?: { [name: string]: number | string }
+  fields?: { [name: string]: number | string } | string[]
   // TODO (major): move signature into params.auth.
   signature?: string
 }
@@ -143,7 +143,7 @@ export type TransloaditOptions = Options &
         /** @deprecated use `assemblyOptions` instead */
         params?: AssemblyParameters
         /** @deprecated use `assemblyOptions` instead */
-        fields?: { [name: string]: number | string }
+        fields?: { [name: string]: number | string } | string[]
         /** @deprecated use `assemblyOptions` instead */
         signature?: string
         /** @deprecated use `assemblyOptions` instead */

+ 3 - 3
private/dev/Dashboard.js

@@ -41,7 +41,7 @@ console.log(import.meta.env)
 
 const RESTORE = false
 
-async function getAssemblyOptions () {
+async function assemblyOptions () {
   return generateSignatureIfSecret(TRANSLOADIT_SECRET, {
     auth: {
       key: TRANSLOADIT_KEY,
@@ -124,7 +124,7 @@ export default () => {
       uppyDashboard.use(Transloadit, {
         service: TRANSLOADIT_SERVICE_URL,
         waitForEncoding: true,
-        getAssemblyOptions,
+        assemblyOptions,
       })
       break
     case 'transloadit-s3':
@@ -132,7 +132,7 @@ export default () => {
       uppyDashboard.use(Transloadit, {
         waitForEncoding: true,
         importFromUploadURLs: true,
-        getAssemblyOptions,
+        assemblyOptions,
       })
       break
     case 'transloadit-xhr':