Skip to content

Commit fc77ff8

Browse files
renovate[bot]jsonnull
authored andcommitted
Simplify API surface area.
1 parent fa9ffad commit fc77ff8

39 files changed

+407
-182
lines changed

.changeset/healthy-wolves-boil.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'electron-trpc': patch
3+
---
4+
5+
Updated API to be simpler and require fewer steps.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
node_modules
22
dist/
33
coverage/
4+
packages/electron-trpc/renderer.d.ts
5+
packages/electron-trpc/main.d.ts

README.md

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,44 +34,37 @@ yarn add electron-trpc
3434
npm install --save electron-trpc
3535
```
3636

37-
## Setup
37+
## Basic Setup
3838

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`:
4040

4141
```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';
4545

4646
app.on('ready', () => {
47-
createIPCHandler({ ipcMain, router, createContext });
47+
createIPCHandler({ router });
4848

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+
});
6154
});
6255
```
6356

6457
> Note: `electron-trpc` depends on `contextIsolation` being enabled, which is the default.
6558
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):
6760

6861
```ts
6962
import * as trpc from '@trpc/client';
70-
import { ipcLink } from 'electron-trpc';
63+
import { ipcLink } from 'electron-trpc/renderer';
7164

7265
export const trpcClient = trpc.createTRPCClient({
7366
links: [ipcLink()],
7467
});
7568
```
7669

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`).

examples/basic/.npmrc

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/basic/main/main.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import path from 'path';
2-
import { app, ipcMain, BrowserWindow } from 'electron';
3-
import { createIPCHandler } from 'electron-trpc';
2+
import { app, BrowserWindow } from 'electron';
3+
import { createIPCHandler, getPreloadFile } from 'electron-trpc/main';
44
import { router } from './api';
55

66
app.on('ready', () => {
7-
createIPCHandler({ ipcMain, router: router as any });
7+
createIPCHandler({ router });
88

99
const win = new BrowserWindow({
1010
webPreferences: {
11-
preload: path.resolve(__dirname, 'preload.js'),
11+
preload: getPreloadFile(),
1212
},
1313
});
1414

examples/basic/main/preload.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

examples/basic/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"@trpc/client": "10.0.0-rc.4",
1515
"@trpc/react-query": "10.0.0-rc.4",
1616
"@trpc/server": "10.0.0-rc.4",
17-
"electron": "^19.0.9",
17+
"electron": "^21.2.2",
1818
"electron-trpc": "0.2.0-next.5",
1919
"react": "^18.2.0",
2020
"react-dom": "^18.2.0",
@@ -25,6 +25,7 @@
2525
"@types/react": "^18.0.21",
2626
"@types/react-dom": "^18.0.6",
2727
"@vitejs/plugin-react": "^2.1.0",
28-
"vite": "^3.0.3"
28+
"vite": "^3.0.3",
29+
"vite-plugin-electron-renderer": "^0.10.2"
2930
}
3031
}

examples/basic/renderer/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState } from 'react';
22
import ReactDom from 'react-dom';
3-
import { ipcLink } from 'electron-trpc';
3+
import { ipcLink } from 'electron-trpc/renderer';
44
import { createTRPCReact } from '@trpc/react-query';
55
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
66
import type { AppRouter } from '../main/api';
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { defineConfig } from 'vite';
22
import react from '@vitejs/plugin-react';
3+
import renderer from 'vite-plugin-electron-renderer';
34

45
export default defineConfig({
56
base: './',
6-
define: {
7-
'process.env': {},
7+
build: {
8+
rollupOptions: {
9+
external: ['electron', 'path'],
10+
},
811
},
9-
plugins: [react()],
12+
plugins: [renderer(), react()],
1013
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import z from 'zod';
2+
import { initTRPC } from '@trpc/server';
3+
4+
const t = initTRPC.create({ isServer: true });
5+
6+
export const router = t.router({
7+
greeting: t.procedure.input(z.object({ name: z.string() })).query((req) => {
8+
const { input } = req;
9+
10+
return {
11+
text: `Hello ${input.name}` as const,
12+
};
13+
}),
14+
});
15+
16+
export type AppRouter = typeof router;

0 commit comments

Comments
 (0)