Skip to content
This repository was archived by the owner on Sep 1, 2022. It is now read-only.

Commit f12b4f7

Browse files
committed
refactor(upload): data type is FormData
Upload with a FormData #2
1 parent 401e0e3 commit f12b4f7

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"browser-env": "^3.2.5",
6565
"codecov": "^3.0.0",
6666
"cz-conventional-changelog": "^2.1.0",
67+
"form-data": "^2.3.2",
6768
"gh-pages": "^1.0.0",
6869
"npm-run-all": "^4.1.2",
6970
"npm-scripts-info": "^0.3.6",

src/lib/sdk.spec.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,18 +430,38 @@ test('Get file', async t => {
430430
);
431431
});
432432

433-
test('Upload file', async t => {
434-
await t.context.strapi.upload('foo');
433+
test('Upload file on Node.js', async t => {
434+
const FormData = require('form-data');
435+
const form = new FormData();
436+
form.append('files', 'foo', 'file-name.ext');
437+
await t.context.strapi.upload(form);
435438

436439
t.true(
437440
t.context.axiosRequest.calledWithExactly({
438-
data: 'foo',
441+
data: form,
439442
method: 'post',
440443
url: '/upload'
441444
})
442445
);
443446
});
444447

448+
test.serial('Upload file on Browser', async t => {
449+
browserEnv(['window']);
450+
const globalAny: any = global;
451+
const form = new globalAny.window.FormData();
452+
form.append('files', new globalAny.window.Blob(['foo'], { type: 'text/plain' }), 'file-name.ext');
453+
await t.context.strapi.upload(form);
454+
455+
t.true(
456+
t.context.axiosRequest.calledWithExactly({
457+
data: form,
458+
method: 'post',
459+
url: '/upload'
460+
})
461+
);
462+
delete globalAny.window;
463+
});
464+
445465
test('Set token', t => {
446466
t.is(t.context.strapi.axios.defaults.headers.common.Authorization, undefined);
447467
t.context.strapi.setToken('foo');

src/lib/sdk.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,31 @@ export default class Strapi {
314314

315315
/**
316316
* Upload files
317-
* @param data Files
317+
*
318+
* ### Browser example
319+
* ```js
320+
* const form = new FormData();
321+
* form.append('files', fileInputElement.files[0], 'file-name.ext');
322+
* form.append('files', fileInputElement.files[1], 'file-2-name.ext');
323+
* const files = await strapi.upload(form);
324+
* ```
325+
*
326+
* ### Node.js example
327+
* ```js
328+
* const FormData = require('form-data');
329+
* const fs = require('fs');
330+
* const form = new FormData();
331+
* form.append('files', fs.createReadStream('./file-name.ext'), 'file-name.ext');
332+
* const files = await strapi.upload(form, {
333+
* headers: form.getHeaders()
334+
* });
335+
* ```
336+
*
337+
* @param data FormData
318338
* @param requestConfig
319339
*/
320340
public upload(
321-
data: any,
341+
data: FormData,
322342
requestConfig?: AxiosRequestConfig
323343
): Promise<object> {
324344
return this.request('post', '/upload', {

0 commit comments

Comments
 (0)