Skip to content

Commit

Permalink
fix(upload-client/group): use body to send query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nd0ut committed Feb 6, 2024
1 parent e6df820 commit 3560d51
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
7 changes: 5 additions & 2 deletions packages/upload-client/src/api/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,13 +57,15 @@ export default function group(
},
url: getUrl(baseURL, '/group/', {
jsonerrors: 1,
pub_key: publicKey,
files: uuids,
callback: jsonpCallback,
pub_key: publicKey,
signature: secureSignature,
expire: secureExpire,
source
}),
data: buildFormData({
files: uuids
}),
signal
}).then(({ data, headers, request }) => {
const response = camelizeKeys(JSON.parse(data)) as Response
Expand Down
8 changes: 6 additions & 2 deletions packages/upload-client/src/tools/buildFormData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 13 additions & 8 deletions packages/upload-client/test/tools/buildFormData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit 3560d51

Please sign in to comment.