1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-30 07:03:26 +00:00

claude: Type Safety for IPC Messages

This commit is contained in:
neuronull
2025-10-29 15:42:53 -06:00
parent 39fb94a4fe
commit e7a3bfcd3b
3 changed files with 23 additions and 13 deletions

View File

@@ -20,15 +20,15 @@ export class MainDesktopAutotypeService {
}
init() {
ipcMain.handle("autofill.initAutotype", () => {
ipcMain.handle(AUTOTYPE_IPC_CHANNELS.INIT, () => {
this.init();
});
ipcMain.handle("autofill.autotypeIsInitialized", () => {
ipcMain.handle(AUTOTYPE_IPC_CHANNELS.INITIALIZED, () => {
return this.isInitialized;
});
ipcMain.on("autofill.toggleAutotype", (_event, enable: boolean) => {
ipcMain.on(AUTOTYPE_IPC_CHANNELS.TOGGLE, (_event, enable: boolean) => {
if (enable) {
this.enableAutotype();
} else {
@@ -36,7 +36,7 @@ export class MainDesktopAutotypeService {
}
});
ipcMain.on("autofill.configureAutotype", (_event, config: AutotypeConfig) => {
ipcMain.on(AUTOTYPE_IPC_CHANNELS.CONFIGURE, (_event, config: AutotypeConfig) => {
const newKeyboardShortcut = new AutotypeKeyboardShortcut();
const newKeyboardShortcutIsValid = newKeyboardShortcut.set(config.keyboardShortcut);
@@ -48,7 +48,7 @@ export class MainDesktopAutotypeService {
this.setKeyboardShortcut(newKeyboardShortcut);
});
ipcMain.on("autofill.completeAutotypeRequest", (_event, data) => {
ipcMain.on(AUTOTYPE_IPC_CHANNELS.EXECUTE, (_event, data) => {
const { response } = data;
if (
@@ -91,7 +91,7 @@ export class MainDesktopAutotypeService {
() => {
const windowTitle = autotype.getForegroundWindowTitle();
this.windowMain.win.webContents.send("autofill.listenAutotypeRequest", {
this.windowMain.win.webContents.send(AUTOTYPE_IPC_CHANNELS.LISTEN, {
windowTitle,
});
},

View File

@@ -0,0 +1,9 @@
export const AUTOTYPE_IPC_CHANNELS = {
INIT: "autofill.initAutotype",
INITIALIZED: "autofill.autotypeIsInitialized",
TOGGLE: "autofill.toggleAutotype",
CONFIGURE: "autofill.configureAutotype",
LISTEN: "autofill.listenAutotypeRequest",
EXECUTION_ERROR: "autofill.autotypeExecutionError",
EXECUTE: "autofill.executeAutotype",
} as const;

View File

@@ -6,6 +6,7 @@ import { Command } from "../platform/main/autofill/command";
import { RunCommandParams, RunCommandResult } from "../platform/main/autofill/native-autofill.main";
import { AutotypeConfig } from "./models/autotype-configure";
import { AUTOTYPE_IPC_CHANNELS } from "./models/ipc-channels";
export default {
runCommand: <C extends Command>(params: RunCommandParams<C>): Promise<RunCommandResult<C>> =>
@@ -130,16 +131,16 @@ export default {
);
},
initAutotype: () => {
return ipcRenderer.invoke("autofill.initAutotype");
return ipcRenderer.invoke(AUTOTYPE_IPC_CHANNELS.INIT);
},
autotypeIsInitialized: () => {
return ipcRenderer.invoke("autofill.autotypeIsInitialized");
return ipcRenderer.invoke(AUTOTYPE_IPC_CHANNELS.INITIALIZED);
},
configureAutotype: (config: AutotypeConfig) => {
ipcRenderer.send("autofill.configureAutotype", config);
ipcRenderer.send(AUTOTYPE_IPC_CHANNELS.CONFIGURE, config);
},
toggleAutotype: (enable: boolean) => {
ipcRenderer.send("autofill.toggleAutotype", enable);
ipcRenderer.send(AUTOTYPE_IPC_CHANNELS.TOGGLE, enable);
},
listenAutotypeRequest: (
fn: (
@@ -151,7 +152,7 @@ export default {
) => void,
) => {
ipcRenderer.on(
"autofill.listenAutotypeRequest",
AUTOTYPE_IPC_CHANNELS.LISTEN,
(
event,
data: {
@@ -162,14 +163,14 @@ export default {
fn(windowTitle, (error, response) => {
if (error) {
ipcRenderer.send("autofill.completeError", {
ipcRenderer.send(AUTOTYPE_IPC_CHANNELS.EXECUTION_ERROR, {
windowTitle,
error: error.message,
});
return;
}
ipcRenderer.send("autofill.completeAutotypeRequest", {
ipcRenderer.send(AUTOTYPE_IPC_CHANNELS.EXECUTE, {
windowTitle,
response,
});