Adds Download state enum for all possible download states (#42311)

Adds id property to DownloadTask
Adds DownloadHeaders interface for setHeaders parameter
This commit is contained in:
Adam Hunter 2020-02-12 13:38:29 -05:00 committed by GitHub
parent 8d4254034e
commit 3256d8cd9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 4 deletions

View File

@ -1,9 +1,14 @@
// Type definitions for react-native-background-downloader 2.3
// Project: https://github.com/EkoLabs/react-native-background-downloader.git
// Definitions by: Junseong Park <https://github.com/Kweiza>
// Adam Hunter <https://github.com/adamrhunter>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
type SetHeaders = (h: object) => any;
export interface DownloadHeaders {
[key: string]: string | null;
}
type SetHeaders = (h: DownloadHeaders) => void;
export interface TaskInfoObject {
id: string;
@ -17,10 +22,17 @@ export type BeginHandler = (expectedBytes: number) => any;
export type ProgressHandler = (percent: number, bytesWritten: number, totalBytes: number) => any;
export type DoneHandler = () => any;
export type ErrorHandler = (error: any, errorCode: any) => any;
export enum DownloadTaskState {
DOWNLOADING = 'DOWNLOADING',
PAUSED = 'PAUSED',
DONE = 'DONE',
}
export interface DownloadTask {
constructor: (taskInfo: TaskInfo) => DownloadTask;
state: string;
id: string;
state: DownloadTaskState;
percent: number;
bytesWritten: number;
totalBytes: number;
@ -46,7 +58,7 @@ export interface DownloadOption {
id: string;
url: string;
destination: string;
headers?: object;
headers?: DownloadHeaders;
}
export type Download = (options: DownloadOption) => DownloadTask;

View File

@ -1,9 +1,18 @@
import RNBackgroundDownloader, { DownloadTask } from 'react-native-background-downloader';
import RNBackgroundDownloader, { DownloadTask, DownloadTaskState } from 'react-native-background-downloader';
// Set global headers for downloader
RNBackgroundDownloader.setHeaders({
'x-custom': 'Custom Header',
'x-custom-2': 'Another Custom Header',
});
const task = RNBackgroundDownloader.download({
id: 'file123',
url: 'https://link-to-very.large/file.zip',
destination: `${RNBackgroundDownloader.directories.documents}/file.zip`,
headers: {
'x-custom-3': 'a-third-header',
},
})
.begin(expectedBytes => {
console.log(`Going to download ${expectedBytes} bytes!`);
@ -19,6 +28,22 @@ const task = RNBackgroundDownloader.download({
});
const taskFuncTest = (task: DownloadTask) => {
// Check task state
switch (task.state) {
case DownloadTaskState.DONE: {
console.log('Task is in state DONE');
break;
}
case DownloadTaskState.DOWNLOADING: {
console.log('Task is in state DOWNLOADING');
break;
}
case DownloadTaskState.PAUSED: {
console.log('Task is in state PAUSED');
break;
}
}
// Pause the task
task.pause();