@trpc/client 11.22.1 → 11.22.2-canary.14.9880a8bf9
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 +0 -8
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/links/wsLink/wsLink.d.cts +1 -1
- package/dist/links/wsLink/wsLink.d.mts +1 -1
- package/dist/wsLink-Bm5LKL6z.mjs.map +1 -1
- package/dist/{wsLink-DoiyfDgJ.d.cts → wsLink-C3hsDLsL.d.cts} +2 -3
- package/dist/wsLink-C3hsDLsL.d.cts.map +1 -0
- package/dist/{wsLink-U8pJ7LD2.d.mts → wsLink-DnvK9sTv.d.mts} +2 -3
- package/dist/wsLink-DnvK9sTv.d.mts.map +1 -0
- package/package.json +5 -15
- package/src/links/wsLink/wsClient/encoder.ts +1 -1
- package/bin/intent.js +0 -20
- package/dist/wsLink-DoiyfDgJ.d.cts.map +0 -1
- package/dist/wsLink-U8pJ7LD2.d.mts.map +0 -1
- package/skills/client-setup/SKILL.md +0 -317
- package/skills/links/SKILL.md +0 -301
- package/skills/links/references/link-options.md +0 -248
- package/skills/superjson/SKILL.md +0 -273
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
# Link Options Reference
|
|
2
|
-
|
|
3
|
-
## httpLink
|
|
4
|
-
|
|
5
|
-
Terminating link that sends one tRPC operation per HTTP request.
|
|
6
|
-
|
|
7
|
-
```ts
|
|
8
|
-
import { httpLink } from '@trpc/client';
|
|
9
|
-
|
|
10
|
-
httpLink({
|
|
11
|
-
url: 'http://localhost:3000/trpc',
|
|
12
|
-
fetch: customFetch,
|
|
13
|
-
transformer: superjson,
|
|
14
|
-
headers: { Authorization: 'Bearer token' },
|
|
15
|
-
methodOverride: 'POST',
|
|
16
|
-
});
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
| Option | Type | Default | Description |
|
|
20
|
-
| ---------------- | --------------------------------------------------------------------------------- | -------------- | --------------------------------------------- |
|
|
21
|
-
| `url` | `string \| URL` | required | Server endpoint URL |
|
|
22
|
-
| `fetch` | `typeof fetch` | global `fetch` | Fetch ponyfill |
|
|
23
|
-
| `transformer` | `DataTransformerOptions` | none | Data transformer (e.g. superjson) |
|
|
24
|
-
| `headers` | `HTTPHeaders \| (opts: { op: Operation }) => HTTPHeaders \| Promise<HTTPHeaders>` | `{}` | Static headers object or per-request callback |
|
|
25
|
-
| `methodOverride` | `'POST'` | none | Force all requests as POST |
|
|
26
|
-
|
|
27
|
-
## httpBatchLink
|
|
28
|
-
|
|
29
|
-
Terminating link that batches multiple operations into a single HTTP request.
|
|
30
|
-
|
|
31
|
-
```ts
|
|
32
|
-
import { httpBatchLink } from '@trpc/client';
|
|
33
|
-
|
|
34
|
-
httpBatchLink({
|
|
35
|
-
url: 'http://localhost:3000/trpc',
|
|
36
|
-
maxURLLength: 2083,
|
|
37
|
-
maxItems: 10,
|
|
38
|
-
headers({ opList }) {
|
|
39
|
-
return { Authorization: `Bearer ${opList[0]?.context.token}` };
|
|
40
|
-
},
|
|
41
|
-
transformer: superjson,
|
|
42
|
-
});
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
| Option | Type | Default | Description |
|
|
46
|
-
| ---------------- | --------------------------------------------------------------------------------------- | -------------- | ---------------------------------------------------- |
|
|
47
|
-
| `url` | `string \| URL` | required | Server endpoint URL |
|
|
48
|
-
| `fetch` | `typeof fetch` | global `fetch` | Fetch ponyfill |
|
|
49
|
-
| `transformer` | `DataTransformerOptions` | none | Data transformer |
|
|
50
|
-
| `headers` | `HTTPHeaders \| (opts: { opList: Operation[] }) => HTTPHeaders \| Promise<HTTPHeaders>` | `{}` | Headers callback receives `opList` (array), not `op` |
|
|
51
|
-
| `maxURLLength` | `number` | `Infinity` | Split batch if URL exceeds this length |
|
|
52
|
-
| `maxItems` | `number` | `Infinity` | Maximum operations per batch |
|
|
53
|
-
| `methodOverride` | `'POST'` | none | Force all requests as POST |
|
|
54
|
-
|
|
55
|
-
## httpBatchStreamLink
|
|
56
|
-
|
|
57
|
-
Terminating link similar to httpBatchLink but streams responses as they arrive instead of waiting for all to complete.
|
|
58
|
-
|
|
59
|
-
```ts
|
|
60
|
-
import { httpBatchStreamLink } from '@trpc/client';
|
|
61
|
-
|
|
62
|
-
httpBatchStreamLink({
|
|
63
|
-
url: 'http://localhost:3000/trpc',
|
|
64
|
-
maxURLLength: 2083,
|
|
65
|
-
maxItems: 10,
|
|
66
|
-
transformer: superjson,
|
|
67
|
-
streamHeader: 'accept',
|
|
68
|
-
});
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
| Option | Type | Default | Description |
|
|
72
|
-
| --------------------------- | --------------------------- | --------------- | ------------------------------------------------------------------------------------------------- |
|
|
73
|
-
| All `httpBatchLink` options | | | Inherits all httpBatchLink options |
|
|
74
|
-
| `streamHeader` | `'trpc-accept' \| 'accept'` | `'trpc-accept'` | Header used to signal streaming. Use `'accept'` to avoid CORS preflight on cross-origin requests. |
|
|
75
|
-
|
|
76
|
-
Sends `trpc-accept: application/jsonl` (or `Accept: application/jsonl`). Response arrives as `transfer-encoding: chunked` with `content-type: application/json` by default. Servers can set the streamed response `content-type` to `application/jsonl`, `application/x-ndjson`, or another value via `initTRPC.create({ jsonl: { contentType: ... } })`. Cannot set response headers (including cookies) after stream begins.
|
|
77
|
-
|
|
78
|
-
## splitLink
|
|
79
|
-
|
|
80
|
-
Non-terminating link that branches the link chain based on a condition.
|
|
81
|
-
|
|
82
|
-
```ts
|
|
83
|
-
import {
|
|
84
|
-
httpBatchLink,
|
|
85
|
-
httpLink,
|
|
86
|
-
httpSubscriptionLink,
|
|
87
|
-
splitLink,
|
|
88
|
-
} from '@trpc/client';
|
|
89
|
-
|
|
90
|
-
splitLink({
|
|
91
|
-
condition: (op) => op.type === 'subscription',
|
|
92
|
-
true: httpSubscriptionLink({ url }),
|
|
93
|
-
false: httpBatchLink({ url }),
|
|
94
|
-
});
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
| Option | Type | Default | Description |
|
|
98
|
-
| ----------- | ---------------------------- | -------- | ------------------------------------------------------------- |
|
|
99
|
-
| `condition` | `(op: Operation) => boolean` | required | Route predicate |
|
|
100
|
-
| `true` | `TRPCLink \| TRPCLink[]` | required | Link(s) for condition=true. Must include a terminating link. |
|
|
101
|
-
| `false` | `TRPCLink \| TRPCLink[]` | required | Link(s) for condition=false. Must include a terminating link. |
|
|
102
|
-
|
|
103
|
-
Each branch creates its own sub-chain, so both branches need a terminating link.
|
|
104
|
-
|
|
105
|
-
## loggerLink
|
|
106
|
-
|
|
107
|
-
Non-terminating link that logs operations to the console.
|
|
108
|
-
|
|
109
|
-
```ts
|
|
110
|
-
import { loggerLink } from '@trpc/client';
|
|
111
|
-
|
|
112
|
-
loggerLink({
|
|
113
|
-
enabled: (opts) =>
|
|
114
|
-
(process.env.NODE_ENV === 'development' && typeof window !== 'undefined') ||
|
|
115
|
-
(opts.direction === 'down' && opts.result instanceof Error),
|
|
116
|
-
colorMode: 'ansi',
|
|
117
|
-
});
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
| Option | Type | Default | Description |
|
|
121
|
-
| ------------- | -------------------------------------------------------------------- | ------------------------------------ | -------------------------------- |
|
|
122
|
-
| `enabled` | `(opts: { direction: 'up' \| 'down'; result?: unknown }) => boolean` | `() => true` | Control when logging is active |
|
|
123
|
-
| `logger` | `(opts: LoggerOpts) => void` | built-in pretty logger | Custom log function |
|
|
124
|
-
| `console` | `{ log: Function; error: Function }` | `globalThis.console` | Console implementation |
|
|
125
|
-
| `colorMode` | `'ansi' \| 'css' \| 'none'` | `'css'` in browser, `'ansi'` in Node | Color output mode |
|
|
126
|
-
| `withContext` | `boolean` | `false` (true if css) | Include operation context in log |
|
|
127
|
-
|
|
128
|
-
## retryLink
|
|
129
|
-
|
|
130
|
-
Non-terminating link that retries failed operations.
|
|
131
|
-
|
|
132
|
-
```ts
|
|
133
|
-
import { retryLink } from '@trpc/client';
|
|
134
|
-
|
|
135
|
-
retryLink({
|
|
136
|
-
retry(opts) {
|
|
137
|
-
if (opts.error.data?.code === 'INTERNAL_SERVER_ERROR') {
|
|
138
|
-
return opts.attempts <= 3;
|
|
139
|
-
}
|
|
140
|
-
return false;
|
|
141
|
-
},
|
|
142
|
-
retryDelayMs: (attempt) => Math.min(1000 * 2 ** attempt, 30000),
|
|
143
|
-
});
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
| Option | Type | Default | Description |
|
|
147
|
-
| -------------- | -------------------------------------------- | --------- | --------------------------- |
|
|
148
|
-
| `retry` | `(opts: { op, error, attempts }) => boolean` | required | Return true to retry |
|
|
149
|
-
| `retryDelayMs` | `(attempt: number) => number` | `() => 0` | Delay between retries in ms |
|
|
150
|
-
|
|
151
|
-
When used with subscriptions that use `tracked()`, automatically includes the last known event ID on retry.
|
|
152
|
-
|
|
153
|
-
## wsLink
|
|
154
|
-
|
|
155
|
-
Terminating link for WebSocket connections. Requires a `TRPCWebSocketClient`.
|
|
156
|
-
|
|
157
|
-
```ts
|
|
158
|
-
import { createWSClient, wsLink } from '@trpc/client';
|
|
159
|
-
|
|
160
|
-
const wsClient = createWSClient({
|
|
161
|
-
url: 'ws://localhost:3000',
|
|
162
|
-
connectionParams: () => ({ token: 'supersecret' }),
|
|
163
|
-
lazy: { enabled: true, closeMs: 10_000 },
|
|
164
|
-
keepAlive: { enabled: true, intervalMs: 5_000, pongTimeoutMs: 1_000 },
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
wsLink<AppRouter>({
|
|
168
|
-
client: wsClient,
|
|
169
|
-
transformer: superjson,
|
|
170
|
-
});
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
### wsLink Options
|
|
174
|
-
|
|
175
|
-
| Option | Type | Default | Description |
|
|
176
|
-
| ------------- | ------------------------ | -------- | ------------------------------------ |
|
|
177
|
-
| `client` | `TRPCWebSocketClient` | required | WebSocket client from createWSClient |
|
|
178
|
-
| `transformer` | `DataTransformerOptions` | none | Data transformer |
|
|
179
|
-
|
|
180
|
-
### createWSClient Options
|
|
181
|
-
|
|
182
|
-
| Option | Type | Default | Description |
|
|
183
|
-
| ------------------------- | ---------------------------------------------------------------------------------------- | ------------------- | ----------------------------------------------------------------- |
|
|
184
|
-
| `url` | `string \| (() => MaybePromise<string>)` | required | WebSocket server URL |
|
|
185
|
-
| `connectionParams` | `Record<string, string> \| null \| (() => MaybePromise<Record<string, string> \| null>)` | `null` | Auth params sent as first message, available in `createContext()` |
|
|
186
|
-
| `WebSocket` | `typeof WebSocket` | global `WebSocket` | WebSocket ponyfill |
|
|
187
|
-
| `retryDelayMs` | `(attemptIndex: number) => number` | exponential backoff | Reconnection delay |
|
|
188
|
-
| `onOpen` | `() => void` | none | Connection opened callback |
|
|
189
|
-
| `onError` | `(evt?: Event) => void` | none | Connection error callback |
|
|
190
|
-
| `onClose` | `(cause?: { code?: number }) => void` | none | Connection closed callback |
|
|
191
|
-
| `lazy.enabled` | `boolean` | `false` | Close WS after inactivity |
|
|
192
|
-
| `lazy.closeMs` | `number` | `0` | Idle timeout before closing |
|
|
193
|
-
| `keepAlive.enabled` | `boolean` | `false` | Send ping messages |
|
|
194
|
-
| `keepAlive.intervalMs` | `number` | `5000` | Ping interval |
|
|
195
|
-
| `keepAlive.pongTimeoutMs` | `number` | `1000` | Close if no pong within this time |
|
|
196
|
-
|
|
197
|
-
## httpSubscriptionLink
|
|
198
|
-
|
|
199
|
-
Terminating link for Server-Sent Events (SSE) subscriptions.
|
|
200
|
-
|
|
201
|
-
```ts
|
|
202
|
-
import { httpSubscriptionLink } from '@trpc/client';
|
|
203
|
-
import { EventSourcePolyfill } from 'event-source-polyfill';
|
|
204
|
-
|
|
205
|
-
httpSubscriptionLink({
|
|
206
|
-
url: 'http://localhost:3000/trpc',
|
|
207
|
-
connectionParams: async () => ({ token: 'supersecret' }),
|
|
208
|
-
transformer: superjson,
|
|
209
|
-
EventSource: EventSourcePolyfill,
|
|
210
|
-
eventSourceOptions: async ({ op }) => ({
|
|
211
|
-
headers: {
|
|
212
|
-
authorization: 'Bearer token',
|
|
213
|
-
},
|
|
214
|
-
}),
|
|
215
|
-
});
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
| Option | Type | Default | Description |
|
|
219
|
-
| -------------------- | ------------------------------------------------------------------------------------ | -------------------- | ----------------------------------------- |
|
|
220
|
-
| `url` | `string \| (() => string \| Promise<string>)` | required | Server endpoint URL |
|
|
221
|
-
| `connectionParams` | `Record<string, string> \| null \| (() => MaybePromise<...>)` | none | Serialized as URL query param |
|
|
222
|
-
| `transformer` | `DataTransformerOptions` | none | Data transformer |
|
|
223
|
-
| `EventSource` | EventSource constructor | global `EventSource` | EventSource ponyfill for custom headers |
|
|
224
|
-
| `eventSourceOptions` | `EventSourceInit \| ((opts: { op }) => EventSourceInit \| Promise<EventSourceInit>)` | none | Options passed to EventSource constructor |
|
|
225
|
-
|
|
226
|
-
For cross-domain cookies, use `eventSourceOptions: () => ({ withCredentials: true })`.
|
|
227
|
-
|
|
228
|
-
## unstable_localLink
|
|
229
|
-
|
|
230
|
-
Terminating link for direct procedure calls without HTTP. Useful for testing and server-side usage.
|
|
231
|
-
|
|
232
|
-
```ts
|
|
233
|
-
import { unstable_localLink } from '@trpc/client';
|
|
234
|
-
import { appRouter } from './server';
|
|
235
|
-
|
|
236
|
-
unstable_localLink({
|
|
237
|
-
router: appRouter,
|
|
238
|
-
createContext: async () => ({ db: prisma }),
|
|
239
|
-
onError: (opts) => console.error('Error:', opts.error),
|
|
240
|
-
});
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
| Option | Type | Default | Description |
|
|
244
|
-
| --------------- | ------------------------------------- | -------- | ------------------------ |
|
|
245
|
-
| `router` | `AnyRouter` | required | tRPC router instance |
|
|
246
|
-
| `createContext` | `() => Promise<Context>` | required | Context factory per call |
|
|
247
|
-
| `onError` | `(opts: ErrorHandlerOptions) => void` | none | Error handler |
|
|
248
|
-
| `transformer` | `DataTransformerOptions` | none | Data transformer |
|
|
@@ -1,273 +0,0 @@
|
|
|
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.16.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
|