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
8 changes: 6 additions & 2 deletions packages/@uppy/transloadit/src/AssemblyOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ class AssemblyOptions {
if (file == null) return undefined

const options = this.opts
const assemblyOptions = await options.getAssemblyOptions(file, options)
const assemblyOptions = typeof options.assemblyOptions === 'function'
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
? await options.assemblyOptions(file, options)
: options.assemblyOptions

// 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
Expand Down Expand Up @@ -105,7 +107,9 @@ class AssemblyOptions {

if (options.alwaysRunAssembly) {
// No files, just generate one Assembly
const assemblyOptions = await options.getAssemblyOptions(null, options)
const assemblyOptions = typeof options.assemblyOptions === 'function'
? await options.assemblyOptions()
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
: options.assemblyOptions

validateParams(assemblyOptions.params)
return [{
Expand Down
16 changes: 8 additions & 8 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,8 +88,8 @@ 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) {
expect(file).toBe(null)
async assemblyOptions (file) {
expect(file).toBe(undefined)
return {
params: {
auth: { key: 'fake key' },
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
262 changes: 150 additions & 112 deletions packages/@uppy/transloadit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,124 +2,155 @@ import type { PluginOptions, UppyFile, BasePlugin } from '@uppy/core'
import TransloaditLocale from './generatedLocale'

export interface FileInfo {
id: string,
name: string,
basename: string,
ext: string,
size: number,
mime: string,
type: string,
field: string,
md5hash: string,
is_tus_file: boolean,
original_md5hash: string,
original_id: string,
original_name: string
original_basename: string,
original_path: string,
url: string,
ssl_url: string,
tus_upload_url: string,
meta: Record<string, any>
}
id: string
name: string
basename: string
ext: string
size: number
mime: string
type: string
field: string
md5hash: string
is_tus_file: boolean
original_md5hash: string
original_id: string
original_name: string
original_basename: string
original_path: string
url: string
ssl_url: string
tus_upload_url: string
meta: Record<string, any>
}

export interface Result extends FileInfo {
cost: number,
execTime: number,
queue: string,
queueTime: number,
localId: string | null
}
cost: number
execTime: number
queue: string
queueTime: number
localId: string | null
}

export interface Assembly {
ok?: string,
message?: string,
assembly_id: string,
parent_id?: string,
account_id: string,
template_id?: string,
instance: string,
assembly_url: string,
assembly_ssl_url: string,
uppyserver_url: string,
companion_url: string,
websocket_url: string,
tus_url: string,
bytes_received: number,
bytes_expected: number,
upload_duration: number,
client_agent?: string,
client_ip?: string,
client_referer?: string,
transloadit_client: string,
start_date: string,
upload_meta_data_extracted: boolean,
warnings: any[],
is_infinite: boolean,
has_dupe_jobs: boolean,
execution_start: string,
execution_duration: number,
queue_duration: number,
jobs_queue_duration: number,
notify_start?: any,
notify_url?: string,
notify_status?: any,
notify_response_code?: any,
notify_duration?: any,
last_job_completed?: string,
fields: Record<string, any>,
running_jobs: any[],
bytes_usage: number,
executing_jobs: any[],
started_jobs: string[],
parent_assembly_status: any,
params: string,
template?: any,
merged_params: string,
uploads: FileInfo[],
results: Record<string, Result[]>,
build_id: string,
error?: string,
stderr?: string,
stdout?: string,
reason?: string,
}
ok?: string
message?: string
assembly_id: string
parent_id?: string
account_id: string
template_id?: string
instance: string
assembly_url: string
assembly_ssl_url: string
uppyserver_url: string
companion_url: string
websocket_url: string
tus_url: string
bytes_received: number
bytes_expected: number
upload_duration: number
client_agent?: string
client_ip?: string
client_referer?: string
transloadit_client: string
start_date: string
upload_meta_data_extracted: boolean
warnings: any[]
is_infinite: boolean
has_dupe_jobs: boolean
execution_start: string
execution_duration: number
queue_duration: number
jobs_queue_duration: number
notify_start?: any
notify_url?: string
notify_status?: any
notify_response_code?: any
notify_duration?: any
last_job_completed?: string
fields: Record<string, any>
running_jobs: any[]
bytes_usage: number
executing_jobs: any[]
started_jobs: string[]
parent_assembly_status: any
params: string
template?: any
merged_params: string
uploads: FileInfo[]
results: Record<string, Result[]>
build_id: string
error?: string
stderr?: string
stdout?: string
reason?: string
}

interface AssemblyParameters {
auth: {
key: string,
expires?: string
}
template_id?: string
steps?: { [step: string]: Record<string, unknown> }
notify_url?: string
fields?: { [name: string]: number | string }
interface AssemblyParameters {
auth: {
key: string
expires?: string
}
template_id?: string
steps?: { [step: string]: Record<string, unknown> }
notify_url?: string
}

interface AssemblyOptions {
params: AssemblyParameters
fields?: { [name: string]: number | string }
signature?: string
}
params?: AssemblyParameters
fields?: { [name: string]: number | string }
// TODO (major): move signature into params.auth.
signature?: string
}

interface TransloaditOptionsBase extends PluginOptions {
service?: string
errorReporting?: boolean
waitForEncoding?: boolean
waitForMetadata?: boolean
importFromUploadURLs?: boolean
alwaysRunAssembly?: boolean
locale?: TransloaditLocale
limit?: number
interface Options extends PluginOptions {
service?: string
errorReporting?: boolean
waitForEncoding?: boolean
waitForMetadata?: boolean
importFromUploadURLs?: boolean
alwaysRunAssembly?: boolean
locale?: TransloaditLocale
limit?: number
}

// Either have a getAssemblyOptions() that returns an AssemblyOptions, *or* have them embedded in the options
export type TransloaditOptions = TransloaditOptionsBase &
(
| {
getAssemblyOptions?: (file: UppyFile) => AssemblyOptions | Promise<AssemblyOptions>
}
| AssemblyOptions)
export type TransloaditOptions = Options &
(
| {
assemblyOptions?: AssemblyOptions | ((file: UppyFile) => Promise<AssemblyOptions> | AssemblyOptions)
/** @deprecated use `assemblyOptions` instead */
getAssemblyOptions?: never
/** @deprecated use `assemblyOptions` instead */
params?: never
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
/** @deprecated use `assemblyOptions` instead */
fields?: never
/** @deprecated use `assemblyOptions` instead */
signature?: never
}
| {
/** @deprecated use `assemblyOptions` instead */
getAssemblyOptions?: (
file: UppyFile
) => AssemblyOptions | Promise<AssemblyOptions>
assemblyOptions?: never
/** @deprecated use `assemblyOptions` instead */
params?: never
/** @deprecated use `assemblyOptions` instead */
fields?: never
/** @deprecated use `assemblyOptions` instead */
signature?: never
}
| {
/** @deprecated use `assemblyOptions` instead */
params?: AssemblyParameters
/** @deprecated use `assemblyOptions` instead */
fields?: { [name: string]: number | string }
/** @deprecated use `assemblyOptions` instead */
signature?: string
/** @deprecated use `assemblyOptions` instead */
getAssemblyOptions?: never
assemblyOptions?: never
}
)

export default class Transloadit extends BasePlugin<TransloaditOptions> {
/** @deprecated */
Expand All @@ -134,11 +165,18 @@ export const COMPANION_ALLOWED_HOSTS: RegExp

// Events

export type TransloaditAssemblyCreatedCallback = (assembly: Assembly, fileIDs: string[]) => void;
export type TransloaditUploadedCallback = (file: FileInfo, assembly: Assembly) => void;
export type TransloaditAssemblyExecutingCallback = (assembly: Assembly) => void;
export type TransloaditResultCallback = (stepName: string, result: Result, assembly: Assembly) => void;
export type TransloaditCompleteCallback = (assembly: Assembly) => void;
export type TransloaditAssemblyCreatedCallback = (
assembly: Assembly,
fileIDs: string[]
) => void
export type TransloaditUploadedCallback = (file: FileInfo, assembly: Assembly) => void
export type TransloaditAssemblyExecutingCallback = (assembly: Assembly) => void
export type TransloaditResultCallback = (
stepName: string,
result: Result,
assembly: Assembly
) => void
export type TransloaditCompleteCallback = (assembly: Assembly) => void

declare module '@uppy/core' {
export interface UppyEventMap {
Expand Down
Loading