diff --git a/packages/upload-client/mock-server/controllers/group.ts b/packages/upload-client/mock-server/controllers/group.ts index 02893d120..6a4963e47 100644 --- a/packages/upload-client/mock-server/controllers/group.ts +++ b/packages/upload-client/mock-server/controllers/group.ts @@ -58,8 +58,8 @@ const isValidFile = (file: string): boolean => { * @param {object} ctx */ const index = (ctx) => { - let files = ctx.query && ctx.query['files[]'] - const publicKey = ctx.query && ctx.query.pub_key + let files = ctx.request.body && ctx.request.body['files[]'] + const publicKey = ctx.request.body && ctx.request.body.pub_key if (!files || files.length === 0) { return error(ctx, { diff --git a/packages/upload-client/mock-server/middleware/auth.ts b/packages/upload-client/mock-server/middleware/auth.ts index 7f85b465e..df8550f7a 100644 --- a/packages/upload-client/mock-server/middleware/auth.ts +++ b/packages/upload-client/mock-server/middleware/auth.ts @@ -71,6 +71,11 @@ const auth = (ctx, next) => { publicKey: getPublicKeyFromSource(ctx.query, key) } + // pub_key in body + if (url.includes('group') && !url.includes('group/info')) { + params.publicKey = getPublicKeyFromSource(ctx.request.body, key) + } + // UPLOADCARE_PUB_KEY in body if ( url.includes('base') || diff --git a/packages/upload-client/src/api/group.ts b/packages/upload-client/src/api/group.ts index 1209c4e7b..e2ee8388a 100644 --- a/packages/upload-client/src/api/group.ts +++ b/packages/upload-client/src/api/group.ts @@ -9,6 +9,7 @@ import defaultSettings from '../defaultSettings' import { getUserAgent } from '../tools/getUserAgent' import { UploadError } from '../tools/UploadError' import { retryIfFailed } from '../tools/retryIfFailed' +import buildFormData from '../tools/buildFormData' export type GroupOptions = { publicKey: string @@ -55,10 +56,12 @@ export default function group( 'X-UC-User-Agent': getUserAgent({ publicKey, integration, userAgent }) }, url: getUrl(baseURL, '/group/', { - jsonerrors: 1, - pub_key: publicKey, + jsonerrors: 1 + }), + data: buildFormData({ files: uuids, callback: jsonpCallback, + pub_key: publicKey, signature: secureSignature, expire: secureExpire, source diff --git a/packages/upload-client/src/tools/buildFormData.ts b/packages/upload-client/src/tools/buildFormData.ts index 0f321b354..4c134d7f8 100644 --- a/packages/upload-client/src/tools/buildFormData.ts +++ b/packages/upload-client/src/tools/buildFormData.ts @@ -24,7 +24,7 @@ interface FileType extends FileOptions { data: SupportedFileInput } -type InputValue = FileType | SimpleType | ObjectType +type InputValue = FileType | SimpleType | ObjectType | SimpleType[] type FormDataOptions = { [key: string]: InputValue @@ -61,7 +61,11 @@ function collectParams( inputKey: string, inputValue: InputValue ): void { - if (isFileValue(inputValue)) { + if (Array.isArray(inputValue)) { + for (const value of inputValue) { + collectParams(params, `${inputKey}[]`, value) + } + } else if (isFileValue(inputValue)) { const { name, contentType }: FileOptions = inputValue const file = transformFile( inputValue.data, diff --git a/packages/upload-client/test/tools/buildFormData.test.ts b/packages/upload-client/test/tools/buildFormData.test.ts index 67b54c6f3..7c9d5d558 100644 --- a/packages/upload-client/test/tools/buildFormData.test.ts +++ b/packages/upload-client/test/tools/buildFormData.test.ts @@ -37,6 +37,19 @@ describe('getFormDataParams', () => { expect(params).toContainEqual(['key', 'value']) }) + it('should accept array parameters with simple types - string, number, undefined', () => { + const params = getFormDataParams({ + key: ['string', 100, undefined] + }) + + expect(params).toEqual( + expect.objectContaining([ + ['key[]', 'string'], + ['key[]', '100'] + ]) + ) + }) + it('should convert numbers to strings', () => { const params = getFormDataParams({ key: '1234' @@ -66,14 +79,6 @@ describe('getFormDataParams', () => { expect(params.length).toEqual(0) }) - it('should not process arrays', () => { - const params = getFormDataParams({ - key1: ['1', 2, '3'] as never - }) - - expect(params.length).toEqual(0) - }) - it('should accept key-value objects, transforming any defined values to string', () => { const params = getFormDataParams({ key1: {