-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor TypeScript definition to CommonJS compatible export (#75)
- Loading branch information
1 parent
0265a3d
commit 118650b
Showing
4 changed files
with
125 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,123 @@ | ||
import {BrowserWindow, DownloadItem} from 'electron'; | ||
|
||
export interface 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; | ||
declare namespace electronDl { | ||
interface Options { | ||
/** | ||
Show a `Save As…` dialog instead of downloading immediately. | ||
/** | ||
* Directory to save the file in. | ||
* | ||
* Default: [User's downloads directory](https://electronjs.org/docs/api/app/#appgetpathname) | ||
*/ | ||
readonly directory?: string; | ||
Note: Only use this option when strictly necessary. Downloading directly without a prompt is a much better user experience. | ||
/** | ||
* 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; | ||
@default false | ||
*/ | ||
readonly saveAs?: boolean; | ||
|
||
/** | ||
* Title of the error dialog. Can be customized for localization. | ||
* | ||
* @default 'Download Error' | ||
*/ | ||
readonly errorTitle?: string; | ||
/** | ||
Directory to save the file in. | ||
/** | ||
* Message of the error dialog. `{filename}` is replaced with the name of the actual file. Can be customized for localization. | ||
* | ||
* @default 'The download of {filename} was interrupted' | ||
*/ | ||
readonly errorMessage?: string; | ||
Default: [User's downloads directory](https://electronjs.org/docs/api/app/#appgetpathname) | ||
*/ | ||
readonly directory?: 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; | ||
/** | ||
Name of the saved file. | ||
This option only makes sense for `electronDl.download()`. | ||
/** | ||
* Optional callback that receives a number between `0` and `1` representing the progress of the current download. | ||
*/ | ||
readonly onProgress?: (percent: number) => void; | ||
Default: [`downloadItem.getFilename()`](https://electronjs.org/docs/api/download-item/#downloaditemgetfilename) | ||
*/ | ||
readonly filename?: string; | ||
|
||
/** | ||
* 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; | ||
/** | ||
Title of the error dialog. Can be customized for localization. | ||
@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. | ||
@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 a number between `0` and `1` representing the progress of the current download. | ||
*/ | ||
readonly onProgress?: (percent: number) => 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; | ||
|
||
/** | ||
Reveal the downloaded file in the system file manager, and if possible, select the file. | ||
@default false | ||
*/ | ||
readonly openFolderWhenDone?: boolean; | ||
|
||
/** | ||
Shows the file count badge on macOS/Linux dock icons when download is in progress. | ||
@default true | ||
*/ | ||
readonly showBadge?: boolean; | ||
} | ||
} | ||
|
||
declare const electronDl: { | ||
/** | ||
* Reveal the downloaded file in the system file manager, and if possible, select the file. | ||
* | ||
* @default false | ||
*/ | ||
readonly openFolderWhenDone?: boolean; | ||
Register the helper for all windows. | ||
@example | ||
``` | ||
import {app, BrowserWindow} from 'electron'; | ||
import electronDl = require('electron-dl'); | ||
electronDl(); | ||
let win; | ||
(async () => { | ||
await app.whenReady(); | ||
win = new BrowserWindow(); | ||
})(); | ||
``` | ||
*/ | ||
(options?: electronDl.Options): void; | ||
|
||
/** | ||
* Shows the file count badge on macOS/Linux dock icons when download is in progress. | ||
* | ||
* @default true | ||
*/ | ||
readonly showBadge?: boolean; | ||
} | ||
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. | ||
@example | ||
``` | ||
import {BrowserWindow, ipcMain} from 'electron'; | ||
import electronDl = require('electron-dl'); | ||
ipcMain.on('download-button', async (event, {url}) => { | ||
const win = BrowserWindow.getFocusedWindow(); | ||
console.log(await electronDl.download(win, url)); | ||
}); | ||
``` | ||
*/ | ||
download( | ||
window: BrowserWindow, | ||
url: string, | ||
options?: electronDl.Options | ||
): Promise<DownloadItem>; | ||
|
||
// TODO: Remove this for the next major release | ||
default: typeof electronDl; | ||
}; | ||
|
||
/** | ||
* Register the helper for all windows. | ||
* | ||
* @example | ||
* | ||
* import {app, BrowserWindow} from 'electron'; | ||
* import electronDl from 'electron-dl'; | ||
* | ||
* electronDl(); | ||
* | ||
* let win; | ||
* (async () => { | ||
* await app.whenReady(); | ||
* win = 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. | ||
* | ||
* @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, | ||
url: string, | ||
options?: Options | ||
): Promise<DownloadItem>; | ||
export = electronDl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import {expectType} from 'tsd-check'; | ||
/// <reference lib="dom"/> | ||
/// <reference types="node"/> | ||
import {expectType} from 'tsd'; | ||
import {BrowserWindow, DownloadItem} from 'electron'; | ||
import electronDl, {download} from '.'; | ||
import electronDl = require('.'); | ||
import {download} from '.'; | ||
|
||
expectType<void>(electronDl()); | ||
expectType<DownloadItem>(await download(new BrowserWindow(), 'test', {errorTitle: 'Nope'})); | ||
expectType<Promise<DownloadItem>>( | ||
download(new BrowserWindow(), 'test', {errorTitle: 'Nope'}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters