Skip to content

Commit

Permalink
Typescript patches (#255)
Browse files Browse the repository at this point in the history
* Two typescript fixes for react-widget

Reverse-engineer and fix type type problems:

DialogApi:
  fileColl: FileInfo[] => CollectionOfPromises<FileInfo>;

The fileColl property is not an array of file infos.  It is a collection of promises that resolve to file infos when they are uploaded.

This is initialized here:
  https://github.com/uploadcare/uploadcare-widget/blob/1b33ac45986bffeb311bdb1293170b3925932aa1/src/widget/dialog.js#L265

```javascript
    // files collection
    this.files = new CollectionOfPromises(files)
```

Second, FileUpload is cancelable.  This is added in uploadcare-widget:
https://github.com/uploadcare/uploadcare-widget/blob/1b33ac45986bffeb311bdb1293170b3925932aa1/src/files/base.js#L230

```javascript
api.cancel = this.__cancel.bind(this)
```

* Add definition for FileGroup and update the promise for FilesUpload

* Typescript patch for FileInfo

FileInfo: the uuid/mimeType can both be null for new files when the validator is invoked

* Add changelog entries

* Fix the FilesUpload interface.   The promise is a FileGruopUpload, not a FileGruop.  The difference being a promise that resolves to a FileGroup vs a FileGroup
  • Loading branch information
johnnaegle authored Mar 31, 2021
1 parent 3953716 commit 438b9e4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
### Bug Fixes

* **types** Export types FileUpload and FilesUpload.
* **types** FileInfo type updated to account for null uuid and mime-type during validation callback
* **types** Fix the type of the fileColl attibute of the dialog api
* **types** Add and export the FileGroup type

## [1.3.5](https://github.com/uploadcare/react-widget/compare/v1.3.4...v1.3.5) (2021-02-26)

Expand Down
64 changes: 59 additions & 5 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,13 @@ interface SourceInfo {
}

interface FileInfo {
uuid: Uuid;
uuid: null | Uuid;
name: null | string;
size: null | number;
isStored: null | boolean;
isImage: null | boolean;
originalImageInfo: null | OriginalImageInfo;
mimeType: string;
mimeType: null | string;
originalUrl: null | string;
cdnUrl: null | string;
cdnUrlModifiers: null | string;
Expand All @@ -252,10 +252,40 @@ interface FileInfo {

type OnTabVisibilityCallback = (tab: string, shown: boolean) => void;

interface Collection<T> {
onAdd: JQuery.Callbacks;
onRemove: JQuery.Callbacks;
onSort: JQuery.Callbacks;
onReplace: JQuery.Callbacks;
__items: T[];

add: (item: T) => JQuery.Callbacks | undefined;
remove: (item: T) => JQuery.Callbacks | undefined;
clear: () => JQuery.Callbacks[];
replace: (oldItem: T, newItem: T) => JQuery.Callbacks | undefined;
sort: (comparator: (a: T, b: T) => boolean) => JQuery.Callbacks;
get: (index: number) => T;
length: () => number;
}

interface UniqCollection<T> extends Collection<T> {
add: (item: T) => JQuery.Callbacks | undefined;
}
interface CollectionOfPromises<T> extends UniqCollection<JQuery.Deferred<T>> {
anyDoneList: JQuery.Callbacks;
anyFailList: JQuery.Callbacks;
anyProgressList: JQuery.Callbacks;

onAnyDone: unknown;
onAnyFail: unknown;
onAnyProgress: unknown;
lastProgresses: unknown;
autoThen: unknown;
}
interface DialogApi {
addFiles(files: FileInfo[]): void;
switchTab(tab: string): void;
fileColl: FileInfo[];
fileColl: CollectionOfPromises<FileInfo>;
hideTab(tab: string): void;
showTab(tab: string): void;
isTabVisible(tab: string): boolean;
Expand Down Expand Up @@ -322,9 +352,31 @@ interface WidgetAPI {
getInput: () => HTMLInputElement;
}

type FileUpload = JQuery.Deferred<FileInfo>;
interface FileUpload extends JQuery.Deferred<FileInfo> {
cancel: () => FileUpload;
}

/**
* The result of uploading multiple files to upload care is a file group.
*
* The react upload care widget does not (yet) define this type.
*
* This type is reverse engineered from stepping into the debugger.
*/
interface FileGroup {
cdnUrl: string;
count: number;
isImage: boolean;
isStored: boolean;
name: string;
size: number;
uuid: string;
}

interface FileGroupUpload extends JQuery.Deferred<FileGroup> { }

interface FilesUpload {
promise: () => FileUpload;
promise: () => FileGroupUpload;
files: () => FileUpload[];
}

Expand Down Expand Up @@ -365,6 +417,8 @@ export {
Uuid,
SourceInfo,
FileInfo,
FileGroup,
FileGroupUpload,
FileUpload,
FilesUpload,
DialogApi,
Expand Down

0 comments on commit 438b9e4

Please sign in to comment.