-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathindex.d.ts
184 lines (141 loc) · 4.71 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import {
type BrowserView,
type BrowserWindow,
type DownloadItem,
type SaveDialogOptions,
} from 'electron';
export type Progress = {
percent: number;
transferredBytes: number;
totalBytes: number;
};
export type File = {
filename: string;
path: string;
fileSize: number;
mimeType: string;
url: string;
};
export type Options = {
/**
Show a `Save As…` dialog instead of downloading immediately.
Note: Only use this option when strictly necessary. Downloading directly without a prompt is a much better user experience.
@default false
*/
readonly saveAs?: boolean;
/**
The directory to save the file in.
Must be an absolute path.
Default: [User's downloads directory](https://electronjs.org/docs/api/app/#appgetpathname)
*/
readonly directory?: string;
/**
Name of the saved file.
This option only makes sense for `electronDl.download()`.
Default: [`downloadItem.getFilename()`](https://electronjs.org/docs/api/download-item/#downloaditemgetfilename)
*/
readonly filename?: string;
/**
Title of the error dialog. Can be customized for localization.
Note: Error dialog will not be shown in `electronDl.download()`. Please handle error manually.
@default 'Download Error'
*/
readonly errorTitle?: string;
/**
Message of the error dialog. `{filename}` is replaced with the name of the actual file. Can be customized for localization.
Note: Error dialog will not be shown in `electronDl.download()`. Please handle error manually.
@default 'The download of {filename} was interrupted'
*/
readonly errorMessage?: string;
/**
Optional callback that receives the [download item](https://electronjs.org/docs/api/download-item).
You can use this for advanced handling such as canceling the item like `item.cancel()`.
*/
readonly onStarted?: (item: DownloadItem) => void;
/**
Optional callback that receives an object containing information about the progress of the current download item.
*/
readonly onProgress?: (progress: Progress) => void;
/**
Optional callback that receives an object containing information about the combined progress of all download items done within any registered window.
Each time a new download is started, the next callback will include it. The progress percentage could therefore become smaller again.
This callback provides the same data that is used for the progress bar on the app icon.
*/
readonly onTotalProgress?: (progress: Progress) => void;
/**
Optional callback that receives the [download item](https://electronjs.org/docs/api/download-item) for which the download has been cancelled.
*/
readonly onCancel?: (item: DownloadItem) => void;
/**
Optional callback that receives an object with information about an item that has been completed. It is called for each completed item.
*/
readonly onCompleted?: (file: File) => void;
/**
Reveal the downloaded file in the system file manager, and if possible, select the file.
@default false
*/
readonly openFolderWhenDone?: boolean;
/**
Show a file count badge on the macOS/Linux dock/taskbar icon when a download is in progress.
@default true
*/
readonly showBadge?: boolean;
/**
Show a progress bar on the dock/taskbar icon when a download is in progress.
@default true
*/
readonly showProgressBar?: boolean;
/**
Allow downloaded files to overwrite files with the same name in the directory they are saved to.
The default behavior is to append a number to the filename.
@default false
*/
readonly overwrite?: boolean;
/**
Customize the save dialog.
If `defaultPath` is not explicity defined, a default value is assigned based on the file path.
@default {}
*/
readonly dialogOptions?: SaveDialogOptions;
};
/**
Error thrown if `item.cancel()` was called.
*/
export class CancelError extends Error {}
/**
Register the helper for all windows.
@example
```
import {app, BrowserWindow} from 'electron';
import electronDl from 'electron-dl';
electronDl();
let mainWindow;
(async () => {
await app.whenReady();
mainWindow = new BrowserWindow();
})();
```
*/
export default function electronDl(options?: Options): void;
/**
This can be useful if you need download functionality in a reusable module.
@param window - Window to register the behavior on.
@param url - URL to download.
@returns A promise for the downloaded file.
@throws {CancelError} An error if the user calls `item.cancel()`.
@throws {Error} An error if the download fails.
@example
```
import {BrowserWindow, ipcMain} from 'electron';
import {download} from 'electron-dl';
ipcMain.on('download-button', async (event, {url}) => {
const win = BrowserWindow.getFocusedWindow();
console.log(await download(win, url));
});
```
*/
export function download(
window: BrowserWindow | BrowserView,
url: string,
options?: Options
): Promise<DownloadItem>;