@demokit-ai/trpc 0.0.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +203 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# @demokit-ai/trpc
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
tRPC v11 type-safe adapter for DemoKit - mock procedures with full type inference.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @demokit-ai/trpc @demokit-ai/core @trpc/client @trpc/server
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- Type-safe fixture definitions matching your tRPC router
|
|
17
|
+
- Demo link for intercepting tRPC calls client-side
|
|
18
|
+
- React hooks for managing tRPC demo state
|
|
19
|
+
- Procedure-level fixture matching
|
|
20
|
+
- Support for queries, mutations, and subscriptions
|
|
21
|
+
- Full TypeScript support with inference
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Define Fixtures
|
|
26
|
+
|
|
27
|
+
Create type-safe fixtures for your tRPC procedures:
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { defineTRPCFixtures } from '@demokit-ai/trpc'
|
|
31
|
+
import type { AppRouter } from './router'
|
|
32
|
+
|
|
33
|
+
export const fixtures = defineTRPCFixtures<AppRouter>({
|
|
34
|
+
user: {
|
|
35
|
+
getById: (input) => ({
|
|
36
|
+
id: input.id,
|
|
37
|
+
name: 'Demo User',
|
|
38
|
+
email: 'demo@example.com',
|
|
39
|
+
}),
|
|
40
|
+
list: [
|
|
41
|
+
{ id: '1', name: 'User One' },
|
|
42
|
+
{ id: '2', name: 'User Two' },
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
post: {
|
|
46
|
+
create: (input) => ({
|
|
47
|
+
id: 'new-post',
|
|
48
|
+
title: input.title,
|
|
49
|
+
content: input.content,
|
|
50
|
+
createdAt: new Date(),
|
|
51
|
+
}),
|
|
52
|
+
},
|
|
53
|
+
})
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Client Setup with Demo Link
|
|
57
|
+
|
|
58
|
+
Add the demo link to your tRPC client:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { createTRPCClient, httpBatchLink } from '@trpc/client'
|
|
62
|
+
import { createDemoLink } from '@demokit-ai/trpc'
|
|
63
|
+
import { fixtures } from './fixtures'
|
|
64
|
+
|
|
65
|
+
const demoLink = createDemoLink({
|
|
66
|
+
fixtures,
|
|
67
|
+
isEnabled: () => localStorage.getItem('demoMode') === 'true',
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
const client = createTRPCClient<AppRouter>({
|
|
71
|
+
links: [
|
|
72
|
+
demoLink,
|
|
73
|
+
httpBatchLink({
|
|
74
|
+
url: '/api/trpc',
|
|
75
|
+
}),
|
|
76
|
+
],
|
|
77
|
+
})
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### React Integration
|
|
81
|
+
|
|
82
|
+
Use the React hooks for component-level control:
|
|
83
|
+
|
|
84
|
+
```tsx
|
|
85
|
+
import { DemoTRPCProvider, useDemoTRPC } from '@demokit-ai/trpc/react'
|
|
86
|
+
import { fixtures } from './fixtures'
|
|
87
|
+
|
|
88
|
+
function App() {
|
|
89
|
+
return (
|
|
90
|
+
<DemoTRPCProvider fixtures={fixtures}>
|
|
91
|
+
<YourApp />
|
|
92
|
+
<DemoControls />
|
|
93
|
+
</DemoTRPCProvider>
|
|
94
|
+
)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function DemoControls() {
|
|
98
|
+
const { enabled, toggle, setFixture } = useDemoTRPC()
|
|
99
|
+
|
|
100
|
+
return (
|
|
101
|
+
<div>
|
|
102
|
+
<button onClick={toggle}>
|
|
103
|
+
Demo: {enabled ? 'ON' : 'OFF'}
|
|
104
|
+
</button>
|
|
105
|
+
<button
|
|
106
|
+
onClick={() =>
|
|
107
|
+
setFixture('user.getById', () => ({
|
|
108
|
+
id: 'custom',
|
|
109
|
+
name: 'Custom User',
|
|
110
|
+
}))
|
|
111
|
+
}
|
|
112
|
+
>
|
|
113
|
+
Override User Fixture
|
|
114
|
+
</button>
|
|
115
|
+
</div>
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Dynamic Fixtures
|
|
121
|
+
|
|
122
|
+
Use functions for dynamic fixture responses:
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
const fixtures = defineTRPCFixtures<AppRouter>({
|
|
126
|
+
user: {
|
|
127
|
+
search: (input) => {
|
|
128
|
+
const users = [
|
|
129
|
+
{ id: '1', name: 'Alice' },
|
|
130
|
+
{ id: '2', name: 'Bob' },
|
|
131
|
+
{ id: '3', name: 'Charlie' },
|
|
132
|
+
]
|
|
133
|
+
return users.filter((u) =>
|
|
134
|
+
u.name.toLowerCase().includes(input.query.toLowerCase())
|
|
135
|
+
)
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
order: {
|
|
139
|
+
getByStatus: (input) => {
|
|
140
|
+
const orders = generateDemoOrders(10)
|
|
141
|
+
return orders.filter((o) => o.status === input.status)
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
})
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Procedure Path Matching
|
|
148
|
+
|
|
149
|
+
Fixtures support nested router paths:
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
const fixtures = defineTRPCFixtures<AppRouter>({
|
|
153
|
+
// Matches trpc.user.profile.get()
|
|
154
|
+
'user.profile': {
|
|
155
|
+
get: { name: 'Demo User', avatar: '/demo-avatar.png' },
|
|
156
|
+
},
|
|
157
|
+
// Matches trpc.admin.users.list()
|
|
158
|
+
'admin.users': {
|
|
159
|
+
list: [{ id: '1', role: 'admin' }],
|
|
160
|
+
},
|
|
161
|
+
})
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## API Reference
|
|
165
|
+
|
|
166
|
+
### `defineTRPCFixtures<Router>(fixtures)`
|
|
167
|
+
|
|
168
|
+
Type-safe fixture definition helper.
|
|
169
|
+
|
|
170
|
+
### `createDemoLink(options)`
|
|
171
|
+
|
|
172
|
+
Creates a tRPC link that intercepts calls when demo mode is enabled.
|
|
173
|
+
|
|
174
|
+
Options:
|
|
175
|
+
- `fixtures` - Fixture definitions
|
|
176
|
+
- `isEnabled` - Function to check if demo mode is enabled
|
|
177
|
+
- `delay` - Simulated delay in milliseconds
|
|
178
|
+
- `onIntercept` - Callback when a call is intercepted
|
|
179
|
+
|
|
180
|
+
### `DemoTRPCProvider`
|
|
181
|
+
|
|
182
|
+
Props:
|
|
183
|
+
- `fixtures` - Fixture definitions
|
|
184
|
+
- `enabled` - Initial enabled state
|
|
185
|
+
- `children` - React children
|
|
186
|
+
|
|
187
|
+
### `useDemoTRPC()`
|
|
188
|
+
|
|
189
|
+
Returns:
|
|
190
|
+
- `enabled` - Demo mode state
|
|
191
|
+
- `toggle()` - Toggle demo mode
|
|
192
|
+
- `fixtures` - Current fixtures
|
|
193
|
+
- `setFixture(path, fixture)` - Override a fixture
|
|
194
|
+
- `removeFixture(path)` - Remove a fixture override
|
|
195
|
+
- `clearFixtures()` - Clear all overrides
|
|
196
|
+
|
|
197
|
+
### `useIsDemoTRPCMode()`
|
|
198
|
+
|
|
199
|
+
Returns `true` if tRPC demo mode is enabled.
|
|
200
|
+
|
|
201
|
+
## License
|
|
202
|
+
|
|
203
|
+
MIT
|