Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@uppy/transloadit: fix assemblyOptions option #4316

Merged
merged 5 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/@uppy/transloadit/src/AssemblyOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
36 changes: 15 additions & 21 deletions packages/@uppy/transloadit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions packages/@uppy/transloadit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 */
Expand Down
6 changes: 3 additions & 3 deletions private/dev/Dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -124,15 +124,15 @@ export default () => {
uppyDashboard.use(Transloadit, {
service: TRANSLOADIT_SERVICE_URL,
waitForEncoding: true,
getAssemblyOptions,
assemblyOptions,
})
break
case 'transloadit-s3':
uppyDashboard.use(AwsS3, { companionUrl: COMPANION_URL })
uppyDashboard.use(Transloadit, {
waitForEncoding: true,
importFromUploadURLs: true,
getAssemblyOptions,
assemblyOptions,
})
break
case 'transloadit-xhr':
Expand Down