Przeglądaj źródła

@uppy/core: simplify types with class generic (#4761)

make Uppy class generic
JokcyLou 1 rok temu
rodzic
commit
0854f12392

+ 18 - 46
packages/@uppy/core/types/index.d.ts

@@ -291,26 +291,23 @@ export interface UppyEventMap<
   upload: UploadCallback
 }
 
-export class Uppy {
+export class Uppy<
+  TMeta extends IndexedObject<any> = Record<string, unknown>,
+  TBody extends IndexedObject<any> = Record<string, unknown>,
+> {
   constructor(opts?: UppyOptions)
 
-  on<K extends keyof UppyEventMap>(event: K, callback: UppyEventMap[K]): this
-
-  on<K extends keyof UppyEventMap, TMeta extends IndexedObject<any>>(
+  on<K extends keyof UppyEventMap>(
     event: K,
     callback: UppyEventMap<TMeta>[K],
   ): this
 
-  once<K extends keyof UppyEventMap>(event: K, callback: UppyEventMap[K]): this
-
-  once<K extends keyof UppyEventMap, TMeta extends IndexedObject<any>>(
+  once<K extends keyof UppyEventMap>(
     event: K,
     callback: UppyEventMap<TMeta>[K],
   ): this
 
-  off<K extends keyof UppyEventMap>(event: K, callback: UppyEventMap[K]): this
-
-  off<K extends keyof UppyEventMap, TMeta extends IndexedObject<any>>(
+  off<K extends keyof UppyEventMap>(
     event: K,
     callback: UppyEventMap<TMeta>[K],
   ): this
@@ -326,9 +323,7 @@ export class Uppy {
 
   setState(patch: Record<string, unknown>): void
 
-  getState<
-    TMeta extends IndexedObject<any> = Record<string, unknown>,
-  >(): State<TMeta>
+  getState(): State<TMeta>
 
   setFileState(fileID: string, state: Record<string, unknown>): void
 
@@ -346,24 +341,13 @@ export class Uppy {
 
   removeUploader(fn: UploadHandler): void
 
-  setMeta<TMeta extends IndexedObject<any> = Record<string, unknown>>(
-    data: TMeta,
-  ): void
+  setMeta(data: TMeta): void
 
-  setFileMeta<TMeta extends IndexedObject<any> = Record<string, unknown>>(
-    fileID: string,
-    data: TMeta,
-  ): void
+  setFileMeta(fileID: string, data: TMeta): void
 
-  getFile<
-    TMeta extends IndexedObject<any> = Record<string, unknown>,
-    TBody extends IndexedObject<any> = Record<string, unknown>,
-  >(fileID: string): UppyFile<TMeta, TBody>
+  getFile(fileID: string): UppyFile<TMeta, TBody>
 
-  getFiles<
-    TMeta extends IndexedObject<any> = Record<string, unknown>,
-    TBody extends IndexedObject<any> = Record<string, unknown>,
-  >(): Array<UppyFile<TMeta, TBody>>
+  getFiles(): Array<UppyFile<TMeta, TBody>>
 
   getObjectOfFilesPerState(): {
     newFiles: Array<UppyFile>
@@ -383,13 +367,9 @@ export class Uppy {
     isSomeGhost: boolean
   }
 
-  addFile<TMeta extends IndexedObject<any> = Record<string, unknown>>(
-    file: AddFileOptions<TMeta>,
-  ): string
+  addFile(file: AddFileOptions<TMeta>): string
 
-  addFiles<TMeta extends IndexedObject<any> = Record<string, unknown>>(
-    files: AddFileOptions<TMeta>[],
-  ): void
+  addFiles(files: AddFileOptions<TMeta>[]): void
 
   removeFile(fileID: string, reason?: FileRemoveReason): void
 
@@ -399,15 +379,11 @@ export class Uppy {
 
   resumeAll(): void
 
-  retryAll<
-    TMeta extends IndexedObject<any> = Record<string, unknown>,
-  >(): Promise<UploadResult<TMeta>>
+  retryAll(): Promise<UploadResult<TMeta>>
 
   cancelAll(options?: CancelOptions): void
 
-  retryUpload<TMeta extends IndexedObject<any> = Record<string, unknown>>(
-    fileID: string,
-  ): Promise<UploadResult<TMeta>>
+  retryUpload(fileID: string): Promise<UploadResult<TMeta>>
 
   getID(): string
 
@@ -441,15 +417,11 @@ export class Uppy {
 
   log(msg: string, type?: LogLevel): void
 
-  restore<TMeta extends IndexedObject<any> = Record<string, unknown>>(
-    uploadID: string,
-  ): Promise<UploadResult<TMeta>>
+  restore(uploadID: string): Promise<UploadResult<TMeta>>
 
   addResultData(uploadID: string, data: Record<string, unknown>): void
 
-  upload<TMeta extends IndexedObject<any> = Record<string, unknown>>(): Promise<
-    UploadResult<TMeta>
-  >
+  upload(): Promise<UploadResult<TMeta>>
 }
 
 export default Uppy

+ 8 - 8
packages/@uppy/core/types/index.test-d.ts

@@ -51,8 +51,8 @@ type anyObject = Record<string, unknown>
   type ResponseBody = {
     averageColor: string
   }
-  const uppy = new Uppy()
-  const f = uppy.getFile<Meta, ResponseBody>('virtual')
+  const uppy = new Uppy<Meta, ResponseBody>()
+  const f = uppy.getFile('virtual')
   expectType<ResponseBody>(f.response!.body) // eslint-disable-line @typescript-eslint/no-non-null-assertion
 }
 
@@ -78,8 +78,10 @@ type anyObject = Record<string, unknown>
 }
 
 {
+  // Meta signature
+  type Meta = { myCustomMetadata: string }
   /* eslint-disable @typescript-eslint/no-empty-function */
-  const uppy = new Uppy()
+  const uppy = new Uppy<Meta>()
   // can emit events with internal event types
   uppy.emit('upload')
   uppy.emit('complete', () => {})
@@ -103,9 +105,7 @@ type anyObject = Record<string, unknown>
     const successResults = result.successful
   })
 
-  // Meta signature
-  type Meta = { myCustomMetadata: string }
-  uppy.on<'complete', Meta>('complete', (result) => {
+  uppy.on('complete', (result) => {
     // eslint-disable-next-line @typescript-eslint/no-unused-vars
     const meta = result.successful[0].meta.myCustomMetadata
   })
@@ -115,7 +115,7 @@ type anyObject = Record<string, unknown>
     const meta = file?.meta.myCustomMetadata
   }
 
-  uppy.off<'upload-success', Meta>('upload-success', handleUpload)
+  uppy.off('upload-success', handleUpload)
 
   interface CustomResponse extends SuccessResponse {
     body?: { someValue: string }
@@ -127,7 +127,7 @@ type anyObject = Record<string, unknown>
   ) => {
     const res = response.body?.someValue
   }
-  uppy.on<'upload-success', Meta>('upload-success', onUploadSuccess)
+  uppy.on('upload-success', onUploadSuccess)
 }
 
 {