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: introduce assemblyOptions, deprecate other options #4059

Merged
merged 9 commits into from
Dec 1, 2022
82 changes: 44 additions & 38 deletions packages/@uppy/transloadit/src/AssemblyOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ function dedupe (list) {
}))
}

async function getAssemblyOptions (file, options) {
const assemblyOptions = typeof options.assemblyOptions === 'function'
? await options.assemblyOptions(file, options)
: options.assemblyOptions

validateParams(assemblyOptions.params)

return assemblyOptions
}

function getFields (file, assemblyOptions) {
if (Array.isArray(assemblyOptions.fields)) {
return Object.fromEntries(
assemblyOptions.fields.map((fieldName) => [fieldName, file.meta[fieldName]]),
)
}
return {}
}

/**
* Turn Transloadit plugin options and a list of files into a list of Assembly
* options.
Expand All @@ -58,36 +77,6 @@ class AssemblyOptions {
this.opts = opts
}

/**
* Get Assembly options for a file.
*/
async #getAssemblyOptions (file) {
if (file == null) return undefined

const options = this.opts
const assemblyOptions = await options.getAssemblyOptions(file, options)

// We check if the file is present here again, because it could had been
// removed during the await, e.g. if the user hit cancel while we were
// waiting for the options.
if (file == null) return undefined

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)

return {
fileIDs: [file.id],
options: assemblyOptions,
}
}

/**
* Generate a set of Assemblies that will handle the upload.
* Returns a Promise for an object with keys:
Expand All @@ -99,19 +88,36 @@ class AssemblyOptions {

if (this.files.length > 0) {
return Promise.all(
this.files.map((file) => this.#getAssemblyOptions(file)),
this.files.map(async (file) => {
if (file == null) return undefined

const assemblyOptions = await getAssemblyOptions(file, options)

// We check if the file is present here again, because it could had been
// removed during the await, e.g. if the user hit cancel while we were
// waiting for the options.
if (file == null) return undefined

assemblyOptions.fields = getFields(file, assemblyOptions)

return {
fileIDs: [file.id],
options: assemblyOptions,
}
}),
).then(dedupe)
}

if (options.alwaysRunAssembly) {
// No files, just generate one Assembly
const assemblyOptions = await options.getAssemblyOptions(null, options)

validateParams(assemblyOptions.params)
return [{
fileIDs: this.files.map((file) => file.id),
options: assemblyOptions,
}]
const assemblyOptions = await getAssemblyOptions(null, options)

return [
{
fileIDs: this.files.map((file) => file.id),
options: assemblyOptions,
},
]
}

// If there are no files and we do not `alwaysRunAssembly`,
Expand Down
14 changes: 7 additions & 7 deletions packages/@uppy/transloadit/src/AssemblyOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { describe, expect, it } from '@jest/globals'
import AssemblyOptions from './AssemblyOptions.js'

describe('Transloadit/AssemblyOptions', () => {
it('Validates response from getAssemblyOptions()', async () => {
it('Validates response from assemblyOptions()', async () => {
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
const options = new AssemblyOptions([
{ name: 'testfile' },
], {
getAssemblyOptions: (file) => {
assemblyOptions: (file) => {
expect(file.name).toBe('testfile')
return {
params: '{"some":"json"}',
Expand All @@ -29,7 +29,7 @@ describe('Transloadit/AssemblyOptions', () => {
{ name: 'c.png', data },
{ name: 'd.png', data },
], {
getAssemblyOptions: (file) => ({
assemblyOptions: (file) => ({
params: {
auth: { key: 'fake key' },
steps: {
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('Transloadit/AssemblyOptions', () => {
{ name: 'c.png', data, size: data.byteLength },
{ name: 'd.png', data: data2, size: data2.byteLength },
], {
getAssemblyOptions: (file) => ({
assemblyOptions: (file) => ({
params: {
auth: { key: 'fake key' },
steps: {
Expand All @@ -77,7 +77,7 @@ describe('Transloadit/AssemblyOptions', () => {

it('Does not create an Assembly if no files are being uploaded', async () => {
const options = new AssemblyOptions([], {
getAssemblyOptions () {
assemblyOptions () {
throw new Error('should not create Assembly')
},
})
Expand All @@ -88,7 +88,7 @@ describe('Transloadit/AssemblyOptions', () => {
it('Creates an Assembly if no files are being uploaded but `alwaysRunAssembly` is enabled', async () => {
const options = new AssemblyOptions([], {
alwaysRunAssembly: true,
getAssemblyOptions (file) {
async assemblyOptions (file) {
expect(file).toBe(null)
return {
params: {
Expand Down Expand Up @@ -122,7 +122,7 @@ describe('Transloadit/AssemblyOptions', () => {
params: {
auth: { key: 'fake key' },
},
getAssemblyOptions: defaultGetAssemblyOptions,
assemblyOptions: defaultGetAssemblyOptions,
})

const assemblies = await options.build()
Expand Down
8 changes: 7 additions & 1 deletion packages/@uppy/transloadit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,26 @@ export default class Transloadit extends BasePlugin {
waitForMetadata: false,
alwaysRunAssembly: false,
importFromUploadURLs: false,
/** @deprecated use `assemblyOptions` instead */
signature: null,
/** @deprecated use `assemblyOptions` instead */
params: null,
/** @deprecated use `assemblyOptions` instead */
fields: {},
/** @deprecated use `assemblyOptions` instead */
getAssemblyOptions: defaultGetAssemblyOptions,
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()

const hasCustomAssemblyOptions = this.opts.getAssemblyOptions !== defaultOptions.getAssemblyOptions
const hasCustomAssemblyOptions = this.opts.assemblyOptions !== defaultOptions.assemblyOptions
if (this.opts.params) {
validateParams(this.opts.params)
} else if (!hasCustomAssemblyOptions) {
Expand Down
Loading