@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.
- 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
|
@@ -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/
|
|
15
|
-
- www/docs/client/vanilla/
|
|
16
|
-
- www/docs/client/vanilla/
|
|
17
|
-
- www/docs/client/vanilla/
|
|
18
|
-
- www/docs/client/headers.md
|
|
19
|
-
|
|
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
|
-
|
|
18
|
+
<!-- Generated from www/v11/docs/client/vanilla/setup.md by www/build/skills.ts. Edit the docs instead. -->
|
|
23
19
|
|
|
24
|
-
|
|
20
|
+
# Set up a tRPC Client
|
|
25
21
|
|
|
26
|
-
|
|
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
|
-
|
|
24
|
+
### 1. Install the tRPC Client library
|
|
32
25
|
|
|
33
|
-
|
|
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
|
-
|
|
28
|
+
```bash
|
|
29
|
+
npm install @trpc/server @trpc/client
|
|
45
30
|
```
|
|
46
31
|
|
|
47
|
-
|
|
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
|
-
|
|
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
|
-
|
|
61
|
-
|
|
36
|
+
```ts no-check title="utils/trpc.ts"
|
|
37
|
+
import type { AppRouter } from '../server/router';
|
|
62
38
|
```
|
|
63
39
|
|
|
64
|
-
|
|
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
|
-
|
|
69
|
-
import { createTRPCClient, httpBatchLink } from '@trpc/client';
|
|
70
|
-
import type { AppRouter } from './server';
|
|
44
|
+
### 3. Initialize the tRPC client
|
|
71
45
|
|
|
72
|
-
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
48
|
+
```ts no-check title="client.ts"
|
|
49
|
+
import { createTRPCClient, httpBatchLink } from '@trpc/client';
|
|
50
|
+
import type { AppRouter } from './server';
|
|
77
51
|
|
|
78
|
-
|
|
52
|
+
const client = createTRPCClient<AppRouter>({
|
|
79
53
|
links: [
|
|
80
54
|
httpBatchLink({
|
|
81
55
|
url: 'http://localhost:3000/trpc',
|
|
82
|
-
|
|
56
|
+
|
|
57
|
+
// You can pass any HTTP headers you wish here
|
|
58
|
+
async headers() {
|
|
83
59
|
return {
|
|
84
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
### Inferring Procedure Input and Output Types
|
|
68
|
+
### 4. Use the tRPC Client
|
|
95
69
|
|
|
96
|
-
|
|
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
|
-
|
|
101
|
-
|
|
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
|
-
|
|
104
|
-
|
|
76
|
+
const frodo = await client.createUser.mutate({ name: 'Frodo' });
|
|
77
|
+
// => { id: 'id_frodo', name: 'Frodo' };
|
|
105
78
|
```
|
|
106
79
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
```ts
|
|
110
|
-
import { createTRPCClient, httpBatchLink } from '@trpc/client';
|
|
111
|
-
import type { AppRouter } from './server';
|
|
80
|
+
You're all set!
|
|
112
81
|
|
|
113
|
-
|
|
114
|
-
links: [httpBatchLink({ url: 'http://localhost:3000/trpc' })],
|
|
115
|
-
});
|
|
82
|
+
## Common mistakes
|
|
116
83
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
91
|
+
## Inferring Types
|
|
123
92
|
|
|
124
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
99
|
+
- `inferRouterInputs<TRouter>`
|
|
100
|
+
- `inferRouterOutputs<TRouter>`
|
|
101
|
+
- `inferProcedureInput<TProcedure>`
|
|
102
|
+
- `inferProcedureOutput<TProcedure>`
|
|
103
|
+
- `inferSubscriptionInput<TProcedure>`
|
|
104
|
+
- `inferSubscriptionOutput<TProcedure>`
|
|
144
105
|
|
|
145
|
-
###
|
|
106
|
+
### Inferring Input & Output Types
|
|
146
107
|
|
|
147
|
-
|
|
108
|
+
Let's assume we have this example router:
|
|
148
109
|
|
|
149
|
-
```ts
|
|
150
|
-
|
|
151
|
-
|
|
110
|
+
```ts no-check title="server.ts"
|
|
111
|
+
import { initTRPC } from '@trpc/server';
|
|
112
|
+
import { z } from "zod";
|
|
152
113
|
|
|
153
|
-
|
|
114
|
+
const t = initTRPC.create();
|
|
154
115
|
|
|
155
|
-
|
|
156
|
-
|
|
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
|
-
|
|
148
|
+
export type AppRouter = typeof appRouter;
|
|
159
149
|
```
|
|
160
150
|
|
|
161
|
-
|
|
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
|
-
|
|
166
|
-
|
|
167
|
-
|
|
153
|
+
```ts no-check title="client.ts"
|
|
154
|
+
import type { inferRouterInputs, inferRouterOutputs } from '@trpc/server';
|
|
155
|
+
import type { AppRouter } from './server';
|
|
168
156
|
|
|
169
|
-
|
|
170
|
-
|
|
157
|
+
type RouterInput = inferRouterInputs<AppRouter>;
|
|
158
|
+
type RouterOutput = inferRouterOutputs<AppRouter>;
|
|
171
159
|
|
|
172
|
-
|
|
173
|
-
|
|
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
|
-
|
|
164
|
+
### Inferring Individual Procedure Types
|
|
183
165
|
|
|
184
|
-
|
|
166
|
+
If you already have access to a specific procedure on your router, you can infer its input or output directly:
|
|
185
167
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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
|
-
|
|
191
|
-
|
|
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
|
-
|
|
179
|
+
For subscriptions, you can infer the subscription input and the emitted data type:
|
|
197
180
|
|
|
198
|
-
```ts
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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
|
-
|
|
188
|
+
type OnPostAddInput = inferSubscriptionInput<AppRouter['post']['onPostAdd']>;
|
|
189
|
+
type OnPostAddOutput = inferSubscriptionOutput<AppRouter['post']['onPostAdd']>;
|
|
190
|
+
```
|
|
205
191
|
|
|
206
|
-
|
|
192
|
+
### Infer `TRPCClientError` types
|
|
207
193
|
|
|
208
|
-
|
|
194
|
+
It's also useful to infer the error type for your `AppRouter`
|
|
209
195
|
|
|
210
|
-
|
|
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
|
-
|
|
213
|
-
|
|
214
|
-
|
|
201
|
+
export function isTRPCClientError(
|
|
202
|
+
cause: unknown,
|
|
203
|
+
): cause is TRPCClientError<AppRouter> {
|
|
204
|
+
return cause instanceof TRPCClientError;
|
|
205
|
+
}
|
|
215
206
|
|
|
216
|
-
|
|
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
|
-
|
|
219
|
-
import type { AppRouter } from '../server/router';
|
|
220
|
+
main();
|
|
220
221
|
```
|
|
221
222
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
Source: www/docs/client/vanilla/setup.mdx
|
|
223
|
+
## Error Handling
|
|
225
224
|
|
|
226
|
-
|
|
225
|
+
From https://trpc.io/docs/client/vanilla/error-handling
|
|
227
226
|
|
|
228
|
-
|
|
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
|
-
|
|
231
|
-
import { appRouter } from '../server/router';
|
|
229
|
+
### Catching a thrown error
|
|
232
230
|
|
|
233
|
-
|
|
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
|
-
|
|
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
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
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
|
-
|
|
249
|
+
See [Inferring Types](https://trpc.io/docs/client/vanilla/infer-types#infer-trpcclienterror-types) for a reusable `isTRPCClientError` guard.
|
|
245
250
|
|
|
246
|
-
|
|
251
|
+
### Errors as values with `safe()`
|
|
247
252
|
|
|
248
|
-
|
|
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
|
-
|
|
255
|
+
```ts no-check title="client.ts"
|
|
256
|
+
import { safe } from '@trpc/client';
|
|
257
|
+
import { trpc } from './trpc';
|
|
251
258
|
|
|
252
|
-
|
|
253
|
-
const client = createTRPCClient<any>({ links: [httpBatchLink({ url })] });
|
|
254
|
-
```
|
|
259
|
+
const [post, error] = await safe(trpc.post.byId.query('1'));
|
|
255
260
|
|
|
256
|
-
|
|
261
|
+
if (error) {
|
|
262
|
+
console.log(error.data);
|
|
263
|
+
} else {
|
|
264
|
+
console.log(post.title);
|
|
265
|
+
}
|
|
266
|
+
```
|
|
257
267
|
|
|
258
|
-
|
|
259
|
-
|
|
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
|
-
|
|
263
|
-
```
|
|
271
|
+
## Aborting Procedure Calls
|
|
264
272
|
|
|
265
|
-
|
|
273
|
+
From https://trpc.io/docs/client/vanilla/aborting-procedure-calls
|
|
266
274
|
|
|
267
|
-
|
|
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
|
-
|
|
277
|
+
```ts no-check title="utils.ts"
|
|
278
|
+
import { createTRPCClient, httpBatchLink } from '@trpc/client';
|
|
279
|
+
import type { AppRouter } from './server';
|
|
270
280
|
|
|
271
|
-
|
|
281
|
+
const client = createTRPCClient<AppRouter>({
|
|
282
|
+
links: [
|
|
283
|
+
httpBatchLink({
|
|
284
|
+
url: 'http://localhost:3000/trpc',
|
|
285
|
+
}),
|
|
286
|
+
],
|
|
287
|
+
});
|
|
272
288
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
```
|
|
289
|
+
// 1. Create an AbortController instance - this is a standard javascript API
|
|
290
|
+
const ac = new AbortController();
|
|
276
291
|
|
|
277
|
-
|
|
292
|
+
// 2. Pass the signal to a query or mutation
|
|
293
|
+
const query = client.userById.query('id_bilbo', { signal: ac.signal });
|
|
278
294
|
|
|
279
|
-
|
|
280
|
-
|
|
295
|
+
// 3. Cancel the request if needed
|
|
296
|
+
ac.abort();
|
|
281
297
|
```
|
|
282
298
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
Source: www/docs/client/vanilla/setup.mdx
|
|
299
|
+
## Headers
|
|
286
300
|
|
|
287
|
-
|
|
301
|
+
From https://trpc.io/docs/client/headers
|
|
288
302
|
|
|
289
|
-
|
|
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
|
-
|
|
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
|
-
|
|
307
|
+
```ts no-check title="utils/trpc.ts"
|
|
308
|
+
import { createTRPCClient, httpBatchLink } from '@trpc/client';
|
|
309
|
+
import type { AppRouter } from './server';
|
|
296
310
|
|
|
297
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
###
|
|
338
|
+
### Example with auth login
|
|
307
339
|
|
|
308
|
-
|
|
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
|
-
|
|
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
|
-
##
|
|
348
|
+
## Related skills
|
|
313
349
|
|
|
314
|
-
- `
|
|
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`
|