Skip to content

Commit

Permalink
fixed multiple File/Blob arrayvalues in the object->FormData transfor…
Browse files Browse the repository at this point in the history
…mation
  • Loading branch information
ganigeorgiev committed Aug 27, 2023
1 parent 14b816b commit 14ab4c5
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.17.2

- Fixed mulitple File/Blob array values not transformed properly to their FormData equivalent when an object syntax is used.


## 0.17.1

- Fixed typo in the deprecation console.warn messages ([#235](https://github.com/pocketbase/js-sdk/pull/235); thanks @heloineto).
Expand Down
2 changes: 1 addition & 1 deletion dist/pocketbase.cjs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.cjs.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.mjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.iife.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.iife.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.umd.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.17.1",
"version": "0.17.2",
"name": "pocketbase",
"description": "PocketBase JavaScript SDK",
"author": "Gani Georgiev",
Expand Down
5 changes: 4 additions & 1 deletion src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,10 @@ export default class Client {
const form = new FormData();

for (let key in body) {
form.append(key, body[key]);
const values = Array.isArray(body[key]) ? body[key] : [body[key]];
for (let val of values) {
form.append(key, val);
}
}

return form;
Expand Down
29 changes: 22 additions & 7 deletions tests/Client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe('Client', function() {
method: 'GET',
url: 'test_base_url/multipart',
additionalMatcher: (_, config: any): boolean => {
// multipart/form-data requests doesn't have explicitly set Content-Type
// multipart/form-data requests shouldn't have explicitly set Content-Type
return !config?.headers?.['Content-Type'];
},
replyCode: 200,
Expand All @@ -204,12 +204,23 @@ describe('Client', function() {
method: 'GET',
url: 'test_base_url/multipartAuto',
additionalMatcher: (_, config: any): boolean => {
return (
// multipart/form-data requests doesn't have explicitly set Content-Type
!config?.headers?.['Content-Type'] &&
if (
// multipart/form-data requests shouldn't have explicitly set Content-Type
config?.headers?.['Content-Type'] ||
// the body should have been converted to FormData
config.body instanceof FormData
);
!(config.body instanceof FormData)
) {
return false;
}

// check FormData transformation
assert.deepEqual(config.body.getAll('title'), ['test']);
assert.deepEqual(config.body.getAll('roles'), ['a', 'b']);
assert.equal(config.body.getAll('files').length, 2);
assert.equal(config.body.getAll('files')[0].size, 2);
assert.equal(config.body.getAll('files')[1].size, 1);

return true;
},
replyCode: 200,
replyBody: 'successMultipartAuto',
Expand All @@ -222,7 +233,11 @@ describe('Client', function() {
[client.send('/123', { method: 'PATCH' }), 'successPatch'],
[client.send('/123', { method: 'DELETE' }), 'successDelete'],
[client.send('/multipart', { method: 'GET', body: new FormData() }), 'successMultipart'],
[client.send('/multipartAuto', { method: 'GET', body: {title: "test", file: new Blob([])} }), 'successMultipartAuto'],
[client.send('/multipartAuto', { method: 'GET', body: {
title: "test",
roles: ["a", "b"],
files: [new Blob(['11']), new Blob(['2'])]},
}), 'successMultipartAuto'],
];
for (let testCase of testCases) {
const responseData = await testCase[0]
Expand Down

0 comments on commit 14ab4c5

Please sign in to comment.