@trpc/client 11.14.0 → 11.14.1-canary.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 +8 -0
- package/bin/intent.js +20 -0
- package/dist/{httpBatchLink-LhidKAPw.mjs → httpBatchLink-CaWjh1oW.mjs} +2 -2
- package/dist/{httpBatchLink-LhidKAPw.mjs.map → httpBatchLink-CaWjh1oW.mjs.map} +1 -1
- package/dist/{httpLink-lG_6juPY.mjs → httpLink-oiU8eqFi.mjs} +2 -2
- package/dist/{httpLink-lG_6juPY.mjs.map → httpLink-oiU8eqFi.mjs.map} +1 -1
- package/dist/{httpUtils-pyf5RF99.mjs → httpUtils-BNq9QC3d.mjs} +2 -2
- package/dist/{httpUtils-pyf5RF99.mjs.map → httpUtils-BNq9QC3d.mjs.map} +1 -1
- package/dist/index.mjs +3 -3
- package/dist/links/httpBatchLink.mjs +2 -2
- package/dist/links/httpLink.mjs +2 -2
- package/package.json +15 -5
- package/skills/client-setup/SKILL.md +317 -0
- package/skills/links/SKILL.md +300 -0
- package/skills/links/references/link-options.md +248 -0
- package/skills/superjson/SKILL.md +273 -0
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: superjson
|
|
3
|
+
description: >
|
|
4
|
+
Configure SuperJSON transformer on both server initTRPC.create({ transformer:
|
|
5
|
+
superjson }) and every client terminating link (httpBatchLink, httpLink, wsLink,
|
|
6
|
+
httpSubscriptionLink) to support Date, Map, Set, BigInt over the wire. Transformer
|
|
7
|
+
must match on both sides. In v11, transformer goes on individual links, not the
|
|
8
|
+
client constructor.
|
|
9
|
+
type: composition
|
|
10
|
+
library: trpc
|
|
11
|
+
library_version: '11.14.0'
|
|
12
|
+
requires:
|
|
13
|
+
- server-setup
|
|
14
|
+
- client-setup
|
|
15
|
+
sources:
|
|
16
|
+
- www/docs/server/data-transformers.md
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
# tRPC -- SuperJSON Transformer
|
|
20
|
+
|
|
21
|
+
## Setup
|
|
22
|
+
|
|
23
|
+
### 1. Install superjson
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install superjson
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### 2. Add to initTRPC on the server
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
// server/trpc.ts
|
|
33
|
+
import { initTRPC } from '@trpc/server';
|
|
34
|
+
import superjson from 'superjson';
|
|
35
|
+
|
|
36
|
+
const t = initTRPC.create({
|
|
37
|
+
transformer: superjson,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
export const router = t.router;
|
|
41
|
+
export const publicProcedure = t.procedure;
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 3. Add to every terminating link on the client
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
// client.ts
|
|
48
|
+
import { createTRPCClient, httpBatchLink } from '@trpc/client';
|
|
49
|
+
import superjson from 'superjson';
|
|
50
|
+
import type { AppRouter } from './server/trpc';
|
|
51
|
+
|
|
52
|
+
const client = createTRPCClient<AppRouter>({
|
|
53
|
+
links: [
|
|
54
|
+
httpBatchLink({
|
|
55
|
+
url: 'http://localhost:3000/trpc',
|
|
56
|
+
transformer: superjson,
|
|
57
|
+
}),
|
|
58
|
+
],
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Now Date, Map, Set, BigInt, RegExp, undefined, and other non-JSON types survive the round trip.
|
|
63
|
+
|
|
64
|
+
## Core Patterns
|
|
65
|
+
|
|
66
|
+
### SuperJSON with splitLink and Subscriptions
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import {
|
|
70
|
+
createTRPCClient,
|
|
71
|
+
httpBatchLink,
|
|
72
|
+
httpSubscriptionLink,
|
|
73
|
+
splitLink,
|
|
74
|
+
} from '@trpc/client';
|
|
75
|
+
import superjson from 'superjson';
|
|
76
|
+
import type { AppRouter } from './server/trpc';
|
|
77
|
+
|
|
78
|
+
const client = createTRPCClient<AppRouter>({
|
|
79
|
+
links: [
|
|
80
|
+
splitLink({
|
|
81
|
+
condition: (op) => op.type === 'subscription',
|
|
82
|
+
true: httpSubscriptionLink({
|
|
83
|
+
url: 'http://localhost:3000/trpc',
|
|
84
|
+
transformer: superjson,
|
|
85
|
+
}),
|
|
86
|
+
false: httpBatchLink({
|
|
87
|
+
url: 'http://localhost:3000/trpc',
|
|
88
|
+
transformer: superjson,
|
|
89
|
+
}),
|
|
90
|
+
}),
|
|
91
|
+
],
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Every terminating link in every branch must have `transformer: superjson`.
|
|
96
|
+
|
|
97
|
+
### SuperJSON with wsLink
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import { createTRPCClient, createWSClient, wsLink } from '@trpc/client';
|
|
101
|
+
import superjson from 'superjson';
|
|
102
|
+
import type { AppRouter } from './server/trpc';
|
|
103
|
+
|
|
104
|
+
const wsClient = createWSClient({
|
|
105
|
+
url: 'ws://localhost:3000',
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const client = createTRPCClient<AppRouter>({
|
|
109
|
+
links: [
|
|
110
|
+
wsLink<AppRouter>({
|
|
111
|
+
client: wsClient,
|
|
112
|
+
transformer: superjson,
|
|
113
|
+
}),
|
|
114
|
+
],
|
|
115
|
+
});
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Returning Dates from Procedures
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
// server
|
|
122
|
+
import { z } from 'zod';
|
|
123
|
+
import { publicProcedure, router } from './trpc';
|
|
124
|
+
|
|
125
|
+
const appRouter = router({
|
|
126
|
+
getEvent: publicProcedure
|
|
127
|
+
.input(z.object({ id: z.string() }))
|
|
128
|
+
.query(({ input }) => {
|
|
129
|
+
return {
|
|
130
|
+
id: input.id,
|
|
131
|
+
name: 'Launch Party',
|
|
132
|
+
date: new Date('2025-01-01T00:00:00Z'),
|
|
133
|
+
};
|
|
134
|
+
}),
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
export type AppRouter = typeof appRouter;
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
// client
|
|
142
|
+
const event = await client.getEvent.query({ id: '1' });
|
|
143
|
+
console.log(event.date instanceof Date); // true
|
|
144
|
+
console.log(event.date.getFullYear()); // 2025
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Without superjson, `event.date` would be a string like `"2025-01-01T00:00:00.000Z"`.
|
|
148
|
+
|
|
149
|
+
## Common Mistakes
|
|
150
|
+
|
|
151
|
+
### [CRITICAL] Transformer on server but missing from client link
|
|
152
|
+
|
|
153
|
+
Wrong:
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
// Server
|
|
157
|
+
const t = initTRPC.create({ transformer: superjson });
|
|
158
|
+
|
|
159
|
+
// Client
|
|
160
|
+
const client = createTRPCClient<AppRouter>({
|
|
161
|
+
links: [httpBatchLink({ url: 'http://localhost:3000/trpc' })],
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Correct:
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
// Server
|
|
169
|
+
const t = initTRPC.create({ transformer: superjson });
|
|
170
|
+
|
|
171
|
+
// Client
|
|
172
|
+
const client = createTRPCClient<AppRouter>({
|
|
173
|
+
links: [
|
|
174
|
+
httpBatchLink({
|
|
175
|
+
url: 'http://localhost:3000/trpc',
|
|
176
|
+
transformer: superjson,
|
|
177
|
+
}),
|
|
178
|
+
],
|
|
179
|
+
});
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Server encodes with superjson but client tries to parse raw JSON, causing "Unable to transform response" or garbled data.
|
|
183
|
+
|
|
184
|
+
Source: www/docs/server/data-transformers.md
|
|
185
|
+
|
|
186
|
+
### [CRITICAL] Transformer goes on individual links, not createTRPCClient
|
|
187
|
+
|
|
188
|
+
The `transformer` option is on individual terminating links:
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
createTRPCClient<AppRouter>({
|
|
192
|
+
links: [
|
|
193
|
+
httpBatchLink({
|
|
194
|
+
url: 'http://localhost:3000/trpc',
|
|
195
|
+
transformer: superjson,
|
|
196
|
+
}),
|
|
197
|
+
],
|
|
198
|
+
});
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
In v11, `transformer` was moved from the client constructor to individual links. Passing it to `createTRPCClient` throws a TypeError.
|
|
202
|
+
|
|
203
|
+
Source: packages/client/src/internals/TRPCUntypedClient.ts
|
|
204
|
+
|
|
205
|
+
### [CRITICAL] Transformer on only some terminating links in splitLink
|
|
206
|
+
|
|
207
|
+
Wrong:
|
|
208
|
+
|
|
209
|
+
```ts
|
|
210
|
+
splitLink({
|
|
211
|
+
condition: (op) => op.type === 'subscription',
|
|
212
|
+
true: httpSubscriptionLink({
|
|
213
|
+
url: 'http://localhost:3000/trpc',
|
|
214
|
+
// missing transformer!
|
|
215
|
+
}),
|
|
216
|
+
false: httpBatchLink({
|
|
217
|
+
url: 'http://localhost:3000/trpc',
|
|
218
|
+
transformer: superjson,
|
|
219
|
+
}),
|
|
220
|
+
});
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
Correct:
|
|
224
|
+
|
|
225
|
+
```ts
|
|
226
|
+
splitLink({
|
|
227
|
+
condition: (op) => op.type === 'subscription',
|
|
228
|
+
true: httpSubscriptionLink({
|
|
229
|
+
url: 'http://localhost:3000/trpc',
|
|
230
|
+
transformer: superjson,
|
|
231
|
+
}),
|
|
232
|
+
false: httpBatchLink({
|
|
233
|
+
url: 'http://localhost:3000/trpc',
|
|
234
|
+
transformer: superjson,
|
|
235
|
+
}),
|
|
236
|
+
});
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Every terminating link must have the same transformer. A missing transformer on one branch causes deserialization failures only for operations routed through that branch.
|
|
240
|
+
|
|
241
|
+
Source: www/docs/server/data-transformers.md
|
|
242
|
+
|
|
243
|
+
### [HIGH] Using transformer on client but not on server
|
|
244
|
+
|
|
245
|
+
Wrong:
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
// Server -- no transformer
|
|
249
|
+
const t = initTRPC.create();
|
|
250
|
+
|
|
251
|
+
// Client
|
|
252
|
+
httpBatchLink({ url, transformer: superjson });
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
Correct:
|
|
256
|
+
|
|
257
|
+
```ts
|
|
258
|
+
// Server
|
|
259
|
+
const t = initTRPC.create({ transformer: superjson });
|
|
260
|
+
|
|
261
|
+
// Client
|
|
262
|
+
httpBatchLink({ url, transformer: superjson });
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
The transformer must be configured on both `initTRPC.create()` and every client link. Client-only transformer corrupts the request encoding because the server expects plain JSON.
|
|
266
|
+
|
|
267
|
+
Source: www/docs/server/data-transformers.md
|
|
268
|
+
|
|
269
|
+
## See Also
|
|
270
|
+
|
|
271
|
+
- `client-setup` -- create the tRPC client and configure links
|
|
272
|
+
- `links` -- detailed options for each link type including transformer
|
|
273
|
+
- `server-setup` -- initTRPC.create() where the server transformer is configured
|