react-native-background-upload icon indicating copy to clipboard operation
react-native-background-upload copied to clipboard

Typescript Definition

Open dwilt opened this issue 5 years ago • 6 comments

Hey guys, I wanted to contribute some TypeScript definitions that we used in our project. I tried to do the best I could to infer from the index.js how to best set this up. Would you mind reviewing so I can publish to DefinitelyTyped?

declare module 'react-native-background-upload' {
  type UploadEvent = 'progress' | 'error' | 'completed' | 'cancelled';

  interface NotificationArgs {
    enabled: boolean;
  }

  interface StartUploadArgs {
    url: string;
    path: string;
    method?: 'PUT' | 'POST';
    // Optional, because raw is default
    type?: 'raw' | 'multipart';
    // This option is needed for multipart type
    field?: string;
    customUploadId?: string;
    // parameters are supported only in multipart type
    parameters?: { [key: string]: string };
    headers?: Object;
    notification?: NotificationArgs;
  }

  export interface ProgressListener {
    ({ progress }: { progress: number }): void;
  }

  export interface ErrorListener {
    ({ error }: { error: Error }): void;
  }

  export interface CompletedListener {
    ({ responseCode }: { responseCode: number }): void;
  }

  type ListenerCallback = ProgressListener | ErrorListener | CompletedListener;

  function addListener(event: UploadEvent, uploadId: string | null, listener: ListenerCallback): EventSubscription;

  function cancelUpload(uploadId: string): Promise<boolean>;

  function startUpload(options: StartUploadArgs): Promise<string>;

  interface FileInfo {
    name: string;
    exists: boolean;
    size?: number;
    extension?: string;
    mimeType?: string;
  }

  function getFileInfo(path: string): Promise<FileInfo>;
}

dwilt avatar Jun 02 '19 19:06 dwilt

addListener returns an EventSubscription

maxschmeling avatar Aug 06 '19 16:08 maxschmeling

Also, the second parameter to addListener (uploadId) should be string | null.

maxschmeling avatar Aug 27 '19 19:08 maxschmeling

startUpload returns a Promise<string> with the uploadId if I am not mistaken

kdawgwilk avatar Oct 08 '19 19:10 kdawgwilk

Also missing:

interface FileInfo {
    name: string;
    exists: boolean;
    size?: number;
    extension?: string;
    mimeType?: string;
  }

  function getFileInfo(path: string): Promise<FileInfo>;

kdawgwilk avatar Oct 08 '19 19:10 kdawgwilk

Also, the second parameter to addListener (uploadId) should be string | null.

@maxschmeling how could the uploadId be null? The way the Usage section looks, it would seem that all of the events you can add a listener for (EventType) require an uploadId, no?

dwilt avatar Oct 08 '19 22:10 dwilt

Wow, never saw this comment, and this is waaaaay late, but you can pass null to listen for all events instead of for a specific uploadId.

maxschmeling avatar Jan 12 '21 18:01 maxschmeling