@@ -34,44 +34,37 @@ yarn add electron-trpc
34
34
npm install --save electron-trpc
35
35
```
36
36
37
- ## Setup
37
+ ## Basic Setup
38
38
39
- 1 . Add your tRPC router to the Electron main process using ` createIPCHandler ` :
39
+ 1 . Add your tRPC router to the Electron main process using ` createIPCHandler ` and ` getPreloadFile ` :
40
40
41
41
``` ts
42
- import { app , ipcMain } from ' electron' ;
43
- import { createIPCHandler } from ' electron-trpc' ;
44
- import { router , createContext } from ' ./api' ;
42
+ import { app } from ' electron' ;
43
+ import { createIPCHandler , getPreloadFile } from ' electron-trpc/main ' ;
44
+ import { router } from ' ./api' ;
45
45
46
46
app .on (' ready' , () => {
47
- createIPCHandler ({ ipcMain , router , createContext });
47
+ createIPCHandler ({ router });
48
48
49
- // ...
50
- });
51
- ```
52
-
53
- 2 . Expose the IPC to the render process from the [ preload file] ( https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts ) :
54
-
55
- ``` ts
56
- import { contextBridge , ipcRenderer } from ' electron' ;
57
- import { exposeElectronTRPC } from ' electron-trpc' ;
58
-
59
- process .once (' loaded' , async () => {
60
- exposeElectronTRPC ({ contextBridge , ipcRenderer });
49
+ const win = new BrowserWindow ({
50
+ webPreferences: {
51
+ preload: getPreloadFile (),
52
+ },
53
+ });
61
54
});
62
55
```
63
56
64
57
> Note: ` electron-trpc ` depends on ` contextIsolation ` being enabled, which is the default.
65
58
66
- 3 . When creating the client in the render process, use the ` ipcLink ` (instead of the HTTP or batch HTTP links):
59
+ 2 . When creating the client in the render process, use the ` ipcLink ` (instead of the HTTP or batch HTTP links):
67
60
68
61
``` ts
69
62
import * as trpc from ' @trpc/client' ;
70
- import { ipcLink } from ' electron-trpc' ;
63
+ import { ipcLink } from ' electron-trpc/renderer ' ;
71
64
72
65
export const trpcClient = trpc .createTRPCClient ({
73
66
links: [ipcLink ()],
74
67
});
75
68
```
76
69
77
- 4 . Now you can use the client in your render process as you normally would (e.g. using ` @trpc/react ` ).
70
+ 3 . Now you can use the client in your render process as you normally would (e.g. using ` @trpc/react ` ).
0 commit comments