@trpc/client 11.22.2-canary.7.c01346ec8 → 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.
Files changed (59) hide show
  1. package/README.md +0 -8
  2. package/dist/TRPCClientError-CxFRO7Js.mjs.map +1 -1
  3. package/dist/{httpBatchLink-DO3WQoxu.cjs → httpBatchLink-Cmao5uUF.cjs} +1 -1
  4. package/dist/{httpBatchLink-BMtWxLJV.mjs → httpBatchLink-DPypDRI1.mjs} +2 -2
  5. package/dist/httpBatchLink-DPypDRI1.mjs.map +1 -0
  6. package/dist/{httpLink-Tm93WWFA.mjs → httpLink--yqaZ9lA.mjs} +2 -2
  7. package/dist/{httpLink-Tm93WWFA.mjs.map → httpLink--yqaZ9lA.mjs.map} +1 -1
  8. package/dist/{httpLink-MDJTSlpi.cjs → httpLink-B6E1HSLY.cjs} +1 -1
  9. package/dist/{httpUtils-CB_100ND.mjs → httpUtils-CAZsqwOQ.mjs} +3 -2
  10. package/dist/httpUtils-CAZsqwOQ.mjs.map +1 -0
  11. package/dist/{httpUtils-cggqcHz0.cjs → httpUtils-CrSF2CZF.cjs} +2 -1
  12. package/dist/index.cjs +13 -7
  13. package/dist/index.d.cts +8 -3
  14. package/dist/index.d.cts.map +1 -1
  15. package/dist/index.d.mts +8 -3
  16. package/dist/index.d.mts.map +1 -1
  17. package/dist/index.mjs +13 -8
  18. package/dist/index.mjs.map +1 -1
  19. package/dist/links/httpBatchLink.cjs +1 -1
  20. package/dist/links/httpBatchLink.mjs +1 -1
  21. package/dist/links/httpLink.cjs +1 -1
  22. package/dist/links/httpLink.mjs +1 -1
  23. package/dist/links/wsLink/wsLink.cjs +1 -1
  24. package/dist/links/wsLink/wsLink.mjs +1 -1
  25. package/dist/types-BxbMkv6C.d.cts.map +1 -1
  26. package/dist/types-CAPs7vhA.d.mts.map +1 -1
  27. package/dist/{wsLink-Bm5LKL6z.mjs → wsLink--7trqYPG.mjs} +2 -1
  28. package/dist/wsLink--7trqYPG.mjs.map +1 -0
  29. package/dist/wsLink-C3hsDLsL.d.cts.map +1 -1
  30. package/dist/{wsLink-CKAsNP9P.cjs → wsLink-CQGBu_VZ.cjs} +1 -0
  31. package/dist/wsLink-DnvK9sTv.d.mts.map +1 -1
  32. package/package.json +10 -21
  33. package/skills/client-setup/SKILL.md +240 -207
  34. package/skills/links/SKILL.md +98 -224
  35. package/skills/links/references/client-links-httpBatchLink.md +210 -0
  36. package/skills/links/references/client-links-httpBatchStreamLink.md +159 -0
  37. package/skills/links/references/client-links-httpLink.md +63 -0
  38. package/skills/links/references/client-links-httpSubscriptionLink.md +404 -0
  39. package/skills/links/references/client-links-localLink.md +78 -0
  40. package/skills/links/references/client-links-loggerLink.md +66 -0
  41. package/skills/links/references/client-links-retryLink.md +89 -0
  42. package/skills/links/references/client-links-splitLink.md +103 -0
  43. package/skills/links/references/client-links-wsLink.md +122 -0
  44. package/skills/superjson/SKILL.md +132 -183
  45. package/src/TRPCClientError.ts +2 -2
  46. package/src/createTRPCClient.ts +1 -1
  47. package/src/getFetch.ts +0 -1
  48. package/src/internals/dataLoader.ts +1 -1
  49. package/src/links/httpBatchStreamLink.ts +1 -1
  50. package/src/links/internals/httpUtils.ts +6 -1
  51. package/src/links/localLink.ts +10 -4
  52. package/src/links/types.ts +2 -1
  53. package/src/links/wsLink/wsClient/utils.ts +1 -1
  54. package/src/links/wsLink/wsClient/wsClient.ts +4 -0
  55. package/bin/intent.js +0 -20
  56. package/dist/httpBatchLink-BMtWxLJV.mjs.map +0 -1
  57. package/dist/httpUtils-CB_100ND.mjs.map +0 -1
  58. package/dist/wsLink-Bm5LKL6z.mjs.map +0 -1
  59. package/skills/links/references/link-options.md +0 -248
