@trpc/client 11.22.2-canary.9.85bcf328a → 11.23.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 +0 -8
- package/dist/TRPCClientError-CxFRO7Js.mjs.map +1 -1
- package/dist/{httpBatchLink-DO3WQoxu.cjs → httpBatchLink-Cmao5uUF.cjs} +1 -1
- package/dist/{httpBatchLink-BMtWxLJV.mjs → httpBatchLink-DPypDRI1.mjs} +2 -2
- package/dist/httpBatchLink-DPypDRI1.mjs.map +1 -0
- package/dist/{httpLink-Tm93WWFA.mjs → httpLink--yqaZ9lA.mjs} +2 -2
- package/dist/{httpLink-Tm93WWFA.mjs.map → httpLink--yqaZ9lA.mjs.map} +1 -1
- package/dist/{httpLink-MDJTSlpi.cjs → httpLink-B6E1HSLY.cjs} +1 -1
- package/dist/{httpUtils-CB_100ND.mjs → httpUtils-CAZsqwOQ.mjs} +3 -2
- package/dist/httpUtils-CAZsqwOQ.mjs.map +1 -0
- package/dist/{httpUtils-cggqcHz0.cjs → httpUtils-CrSF2CZF.cjs} +2 -1
- package/dist/index.cjs +13 -7
- package/dist/index.d.cts +8 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +8 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +13 -8
- package/dist/index.mjs.map +1 -1
- package/dist/links/httpBatchLink.cjs +1 -1
- package/dist/links/httpBatchLink.mjs +1 -1
- package/dist/links/httpLink.cjs +1 -1
- package/dist/links/httpLink.mjs +1 -1
- package/dist/links/wsLink/wsLink.cjs +1 -1
- package/dist/links/wsLink/wsLink.mjs +1 -1
- package/dist/types-BxbMkv6C.d.cts.map +1 -1
- package/dist/types-CAPs7vhA.d.mts.map +1 -1
- package/dist/{wsLink-Bm5LKL6z.mjs → wsLink--7trqYPG.mjs} +2 -1
- package/dist/wsLink--7trqYPG.mjs.map +1 -0
- package/dist/wsLink-C3hsDLsL.d.cts.map +1 -1
- package/dist/{wsLink-CKAsNP9P.cjs → wsLink-CQGBu_VZ.cjs} +1 -0
- package/dist/wsLink-DnvK9sTv.d.mts.map +1 -1
- package/package.json +10 -21
- package/skills/client-setup/SKILL.md +240 -207
- package/skills/links/SKILL.md +98 -224
- package/skills/links/references/client-links-httpBatchLink.md +210 -0
- package/skills/links/references/client-links-httpBatchStreamLink.md +159 -0
- package/skills/links/references/client-links-httpLink.md +63 -0
- package/skills/links/references/client-links-httpSubscriptionLink.md +404 -0
- package/skills/links/references/client-links-localLink.md +78 -0
- package/skills/links/references/client-links-loggerLink.md +66 -0
- package/skills/links/references/client-links-retryLink.md +89 -0
- package/skills/links/references/client-links-splitLink.md +103 -0
- package/skills/links/references/client-links-wsLink.md +122 -0
- package/skills/superjson/SKILL.md +132 -183
- package/src/TRPCClientError.ts +2 -2
- package/src/createTRPCClient.ts +1 -1
- package/src/getFetch.ts +0 -1
- package/src/internals/dataLoader.ts +1 -1
- package/src/links/httpBatchStreamLink.ts +1 -1
- package/src/links/internals/httpUtils.ts +6 -1
- package/src/links/localLink.ts +10 -4
- package/src/links/types.ts +2 -1
- package/src/links/wsLink/wsClient/utils.ts +1 -1
- package/src/links/wsLink/wsClient/wsClient.ts +4 -0
- package/bin/intent.js +0 -20
- package/dist/httpBatchLink-BMtWxLJV.mjs.map +0 -1
- package/dist/httpUtils-CB_100ND.mjs.map +0 -1
- package/dist/wsLink-Bm5LKL6z.mjs.map +0 -1
- package/skills/links/references/link-options.md +0 -248
package/skills/links/SKILL.md
CHANGED
|
@@ -1,301 +1,175 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: links
|
|
3
|
-
description:
|
|
4
|
-
Configure the tRPC client link chain: httpLink, httpBatchLink,
|
|
5
|
-
httpBatchStreamLink, splitLink, loggerLink, wsLink, createWSClient,
|
|
6
|
-
httpSubscriptionLink, unstable_localLink, retryLink. Choose the right
|
|
7
|
-
terminating link. Route subscriptions via splitLink. Build custom links
|
|
8
|
-
for SOA routing. Link options: url, headers, transformer, maxURLLength,
|
|
9
|
-
maxItems, connectionParams, EventSource ponyfill.
|
|
10
|
-
type: core
|
|
11
|
-
library: trpc
|
|
12
|
-
library_version: '11.16.0'
|
|
3
|
+
description: "Configure the tRPC client link chain: httpLink, httpBatchLink, httpBatchStreamLink, splitLink, loggerLink, wsLink, createWSClient, httpSubscriptionLink, unstable_localLink, retryLink. Choose the right terminating link, route subscriptions or non-batched calls with splitLink, pass per-operation context, and write custom links. Link options: url, headers, transformer, maxURLLength, maxItems, methodOverride, connectionParams, EventSource ponyfill."
|
|
13
4
|
requires:
|
|
14
5
|
- client-setup
|
|
15
6
|
sources:
|
|
16
|
-
- www/docs/client/links
|
|
17
|
-
- www/docs/client/links/
|
|
18
|
-
- www/docs/client/links/
|
|
19
|
-
- www/docs/client/links/httpBatchStreamLink.md
|
|
20
|
-
- www/docs/client/links/
|
|
21
|
-
- www/docs/client/links/wsLink.md
|
|
22
|
-
- www/docs/client/links/
|
|
23
|
-
- www/docs/client/links/
|
|
24
|
-
- www/docs/client/links/loggerLink.md
|
|
25
|
-
-
|
|
7
|
+
- www/v11/docs/client/links.md
|
|
8
|
+
- www/v11/docs/client/links/httpBatchLink.md
|
|
9
|
+
- www/v11/docs/client/links/httpLink.md
|
|
10
|
+
- www/v11/docs/client/links/httpBatchStreamLink.md
|
|
11
|
+
- www/v11/docs/client/links/httpSubscriptionLink.md
|
|
12
|
+
- www/v11/docs/client/links/wsLink.md
|
|
13
|
+
- www/v11/docs/client/links/localLink.md
|
|
14
|
+
- www/v11/docs/client/links/splitLink.md
|
|
15
|
+
- www/v11/docs/client/links/loggerLink.md
|
|
16
|
+
- www/v11/docs/client/links/retryLink.md
|
|
17
|
+
metadata:
|
|
18
|
+
type: core
|
|
19
|
+
library: trpc
|
|
20
|
+
library_version: 11.23.0
|
|
26
21
|
---
|
|
27
22
|
|
|
28
|
-
|
|
23
|
+
<!-- Generated from www/v11/docs/client/links.md by www/build/skills.ts. Edit the docs instead. -->
|
|
29
24
|
|
|
30
|
-
|
|
25
|
+
# Links Overview
|
|
31
26
|
|
|
32
|
-
|
|
33
|
-
import { createTRPCClient, httpBatchLink, loggerLink } from '@trpc/client';
|
|
34
|
-
import type { AppRouter } from './server';
|
|
27
|
+
From https://trpc.io/docs/client/links
|
|
35
28
|
|
|
36
|
-
|
|
37
|
-
links: [
|
|
38
|
-
loggerLink(),
|
|
39
|
-
httpBatchLink({
|
|
40
|
-
url: 'http://localhost:3000/trpc',
|
|
41
|
-
}),
|
|
42
|
-
],
|
|
43
|
-
});
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
The `links` array is a chain: non-terminating links (loggerLink, splitLink, retryLink) forward operations; the chain must end with a terminating link (httpBatchLink, httpLink, httpBatchStreamLink, wsLink, httpSubscriptionLink, unstable_localLink).
|
|
29
|
+
Links enable you to customize the flow of data between the tRPC Client and Server. A link should do only one thing, which can be either a self-contained modification to a tRPC operation (query, mutation, or subscription) or a side-effect based on the operation (such as logging).
|
|
47
30
|
|
|
48
|
-
|
|
31
|
+
You can compose links together into an array that you can provide to the tRPC client configuration via the `links` property, which represents a link chain. This means that the tRPC client will execute the links in the order they are added to the `links` array when doing a request and will execute them again in reverse when it's handling a response. Here's a visual representation of the link chain:
|
|
49
32
|
|
|
50
|
-
|
|
33
|
+
<div align="center" style="margin-bottom: 12px">
|
|
34
|
+
<img src="/v11/img/links-diagram.svg" style="background: white" alt="tRPC Link Diagram"/>
|
|
35
|
+
<small>tRPC Link Diagram. Based on <a href="https://www.apollographql.com/docs/react/api/link/introduction/" target="_blank">Apollo's</a>.</small>
|
|
36
|
+
</div>
|
|
51
37
|
|
|
52
|
-
```ts
|
|
53
|
-
import { createTRPCClient, httpBatchLink } from '@trpc/client';
|
|
38
|
+
```ts no-check title="utils/trpc.ts"
|
|
39
|
+
import { createTRPCClient, httpBatchLink, loggerLink } from '@trpc/client';
|
|
54
40
|
import type { AppRouter } from './server';
|
|
55
41
|
|
|
56
|
-
const
|
|
42
|
+
export const trpc = createTRPCClient<AppRouter>({
|
|
57
43
|
links: [
|
|
44
|
+
loggerLink(),
|
|
58
45
|
httpBatchLink({
|
|
59
|
-
url: 'http://localhost:3000
|
|
60
|
-
maxURLLength: 2083,
|
|
61
|
-
maxItems: 10,
|
|
46
|
+
url: 'http://localhost:3000',
|
|
62
47
|
}),
|
|
63
48
|
],
|
|
64
49
|
});
|
|
65
|
-
|
|
66
|
-
const [post1, post2, post3] = await Promise.all([
|
|
67
|
-
client.post.byId.query(1),
|
|
68
|
-
client.post.byId.query(2),
|
|
69
|
-
client.post.byId.query(3),
|
|
70
|
-
]);
|
|
71
50
|
```
|
|
72
51
|
|
|
73
|
-
|
|
52
|
+
## Creating a custom link
|
|
74
53
|
|
|
75
|
-
|
|
54
|
+
A link is a function that follows the `TRPCLink` type. Each link is composed of three parts:
|
|
76
55
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
httpBatchLink,
|
|
81
|
-
httpSubscriptionLink,
|
|
82
|
-
splitLink,
|
|
83
|
-
} from '@trpc/client';
|
|
84
|
-
import type { AppRouter } from './server';
|
|
56
|
+
1. The link returns a function that has no parameters. This is the setup phase where the link is initialized — it happens once per app and is useful for storing caches or other state.
|
|
57
|
+
2. The function in step 1 returns another function that receives an object with two properties: `op` which is the `Operation` that is being executed by the client, and `next` which is the function we use to call the next link down the chain.
|
|
58
|
+
3. The function in step 2 returns a final function that returns the `observable` function provided by `@trpc/server`. The `observable` accepts a function that receives an `observer` which helps our link notify the next link up the chain how they should handle the operation result. In this function, we can just return `next(op)` and leave it as is, or we can subscribe to `next`, which enables our link to handle the operation result.
|
|
85
59
|
|
|
86
|
-
|
|
87
|
-
links: [
|
|
88
|
-
splitLink({
|
|
89
|
-
condition: (op) => op.type === 'subscription',
|
|
90
|
-
true: httpSubscriptionLink({
|
|
91
|
-
url: 'http://localhost:3000/trpc',
|
|
92
|
-
}),
|
|
93
|
-
false: httpBatchLink({
|
|
94
|
-
url: 'http://localhost:3000/trpc',
|
|
95
|
-
}),
|
|
96
|
-
}),
|
|
97
|
-
],
|
|
98
|
-
});
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
### splitLink -- Disable Batching Per-Request via Context
|
|
102
|
-
|
|
103
|
-
```ts
|
|
104
|
-
import {
|
|
105
|
-
createTRPCClient,
|
|
106
|
-
httpBatchLink,
|
|
107
|
-
httpLink,
|
|
108
|
-
splitLink,
|
|
109
|
-
} from '@trpc/client';
|
|
110
|
-
import type { AppRouter } from './server';
|
|
111
|
-
|
|
112
|
-
const client = createTRPCClient<AppRouter>({
|
|
113
|
-
links: [
|
|
114
|
-
splitLink({
|
|
115
|
-
condition: (op) => Boolean(op.context.skipBatch),
|
|
116
|
-
true: httpLink({ url: 'http://localhost:3000/trpc' }),
|
|
117
|
-
false: httpBatchLink({ url: 'http://localhost:3000/trpc' }),
|
|
118
|
-
}),
|
|
119
|
-
],
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
const result = await client.post.byId.query(1, {
|
|
123
|
-
context: { skipBatch: true },
|
|
124
|
-
});
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
### httpBatchStreamLink -- Stream Responses as They Arrive
|
|
128
|
-
|
|
129
|
-
```ts
|
|
130
|
-
import { createTRPCClient, httpBatchStreamLink } from '@trpc/client';
|
|
131
|
-
import type { AppRouter } from './server';
|
|
60
|
+
### Example
|
|
132
61
|
|
|
133
|
-
|
|
134
|
-
links: [
|
|
135
|
-
httpBatchStreamLink({
|
|
136
|
-
url: 'http://localhost:3000/trpc',
|
|
137
|
-
}),
|
|
138
|
-
],
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
const iterable = await client.examples.iterable.query();
|
|
142
|
-
for await (const value of iterable) {
|
|
143
|
-
console.log(value);
|
|
144
|
-
}
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
### wsLink -- WebSocket Terminating Link
|
|
148
|
-
|
|
149
|
-
```ts
|
|
150
|
-
import { createTRPCClient, createWSClient, wsLink } from '@trpc/client';
|
|
151
|
-
import type { AppRouter } from './server';
|
|
152
|
-
|
|
153
|
-
const wsClient = createWSClient({
|
|
154
|
-
url: 'ws://localhost:3000',
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
const client = createTRPCClient<AppRouter>({
|
|
158
|
-
links: [wsLink<AppRouter>({ client: wsClient })],
|
|
159
|
-
});
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
### Custom Link
|
|
163
|
-
|
|
164
|
-
```ts
|
|
62
|
+
```tsx no-check title="utils/customLink.ts"
|
|
165
63
|
import { TRPCLink } from '@trpc/client';
|
|
166
64
|
import { observable } from '@trpc/server/observable';
|
|
167
65
|
import type { AppRouter } from './server';
|
|
168
66
|
|
|
169
|
-
export const
|
|
67
|
+
export const customLink: TRPCLink<AppRouter> = () => {
|
|
68
|
+
// here we just got initialized in the app - this happens once per app
|
|
69
|
+
// useful for storing cache for instance
|
|
170
70
|
return ({ next, op }) => {
|
|
71
|
+
// this is when passing the result to the next link
|
|
72
|
+
|
|
73
|
+
// each link needs to return an observable which propagates results
|
|
171
74
|
return observable((observer) => {
|
|
172
|
-
|
|
75
|
+
console.log('performing operation:', op);
|
|
173
76
|
const unsubscribe = next(op).subscribe({
|
|
174
77
|
next(value) {
|
|
78
|
+
console.log('we received value', value);
|
|
175
79
|
observer.next(value);
|
|
176
80
|
},
|
|
177
81
|
error(err) {
|
|
178
|
-
console.
|
|
82
|
+
console.log('we received error', err);
|
|
179
83
|
observer.error(err);
|
|
180
84
|
},
|
|
181
85
|
complete() {
|
|
182
|
-
console.log(`${op.path} completed in ${Date.now() - start}ms`);
|
|
183
86
|
observer.complete();
|
|
184
87
|
},
|
|
185
88
|
});
|
|
89
|
+
|
|
186
90
|
return unsubscribe;
|
|
187
91
|
});
|
|
188
92
|
};
|
|
189
93
|
};
|
|
190
94
|
```
|
|
191
95
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
### [CRITICAL] No terminating link in the chain
|
|
195
|
-
|
|
196
|
-
Wrong:
|
|
96
|
+
### References
|
|
197
97
|
|
|
198
|
-
|
|
199
|
-
const client = createTRPCClient<AppRouter>({
|
|
200
|
-
links: [loggerLink()],
|
|
201
|
-
});
|
|
202
|
-
```
|
|
203
|
-
|
|
204
|
-
Correct:
|
|
98
|
+
If you need a more real reference for creating your custom link, you can check out some of the built-in links tRPC provides on [GitHub](https://github.com/trpc/trpc/tree/main/packages/client/src/links).
|
|
205
99
|
|
|
206
|
-
|
|
207
|
-
const client = createTRPCClient<AppRouter>({
|
|
208
|
-
links: [loggerLink(), httpBatchLink({ url: 'http://localhost:3000/trpc' })],
|
|
209
|
-
});
|
|
210
|
-
```
|
|
100
|
+
## The terminating link
|
|
211
101
|
|
|
212
|
-
The link
|
|
102
|
+
The **terminating link** is the last link in a link chain. Instead of calling the `next` function, the terminating link is responsible for sending your composed tRPC operation to the tRPC server and returning an `OperationResultEnvelope`.
|
|
213
103
|
|
|
214
|
-
|
|
104
|
+
The `links` array that you add to the tRPC client config should have at least one link, and that link should be a terminating link. If `links` don't have a terminating link at the end of them, the tRPC operation will not be sent to the tRPC server.
|
|
215
105
|
|
|
216
|
-
|
|
106
|
+
[`httpBatchLink`](https://trpc.io/docs/client/links/httpBatchLink) is the recommended terminating link by tRPC. Pick another one depending on your needs:
|
|
217
107
|
|
|
218
|
-
|
|
108
|
+
| Link | Use it for |
|
|
109
|
+
| --- | --- |
|
|
110
|
+
| [`httpBatchLink`](https://trpc.io/docs/client/links/httpBatchLink) | Queries and mutations. Calls made together, for example in a `Promise.all`, are sent as one HTTP request. |
|
|
111
|
+
| [`httpLink`](https://trpc.io/docs/client/links/httpLink) | One HTTP request per call, for example to [skip batching](https://trpc.io/docs/client/links/splitLink#disable-batching-for-certain-requests) or to send [`FormData` and binary inputs](https://trpc.io/docs/server/non-json-content-types). |
|
|
112
|
+
| [`httpBatchStreamLink`](https://trpc.io/docs/client/links/httpBatchStreamLink) | Batching, but each response is streamed as soon as it's ready. Required for procedures that return async generators or promises nested in the output. |
|
|
113
|
+
| [`httpSubscriptionLink`](https://trpc.io/docs/client/links/httpSubscriptionLink) | Subscriptions over Server-sent Events. |
|
|
114
|
+
| [`wsLink`](https://trpc.io/docs/client/links/wsLink) | Any operation over a WebSocket. |
|
|
115
|
+
| [`unstable_localLink`](https://trpc.io/docs/client/links/localLink) | Calling a router in the same process, without HTTP. |
|
|
219
116
|
|
|
220
|
-
|
|
221
|
-
const client = createTRPCClient<AppRouter>({
|
|
222
|
-
links: [httpBatchLink({ url: 'http://localhost:3000/trpc' })],
|
|
223
|
-
});
|
|
224
|
-
await client.onMessage.subscribe({});
|
|
225
|
-
```
|
|
117
|
+
The HTTP links don't handle subscriptions, so to use subscriptions alongside them, route by operation type with a [`splitLink`](https://trpc.io/docs/client/links/splitLink):
|
|
226
118
|
|
|
227
|
-
|
|
119
|
+
```ts no-check title="utils/trpc.ts"
|
|
120
|
+
import {
|
|
121
|
+
createTRPCClient,
|
|
122
|
+
httpBatchLink,
|
|
123
|
+
httpSubscriptionLink,
|
|
124
|
+
loggerLink,
|
|
125
|
+
splitLink,
|
|
126
|
+
} from '@trpc/client';
|
|
127
|
+
import type { AppRouter } from './server';
|
|
228
128
|
|
|
229
|
-
|
|
230
|
-
const client = createTRPCClient<AppRouter>({
|
|
129
|
+
export const trpc = createTRPCClient<AppRouter>({
|
|
231
130
|
links: [
|
|
131
|
+
loggerLink(),
|
|
232
132
|
splitLink({
|
|
233
133
|
condition: (op) => op.type === 'subscription',
|
|
234
|
-
true: httpSubscriptionLink({ url: 'http://localhost:3000
|
|
235
|
-
false: httpBatchLink({ url: 'http://localhost:3000
|
|
134
|
+
true: httpSubscriptionLink({ url: 'http://localhost:3000' }),
|
|
135
|
+
false: httpBatchLink({ url: 'http://localhost:3000' }),
|
|
236
136
|
}),
|
|
237
137
|
],
|
|
238
138
|
});
|
|
239
139
|
```
|
|
240
140
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
Source: packages/client/src/links/httpLink.ts
|
|
244
|
-
|
|
245
|
-
### [HIGH] httpBatchLink and httpBatchStreamLink headers callback receives { opList }
|
|
246
|
-
|
|
247
|
-
`httpBatchLink` and `httpBatchStreamLink` headers callbacks receive `{ opList }` (a `NonEmptyArray<Operation>`), not `{ op }` like `httpLink`. Access per-operation context via `opList[0]?.context`:
|
|
248
|
-
|
|
249
|
-
```ts
|
|
250
|
-
httpBatchLink({
|
|
251
|
-
url: 'http://localhost:3000/trpc',
|
|
252
|
-
headers({ opList }) {
|
|
253
|
-
return { authorization: opList[0]?.context.token };
|
|
254
|
-
},
|
|
255
|
-
});
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
`httpBatchLink` headers callback receives `{ opList }` (an array of operations)
|
|
259
|
-
|
|
260
|
-
Source: packages/client/src/links/httpBatchLink.ts
|
|
261
|
-
|
|
262
|
-
### [MEDIUM] Default batch limits are Infinity
|
|
263
|
-
|
|
264
|
-
Wrong:
|
|
265
|
-
|
|
266
|
-
```ts
|
|
267
|
-
httpBatchLink({ url: 'http://localhost:3000/trpc' });
|
|
268
|
-
```
|
|
269
|
-
|
|
270
|
-
Correct:
|
|
141
|
+
## Managing context
|
|
271
142
|
|
|
272
|
-
|
|
273
|
-
httpBatchLink({
|
|
274
|
-
url: 'http://localhost:3000/trpc',
|
|
275
|
-
maxURLLength: 2083,
|
|
276
|
-
// should be the same or lower than the server's maxBatchSize
|
|
277
|
-
maxItems: 10,
|
|
278
|
-
});
|
|
279
|
-
```
|
|
143
|
+
As an operation moves along your link chain, it maintains a context that each link can read and modify. This allows links to pass metadata along the chain that other links use in their execution logic.
|
|
280
144
|
|
|
281
|
-
|
|
145
|
+
Obtain the current context object and modify it by accessing `op.context`.
|
|
282
146
|
|
|
283
|
-
|
|
147
|
+
You can set the context object's initial value for a particular operation by providing the context parameter to the `query` or `useQuery` hook (or `mutation`, `subscription`, etc.).
|
|
284
148
|
|
|
285
|
-
|
|
149
|
+
For an example use case, see [Disable batching for certain requests](https://trpc.io/docs/client/links/splitLink#disable-batching-for-certain-requests).
|
|
286
150
|
|
|
287
|
-
|
|
151
|
+
## Common mistakes
|
|
288
152
|
|
|
289
|
-
|
|
153
|
+
- **No terminating link at the end of the chain.** Calls fail with `No more links to execute - did you forget to add an ending link?`. Links listed after the terminating link never run, so put `loggerLink`, `retryLink` and your own links before it.
|
|
154
|
+
- **A `splitLink` branch without a terminating link.** Each branch is its own chain, so each one needs to end in a terminating link.
|
|
155
|
+
- **Sending subscriptions through `httpLink`, `httpBatchLink` or `httpBatchStreamLink`.** They throw on subscriptions. Route them to `httpSubscriptionLink` or `wsLink` with a `splitLink`, as shown in [The terminating link](https://trpc.io/docs/client/links#the-terminating-link).
|
|
156
|
+
- **Returning an async generator from a query through `httpLink` or `httpBatchLink`.** The server responds with `Cannot use stream-like response in non-streaming request - use httpBatchStreamLink`.
|
|
157
|
+
- **Reading `op` in a batch link's `headers` callback.** `httpBatchLink` and `httpBatchStreamLink` call `headers({ opList })` once per batch, with all its operations. Only `httpLink` gets a single `{ op }`.
|
|
158
|
+
- **Unbounded batches.** `maxURLLength` and `maxItems` default to `Infinity`. If long URLs hit limits in your server or CDN, set `maxURLLength`. If the server sets `maxBatchSize`, set `maxItems` to the same or a lower value, or larger batches are rejected with `400 Bad Request`. See [`httpBatchLink`](https://trpc.io/docs/client/links/httpBatchLink#setting-a-maximum-url-length).
|
|
159
|
+
- **Forgetting the transformer on one link.** If your server uses a [data transformer](https://trpc.io/docs/server/data-transformers), every terminating link needs the same `transformer`, in every `splitLink` branch.
|
|
290
160
|
|
|
291
161
|
## References
|
|
292
162
|
|
|
293
|
-
- [
|
|
163
|
+
- [HTTP Batch Link](references/client-links-httpBatchLink.md): A terminating link that batches concurrent tRPC calls into a single HTTP request
|
|
164
|
+
- [HTTP Link](references/client-links-httpLink.md): A terminating link that sends each tRPC call as its own HTTP request
|
|
165
|
+
- [HTTP Batch Stream Link](references/client-links-httpBatchStreamLink.md): A terminating link that batches tRPC calls and streams each response as soon as it is ready
|
|
166
|
+
- [HTTP Subscription Link](references/client-links-httpSubscriptionLink.md): A terminating link for subscriptions over Server-sent Events
|
|
167
|
+
- [WebSocket Link](references/client-links-wsLink.md): A terminating link that sends tRPC operations over a WebSocket
|
|
168
|
+
- [Local Link](references/client-links-localLink.md): A link for direct procedure calls without HTTP overhead
|
|
169
|
+
- [Split Link](references/client-links-splitLink.md): A link that routes each operation down one of two link chains based on a condition
|
|
170
|
+
- [Logger Link](references/client-links-loggerLink.md): A link that logs tRPC operations and their results
|
|
171
|
+
- [Retry Link](references/client-links-retryLink.md): A link that retries failed operations
|
|
294
172
|
|
|
295
|
-
##
|
|
173
|
+
## Related skills
|
|
296
174
|
|
|
297
|
-
- `client-setup`
|
|
298
|
-
- `superjson` -- add transformer to links for Date/Map/Set support
|
|
299
|
-
- `subscriptions` -- set up SSE or WebSocket real-time streams
|
|
300
|
-
- `non-json-content-types` -- route FormData/binary through splitLink + httpLink
|
|
301
|
-
- `service-oriented-architecture` -- build custom routing links for multi-service backends
|
|
175
|
+
- `client-setup` (https://trpc.io/docs/client/vanilla/setup): `npx @tanstack/intent@latest load @trpc/client#client-setup`
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# HTTP Batch Link
|
|
2
|
+
|
|
3
|
+
> A terminating link that batches concurrent tRPC calls into a single HTTP request
|
|
4
|
+
|
|
5
|
+
From https://trpc.io/docs/client/links/httpBatchLink
|
|
6
|
+
|
|
7
|
+
`httpBatchLink` is a [**terminating link**](https://trpc.io/docs/client/links#the-terminating-link) that batches an array of individual tRPC operations into a single HTTP request that's sent to a single tRPC procedure.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
You can import and add the `httpBatchLink` to the `links` array as such:
|
|
12
|
+
|
|
13
|
+
```ts no-check title="client/index.ts"
|
|
14
|
+
import { createTRPCClient, httpBatchLink } from '@trpc/client';
|
|
15
|
+
import type { AppRouter } from './server';
|
|
16
|
+
|
|
17
|
+
const client = createTRPCClient<AppRouter>({
|
|
18
|
+
links: [
|
|
19
|
+
httpBatchLink({
|
|
20
|
+
url: 'http://localhost:3000',
|
|
21
|
+
// transformer,
|
|
22
|
+
}),
|
|
23
|
+
],
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
After that, you can make use of batching by setting all your procedures in a `Promise.all`. The code below will produce exactly **one** HTTP request and on the server exactly **one** database query:
|
|
28
|
+
|
|
29
|
+
```ts no-check
|
|
30
|
+
const somePosts = await Promise.all([
|
|
31
|
+
trpc.post.byId.query(1),
|
|
32
|
+
trpc.post.byId.query(2),
|
|
33
|
+
trpc.post.byId.query(3),
|
|
34
|
+
]);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## `httpBatchLink` Options
|
|
38
|
+
|
|
39
|
+
The `httpBatchLink` function takes an options object that has the `HTTPBatchLinkOptions` shape.
|
|
40
|
+
|
|
41
|
+
```ts no-check
|
|
42
|
+
export interface HTTPBatchLinkOptions extends HTTPLinkOptions {
|
|
43
|
+
/**
|
|
44
|
+
* Maximum length of HTTP URL allowed before operations are split into multiple requests
|
|
45
|
+
* @default Infinity
|
|
46
|
+
*/
|
|
47
|
+
maxURLLength?: number;
|
|
48
|
+
/**
|
|
49
|
+
* Maximum number of operations allowed in a single batch request
|
|
50
|
+
* @default Infinity
|
|
51
|
+
*/
|
|
52
|
+
maxItems?: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface HTTPLinkOptions {
|
|
56
|
+
url: string | URL;
|
|
57
|
+
/**
|
|
58
|
+
* Add ponyfill for fetch
|
|
59
|
+
*/
|
|
60
|
+
fetch?: typeof fetch;
|
|
61
|
+
/**
|
|
62
|
+
* Data transformer
|
|
63
|
+
* @see https://trpc.io/docs/server/data-transformers
|
|
64
|
+
**/
|
|
65
|
+
transformer?: DataTransformerOptions;
|
|
66
|
+
/**
|
|
67
|
+
* Headers to be set on outgoing requests or a callback that of said headers
|
|
68
|
+
* @see https://trpc.io/docs/client/headers
|
|
69
|
+
*/
|
|
70
|
+
headers?:
|
|
71
|
+
| HTTPHeaders
|
|
72
|
+
| ((opts: { opList: NonEmptyArray<Operation> }) => HTTPHeaders | Promise<HTTPHeaders>);
|
|
73
|
+
/**
|
|
74
|
+
* Send all requests as POST requests regardless of the procedure type
|
|
75
|
+
* The server must separately allow overriding the method. See:
|
|
76
|
+
* @see https://trpc.io/docs/rpc
|
|
77
|
+
*/
|
|
78
|
+
methodOverride?: 'POST';
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Setting a maximum URL length
|
|
83
|
+
|
|
84
|
+
When sending batch requests, sometimes the URL can become too large causing HTTP errors like [`413 Payload Too Large`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/413), [`414 URI Too Long`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/414), and [`404 Not Found`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404). The `maxURLLength` option will limit the number of requests that can be sent together in a batch.
|
|
85
|
+
|
|
86
|
+
```ts no-check title="client/index.ts"
|
|
87
|
+
import { createTRPCClient, httpBatchLink } from '@trpc/client';
|
|
88
|
+
import type { AppRouter } from './server';
|
|
89
|
+
|
|
90
|
+
const client = createTRPCClient<AppRouter>({
|
|
91
|
+
links: [
|
|
92
|
+
httpBatchLink({
|
|
93
|
+
url: 'http://localhost:3000',
|
|
94
|
+
maxURLLength: 2083, // a suitable size
|
|
95
|
+
// alternatively, you can make all RPC-calls to be called with POST (also requires `allowMethodOverride: true` on the server)
|
|
96
|
+
// methodOverride: 'POST',
|
|
97
|
+
}),
|
|
98
|
+
],
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Limiting batch size
|
|
103
|
+
|
|
104
|
+
### 1. Set a maximum batch size on your server:
|
|
105
|
+
|
|
106
|
+
`maxBatchSize` limits how many operations may be sent in a single batch request. Requests exceeding this limit will be rejected with a `400 Bad Request` error. This can be passed to any tRPC adapter.
|
|
107
|
+
|
|
108
|
+
```ts no-check title="server.ts"
|
|
109
|
+
import { createHTTPServer } from '@trpc/server/adapters/standalone';
|
|
110
|
+
import { appRouter } from './router';
|
|
111
|
+
|
|
112
|
+
createHTTPServer({
|
|
113
|
+
router: appRouter,
|
|
114
|
+
maxBatchSize: 10,
|
|
115
|
+
});
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
or for example if using Next.js:
|
|
119
|
+
|
|
120
|
+
```ts no-check title="pages/api/trpc/[trpc].ts"
|
|
121
|
+
import { createNextApiHandler } from '@trpc/server/adapters/next';
|
|
122
|
+
import { appRouter } from '../../../router';
|
|
123
|
+
|
|
124
|
+
export default createNextApiHandler({
|
|
125
|
+
router: appRouter,
|
|
126
|
+
maxBatchSize: 10,
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### 2. Set a matching limit on the client:
|
|
131
|
+
|
|
132
|
+
Use the `maxItems` option on your batch link to ensure the client doesn't exceed the server's limit. This splits large batches into multiple HTTP requests automatically
|
|
133
|
+
|
|
134
|
+
```ts title="client/index.ts"
|
|
135
|
+
import { createTRPCClient, httpBatchLink } from '@trpc/client';
|
|
136
|
+
import type { AppRouter } from '../server';
|
|
137
|
+
|
|
138
|
+
const client = createTRPCClient<AppRouter>({
|
|
139
|
+
links: [
|
|
140
|
+
httpBatchLink({
|
|
141
|
+
url: 'http://localhost:3000',
|
|
142
|
+
// 👇 should be the same or lower than the server's maxBatchSize
|
|
143
|
+
maxItems: 10,
|
|
144
|
+
}),
|
|
145
|
+
],
|
|
146
|
+
});
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Disabling request batching
|
|
150
|
+
|
|
151
|
+
### 1. Disable `batching` on your server:
|
|
152
|
+
|
|
153
|
+
```ts no-check title="server.ts"
|
|
154
|
+
import { createHTTPServer } from '@trpc/server/adapters/standalone';
|
|
155
|
+
import { appRouter } from './router';
|
|
156
|
+
|
|
157
|
+
createHTTPServer({
|
|
158
|
+
router: appRouter,
|
|
159
|
+
// 👇 disable batching
|
|
160
|
+
allowBatching: false,
|
|
161
|
+
});
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
or, if you're using Next.js:
|
|
165
|
+
|
|
166
|
+
```ts no-check title="pages/api/trpc/[trpc].ts"
|
|
167
|
+
import { createNextApiHandler } from '@trpc/server/adapters/next';
|
|
168
|
+
import { appRouter } from '../../../router';
|
|
169
|
+
|
|
170
|
+
export default createNextApiHandler({
|
|
171
|
+
router: appRouter,
|
|
172
|
+
// 👇 disable batching
|
|
173
|
+
allowBatching: false,
|
|
174
|
+
});
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### 2. Replace `httpBatchLink` with [`httpLink`](https://trpc.io/docs/client/links/httpLink) in your tRPC Client
|
|
178
|
+
|
|
179
|
+
```ts no-check title="client/index.ts"
|
|
180
|
+
import { createTRPCClient, httpLink } from '@trpc/client';
|
|
181
|
+
import type { AppRouter } from './server';
|
|
182
|
+
|
|
183
|
+
const client = createTRPCClient<AppRouter>({
|
|
184
|
+
links: [
|
|
185
|
+
httpLink({
|
|
186
|
+
url: 'http://localhost:3000',
|
|
187
|
+
}),
|
|
188
|
+
],
|
|
189
|
+
});
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
or, if you're using Next.js:
|
|
193
|
+
|
|
194
|
+
```tsx no-check title="utils/trpc.ts"
|
|
195
|
+
import type { AppRouter } from '../server';
|
|
196
|
+
import { httpLink } from '@trpc/client';
|
|
197
|
+
import { createTRPCNext } from '@trpc/next';
|
|
198
|
+
|
|
199
|
+
export const trpc = createTRPCNext<AppRouter>({
|
|
200
|
+
config() {
|
|
201
|
+
return {
|
|
202
|
+
links: [
|
|
203
|
+
httpLink({
|
|
204
|
+
url: '/api/trpc',
|
|
205
|
+
}),
|
|
206
|
+
],
|
|
207
|
+
};
|
|
208
|
+
},
|
|
209
|
+
});
|
|
210
|
+
```
|