-
Notifications
You must be signed in to change notification settings - Fork 46
feat: add @status-im/trpc-webext #704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yqrashawn
wants to merge
8
commits into
status-im:main
Choose a base branch
from
yqrashawn:feat/trpc-webext
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
511be84
feat: add @status-im/trpc-webext
yqrashawn d925995
feat: use trpc's builtin callProcedure
yqrashawn 7432ebc
feat: refactor to use more trpc types
yqrashawn 2a12a5e
feat: add unit tests
yqrashawn 36c7abf
feat: safeDeserialize, safeSerialize
yqrashawn 07402f6
docs: readme
yqrashawn d48b7a6
docs: add changelog
yqrashawn 6db6dc3
chore: changeset
yqrashawn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@status-im/trpc-webext': patch | ||
--- | ||
|
||
First version of @status-im/trpc-webext |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
layout node | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,3 +104,5 @@ web-build/ | |
|
||
# Contentlayer | ||
.contentlayer | ||
|
||
/.lsp/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { Runtime } from 'wxt/browser' | ||
|
||
export type TRPCClientContextType = 'POPUP' | 'SIDE_PANEL' | 'PAGE' | 'TAB' | ||
|
||
export function runtimePortToClientContextType( | ||
port?: Runtime.Port, | ||
): TRPCClientContextType | undefined { | ||
const { origin } = globalThis.location | ||
if (!port) return | ||
if (port.sender?.url?.startsWith(`${origin}/sidepanel.html`)) | ||
return 'SIDE_PANEL' | ||
if (port.sender?.url?.startsWith(`${origin}/popup.html`)) return 'POPUP' | ||
if (port.sender?.url?.startsWith(`${origin}/page.html`)) return 'PAGE' | ||
return 'TAB' | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"semi": false, | ||
"singleQuote": true, | ||
"arrowParens": "avoid" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @status-im/trpc-webext |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# @status-im/trpc-webext | ||
|
||
A tRPC adapter for web extensions that enables type-safe communication between different extension contexts (background, content scripts, popup, etc.). | ||
|
||
## Installation | ||
|
||
```sh | ||
pnpm add @status-im/trpc-webext | ||
``` | ||
|
||
## Basic Usage | ||
|
||
### 1. Create your tRPC router (typically in background script) | ||
|
||
```typescript | ||
import { initTRPC } from '@trpc/server' | ||
import { createWebExtHandler } from '@status-im/trpc-webext/adapter' | ||
import { browser } from 'webextension-polyfill' | ||
import superjson from 'superjson' | ||
|
||
// Initialize tRPC | ||
const t = initTRPC.context<Context>().create({ | ||
transformer: superjson, | ||
isServer: false, | ||
allowOutsideOfServer: true, | ||
}) | ||
|
||
// Define your router | ||
const appRouter = t.router({ | ||
greeting: t.procedure | ||
.input(z.object({ name: z.string() })) | ||
.query(({ input }) => { | ||
return =Hello ${input.name}!= | ||
}), | ||
}) | ||
|
||
// Create context function | ||
const createContext = async (opts) => { | ||
return { | ||
// Add your context data here | ||
userId: 'user Alice', | ||
} | ||
} | ||
|
||
// Set up the handler in background script | ||
createWebExtHandler({ | ||
router: appRouter, | ||
createContext, | ||
runtime: browser.runtime, | ||
}) | ||
|
||
export type AppRouter = typeof appRouter | ||
``` | ||
|
||
### 2. Create a client (in popup, content script, etc.) | ||
|
||
```typescript | ||
import { createTRPCClient } from '@trpc/client' | ||
import { webExtensionLink } from '@status-im/trpc-webext/link' | ||
import { browser } from 'webextension-polyfill' | ||
import superjson from 'superjson' | ||
import type { AppRouter } from './background' | ||
|
||
const client = createTRPCClient<AppRouter>({ | ||
links: [ | ||
webExtensionLink({ | ||
runtime: browser.runtime, | ||
transformer: superjson, // same transformer as the server | ||
timeoutMS: 30000, // optional, defaults to 10000ms | ||
}), | ||
], | ||
}) | ||
|
||
// Use the client | ||
async function example() { | ||
const result = await client.greeting.query({ name: 'World' }) | ||
console.log(result) // "Hello World!" | ||
} | ||
``` | ||
|
||
## Key Features | ||
|
||
- **Type Safety**: Full TypeScript support with end-to-end type safety | ||
- **Real-time Communication**: Support for subscriptions using observables | ||
- **Multiple Contexts**: Works across all web extension contexts (background, popup, content scripts, options page, etc.) | ||
- **Data Transformation**: Built-in support for data transformers like SuperJSON | ||
- **Error Handling**: Proper error propagation and handling | ||
- **Connection Management**: Automatic cleanup of connections and subscriptions | ||
|
||
## Configuration Options | ||
|
||
### `createWebExtHandler` options: | ||
|
||
- `router`: Your tRPC router | ||
- `createContext`: Function to create request context | ||
- `runtime`: Browser runtime (e.g., `browser.runtime`) | ||
- `onError`: Optional error handler | ||
|
||
### `webExtensionLink` options: | ||
|
||
- `runtime`: Browser runtime (e.g., `browser.runtime`) | ||
- `transformer`: Data transformer (e.g., SuperJSON) | ||
- `timeoutMS`: Request timeout in milliseconds (default: 10000) | ||
|
||
## Notes | ||
|
||
- The handler should be set up in your background script | ||
- Clients can be created in any extension context | ||
- Make sure to use the same transformer on both ends | ||
- Subscriptions are automatically cleaned up when ports disconnect |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import configs from '@status-im/eslint-config' | ||
|
||
/** @type {import('eslint').Linter.Config[]} */ | ||
export default [ | ||
...configs, | ||
{ | ||
files: ['**/*.ts', '**/*.mts', '**/*.mjs', '**/*.tsx'], | ||
}, | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{ | ||
"name": "@status-im/trpc-webext", | ||
"description": "description", | ||
"version": "0.0.0", | ||
"license": "MIT", | ||
"keywords": [ | ||
"trpc", | ||
"extension", | ||
"webext", | ||
"webextension" | ||
], | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.js" | ||
}, | ||
"./adapter": { | ||
"types": "./dist/adapter/index.d.ts", | ||
"import": "./dist/adapter/index.js", | ||
"require": "./dist/adapter/index.js" | ||
}, | ||
"./link": { | ||
"types": "./dist/link/index.d.ts", | ||
"import": "./dist/link/index.js", | ||
"require": "./dist/link/index.js" | ||
} | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"preinstall": "npx only-allow pnpm", | ||
"dev": "tsc -w", | ||
"build": "tsc", | ||
"lint": "eslint src", | ||
"format": "prettier --write .", | ||
"test": "vitest run", | ||
"test:watch": "vitest --watch" | ||
}, | ||
"peerDependencies": { | ||
"@trpc/client": "^11.0.0", | ||
"@trpc/server": "^11.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/webextension-polyfill": "^0.12.3", | ||
"zod": "^3.23.8" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is for direnv