Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support query params #37

Merged
merged 12 commits into from
Jan 22, 2024
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ declare namespace electronServe {
/**
Load the index file in the window.
*/
type loadURL = (window: BrowserWindow) => Promise<void>;
type loadURL = (window: BrowserWindow, searchParameters?: Record<string>) => Promise<void>;
mhkeller marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ module.exports = options => {
session.protocol.registerFileProtocol(options.scheme, handler);
});

return async window_ => {
await window_.loadURL(`${options.scheme}://${options.hostname}`);
return async (window_, searchParameters) => {
const qs = searchParameters ? '?' + new URLSearchParams(searchParameters).toString() : '';
await window_.loadURL(`${options.scheme}://${options.hostname}${qs}`);
};
};
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ let mainWindow;
mainWindow = new BrowserWindow();

await loadURL(mainWindow);
// Or optionally with query params
await loadURL(mainWindow, {id: 4, foo: 'bar'});

// The above is equivalent to this:
await mainWindow.loadURL('app://-');
Expand Down Expand Up @@ -79,6 +81,23 @@ Default: [`electron.session.defaultSession`](https://electronjs.org/docs/api/ses

The [partition](https://electronjs.org/docs/api/session#sessionfrompartitionpartition-options) the protocol should be installed to, if you're not using Electron's default partition.

sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
### loadUrl(window, searchParams?)
mhkeller marked this conversation as resolved.
Show resolved Hide resolved

The `serve` function returns a `loadUrl` function, which you use to serve your HTML file in that window.

##### window

*Required*\
Type: `BrowserWindow`\

The window to load the file in.

##### searchParameters

Type: `object`\

Key value pairs to set as the query parameters.

## Related

- [electron-util](https://github.com/sindresorhus/electron-util) - Useful utilities for developing Electron apps and modules
Expand Down
15 changes: 15 additions & 0 deletions test/fixture-search-params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
const {app, BrowserWindow} = require('electron');
const {join} = require('path');
const serve = require('..');

const loadUrl = serve({directory: join(__dirname, 'sub')});

let mainWindow;

(async () => {
await app.whenReady();

mainWindow = new BrowserWindow();
loadUrl(mainWindow, {id: 4, foo: 'bar'});
})();
9 changes: 9 additions & 0 deletions test/index 2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>🚀</h1>
</body>
</html>
15 changes: 15 additions & 0 deletions test/sub/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script>
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id');
const foo = urlParams.get('foo');
document.getElementById('params').innerHTML = `${id}${foo}`;
</script>
</head>
<body>
<h1 id="params"></h1>
</body>
</html>
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,15 @@ test('throws error if unresolved path has an extension other than .html', async
await t.throwsAsync(client.waitUntilTextExists('h1', '🦄', 5000));
t.pass();
});

test('serves directory index with search params', async t => {
t.context.spectron = new Application({
path: electron,
args: ['fixture-search-params.js']
});
await t.context.spectron.start();
const {client} = t.context.spectron;
await client.waitUntilWindowLoaded();
await client.waitUntilTextExists('h1', '4bar', 5000);
t.pass();
});