diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..7e12b48 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,118 @@ +// Type definitions for @kesha-antonov/react-native-background-downloader 2.6 +// Project: https://github.com/kesha-antonov/react-native-background-downloader +// Definitions by: Philip Su , +// Adam Hunter , +// Junseong Park +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export interface DownloadHeaders { + [key: string]: string | null; +} + +type SetHeaders = (h: DownloadHeaders) => void; + +export interface TaskInfoObject { + id: string; + percent: number; + bytesWritten: number; + totalBytes: number; +} +export type TaskInfo = string | TaskInfoObject; + +export interface BeginHandlerObject { + expectedBytes: number; + headers: { [key: string]: string }; +} + +export type BeginHandler = ({ + expectedBytes, + headers, +}: BeginHandlerObject) => any; +export type ProgressHandler = ( + percent: number, + bytesWritten: number, + totalBytes: number +) => any; +export type DoneHandler = () => any; +export type ErrorHandler = (error: any, errorCode: any) => any; +export type DownloadTaskState = + | "DOWNLOADING" + | "PAUSED" + | "DONE" + | "FAILED" + | "STOPPED"; + +export interface DownloadTask { + constructor: (taskInfo: TaskInfo) => DownloadTask; + + id: string; + state: DownloadTaskState; + percent: number; + bytesWritten: number; + totalBytes: number; + + begin: (handler: BeginHandler) => DownloadTask; + progress: (handler: ProgressHandler) => DownloadTask; + done: (handler: DoneHandler) => DownloadTask; + error: (handler: ErrorHandler) => DownloadTask; + + _beginHandler: BeginHandler; + _progressHandler: ProgressHandler; + _doneHandler: DoneHandler; + _errorHandler: ErrorHandler; + + pause: () => any; + resume: () => any; + stop: () => any; +} + +export type CheckForExistingDownloads = () => Promise; +export type EnsureDownloadsAreRunning = () => Promise; + +export interface DownloadOption { + id: string; + url: string; + destination: string; + headers?: DownloadHeaders | undefined; +} + +export type Download = (options: DownloadOption) => DownloadTask; +export type CompleteHandler = (id: string) => void; + +export interface Directories { + documents: string; +} + +export interface Network { + WIFI_ONLY: string; + ALL: string; +} + +export interface Priority { + HIGH: string; + MEDIUM: string; + LOW: string; +} + +export const setHeaders: SetHeaders; +export const checkForExistingDownloads: CheckForExistingDownloads; +export const ensureDownloadsAreRunning: EnsureDownloadsAreRunning; +export const download: Download; +export const completeHandler: CompleteHandler; +export const directories: Directories; +export const Network: Network; +export const Priority: Priority; + +export interface RNBackgroundDownloader { + setHeaders: SetHeaders; + checkForExistingDownloads: CheckForExistingDownloads; + ensureDownloadsAreRunning: EnsureDownloadsAreRunning; + download: Download; + completeHandler: CompleteHandler; + directories: Directories; + Network: Network; + Priority: Priority; +} + +declare const RNBackgroundDownloader: RNBackgroundDownloader; +export default RNBackgroundDownloader; diff --git a/package.json b/package.json index 1de2323..ef84f37 100644 --- a/package.json +++ b/package.json @@ -25,11 +25,13 @@ } ], "main": "index.js", + "types": "index.d.ts", "files": [ "README.md", "LICENSE", "react-native-background-downloader.podspec", "package.json", + "index.d.ts", "index.js", "lib/", "ios/",