mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-02-03 23:42:50 +00:00
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:
parent
8d4254034e
commit
3256d8cd9f
@ -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;
|
||||
|
||||
@ -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();
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user