refactor(preload): use Proxy for auto IPC routing
Replace manual IPC method mapping with Proxy-based approach. Method names now automatically route to IPC channels, eliminating the need to update this file when adding new APIs. - Uses Proxy to intercept property access - Static properties (platform) handled separately - Supports variable argument counts via rest parameters
This commit is contained in:
@@ -2,38 +2,24 @@ import { contextBridge, ipcRenderer } from "electron";
|
|||||||
import { electronAPI } from "@electron-toolkit/preload";
|
import { electronAPI } from "@electron-toolkit/preload";
|
||||||
import type { SelfAPI } from "./index.d";
|
import type { SelfAPI } from "./index.d";
|
||||||
|
|
||||||
// Generic invoke helper - reduces boilerplate
|
// Static properties that are not IPC methods
|
||||||
const invoke = <T>(channel: string, arg?: unknown): Promise<T> =>
|
const staticProps = {
|
||||||
ipcRenderer.invoke(channel, arg);
|
|
||||||
|
|
||||||
// Custom APIs for renderer - bridges to IPC handlers
|
|
||||||
const api: SelfAPI = {
|
|
||||||
platform: process.platform,
|
platform: process.platform,
|
||||||
|
} as const;
|
||||||
|
|
||||||
// Domain management
|
// Proxy-based API that automatically routes method calls to IPC invoke
|
||||||
getDomains: () => invoke("getDomains"),
|
// Method name = IPC channel name, no manual mapping needed
|
||||||
createDomain: (params) => invoke("createDomain", params),
|
const api: SelfAPI = new Proxy(staticProps as SelfAPI, {
|
||||||
updateDomain: (params) => invoke("updateDomain", params),
|
get(_target, prop: string) {
|
||||||
deleteDomain: (id) => invoke("deleteDomain", id),
|
// Return static property if exists
|
||||||
testConnection: (id) => invoke("testConnection", id),
|
if (prop in staticProps) {
|
||||||
testDomainConnection: (params) => invoke("testDomainConnection", params),
|
return (staticProps as Record<string, unknown>)[prop];
|
||||||
|
}
|
||||||
// Browse
|
// Otherwise, auto-create IPC invoke function
|
||||||
getApps: (params) => invoke("getApps", params),
|
// prop (method name) = IPC channel name
|
||||||
getAppDetail: (params) => invoke("getAppDetail", params),
|
return (...args: unknown[]) => ipcRenderer.invoke(prop, ...args);
|
||||||
getFileContent: (params) => invoke("getFileContent", params),
|
},
|
||||||
|
});
|
||||||
// Deploy
|
|
||||||
deploy: (params) => invoke("deploy", params),
|
|
||||||
|
|
||||||
// Download
|
|
||||||
download: (params) => invoke("download", params),
|
|
||||||
|
|
||||||
// Version management
|
|
||||||
getVersions: (params) => invoke("getVersions", params),
|
|
||||||
deleteVersion: (id) => invoke("deleteVersion", id),
|
|
||||||
rollback: (params) => invoke("rollback", params),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Use `contextBridge` APIs to expose Electron APIs to
|
// Use `contextBridge` APIs to expose Electron APIs to
|
||||||
// renderer only if context isolation is enabled, otherwise
|
// renderer only if context isolation is enabled, otherwise
|
||||||
|
|||||||
Reference in New Issue
Block a user