|
1 | | -# trpc-chrome |
| 1 | + |
2 | 2 |
|
3 | | -### **[Chrome Extension](https://developer.chrome.com/docs/extensions/mv3/) adapter for [tRPC](https://trpc.io/)** 🧩 |
| 3 | +<div align="center"> |
| 4 | + <h1>trpc-chrome</h1> |
| 5 | + <a href="https://www.npmjs.com/package/trpc-chrome"><img src="https://img.shields.io/npm/v/trpc-chrome.svg?style=flat&color=brightgreen" target="_blank" /></a> |
| 6 | + <a href="./LICENSE"><img src="https://img.shields.io/badge/license-MIT-black" /></a> |
| 7 | + <a href="https://trpc.io/discord" target="_blank"><img src="https://img.shields.io/badge/chat-discord-blue.svg" /></a> |
| 8 | + <br /> |
| 9 | + <hr /> |
| 10 | +</div> |
| 11 | + |
| 12 | +## **[Chrome extension](https://developer.chrome.com/docs/extensions/mv3/) support for [tRPC](https://trpc.io/)** 🧩 |
4 | 13 |
|
5 | 14 | - Easy communication for web extensions. |
6 | 15 | - Typesafe messaging between content & background scripts. |
7 | 16 | - Ready for Manifest V3. |
8 | 17 |
|
| 18 | +### Roadmap 🚘 |
| 19 | + |
| 20 | +- Reconnect on background script reload. (maybe?) |
| 21 | +- Add example with client(content) & handler(background). |
| 22 | +- Add example with vice-versa handler(content) & client(background). |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +**1. Install `trpc-chrome`.** |
| 27 | + |
| 28 | +```bash |
| 29 | +# npm |
| 30 | +npm install trpc-chrome |
| 31 | +# yarn |
| 32 | +yarn add trpc-chrome |
| 33 | +``` |
| 34 | + |
| 35 | +**2. Add a `chromeLink` to the client in your content script.** |
| 36 | + |
| 37 | +```typescript |
| 38 | +// content.ts |
| 39 | +import { createTRPCClient } from '@trpc/client'; |
| 40 | +import { chromeLink } from 'trpc-chrome'; |
| 41 | + |
| 42 | +import type { AppRouter } from './appRouter'; |
| 43 | + |
| 44 | +const port = chrome.runtime.connect(chrome.runtime.id); |
| 45 | + |
| 46 | +export const chromeClient = createTRPCClient<AppRouter>({ |
| 47 | + links: [/* 👉 */ chromeLink({ port })], |
| 48 | +}); |
| 49 | +``` |
| 50 | + |
| 51 | +**3. Add `createChromeHandler` in your background script.** |
| 52 | + |
| 53 | +```typescript |
| 54 | +// background.ts |
| 55 | +import { createChromeHandler } from 'trpc-chrome'; |
| 56 | + |
| 57 | +import { appRouter } from './appRouter'; |
| 58 | + |
| 59 | +createChromeHandler({ router: appRouter /* 👈 */ }); |
| 60 | +``` |
| 61 | + |
| 62 | +## Requirements |
| 63 | + |
| 64 | +Peer dependencies: |
| 65 | + |
| 66 | +- [`tRPC`](https://github.com/trpc/trpc) Server v10 (`@trpc/server@next`) must be installed. |
| 67 | +- [`tRPC`](https://github.com/trpc/trpc) Server v10 (`@trpc/server@next`) must be installed. |
| 68 | + |
| 69 | +## Example |
| 70 | + |
| 71 | +Please see [full example here](examples/with-plasmo). |
| 72 | + |
| 73 | +_For advanced use-cases, please find examples in our [complete test suite](test)._ |
| 74 | + |
| 75 | +## Types |
| 76 | + |
| 77 | +#### ChromeLinkOptions |
| 78 | + |
| 79 | +Please see [full typings here](src/link.ts). |
| 80 | + |
| 81 | +| Property | Type | Description | Required | |
| 82 | +| -------- | --------------------- | ---------------------------------------------------------------- | -------- | |
| 83 | +| `port` | `chrome.runtime.Port` | An open web extension port between content & background scripts. | `true` | |
| 84 | + |
| 85 | +#### CreateChromeHandlerOptions |
| 86 | + |
| 87 | +Please see [full typings here](src/handler.ts). |
| 88 | + |
| 89 | +| Property | Type | Description | Required | |
| 90 | +| --------------- | ---------- | ------------------------------------------------------ | -------- | |
| 91 | +| `router` | `Router` | Your application tRPC router. | `true` | |
| 92 | +| `createContext` | `Function` | Passes contextual (`ctx`) data to procedure resolvers. | `false` | |
| 93 | +| `onError` | `Function` | Called if error occurs inside handler. | `false` | |
| 94 | + |
| 95 | +--- |
| 96 | + |
9 | 97 | ## License |
10 | 98 |
|
11 | 99 | Distributed under the MIT License. See LICENSE for more information. |
12 | 100 |
|
13 | 101 | ## Contact |
14 | 102 |
|
15 | | -James Berry - Follow me on Twitter [@jlalmes](https://twitter.com/jlalmes) 💚 |
| 103 | +James Berry - Follow me on Twitter [@jlalmes](https://twitter.com/jlalmes) 💙 |
0 commit comments