Skip to content

Commit

Permalink
fix(upload-client/group): use body to send query parameters (#511)
Browse files Browse the repository at this point in the history
Co-authored-by: nd0ut <[email protected]>
  • Loading branch information
nd0ut and nd0ut authored Feb 6, 2024
1 parent e6df820 commit 16f36f7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/upload-client/mock-server/controllers/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
5 changes: 5 additions & 0 deletions packages/upload-client/mock-server/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') ||
Expand Down
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 @@ -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
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 16f36f7

Please sign in to comment.