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: ensure fields is not nullish when there no uploaded files #4487

Merged
merged 11 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
18 changes: 6 additions & 12 deletions packages/@uppy/transloadit/src/AssemblyOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,16 @@ async function getAssemblyOptions (file, options) {

validateParams(assemblyOptions.params)

return assemblyOptions
}

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

return assemblyOptions
}

/**
Expand Down Expand Up @@ -102,8 +98,6 @@ class AssemblyOptions {
// waiting for the options.
if (file == null) return undefined

assemblyOptions.fields = getFields(file, assemblyOptions)

return {
fileIDs: [file.id],
options: assemblyOptions,
Expand All @@ -118,7 +112,7 @@ class AssemblyOptions {

return [
{
fileIDs: this.files.map((file) => file.id),
fileIDs: [],
options: assemblyOptions,
},
]
Expand Down
6 changes: 2 additions & 4 deletions packages/@uppy/transloadit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,12 @@ export default class Transloadit extends BasePlugin {
return newFile
}

#createAssembly (fileIDs, uploadID, options) {
#createAssembly (fileIDs, uploadID, assemblyOptions) {
this.uppy.log('[Transloadit] Create Assembly')

return this.client.createAssembly({
params: options.params,
fields: options.fields,
Comment on lines -195 to -196
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a breaking change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No we still pass them, see the spreading below.

...assemblyOptions,
expectedFiles: fileIDs.length,
signature: options.signature,
}).then(async (newAssembly) => {
const files = this.uppy.getFiles().filter(({ id }) => fileIDs.includes(id))
if (files.length !== fileIDs.length) {
Expand Down
51 changes: 51 additions & 0 deletions packages/@uppy/transloadit/src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createServer } from 'node:http'
import { once } from 'node:events'
import { describe, expect, it } from '@jest/globals'
import Core from '@uppy/core'
import Transloadit from './index.js'
Expand Down Expand Up @@ -84,4 +86,53 @@ describe('Transloadit', () => {
expect(uppy.getFile(fileID).progress.uploadStarted).toBe(null)
})
})

it('Can start an assembly with no files and no fields', async () => {
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
const server = createServer((req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Headers', '*')
res.setHeader('Content-Type', 'application/json')
res.end('{"websocket_url":"about:blank"}')
}).listen()
await once(server, 'listening')
const uppy = new Core({
autoProceed: false,
})
uppy.use(Transloadit, {
service: `http://localhost:${server.address().port}`,
alwaysRunAssembly: true,
params: {
auth: { key: 'some auth key string' },
template_id: 'some template id string',
},
})

await uppy.upload()
server.close()
})

it('Can start an assembly with no files and some fields', async () => {
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
const server = createServer((req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Headers', '*')
res.setHeader('Content-Type', 'application/json')
res.end('{"websocket_url":"about:blank"}')
}).listen()
await once(server, 'listening')
const uppy = new Core({
autoProceed: false,
})
uppy.use(Transloadit, {
service: `http://localhost:${server.address().port}`,
alwaysRunAssembly: true,
params: {
auth: { key: 'some auth key string' },
template_id: 'some template id string',
},
fields: ['hasOwnProperty'],
})

await uppy.upload()
server.close()
})
})
6 changes: 3 additions & 3 deletions packages/@uppy/transloadit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ interface AssemblyParameters {
}
template_id?: string
steps?: { [step: string]: Record<string, unknown> }
fields?: { [name: string]: number | string }
notify_url?: string
}

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

Expand All @@ -116,7 +116,7 @@ interface Options extends PluginOptions {
export type TransloaditOptions = Options &
(
| {
assemblyOptions?: AssemblyOptions | ((file: UppyFile) => Promise<AssemblyOptions> | AssemblyOptions)
assemblyOptions?: AssemblyOptions | ((file?: UppyFile) => Promise<AssemblyOptions> | AssemblyOptions)
/** @deprecated use `assemblyOptions` instead */
getAssemblyOptions?: never
/** @deprecated use `assemblyOptions` instead */
Expand All @@ -129,7 +129,7 @@ export type TransloaditOptions = Options &
| {
/** @deprecated use `assemblyOptions` instead */
getAssemblyOptions?: (
file: UppyFile
file?: UppyFile
) => AssemblyOptions | Promise<AssemblyOptions>
assemblyOptions?: never
/** @deprecated use `assemblyOptions` instead */
Expand Down
4 changes: 2 additions & 2 deletions packages/@uppy/transloadit/types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const validParams = {
const uppy = new Uppy()
uppy.use(Transloadit, {
getAssemblyOptions (file) {
expectType<UppyFile>(file)
expectType<UppyFile | undefined>(file)
return { params: validParams }
},
waitForEncoding: false,
Expand All @@ -39,7 +39,7 @@ const validParams = {
const uppy = new Uppy()
uppy.use(Transloadit, {
async assemblyOptions (file) {
expectType<UppyFile>(file)
expectType<UppyFile | undefined>(file)
return { params: validParams }
},
})
Expand Down