diff --git a/types/react-native-background-downloader/index.d.ts b/types/react-native-background-downloader/index.d.ts index 92fbac241c..c5eed40d67 100644 --- a/types/react-native-background-downloader/index.d.ts +++ b/types/react-native-background-downloader/index.d.ts @@ -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 +// Adam Hunter // 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; diff --git a/types/react-native-background-downloader/react-native-background-downloader-tests.tsx b/types/react-native-background-downloader/react-native-background-downloader-tests.tsx index 5ee266e781..d7c2ec5b99 100644 --- a/types/react-native-background-downloader/react-native-background-downloader-tests.tsx +++ b/types/react-native-background-downloader/react-native-background-downloader-tests.tsx @@ -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();