-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.ts
47 lines (44 loc) · 1.82 KB
/
preload.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
import { contextBridge, ipcRenderer } from 'electron'
import { BitwardenItem } from '../external/bitwarden'
declare global {
interface Window {
ipc: {
loginWithApi(clientId: string, clientSecret: string, masterPassword: string): Promise<string | string>;
openSearch(sessionKey: string): void;
listItems(): Promise<Array<BitwardenItem> | string>;
writeToClipboard(text: string): void;
setLastSearch(lastSearch: string, index: number): void;
getLastSearch(): Promise<[string, number] | null>;
}
}
}
contextBridge.exposeInMainWorld("ipc", {
async loginWithApi(clientId: string, clientSecret: string, masterPassword: string): Promise<string | string> {
return invoke("loginWithApi", clientId, clientSecret, masterPassword);
},
openSearch(sessionKey: string) {
ipcRenderer.send("openSearch", sessionKey);
},
async listItems(): Promise<Array<BitwardenItem> | string> {
return invoke("listItems");
},
writeToClipboard(text: string) {
ipcRenderer.send("writeToClipboard", text);
},
setLastSearch(value: string, index: number) {
ipcRenderer.send("setLastSearch", value, index);
},
async getLastSearch(): Promise<[string, number] | null> {
return invoke("getLastSearch");
}
});
// Undo the stupid extra fluff that the IPC module adds to the error message
async function invoke(channel: string, ...args: any[]): Promise<any> {
return ipcRenderer.invoke(channel, ...args).catch((error: any) => {
const needle = `Error invoking remote method '${channel}':`;
if (error?.message?.toString?.().startsWith(needle)) {
return Promise.reject(error.message.toString().substring(needle.length).trim());
}
return Promise.reject(error);
});
}