|
| 1 | +# @status-im/trpc-webext |
| 2 | + |
| 3 | +A tRPC adapter for web extensions that enables type-safe communication between different extension contexts (background, content scripts, popup, etc.). |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```sh |
| 8 | +pnpm add @status-im/trpc-webext |
| 9 | +``` |
| 10 | + |
| 11 | +## Basic Usage |
| 12 | + |
| 13 | +### 1. Create your tRPC router (typically in background script) |
| 14 | + |
| 15 | +```typescript |
| 16 | +import { initTRPC } from '@trpc/server' |
| 17 | +import { createWebExtHandler } from '@status-im/trpc-webext/adapter' |
| 18 | +import { browser } from 'webextension-polyfill' |
| 19 | +import superjson from 'superjson' |
| 20 | + |
| 21 | +// Initialize tRPC |
| 22 | +const t = initTRPC.context<Context>().create({ |
| 23 | + transformer: superjson, |
| 24 | + isServer: false, |
| 25 | + allowOutsideOfServer: true, |
| 26 | +}) |
| 27 | + |
| 28 | +// Define your router |
| 29 | +const appRouter = t.router({ |
| 30 | + greeting: t.procedure |
| 31 | + .input(z.object({ name: z.string() })) |
| 32 | + .query(({ input }) => { |
| 33 | + return =Hello ${input.name}!= |
| 34 | + }), |
| 35 | +}) |
| 36 | + |
| 37 | +// Create context function |
| 38 | +const createContext = async (opts) => { |
| 39 | + return { |
| 40 | + // Add your context data here |
| 41 | + userId: 'user Alice', |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// Set up the handler in background script |
| 46 | +createWebExtHandler({ |
| 47 | + router: appRouter, |
| 48 | + createContext, |
| 49 | + runtime: browser.runtime, |
| 50 | +}) |
| 51 | + |
| 52 | +export type AppRouter = typeof appRouter |
| 53 | +``` |
| 54 | +
|
| 55 | +### 2. Create a client (in popup, content script, etc.) |
| 56 | +
|
| 57 | +```typescript |
| 58 | +import { createTRPCClient } from '@trpc/client' |
| 59 | +import { webExtensionLink } from '@status-im/trpc-webext/link' |
| 60 | +import { browser } from 'webextension-polyfill' |
| 61 | +import superjson from 'superjson' |
| 62 | +import type { AppRouter } from './background' |
| 63 | + |
| 64 | +const client = createTRPCClient<AppRouter>({ |
| 65 | + links: [ |
| 66 | + webExtensionLink({ |
| 67 | + runtime: browser.runtime, |
| 68 | + transformer: superjson, // same transformer as the server |
| 69 | + timeoutMS: 30000, // optional, defaults to 10000ms |
| 70 | + }), |
| 71 | + ], |
| 72 | +}) |
| 73 | + |
| 74 | +// Use the client |
| 75 | +async function example() { |
| 76 | + const result = await client.greeting.query({ name: 'World' }) |
| 77 | + console.log(result) // "Hello World!" |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## Key Features |
| 82 | + |
| 83 | +- **Type Safety**: Full TypeScript support with end-to-end type safety |
| 84 | +- **Real-time Communication**: Support for subscriptions using observables |
| 85 | +- **Multiple Contexts**: Works across all web extension contexts (background, popup, content scripts, options page, etc.) |
| 86 | +- **Data Transformation**: Built-in support for data transformers like SuperJSON |
| 87 | +- **Error Handling**: Proper error propagation and handling |
| 88 | +- **Connection Management**: Automatic cleanup of connections and subscriptions |
| 89 | + |
| 90 | +## Configuration Options |
| 91 | + |
| 92 | +### `createWebExtHandler` options: |
| 93 | + |
| 94 | +- `router`: Your tRPC router |
| 95 | +- `createContext`: Function to create request context |
| 96 | +- `runtime`: Browser runtime (e.g., `browser.runtime`) |
| 97 | +- `onError`: Optional error handler |
| 98 | + |
| 99 | +### `webExtensionLink` options: |
| 100 | + |
| 101 | +- `runtime`: Browser runtime (e.g., `browser.runtime`) |
| 102 | +- `transformer`: Data transformer (e.g., SuperJSON) |
| 103 | +- `timeoutMS`: Request timeout in milliseconds (default: 10000) |
| 104 | + |
| 105 | +## Notes |
| 106 | + |
| 107 | +- The handler should be set up in your background script |
| 108 | +- Clients can be created in any extension context |
| 109 | +- Make sure to use the same transformer on both ends |
| 110 | +- Subscriptions are automatically cleaned up when ports disconnect |
0 commit comments