Skip to content

Commit

Permalink
Add TypeScript definition (#12)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
hadeeb and sindresorhus committed May 30, 2019
1 parent a5983bb commit 1c0eeab
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
60 changes: 60 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {BrowserWindow} from 'electron';

declare namespace electronServe {
interface Options {
/**
The directory to serve, relative to the app root directory.
*/
directory: string;

/**
Custom scheme. For example, `foo` results in your `directory` being available at `foo://-`.
@default 'app'
*/
scheme?: string;

/**
The partition the protocol should be installed to, if you're not using Electron's default partition.
@default electron.session.defaultSession
*/
partition?:string
}

interface loadURL {
/**
Load the index file in the window.
*/
(window: BrowserWindow): Promise<void>;
}
}

/**
Static file serving for Electron apps.
@example
```
import {app, BrowserWindow} from 'electron';
import serve = require('electron-serve');
const loadURL = serve({directory: 'renderer'});
let mainWindow;
(async () => {
await app.whenReady();
mainWindow = new BrowserWindow();
await loadURL(mainWindow);
// The above is equivalent to this:
await mainWindow.loadURL('app://-');
// The `-` is just the required hostname.
})();
```
*/
declare function electronServe(options: electronServe.Options): electronServe.loadURL;

export = electronServe;
8 changes: 8 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {expectType} from 'tsd-check';
import {BrowserWindow} from 'electron';
import serve = require('.');

expectType<serve.loadURL>(serve({directory: ''}));

const loadURL = serve({directory: ''});
expectType<Promise<void>>(loadURL(new BrowserWindow()));
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
"url": "sindresorhus.com"
},
"scripts": {
"test": "xo && cd test && ava"
"test": "xo && cd test && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"electron",
Expand All @@ -38,6 +39,7 @@
"ava": "^1.4.1",
"electron": "^3.0.0",
"spectron": "^5.0.0",
"tsd": "^0.7.3",
"xo": "^0.24.0"
},
"xo": {
Expand Down

0 comments on commit 1c0eeab

Please sign in to comment.