Skip to content

Commit

Permalink
Add file support for formData objects
Browse files Browse the repository at this point in the history
Currently, `fusion-plugin-rpc` is able to support key/value pairs that are stored in formData objects, but does not currently process file objects that can be sent through formData. This PR intends to add support for parsed files from formData without breaking the existing implementation for parsing formData key/value pairs.

With this new addition, files will be assigned a key based on the name assigned to them when initially appended to a formData object, and exist in the resulting key/value pairs that will be stored in the `body` object that is returned by `fusion-plugin-rpc`.

Co-authored-by: ghostfruitleaf <49515396+ghostfruitleaf@users.noreply.github.com>
Co-authored-by: shYkiSto <siarhei@uber.com>
  • Loading branch information
3 people authored and fusionjs-sync-bot[bot] committed Nov 24, 2021
1 parent cb8a9b1 commit 8d1f06c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
15 changes: 12 additions & 3 deletions fusion-plugin-rpc/__tests__/index.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,19 @@ test('success status request w/form data', (done) => {
});
expect(typeof rpc.request).toBe('function');
expect(rpc.request('test') instanceof Promise).toBeTruthy();
// eslint-disable-next-line cup/no-undef
/* eslint-disable cup/no-undef */
const formData = new FormData();
formData.append('random', 'some-random');
formData.append('foo', 'foo content');
// TODO do we need to test with file as well?
// formData.append('file', '<how to do this>');

const mockFile_1 = new File(['foo'], 'foo.csv', {type: 'text/csv'});
const mockFile_2 = new File(['bar'], 'bar.csv', {type: 'text/csv'});

formData.append('fooFile', mockFile_1);
formData.append('barFile', mockFile_2);

/* eslint-enable cup/no-undef */

rpc
.request('test', formData)
.then(([url, options]) => {
Expand All @@ -353,6 +360,8 @@ test('success status request w/form data', (done) => {
expect(Array.from(options.body.entries())).toEqual([
['random', 'some-random'],
['foo', 'foo content'],
['fooFile', mockFile_1],
['barFile', mockFile_2],
]);
done();
})
Expand Down
9 changes: 5 additions & 4 deletions fusion-plugin-rpc/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ const pluginFactory: () => RPCPluginType = () =>
if (err) {
reject(err);
}
if (fields && Object.keys(fields).length) {
resolve(fields);
}
resolve(files.file);

resolve({
...fields,
...files,
});
});
});
} else {
Expand Down

0 comments on commit 8d1f06c

Please sign in to comment.