@@ -1,87 +1,63 @@
1
1
  ---
2
2
  name: client-setup
3
- description: >
4
- Create a vanilla tRPC client with createTRPCClient<AppRouter>(), configure
5
- link chain with httpBatchLink/httpLink, dynamic headers for auth, transformer
6
- on links (not client constructor). Infer types with inferRouterInputs and
7
- inferRouterOutputs. AbortController signal support. TRPCClientError typing.
8
- type: core
9
- library: trpc
10
- library_version: '11.16.0'
3
+ description: Create a vanilla tRPC client with createTRPCClient<AppRouter>() and a terminating link such as httpBatchLink, import AppRouter with `import type`, set dynamic auth headers, and put the transformer on links (not the client constructor). Infer types with inferRouterInputs and inferRouterOutputs, handle TRPCClientError with try/catch or safe(), and abort calls with an AbortSignal.
11
4
  requires:
12
5
  - server-setup
13
6
  sources:
14
- - www/docs/client/overview.md
15
- - www/docs/client/vanilla/overview.md
16
- - www/docs/client/vanilla/setup.mdx
17
- - www/docs/client/vanilla/infer-types.md
18
- - www/docs/client/headers.md
19
- - packages/client/src/internals/TRPCUntypedClient.ts
7
+ - www/v11/docs/client/vanilla/setup.md
8
+ - www/v11/docs/client/vanilla/infer-types.md
9
+ - www/v11/docs/client/vanilla/error-handling.md
10
+ - www/v11/docs/client/vanilla/aborting-procedure-calls.md
11
+ - www/v11/docs/client/headers.md
12
+ metadata:
13
+ type: core
14
+ library: trpc
15
+ library_version: 11.23.0
20
16
  ---
21
17
 
22
- # tRPC -- Client Setup
18
+ <!-- Generated from www/v11/docs/client/vanilla/setup.md by www/build/skills.ts. Edit the docs instead. -->
23
19
 
24
- ## Setup
20
+ # Set up a tRPC Client
25
21
 
26
- ```ts
27
- // server.ts
28
- import { initTRPC } from '@trpc/server';
29
- import { z } from 'zod';
22
+ From https://trpc.io/docs/client/vanilla/setup
30
23
 
31
- const t = initTRPC.create();
24
+ ### 1. Install the tRPC Client library
32
25
 
33
- const appRouter = t.router({
34
- user: t.router({
35
- byId: t.procedure
36
- .input(z.object({ id: z.string() }))
37
- .query(({ input }) => ({ id: input.id, name: 'Bilbo' })),
38
- create: t.procedure
39
- .input(z.object({ name: z.string() }))
40
- .mutation(({ input }) => ({ id: '1', ...input })),
41
- }),
42
- });
26
+ Use your preferred package manager to install the `@trpc/client` library, and also install `@trpc/server` which contains some required types.
43
27
 
44
- export type AppRouter = typeof appRouter;
28
+ ```bash
29
+ npm install @trpc/server @trpc/client
45
30
  ```
46
31
 
47
- ```ts
48
- // client.ts
49
- import { createTRPCClient, httpBatchLink } from '@trpc/client';
50
- import type { AppRouter } from './server';
32
+ ### 2. Import your App Router
51
33
 
52
- const client = createTRPCClient<AppRouter>({
53
- links: [
54
- httpBatchLink({
55
- url: 'http://localhost:3000/trpc',
56
- }),
57
- ],
58
- });
34
+ Import your `AppRouter` type into the client application. This type holds the shape of your entire API.
59
35
 
60
- const user = await client.user.byId.query({ id: '1' });
61
- const created = await client.user.create.mutate({ name: 'Frodo' });
36
+ ```ts no-check title="utils/trpc.ts"
37
+ import type { AppRouter } from '../server/router';
62
38
  ```
63
39
 
64
- ## Core Patterns
40
+ > [!TIP]
41
+ > By using `import type` you ensure that the reference will be stripped at compile-time, meaning you don't inadvertently import server-side code into your client. For more information, [see the Typescript docs](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export).
65
42
 
