Browse Source

Improve addFile API (#1739)

* return file id of added files

* add documentation for returned value

* fix linting

* update test to new addFile API

* add docs for the change in the addFile API
Elio Capella Sánchez 5 years ago
parent
commit
ef2cf98de4

+ 3 - 0
packages/@uppy/core/src/index.js

@@ -429,6 +429,7 @@ class Uppy {
    * and start an upload if `autoProceed === true`.
    *
    * @param {Object} file object to add
+   * @returns {string} id for the added file
    */
   addFile (file) {
     const { files, allowNewUpload } = this.getState()
@@ -524,6 +525,8 @@ class Uppy {
         })
       }, 4)
     }
+
+    return fileID
   }
 
   removeFile (fileID) {

+ 1 - 3
packages/@uppy/core/src/index.test.js

@@ -598,14 +598,12 @@ describe('src/Core', () => {
       const core = new Core()
       core.on('file-added', fileAddedEventMock)
 
-      core.addFile({
+      const fileId = core.addFile({
         source: 'jest',
         name: 'foo.jpg',
         type: 'image/jpeg',
         data: fileData
       })
-
-      const fileId = Object.keys(core.getState().files)[0]
       const newFile = {
         extension: 'jpg',
         id: fileId,

+ 2 - 0
website/src/docs/uppy.md

@@ -341,6 +341,8 @@ uppy.addFile({
 
 If `uppy.opts.autoProceed === true`, Uppy will begin uploading automatically when files are added.
 
+This function will return the generated id for the file that was added.
+
 > Sometimes you might need to add a remote file to Uppy. This can be achieved by [fetching the file, then creating a Blob object, or using the Url plugin with Companion](https://github.com/transloadit/uppy/issues/1006#issuecomment-413495493).
 
 ### `uppy.removeFile(fileID)`