@trpc/server 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/adapters/aws-lambda/index.cjs +1 -1
- package/dist/adapters/aws-lambda/index.d.cts.map +1 -1
- package/dist/adapters/aws-lambda/index.d.mts.map +1 -1
- package/dist/adapters/aws-lambda/index.mjs +1 -1
- package/dist/adapters/aws-lambda/index.mjs.map +1 -1
- package/dist/adapters/express.cjs +1 -1
- package/dist/adapters/express.mjs +1 -1
- package/dist/adapters/express.mjs.map +1 -1
- package/dist/adapters/fastify/index.cjs +3 -3
- package/dist/adapters/fastify/index.mjs +3 -3
- package/dist/adapters/fetch/index.cjs +1 -1
- package/dist/adapters/fetch/index.mjs +1 -1
- package/dist/adapters/next-app-dir.mjs.map +1 -1
- package/dist/adapters/next.cjs +1 -1
- package/dist/adapters/next.mjs +1 -1
- package/dist/adapters/next.mjs.map +1 -1
- package/dist/adapters/node-http/index.cjs +1 -1
- package/dist/adapters/node-http/index.mjs +1 -1
- package/dist/adapters/standalone.cjs +9 -3
- package/dist/adapters/standalone.d.cts +0 -1
- package/dist/adapters/standalone.d.cts.map +1 -1
- package/dist/adapters/standalone.d.mts +0 -1
- package/dist/adapters/standalone.d.mts.map +1 -1
- package/dist/adapters/standalone.mjs +9 -3
- package/dist/adapters/standalone.mjs.map +1 -1
- package/dist/adapters/ws.cjs +1 -1
- package/dist/adapters/ws.mjs +1 -1
- package/dist/asyncGeneratorDelegate-FcEJT04i.mjs.map +1 -1
- package/dist/codes-C9N0z0an.mjs.map +1 -1
- package/dist/getErrorShape-zlj81BiC.mjs.map +1 -1
- package/dist/http.cjs +1 -1
- package/dist/http.mjs +1 -1
- package/dist/initTRPC-RMeQIy9H.mjs.map +1 -1
- package/dist/{node-http-B5jkpWn7.mjs → node-http-ChhZEv1k.mjs} +2 -2
- package/dist/node-http-ChhZEv1k.mjs.map +1 -0
- package/dist/{node-http-BZ0HlzkC.cjs → node-http-X8RiQEuH.cjs} +1 -1
- package/dist/parseTRPCMessage-CJ4DJVBp.mjs.map +1 -1
- package/dist/{resolveResponse-b9D_SrLs.cjs → resolveResponse-CoGjFxK0.cjs} +8 -4
- package/dist/{resolveResponse-DH5L33sC.mjs → resolveResponse-DksrxZ2k.mjs} +9 -5
- package/dist/resolveResponse-DksrxZ2k.mjs.map +1 -0
- package/dist/unstable-core-do-not-import-B8epiErP.d.cts.map +1 -1
- package/dist/unstable-core-do-not-import-Dbo98mkh.d.mts.map +1 -1
- package/dist/unstable-core-do-not-import.cjs +1 -1
- package/dist/unstable-core-do-not-import.mjs +1 -1
- package/dist/unstable-core-do-not-import.mjs.map +1 -1
- package/dist/{ws-83TNWW-W.cjs → ws-7qWW7Uec.cjs} +2 -2
- package/dist/{ws-DJ2SSChC.mjs → ws-BYfo-HED.mjs} +3 -3
- package/dist/ws-BYfo-HED.mjs.map +1 -0
- package/package.json +9 -12
- package/skills/adapter-aws-lambda/SKILL.md +152 -134
- package/skills/adapter-express/SKILL.md +127 -113
- package/skills/adapter-fastify/SKILL.md +228 -136
- package/skills/adapter-fetch/SKILL.md +388 -111
- package/skills/adapter-standalone/SKILL.md +184 -94
- package/skills/auth/SKILL.md +327 -202
- package/skills/auth/references/client-cors.md +29 -0
- package/skills/caching/SKILL.md +58 -148
- package/skills/error-handling/SKILL.md +280 -174
- package/skills/middlewares/SKILL.md +320 -121
- package/skills/non-json-content-types/SKILL.md +149 -168
- package/skills/server-setup/SKILL.md +355 -253
- package/skills/server-setup/references/server-context.md +219 -0
- package/skills/server-setup/references/server-merging-routers.md +141 -0
- package/skills/server-setup/references/server-metadata.md +105 -0
- package/skills/server-setup/references/server-procedures.md +172 -0
- package/skills/server-setup/references/server-routers.md +123 -0
- package/skills/server-side-calls/SKILL.md +247 -152
- package/skills/service-oriented-architecture/SKILL.md +81 -177
- package/skills/subscriptions/SKILL.md +341 -297
- package/skills/subscriptions/references/client-links-httpSubscriptionLink.md +404 -0
- package/skills/subscriptions/references/client-links-wsLink.md +122 -0
- package/skills/subscriptions/references/server-websockets.md +297 -0
- package/skills/trpc-router/SKILL.md +156 -86
- package/skills/validators/SKILL.md +384 -128
- package/src/adapters/aws-lambda/getPlanner.ts +4 -3
- package/src/adapters/aws-lambda/index.ts +2 -1
- package/src/adapters/express.ts +1 -1
- package/src/adapters/next-app-dir/nextAppDirCaller.ts +4 -4
- package/src/adapters/next-app-dir/rethrowNextErrors.ts +1 -1
- package/src/adapters/next.ts +1 -1
- package/src/adapters/node-http/incomingMessageToRequest.ts +1 -1
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +1 -1
- package/src/adapters/node-http/types.ts +1 -1
- package/src/adapters/node-http/writeResponse.ts +1 -1
- package/src/adapters/standalone.ts +12 -6
- package/src/adapters/ws.ts +5 -5
- package/src/unstable-core-do-not-import/clientish/inferrable.ts +4 -1
- package/src/unstable-core-do-not-import/clientish/serialize.ts +4 -4
- package/src/unstable-core-do-not-import/error/TRPCError.ts +3 -3
- package/src/unstable-core-do-not-import/http/formDataToObject.ts +1 -1
- package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +1 -1
- package/src/unstable-core-do-not-import/http/resolveResponse.ts +13 -2
- package/src/unstable-core-do-not-import/procedure.ts +5 -2
- package/src/unstable-core-do-not-import/procedureBuilder.ts +3 -2
- package/src/unstable-core-do-not-import/rootConfig.ts +2 -2
- package/src/unstable-core-do-not-import/router.ts +5 -2
- package/src/unstable-core-do-not-import/rpc/envelopes.ts +3 -2
- package/src/unstable-core-do-not-import/stream/jsonl.ts +2 -1
- package/src/unstable-core-do-not-import/stream/sse.types.ts +1 -1
- package/src/unstable-core-do-not-import/stream/utils/asyncIterable.ts +1 -1
- package/src/unstable-core-do-not-import/stream/utils/createDeferred.ts +1 -1
- package/src/unstable-core-do-not-import/stream/utils/disposable.ts +6 -6
- package/src/unstable-core-do-not-import/stream/utils/mergeAsyncIterables.ts +2 -2
- package/src/unstable-core-do-not-import/stream/utils/withPing.ts +3 -1
- package/src/unstable-core-do-not-import/transformer.ts +4 -1
- package/src/unstable-core-do-not-import/types.ts +3 -2
- package/src/unstable-core-do-not-import/utils.ts +1 -1
- package/src/vendor/standard-schema-v1/spec.ts +2 -2
- package/src/vendor/unpromise/unpromise.ts +1 -1
- package/bin/intent.js +0 -20
- package/dist/node-http-B5jkpWn7.mjs.map +0 -1
- package/dist/resolveResponse-DH5L33sC.mjs.map +0 -1
- package/dist/ws-DJ2SSChC.mjs.map +0 -1
|
@@ -1,63 +1,71 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: non-json-content-types
|
|
3
|
-
description:
|
|
4
|
-
Handle FormData, file uploads, Blob, Uint8Array, and ReadableStream inputs in
|
|
5
|
-
tRPC mutations. Use octetInputParser from @trpc/server/http for binary data.
|
|
6
|
-
Route non-JSON requests with splitLink and isNonJsonSerializable() from
|
|
7
|
-
@trpc/client. FormData and binary inputs only work with mutations (POST).
|
|
8
|
-
type: core
|
|
9
|
-
library: trpc
|
|
10
|
-
library_version: '11.16.0'
|
|
3
|
+
description: Handle FormData, file uploads, Blob, Uint8Array, and ReadableStream inputs in tRPC mutations. Use octetInputParser from @trpc/server/http for binary data. Route non-JSON requests with splitLink and isNonJsonSerializable() from @trpc/client, since batching links send them as `{}`. FormData and binary inputs only work with mutations (POST) and skip the transformer. Keep Express or Next.js body parsers off tRPC routes.
|
|
11
4
|
requires:
|
|
12
5
|
- server-setup
|
|
13
6
|
- links
|
|
14
7
|
sources:
|
|
15
|
-
-
|
|
16
|
-
-
|
|
8
|
+
- www/v11/docs/server/non-json-content-types.md
|
|
9
|
+
- www/v11/docs/server/adapters/express.md
|
|
10
|
+
metadata:
|
|
11
|
+
type: core
|
|
12
|
+
library: trpc
|
|
13
|
+
library_version: 11.23.0
|
|
17
14
|
---
|
|
18
15
|
|
|
19
|
-
|
|
16
|
+
<!-- Generated from www/v11/docs/server/non-json-content-types.md by www/build/skills.ts. Edit the docs instead. -->
|
|
20
17
|
|
|
21
|
-
|
|
18
|
+
# Content Types
|
|
22
19
|
|
|
23
|
-
|
|
20
|
+
From https://trpc.io/docs/server/non-json-content-types
|
|
24
21
|
|
|
25
|
-
|
|
26
|
-
// server/trpc.ts
|
|
27
|
-
import { initTRPC } from '@trpc/server';
|
|
22
|
+
tRPC supports multiple content types as procedure inputs: JSON-serializable data, FormData, File, Blob, and other binary types.
|
|
28
23
|
|
|
29
|
-
|
|
24
|
+
## JSON (Default)
|
|
30
25
|
|
|
31
|
-
|
|
32
|
-
export const publicProcedure = t.procedure;
|
|
33
|
-
```
|
|
26
|
+
By default, tRPC sends and receives JSON-serializable data. No extra configuration is needed — any input that can be serialized to JSON works out of the box with all links (`httpLink`, `httpBatchLink`, `httpBatchStreamLink`).
|
|
34
27
|
|
|
35
|
-
```ts
|
|
36
|
-
// server/appRouter.ts
|
|
37
|
-
import { octetInputParser } from '@trpc/server/http';
|
|
28
|
+
```ts no-check
|
|
38
29
|
import { z } from 'zod';
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return { greeting: `Hello ${name}` };
|
|
47
|
-
}),
|
|
48
|
-
uploadFile: publicProcedure.input(octetInputParser).mutation(({ input }) => {
|
|
49
|
-
// input is a ReadableStream
|
|
50
|
-
return { valid: true };
|
|
30
|
+
|
|
31
|
+
export const t = initTRPC.create();
|
|
32
|
+
const publicProcedure = t.procedure;
|
|
33
|
+
|
|
34
|
+
export const appRouter = t.router({
|
|
35
|
+
hello: publicProcedure.input(z.object({ name: z.string() })).query((opts) => {
|
|
36
|
+
return { greeting: `Hello ${opts.input.name}` };
|
|
51
37
|
}),
|
|
52
38
|
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Non-JSON Content Types
|
|
42
|
+
|
|
43
|
+
In addition to JSON, tRPC can use FormData, File, and other binary types as procedure inputs.
|
|
44
|
+
|
|
45
|
+
### Client Setup
|
|
46
|
+
|
|
47
|
+
> [!NOTE] Info
|
|
48
|
+
> While tRPC natively supports several non-JSON serializable types, your client may need a little link configuration to support them depending on your setup.
|
|
49
|
+
|
|
53
50
|
|
|
54
|
-
|
|
51
|
+
`httpLink` supports non-JSON content types out of the box — if you're only using this link, your existing setup should work immediately.
|
|
52
|
+
|
|
53
|
+
```ts no-check
|
|
54
|
+
import { createTRPCClient, httpLink } from '@trpc/client';
|
|
55
|
+
import type { AppRouter } from './server';
|
|
56
|
+
|
|
57
|
+
createTRPCClient<AppRouter>({
|
|
58
|
+
links: [
|
|
59
|
+
httpLink({
|
|
60
|
+
url: 'http://localhost:2022',
|
|
61
|
+
}),
|
|
62
|
+
],
|
|
63
|
+
});
|
|
55
64
|
```
|
|
56
65
|
|
|
57
|
-
|
|
66
|
+
However, not all links support these content types. If you're using `httpBatchLink` or `httpBatchStreamLink`, you will need to include a `splitLink` and route requests based on the content type.
|
|
58
67
|
|
|
59
|
-
```ts
|
|
60
|
-
// client/index.ts
|
|
68
|
+
```ts no-check
|
|
61
69
|
import {
|
|
62
70
|
createTRPCClient,
|
|
63
71
|
httpBatchLink,
|
|
@@ -65,84 +73,29 @@ import {
|
|
|
65
73
|
isNonJsonSerializable,
|
|
66
74
|
splitLink,
|
|
67
75
|
} from '@trpc/client';
|
|
68
|
-
import type { AppRouter } from '
|
|
76
|
+
import type { AppRouter } from './server';
|
|
69
77
|
|
|
70
|
-
const url = 'http://localhost:
|
|
78
|
+
const url = 'http://localhost:2022';
|
|
71
79
|
|
|
72
|
-
|
|
80
|
+
createTRPCClient<AppRouter>({
|
|
73
81
|
links: [
|
|
74
82
|
splitLink({
|
|
75
83
|
condition: (op) => isNonJsonSerializable(op.input),
|
|
76
|
-
true: httpLink({
|
|
77
|
-
|
|
84
|
+
true: httpLink({
|
|
85
|
+
url,
|
|
86
|
+
}),
|
|
87
|
+
false: httpBatchLink({
|
|
88
|
+
url,
|
|
89
|
+
}),
|
|
78
90
|
}),
|
|
79
91
|
],
|
|
80
92
|
});
|
|
81
93
|
```
|
|
82
94
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
### FormData mutation
|
|
86
|
-
|
|
87
|
-
```ts
|
|
88
|
-
// server/appRouter.ts
|
|
89
|
-
import { z } from 'zod';
|
|
90
|
-
import { publicProcedure, router } from './trpc';
|
|
91
|
-
|
|
92
|
-
export const appRouter = router({
|
|
93
|
-
createPost: publicProcedure
|
|
94
|
-
.input(z.instanceof(FormData))
|
|
95
|
-
.mutation(({ input }) => {
|
|
96
|
-
const title = input.get('title') as string;
|
|
97
|
-
const body = input.get('body') as string;
|
|
98
|
-
return { id: '1', title, body };
|
|
99
|
-
}),
|
|
100
|
-
});
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
```ts
|
|
104
|
-
// client usage
|
|
105
|
-
const form = new FormData();
|
|
106
|
-
form.append('title', 'Hello');
|
|
107
|
-
form.append('body', 'World');
|
|
108
|
-
|
|
109
|
-
const result = await trpc.createPost.mutate(form);
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
### Binary file upload with octetInputParser
|
|
113
|
-
|
|
114
|
-
```ts
|
|
115
|
-
// server/appRouter.ts
|
|
116
|
-
import { octetInputParser } from '@trpc/server/http';
|
|
117
|
-
import { publicProcedure, router } from './trpc';
|
|
118
|
-
|
|
119
|
-
export const appRouter = router({
|
|
120
|
-
upload: publicProcedure
|
|
121
|
-
.input(octetInputParser)
|
|
122
|
-
.mutation(async ({ input }) => {
|
|
123
|
-
const reader = input.getReader();
|
|
124
|
-
let totalBytes = 0;
|
|
125
|
-
while (true) {
|
|
126
|
-
const { done, value } = await reader.read();
|
|
127
|
-
if (done) break;
|
|
128
|
-
totalBytes += value.byteLength;
|
|
129
|
-
}
|
|
130
|
-
return { totalBytes };
|
|
131
|
-
}),
|
|
132
|
-
});
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
```ts
|
|
136
|
-
// client usage
|
|
137
|
-
const file = new File(['hello world'], 'test.txt', { type: 'text/plain' });
|
|
138
|
-
const result = await trpc.upload.mutate(file);
|
|
139
|
-
```
|
|
95
|
+
If you are using `transformer` in your tRPC server, TypeScript requires that your tRPC client link defines `transformer` as well.
|
|
96
|
+
Use this example as a base:
|
|
140
97
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
### Client splitLink with superjson transformer
|
|
144
|
-
|
|
145
|
-
```ts
|
|
98
|
+
```ts no-check
|
|
146
99
|
import {
|
|
147
100
|
createTRPCClient,
|
|
148
101
|
httpBatchLink,
|
|
@@ -151,115 +104,143 @@ import {
|
|
|
151
104
|
splitLink,
|
|
152
105
|
} from '@trpc/client';
|
|
153
106
|
import superjson from 'superjson';
|
|
154
|
-
import type { AppRouter } from '
|
|
107
|
+
import type { AppRouter } from './server';
|
|
155
108
|
|
|
156
|
-
const url = 'http://localhost:
|
|
109
|
+
const url = 'http://localhost:2022';
|
|
157
110
|
|
|
158
|
-
|
|
111
|
+
createTRPCClient<AppRouter>({
|
|
159
112
|
links: [
|
|
160
113
|
splitLink({
|
|
161
114
|
condition: (op) => isNonJsonSerializable(op.input),
|
|
162
115
|
true: httpLink({
|
|
163
116
|
url,
|
|
164
117
|
transformer: {
|
|
118
|
+
// request - convert data before sending to the tRPC server
|
|
165
119
|
serialize: (data) => data,
|
|
166
|
-
|
|
120
|
+
// response - convert the tRPC response before using it in client
|
|
121
|
+
deserialize: (data) => superjson.deserialize(data), // or your other transformer
|
|
167
122
|
},
|
|
168
123
|
}),
|
|
169
124
|
false: httpBatchLink({
|
|
170
125
|
url,
|
|
171
|
-
transformer: superjson,
|
|
126
|
+
transformer: superjson, // or your other transformer
|
|
172
127
|
}),
|
|
173
128
|
}),
|
|
174
129
|
],
|
|
175
130
|
});
|
|
176
131
|
```
|
|
177
132
|
|
|
178
|
-
|
|
133
|
+
### Server Setup
|
|
134
|
+
|
|
135
|
+
> [!NOTE] Info
|
|
136
|
+
> When a request is handled by tRPC, it takes care of parsing the request body based on the `Content-Type` header of the request.
|
|
137
|
+
> If you encounter errors like `Failed to parse body as XXX`, make sure that your server (e.g., Express, Next.js) isn't parsing the request body before tRPC handles it:
|
|
138
|
+
>
|
|
139
|
+
> - Express: see [Body parsers](https://trpc.io/docs/server/adapters/express#body-parsers).
|
|
140
|
+
> - Next.js Pages Router: turn off Next's body parser in the tRPC API route with `export const config = { api: { bodyParser: false } }`, as in our [FormData example](https://github.com/trpc/trpc/tree/main/examples/next-formdata).
|
|
179
141
|
|
|
180
|
-
## Common Mistakes
|
|
181
142
|
|
|
182
|
-
|
|
143
|
+
#### `FormData` Input
|
|
183
144
|
|
|
184
|
-
|
|
145
|
+
FormData is natively supported, and for more advanced usage you could also combine this with a library like [zod-form-data](https://www.npmjs.com/package/zod-form-data) to validate inputs in a type-safe way.
|
|
146
|
+
|
|
147
|
+
```ts no-check
|
|
148
|
+
import { z } from 'zod';
|
|
185
149
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
import type { AppRouter } from '../server/appRouter';
|
|
150
|
+
export const t = initTRPC.create();
|
|
151
|
+
const publicProcedure = t.procedure;
|
|
189
152
|
|
|
190
|
-
const
|
|
191
|
-
|
|
153
|
+
export const appRouter = t.router({
|
|
154
|
+
hello: publicProcedure.input(z.instanceof(FormData)).mutation((opts) => {
|
|
155
|
+
const data = opts.input;
|
|
156
|
+
return {
|
|
157
|
+
greeting: `Hello ${data.get('name')}`,
|
|
158
|
+
};
|
|
159
|
+
}),
|
|
192
160
|
});
|
|
193
161
|
```
|
|
194
162
|
|
|
195
|
-
|
|
163
|
+
On the client, pass a `FormData` instance to the mutation:
|
|
196
164
|
|
|
197
|
-
```ts
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
httpBatchLink,
|
|
201
|
-
httpLink,
|
|
202
|
-
isNonJsonSerializable,
|
|
203
|
-
splitLink,
|
|
204
|
-
} from '@trpc/client';
|
|
205
|
-
import type { AppRouter } from '../server/appRouter';
|
|
165
|
+
```ts no-check
|
|
166
|
+
const form = new FormData();
|
|
167
|
+
form.append('name', 'Bilbo');
|
|
206
168
|
|
|
207
|
-
const
|
|
169
|
+
const result = await client.hello.mutate(form);
|
|
170
|
+
```
|
|
208
171
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
172
|
+
For a more advanced code sample you can see our [example project here](https://github.com/juliusmarminge/trpc-interop/blob/66aa760141030ffc421cae1a3bda9b5f1ab340b6/src/server.ts#L28-L43)
|
|
173
|
+
|
|
174
|
+
#### `File` and other Binary Type Inputs
|
|
175
|
+
|
|
176
|
+
tRPC converts many octet content types to a `ReadableStream` which can be consumed in a procedure. Currently these are `Blob` `Uint8Array` and `File`.
|
|
177
|
+
|
|
178
|
+
```ts no-check
|
|
179
|
+
import { octetInputParser } from '@trpc/server/http';
|
|
180
|
+
|
|
181
|
+
export const t = initTRPC.create();
|
|
182
|
+
const publicProcedure = t.procedure;
|
|
183
|
+
|
|
184
|
+
export const appRouter = t.router({
|
|
185
|
+
upload: publicProcedure.input(octetInputParser).mutation(async (opts) => {
|
|
186
|
+
const data = opts.input;
|
|
187
|
+
const reader = data.getReader();
|
|
188
|
+
let bytes = 0;
|
|
189
|
+
while (true) {
|
|
190
|
+
const { done, value } = await reader.read();
|
|
191
|
+
if (done) break;
|
|
192
|
+
bytes += value.byteLength;
|
|
193
|
+
}
|
|
194
|
+
return { bytes };
|
|
195
|
+
}),
|
|
217
196
|
});
|
|
218
197
|
```
|
|
219
198
|
|
|
220
|
-
|
|
199
|
+
On the client, pass a `File`, `Blob` or `Uint8Array` to the mutation:
|
|
221
200
|
|
|
222
|
-
|
|
201
|
+
```ts no-check
|
|
202
|
+
const file = new File(['hello world'], 'hello.txt', { type: 'text/plain' });
|
|
223
203
|
|
|
224
|
-
|
|
204
|
+
await client.upload.mutate(file);
|
|
205
|
+
```
|
|
225
206
|
|
|
226
|
-
|
|
207
|
+
## Common mistakes
|
|
227
208
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
209
|
+
- **Using a query.** FormData and binary inputs are sent as the body of a `POST` request, so they only work with mutations: `httpLink` throws `FormData is only supported for mutations` for a query.
|
|
210
|
+
- **Sending them through a batching link.** `httpBatchLink`, `httpBatchStreamLink` and `wsLink` serialize inputs as JSON, which turns a `FormData` or `File` into `{}`, so input validation fails. Route them to `httpLink` with a [`splitLink`](https://trpc.io/docs/server/non-json-content-types#client-setup).
|
|
211
|
+
- **Transforming the request.** tRPC passes FormData and binary inputs to your procedure as they are, without your `transformer`. If you use one, give the non-JSON `httpLink` a transformer that leaves the request alone, as [shown above](https://trpc.io/docs/server/non-json-content-types#client-setup).
|
|
212
|
+
- **Letting your server parse the body first.** See [Server Setup](https://trpc.io/docs/server/non-json-content-types#server-setup).
|
|
232
213
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
```
|
|
214
|
+
## Body parsers
|
|
215
|
+
|
|
216
|
+
tRPC parses request bodies itself, so tRPC routes don't need `express.json()`. If a body parser runs before tRPC anyway, tRPC uses the `req.body` it parsed, which works for JSON. For [FormData and binary inputs](https://trpc.io/docs/server/non-json-content-types) it depends on your Express version:
|
|
237
217
|
|
|
238
|
-
|
|
218
|
+
- **Express 5**: `express.json()` and `express.urlencoded()` leave other content types unread, so tRPC still reads them.
|
|
219
|
+
- **Express 4**: the parsers set `req.body` to `{}` on content types they skip, so tRPC receives `{}` instead of the form data or file.
|
|
220
|
+
- In both versions, a parser that reads the body you upload, like `express.raw()` for `application/octet-stream`, breaks binary inputs.
|
|
239
221
|
|
|
240
|
-
|
|
222
|
+
To be safe, register body parsers only on the routes that need them:
|
|
223
|
+
|
|
224
|
+
```ts no-check title="server.ts"
|
|
241
225
|
import * as trpcExpress from '@trpc/server/adapters/express';
|
|
242
226
|
import express from 'express';
|
|
243
|
-
import { appRouter } from './
|
|
227
|
+
import { appRouter } from './router';
|
|
244
228
|
|
|
245
229
|
const app = express();
|
|
246
|
-
app.use('/api', express.json());
|
|
247
|
-
app.use('/trpc', trpcExpress.createExpressMiddleware({ router: appRouter }));
|
|
248
|
-
```
|
|
249
|
-
|
|
250
|
-
A global `express.json()` middleware consumes the request body before tRPC can read it; scope body parsing to non-tRPC routes only.
|
|
251
230
|
|
|
252
|
-
|
|
231
|
+
app.use('/api', express.json()); // only for your own `/api/*` routes
|
|
232
|
+
app.post('/api/webhook', (req, res) => {
|
|
233
|
+
res.json({ received: req.body });
|
|
234
|
+
});
|
|
253
235
|
|
|
254
|
-
|
|
236
|
+
app.use('/trpc', trpcExpress.createExpressMiddleware({ router: appRouter }));
|
|
255
237
|
|
|
256
|
-
|
|
238
|
+
app.listen(4000);
|
|
239
|
+
```
|
|
257
240
|
|
|
258
|
-
|
|
241
|
+
From https://trpc.io/docs/server/adapters/express#body-parsers
|
|
259
242
|
|
|
260
|
-
##
|
|
243
|
+
## Related skills
|
|
261
244
|
|
|
262
|
-
- `server-setup`
|
|
263
|
-
- `links`
|
|
264
|
-
- `validators` -- z.instanceof(FormData) for FormData validation
|
|
265
|
-
- `adapter-express` -- Express-specific body parser considerations
|
|
245
|
+
- `server-setup` (https://trpc.io/docs/server/overview): `npx @tanstack/intent@latest load @trpc/server#server-setup`
|
|
246
|
+
- `links` (https://trpc.io/docs/client/links): `npx @tanstack/intent@latest load @trpc/client#links`
|