66
- ### Dynamic Auth Headers
67
43
 
68
- ```ts
69
- import { createTRPCClient, httpBatchLink } from '@trpc/client';
70
- import type { AppRouter } from './server';
44
+ ### 3. Initialize the tRPC client
71
45
 
72
- let token = '';
46
+ Create a tRPC client with the `createTRPCClient` method, and add a `links` array with a [terminating link](https://trpc.io/docs/client/links#the-terminating-link) pointing at your API. To learn more about tRPC links, [click here](https://trpc.io/docs/client/links).
73
47
 
74
- export function setToken(newToken: string) {
75
- token = newToken;
76
- }
48
+ ```ts no-check title="client.ts"
49
+ import { createTRPCClient, httpBatchLink } from '@trpc/client';
50
+ import type { AppRouter } from './server';
77
51
 
78
- export const client = createTRPCClient<AppRouter>({
52
+ const client = createTRPCClient<AppRouter>({
79
53
  links: [
80
54
  httpBatchLink({
81
55
  url: 'http://localhost:3000/trpc',
82
- headers() {
56
+
57
+ // You can pass any HTTP headers you wish here
58
+ async headers() {
83
59
  return {
84
- Authorization: token ? `Bearer ${token}` : '',
60
+ authorization: getAuthCookie(),
85
61
  };
86
62
  },
87
63
  }),
@@ -89,229 +65,286 @@ export const client = createTRPCClient<AppRouter>({
89
65
  });
90
66
  ```
91
67
 
92
- The `headers` callback is invoked on every HTTP request, so token changes take effect immediately.
93
-
94
- ### Inferring Procedure Input and Output Types
68
+ ### 4. Use the tRPC Client
95
69
 
96
- ```ts
97
- import type { inferRouterInputs, inferRouterOutputs } from '@trpc/server';
98
- import type { AppRouter } from './server';
70
+ Under the hood this creates a typed [JavaScript Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) which allows you to interact with your tRPC API in a fully type-safe way:
99
71
 
100
- type RouterInput = inferRouterInputs<AppRouter>;
101
- type RouterOutput = inferRouterOutputs<AppRouter>;
72
+ ```ts no-check title="client.ts"
73
+ const bilbo = await client.getUser.query('id_bilbo');
74
+ // => { id: 'id_bilbo', name: 'Bilbo' };
102
75
 
103
- type UserCreateInput = RouterInput['user']['create'];
104
- type UserByIdOutput = RouterOutput['user']['byId'];
76
+ const frodo = await client.createUser.mutate({ name: 'Frodo' });
77
+ // => { id: 'id_frodo', name: 'Frodo' };
105
78
  ```
106
79
 
107
- ### Aborting Requests with AbortController
108
-
109
- ```ts
110
- import { createTRPCClient, httpBatchLink } from '@trpc/client';
111
- import type { AppRouter } from './server';
80
+ You're all set!
112
81
 
113
- const client = createTRPCClient<AppRouter>({
114
- links: [httpBatchLink({ url: 'http://localhost:3000/trpc' })],
115
- });
82
+ ## Common mistakes
116
83
 
117
- const ac = new AbortController();
118
- const query = client.user.byId.query({ id: '1' }, { signal: ac.signal });
119
- ac.abort();
120
- ```
84
+ - **Leaving out the `AppRouter` type parameter.** `createTRPCClient({ links })` without `<AppRouter>` compiles, but none of your procedures are typed. If the `AppRouter` import doesn't resolve (often a monorepo path or `tsconfig` issue), fix the import rather than reaching for `createTRPCClient<any>`.
85
+ - **Importing the router value instead of its type.** `import { appRouter } from '../server/router'` pulls your server code into the client bundle. Export `type AppRouter = typeof appRouter` from the server and import it with `import type`.
86
+ - **Passing `transformer` to `createTRPCClient`.** The transformer belongs on each terminating link (`httpBatchLink`, `httpLink`, `wsLink`, ...), and TypeScript rejects it on the client itself. See [Data Transformers](https://trpc.io/docs/server/data-transformers).
87
+ - **Using `createTRPCProxyClient`.** It's the v10 name, kept as a deprecated alias of `createTRPCClient`.
88
+ - **Calling procedures like REST endpoints.** Procedures are addressed by their dot-separated path, for example `GET /trpc/post.byId?input=...`, not `/trpc/post/1`. Call them through the client, or see the [HTTP RPC Specification](https://trpc.io/docs/rpc) for the wire format.
89
+ - **A JSON parse error like `Unexpected token '<'`.** The request reached something that answered with an HTML page, usually a 404, so the link's `url` doesn't match where your adapter serves tRPC (check the `/trpc` prefix).
121
90
 
122
- ### Typed Error Handling
91
+ ## Inferring Types
123
92
 
124
- ```ts
125
- import { TRPCClientError } from '@trpc/client';
126
- import type { AppRouter } from './server';
93
+ From https://trpc.io/docs/client/vanilla/infer-types
127
94
 
128
- function isTRPCClientError(
129
- cause: unknown,
130
- ): cause is TRPCClientError<AppRouter> {
131
- return cause instanceof TRPCClientError;
132
- }
95
+ It is often useful to access the types of your API within your clients. For this purpose, you are able to infer the types contained in your `AppRouter`.
133
96
 
134
- try {
135
- await client.user.byId.query({ id: '1' });
136
- } catch (cause) {
137
- if (isTRPCClientError(cause)) {
138
- console.log('tRPC error code:', cause.data?.code);
139
- }
140
- }
141
- ```
97
+ `@trpc/server` exports the following helper types to assist with inferring these types from the `AppRouter` exported by your `@trpc/server` router:
142
98
 
143
- ## Common Mistakes
99
+ - `inferRouterInputs<TRouter>`
100
+ - `inferRouterOutputs<TRouter>`
101
+ - `inferProcedureInput<TProcedure>`
102
+ - `inferProcedureOutput<TProcedure>`
103
+ - `inferSubscriptionInput<TProcedure>`
104
+ - `inferSubscriptionOutput<TProcedure>`
144
105
 
145
- ### [CRITICAL] Missing AppRouter type parameter on createTRPCClient
106
+ ### Inferring Input & Output Types
146
107
 
147
- Wrong:
108
+ Let's assume we have this example router:
148
109
 
149
- ```ts
150
- const client = createTRPCClient({ links: [httpBatchLink({ url })] });
151
- ```
110
+ ```ts no-check title="server.ts"
111
+ import { initTRPC } from '@trpc/server';
112
+ import { z } from "zod";
152
113
 
153
- Correct:
114
+ const t = initTRPC.create();
154
115
 
155
- ```ts
156
- import type { AppRouter } from './server';
116
+ const appRouter = t.router({
117
+ post: t.router({
118
+ list: t.procedure
119
+ .query(() => {
120
+ // imaginary db call
121
+ return [{ id: 1, title: 'tRPC is the best!' }];
122
+ }),
123
+ byId: t.procedure
124
+ .input(z.string())
125
+ .query((opts) => {
126
+ // imaginary db call
127
+ return { id: 1, title: 'tRPC is the best!' };
128
+ }),
129
+ create: t.procedure
130
+ .input(z.object({ title: z.string(), text: z.string(), }))
131
+ .mutation((opts) => {
132
+ // imaginary db call
133
+ return { id: 1, ...opts.input };
134
+ }),
135
+ onPostAdd: t.procedure
136
+ .input(z.object({ authorId: z.string() }))
137
+ .subscription(async function* ({ input }) {
138
+ // imaginary event source
139
+ yield {
140
+ id: 1,
141
+ title: 'tRPC is the best!',
142
+ authorId: input.authorId,
143
+ };
144
+ }),
145
+ }),
146
+ });
157
147
 
158
- const client = createTRPCClient<AppRouter>({ links: [httpBatchLink({ url })] });
148
+ export type AppRouter = typeof appRouter;
159
149
  ```
160
150
 
161
- Without the type parameter, all procedure calls return `any` and type safety is completely lost.
162
-
163
- Source: www/docs/client/vanilla/setup.mdx
151
+ Using the helpers, we can infer the types of our router. The following example shows how to infer the types of the `post.create` procedure:
164
152
 
165
- ### [CRITICAL] Transformer goes on individual links, not createTRPCClient
166
-
167
- In v11, the `transformer` option is on individual terminating links, not the client constructor:
153
+ ```ts no-check title="client.ts"
154
+ import type { inferRouterInputs, inferRouterOutputs } from '@trpc/server';
155
+ import type { AppRouter } from './server';
168
156
 
169
- ```ts
170
- import superjson from 'superjson';
157
+ type RouterInput = inferRouterInputs<AppRouter>;
158
+ type RouterOutput = inferRouterOutputs<AppRouter>;
171
159
 
172
- createTRPCClient<AppRouter>({
173
- links: [
174
- httpBatchLink({
175
- url: 'http://localhost:3000',
176
- transformer: superjson,
177
- }),
178
- ],
179
- });
160
+ type PostCreateInput = RouterInput['post']['create'];
161
+ type PostCreateOutput = RouterOutput['post']['create'];
180
162
  ```
181
163
 
182
- In v11, the `transformer` option was moved from the client constructor to individual terminating links. Passing it to `createTRPCClient` throws a TypeError.
164
+ ### Inferring Individual Procedure Types
183
165
 
184
- Source: packages/client/src/internals/TRPCUntypedClient.ts
166
+ If you already have access to a specific procedure on your router, you can infer its input or output directly:
185
167
 
186
- ### [CRITICAL] Transformer on server but not on client links
187
-
188
- Wrong:
168
+ ```ts no-check title="client.ts"
169
+ import type {
170
+ inferProcedureInput,
171
+ inferProcedureOutput,
172
+ } from '@trpc/server';
173
+ import type { AppRouter } from './server';
189
174
 
190
- ```ts
191
- // Server: initTRPC.create({ transformer: superjson })
192
- // Client:
193
- httpBatchLink({ url: 'http://localhost:3000' });
175
+ type PostByIdInput = inferProcedureInput<AppRouter['post']['byId']>;
176
+ type PostByIdOutput = inferProcedureOutput<AppRouter['post']['byId']>;
194
177
  ```
195
178
 
196
- Correct:
179
+ For subscriptions, you can infer the subscription input and the emitted data type:
197
180
 
198
- ```ts
199
- // Server: initTRPC.create({ transformer: superjson })
200
- // Client:
201
- httpBatchLink({ url: 'http://localhost:3000', transformer: superjson });
202
- ```
181
+ ```ts no-check title="client.ts"
182
+ import type {
183
+ inferSubscriptionInput,
184
+ inferSubscriptionOutput,
185
+ } from '@trpc/server';
186
+ import type { AppRouter } from './server';
203
187
 
204
- If the server uses a transformer, every terminating link on the client must also specify that transformer. Mismatch causes "Unable to transform response" errors.
188
+ type OnPostAddInput = inferSubscriptionInput<AppRouter['post']['onPostAdd']>;
189
+ type OnPostAddOutput = inferSubscriptionOutput<AppRouter['post']['onPostAdd']>;
190
+ ```
205
191
 
206
- Source: https://github.com/trpc/trpc/issues/7083
192
+ ### Infer `TRPCClientError` types
207
193
 
208
- ### [CRITICAL] Using import instead of import type for AppRouter
194
+ It's also useful to infer the error type for your `AppRouter`
209
195
 
210
- Wrong:
196
+ ```ts no-check title="client.ts"
197
+ import { TRPCClientError } from '@trpc/client';
198
+ import type { AppRouter } from './server';
199
+ import { trpc } from './trpc';
211
200
 
212
- ```ts
213
- import { AppRouter } from '../server/router';
214
- ```
201
+ export function isTRPCClientError(
202
+ cause: unknown,
203
+ ): cause is TRPCClientError<AppRouter> {
204
+ return cause instanceof TRPCClientError;
205
+ }
215
206
 
216
- Correct:
207
+ async function main() {
208
+ try {
209
+ await trpc.post.byId.query('1');
210
+ } catch (cause) {
211
+ if (isTRPCClientError(cause)) {
212
+ // `cause` is now typed as your router's `TRPCClientError`
213
+ console.log('data', cause.data);
214
+ } else {
215
+ // [...]
216
+ }
217
+ }
218
+ }
217
219
 
218
- ```ts
219
- import type { AppRouter } from '../server/router';
220
+ main();
220
221
  ```
221
222
 
222
- A non-type import pulls the entire server bundle into the client. Use `import type` so it is erased at build time.
223
-
224
- Source: www/docs/client/vanilla/setup.mdx
223
+ ## Error Handling
225
224
 
226
- ### [CRITICAL] Importing appRouter value to derive type in client
225
+ From https://trpc.io/docs/client/vanilla/error-handling
227
226
 
228
- Wrong:
227
+ A failed procedure call rejects with a [`TRPCClientError`](https://trpc.io/docs/client/vanilla/infer-types#infer-trpcclienterror-types). You can either let it throw and catch it, or use `safe()` to get it back as a type-safe value.
229
228
 
230
- ```ts
231
- import { appRouter } from '../server/router';
229
+ ### Catching a thrown error
232
230
 
233
- type AppRouter = typeof appRouter;
234
- ```
231
+ `query()` and `mutate()` behave like any other promise, so a `try`/`catch` works as you would expect. TypeScript has no way to describe what a function throws, though, so `cause` arrives as `unknown` and has to be narrowed:
235
232
 
236
- Correct:
233
+ ```ts no-check title="client.ts"
234
+ import { TRPCClientError } from '@trpc/client';
235
+ import type { AppRouter } from './server';
236
+ import { trpc } from './trpc';
237
237
 
238
- ```ts
239
- // In server: export type AppRouter = typeof appRouter;
240
- // In client:
241
- import type { AppRouter } from '../server/router';
238
+ try {
239
+ const post = await trpc.post.byId.query('1');
240
+ } catch (cause) {
241
+ if (cause instanceof TRPCClientError) {
242
+ console.log(cause.data);
243
+ } else {
244
+ // something that isn't a tRPC error
245
+ }
246
+ }
242
247
  ```
243
248
 
244
- Importing the `appRouter` value (not just the type) bundles the entire server into the client, shipping server code to the browser.
249
+ See [Inferring Types](https://trpc.io/docs/client/vanilla/infer-types#infer-trpcclienterror-types) for a reusable `isTRPCClientError` guard.
245
250
 
246
- Source: www/docs/server/routers.md
251
+ ### Errors as values with `safe()`
247
252
 
248
- ### [CRITICAL] Using type assertions to bypass AppRouter import errors
253
+ `safe()` awaits a `query()` or `mutate()` call without letting it throw, handing back a `[data, error]` pair instead. Exactly one side is ever set, so checking either one narrows the other:
249
254
 
250
- Wrong:
255
+ ```ts no-check title="client.ts"
256
+ import { safe } from '@trpc/client';
257
+ import { trpc } from './trpc';
251
258
 
252
- ```ts
253
- const client = createTRPCClient<any>({ links: [httpBatchLink({ url })] });
254
- ```
259
+ const [post, error] = await safe(trpc.post.byId.query('1'));
255
260
 
256
- Correct:
261
+ if (error) {
262
+ console.log(error.data);
263
+ } else {
264
+ console.log(post.title);
265
+ }
266
+ ```
257
267
 
258
- ```ts
259
- // Fix the import path or monorepo configuration
260
- import type { AppRouter } from '@myorg/api-types';
268
+ > [!NOTE] Info
269
+ > Subscriptions don't need `safe()` - they never throw at the call site, and report failures through `onError`, which is already typed with the procedure's error shape.
261
270
 
262
- const client = createTRPCClient<AppRouter>({ links: [httpBatchLink({ url })] });
263
- ```
271
+ ## Aborting Procedure Calls
264
272
 
265
- Casting to `any` or manually recreating the router type destroys end-to-end type safety. Fix the import path or monorepo config instead.
273
+ From https://trpc.io/docs/client/vanilla/aborting-procedure-calls
266
274
 
267
- Source: www/docs/client/vanilla/setup.mdx
275
+ tRPC supports the standard `AbortController`/`AbortSignal` API for aborting procedures. All you have to do is pass an `AbortSignal` to the query or mutation options, and call the `AbortController` instance's `abort` method if you need to cancel the request.
268
276
 
269
- ### [CRITICAL] Using createTRPCProxyClient (renamed in v11)
277
+ ```ts no-check title="utils.ts"
278
+ import { createTRPCClient, httpBatchLink } from '@trpc/client';
279
+ import type { AppRouter } from './server';
270
280
 
271
- Wrong:
281
+ const client = createTRPCClient<AppRouter>({
282
+ links: [
283
+ httpBatchLink({
284
+ url: 'http://localhost:3000/trpc',
285
+ }),
286
+ ],
287
+ });
272
288
 
273
- ```ts
274
- import { createTRPCProxyClient } from '@trpc/client';
275
- ```
289
+ // 1. Create an AbortController instance - this is a standard javascript API
290
+ const ac = new AbortController();
276
291
 
277
- Correct:
292
+ // 2. Pass the signal to a query or mutation
293
+ const query = client.userById.query('id_bilbo', { signal: ac.signal });
278
294
 
279
- ```ts
280
- import { createTRPCClient } from '@trpc/client';
295
+ // 3. Cancel the request if needed
296
+ ac.abort();
281
297
  ```
282
298
 
283
- `createTRPCProxyClient` was renamed to `createTRPCClient` in v11.
284
-
285
- Source: www/docs/client/vanilla/setup.mdx
299
+ ## Headers
286
300
 
287
- ### [CRITICAL] Treating tRPC as a REST API
301
+ From https://trpc.io/docs/client/headers
288
302
 
289
- Wrong:
303
+ The headers option may be used with any of our HTTP links: [`httpBatchLink`](https://trpc.io/docs/client/links/httpBatchLink), [`httpBatchStreamLink`](https://trpc.io/docs/client/links/httpBatchStreamLink), [`httpLink`](https://trpc.io/docs/client/links/httpLink), or [`httpSubscriptionLink`](https://trpc.io/docs/client/links/httpSubscriptionLink).
290
304
 
291
- ```ts
292
- fetch('/api/trpc/users/123', { method: 'GET' });
293
- ```
305
+ `headers` can be both an object or a function. If it's a function it will get called dynamically for every HTTP request.
294
306
 
295
- Correct:
307
+ ```ts no-check title="utils/trpc.ts"
308
+ import { createTRPCClient, httpBatchLink } from '@trpc/client';
309
+ import type { AppRouter } from './server';
296
310
 
297
- ```ts
298
- const user = await client.user.byId.query({ id: '123' });
299
- // Raw equivalent: GET /api/trpc/user.byId?input={"id":"123"}
300
- ```
311
+ let token: string;
301
312
 
302
- tRPC uses JSON-RPC over HTTP. Procedures are called by dot-separated name with JSON input, not by REST resource paths.
313
+ export function setToken(newToken: string) {
314
+ /**
315
+ * You can also save the token to cookies, and initialize from
316
+ * cookies above.
317
+ */
318
+ token = newToken;
319
+ }
303
320
 
304
- Source: www/docs/client/overview.md
321
+ export const trpc = createTRPCClient<AppRouter>({
322
+ links: [
323
+ httpBatchLink({
324
+ url: 'http://localhost:3000',
325
+ /**
326
+ * Headers will be called on each request.
327
+ */
328
+ headers() {
329
+ return {
330
+ Authorization: token,
331
+ };
332
+ },
333
+ }),
334
+ ],
335
+ });
336
+ ```
305
337
 
306
- ### [HIGH] HTML error page instead of JSON response
338
+ ### Example with auth login
307
339
 
308
- If you see `couldn't parse JSON, invalid character '<'`, the tRPC endpoint returned an HTML page (404/503) instead of JSON. This means the `url` in your link config is wrong or infrastructure routing is misconfigured -- it is not a tRPC bug. Verify the URL matches your adapter's mount point.
340
+ ```ts no-check title="auth.ts"
341
+ const result = await trpc.auth.login.mutate({ username: 'user', password: 'pass' });
342
+ setToken(result.accessToken);
343
+ ```
309
344
 
310
- Source: www/docs/client/vanilla/setup.mdx
345
+ The `token` can be whatever you want it to be. It's entirely up to you whether that's just a client-side
346
+ variable that you update the value of on success or whether you store the token and pull it from local storage.
311
347
 
312
- ## See Also
348
+ ## Related skills
313
349
 
314
- - `links` -- configure httpBatchLink, httpLink, splitLink, and other link types
315
- - `superjson` -- set up SuperJSON transformer on server and client
316
- - `server-setup` -- define routers, procedures, context, and export AppRouter type
317
- - `react-query-setup` -- use tRPC with TanStack React Query for React applications
350
+ - `server-setup` (https://trpc.io/docs/server/overview): `npx @tanstack/intent@latest load @trpc/server#server-setup`