react-native-background-upload
react-native-background-upload copied to clipboard
Typescript Definition
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>;
}
addListener
returns an EventSubscription
Also, the second parameter to addListener
(uploadId
) should be string | null
.
startUpload
returns a Promise<string>
with the uploadId if I am not mistaken
Also missing:
interface FileInfo {
name: string;
exists: boolean;
size?: number;
extension?: string;
mimeType?: string;
}
function getFileInfo(path: string): Promise<FileInfo>;
Also, the second parameter to
addListener
(uploadId
) should bestring | 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?
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
.