@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,228 +1,484 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: validators
|
|
3
|
-
description:
|
|
4
|
-
Configure input and output validation with .input() and .output() using Zod,
|
|
5
|
-
Yup, Superstruct, ArkType, Valibot, Effect, or custom validator functions.
|
|
6
|
-
Chain multiple .input() calls to merge object schemas. Standard Schema protocol
|
|
7
|
-
support. Output validation returns INTERNAL_SERVER_ERROR on failure.
|
|
8
|
-
type: core
|
|
9
|
-
library: trpc
|
|
10
|
-
library_version: '11.16.0'
|
|
3
|
+
description: Configure input and output validation with .input() and .output() using Zod, Yup, Superstruct, ArkType, Valibot, Effect, any Standard Schema library, or custom validator functions. Chain multiple .input() calls to merge object schemas. Invalid input fails with BAD_REQUEST (the validator's error is the cause); invalid output fails with INTERNAL_SERVER_ERROR.
|
|
11
4
|
requires:
|
|
12
5
|
- server-setup
|
|
13
6
|
sources:
|
|
14
|
-
-
|
|
15
|
-
|
|
7
|
+
- www/v11/docs/server/validators.md
|
|
8
|
+
metadata:
|
|
9
|
+
type: core
|
|
10
|
+
library: trpc
|
|
11
|
+
library_version: 11.23.0
|
|
16
12
|
---
|
|
17
13
|
|
|
18
|
-
|
|
14
|
+
<!-- Generated from www/v11/docs/server/validators.md by www/build/skills.ts. Edit the docs instead. -->
|
|
19
15
|
|
|
20
|
-
|
|
16
|
+
# Input & Output Validators
|
|
21
17
|
|
|
22
|
-
|
|
23
|
-
// server/trpc.ts
|
|
24
|
-
import { initTRPC } from '@trpc/server';
|
|
18
|
+
From https://trpc.io/docs/server/validators
|
|
25
19
|
|
|
26
|
-
|
|
20
|
+
tRPC procedures may define validation logic for their input and/or output, and validators are also used to infer the types of inputs and outputs (using the [Standard Schema](https://standardschema.dev) interface if available, or custom interfaces for supported validators if not). We have first class support for many popular validators, and you can [integrate validators](https://trpc.io/docs/server/validators#contributing-your-own-validator-library) which we don't directly support.
|
|
27
21
|
|
|
28
|
-
|
|
29
|
-
export const publicProcedure = t.procedure;
|
|
30
|
-
```
|
|
22
|
+
## Input Validators
|
|
31
23
|
|
|
32
|
-
|
|
33
|
-
// server/appRouter.ts
|
|
34
|
-
import { z } from 'zod';
|
|
35
|
-
import { publicProcedure, router } from './trpc';
|
|
24
|
+
By defining an input validator, tRPC can check that a procedure call is correct and return a validation error if not.
|
|
36
25
|
|
|
37
|
-
|
|
26
|
+
To set up an input validator, use the `procedure.input()` method:
|
|
27
|
+
|
|
28
|
+
```ts no-check
|
|
29
|
+
export const appRouter = t.router({
|
|
38
30
|
hello: publicProcedure
|
|
39
|
-
.input(
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
31
|
+
.input(
|
|
32
|
+
z.object({
|
|
33
|
+
name: z.string(),
|
|
34
|
+
}),
|
|
35
|
+
)
|
|
36
|
+
.query((opts) => {
|
|
37
|
+
const name = opts.input.name;
|
|
38
|
+
return {
|
|
39
|
+
greeting: `Hello ${opts.input.name}`,
|
|
40
|
+
};
|
|
43
41
|
}),
|
|
44
42
|
});
|
|
45
|
-
|
|
46
|
-
export type AppRouter = typeof appRouter;
|
|
47
43
|
```
|
|
48
44
|
|
|
49
|
-
|
|
45
|
+
If the input fails validation, the procedure doesn't run and the call fails with a `BAD_REQUEST` error whose `cause` is the error your validator threw. To send structured validation errors (e.g. Zod's field errors) to the client, use [error formatting](https://trpc.io/docs/server/error-formatting).
|
|
50
46
|
|
|
51
|
-
### Input
|
|
47
|
+
### Input Merging
|
|
52
48
|
|
|
53
|
-
|
|
54
|
-
import { z } from 'zod';
|
|
55
|
-
import { publicProcedure, router } from './trpc';
|
|
56
|
-
|
|
57
|
-
export const appRouter = router({
|
|
58
|
-
userById: publicProcedure.input(z.string()).query(({ input }) => {
|
|
59
|
-
return { id: input, name: 'Katt' };
|
|
60
|
-
}),
|
|
61
|
-
userCreate: publicProcedure
|
|
62
|
-
.input(z.object({ name: z.string(), email: z.string().email() }))
|
|
63
|
-
.mutation(({ input }) => {
|
|
64
|
-
return { id: '1', ...input };
|
|
65
|
-
}),
|
|
66
|
-
});
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
### Input chaining to merge object schemas
|
|
70
|
-
|
|
71
|
-
```ts
|
|
72
|
-
import { initTRPC } from '@trpc/server';
|
|
73
|
-
import { z } from 'zod';
|
|
49
|
+
`.input()` can be stacked to build more complex types, which is particularly useful when you want to utilise some common input to a collection of procedures in a [middleware](https://trpc.io/docs/server/middlewares).
|
|
74
50
|
|
|
75
|
-
|
|
51
|
+
Input merging works by spreading object properties together. This means only **object types** can be chained — non-object types (like `z.string()`) cannot be merged. If two chained `.input()` calls define the same property, the later one takes precedence. An optional input (e.g. `z.object({...}).optional()`) can only be chained after another optional input.
|
|
76
52
|
|
|
53
|
+
```ts no-check
|
|
77
54
|
const baseProcedure = t.procedure
|
|
78
55
|
.input(z.object({ townName: z.string() }))
|
|
79
56
|
.use((opts) => {
|
|
80
|
-
|
|
57
|
+
const input = opts.input;
|
|
58
|
+
|
|
59
|
+
console.log(`Handling request with user from: ${input.townName}`);
|
|
60
|
+
|
|
81
61
|
return opts.next();
|
|
82
62
|
});
|
|
83
63
|
|
|
84
64
|
export const appRouter = t.router({
|
|
85
65
|
hello: baseProcedure
|
|
86
|
-
.input(
|
|
87
|
-
|
|
88
|
-
|
|
66
|
+
.input(
|
|
67
|
+
z.object({
|
|
68
|
+
name: z.string(),
|
|
69
|
+
}),
|
|
70
|
+
)
|
|
71
|
+
.query((opts) => {
|
|
72
|
+
const input = opts.input;
|
|
73
|
+
return {
|
|
74
|
+
greeting: `Hello ${input.name}, my friend from ${input.townName}`,
|
|
75
|
+
};
|
|
89
76
|
}),
|
|
90
77
|
});
|
|
91
78
|
```
|
|
92
79
|
|
|
93
|
-
|
|
80
|
+
## Output Validators
|
|
94
81
|
|
|
95
|
-
|
|
82
|
+
Validating outputs is not always as important as defining inputs, since tRPC gives you automatic type-safety by inferring the return type of your procedures. Some reasons to define an output validator include:
|
|
96
83
|
|
|
97
|
-
|
|
84
|
+
- Checking that data returned from untrusted sources is correct
|
|
85
|
+
- Ensure that you are not returning more data to the client than necessary
|
|
86
|
+
|
|
87
|
+
> [!NOTE] Info
|
|
88
|
+
> If output validation fails, the server will respond with an `INTERNAL_SERVER_ERROR`.
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
The client receives the value your output validator returns, so a schema that strips unknown keys (like `z.object()`) also strips them from the response.
|
|
92
|
+
|
|
93
|
+
```ts no-check
|
|
98
94
|
import { z } from 'zod';
|
|
99
|
-
import { publicProcedure, router } from './trpc';
|
|
100
95
|
|
|
101
|
-
export const
|
|
96
|
+
export const t = initTRPC.create();
|
|
97
|
+
const publicProcedure = t.procedure;
|
|
98
|
+
|
|
99
|
+
export const appRouter = t.router({
|
|
102
100
|
hello: publicProcedure
|
|
103
|
-
.output(
|
|
104
|
-
|
|
105
|
-
|
|
101
|
+
.output(
|
|
102
|
+
z.object({
|
|
103
|
+
greeting: z.string(),
|
|
104
|
+
}),
|
|
105
|
+
)
|
|
106
|
+
.query((opts) => {
|
|
107
|
+
return {
|
|
108
|
+
greeting: 'hello world',
|
|
109
|
+
};
|
|
106
110
|
}),
|
|
107
111
|
});
|
|
108
112
|
```
|
|
109
113
|
|
|
110
|
-
Output validation
|
|
114
|
+
### Output validation of subscriptions
|
|
111
115
|
|
|
112
|
-
|
|
116
|
+
Since subscriptions are async iterators, you can use the same validation techniques as above.
|
|
113
117
|
|
|
114
|
-
|
|
118
|
+
Have a look at the [subscriptions guide](https://trpc.io/docs/server/subscriptions#output-validation) for more information.
|
|
119
|
+
|
|
120
|
+
## The most basic validator: a function
|
|
121
|
+
|
|
122
|
+
You can define a validator without any 3rd party dependencies, with a function.
|
|
123
|
+
|
|
124
|
+
> [!NOTE] Info
|
|
125
|
+
> We don't recommend making a custom validator unless you have a specific need, but it's important to understand that there's no magic here - it's _just typescript_!
|
|
126
|
+
>
|
|
127
|
+
> In most cases we recommend you use a [validation library](https://trpc.io/docs/server/validators#library-integrations)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
```ts no-check
|
|
115
131
|
import { initTRPC } from '@trpc/server';
|
|
116
132
|
|
|
117
|
-
const t = initTRPC.create();
|
|
133
|
+
export const t = initTRPC.create();
|
|
134
|
+
|
|
135
|
+
const publicProcedure = t.procedure;
|
|
118
136
|
|
|
119
137
|
export const appRouter = t.router({
|
|
120
|
-
hello:
|
|
138
|
+
hello: publicProcedure
|
|
121
139
|
.input((value): string => {
|
|
122
|
-
if (typeof value === 'string')
|
|
140
|
+
if (typeof value === 'string') {
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
123
143
|
throw new Error('Input is not a string');
|
|
124
144
|
})
|
|
125
145
|
.output((value): string => {
|
|
126
|
-
if (typeof value === 'string')
|
|
146
|
+
if (typeof value === 'string') {
|
|
147
|
+
return value;
|
|
148
|
+
}
|
|
127
149
|
throw new Error('Output is not a string');
|
|
128
150
|
})
|
|
129
|
-
.query((
|
|
151
|
+
.query((opts) => {
|
|
152
|
+
const { input } = opts;
|
|
130
153
|
return `hello ${input}`;
|
|
131
154
|
}),
|
|
132
155
|
});
|
|
156
|
+
|
|
157
|
+
export type AppRouter = typeof appRouter;
|
|
133
158
|
```
|
|
134
159
|
|
|
135
|
-
##
|
|
160
|
+
## Library integrations
|
|
136
161
|
|
|
137
|
-
|
|
162
|
+
tRPC works out of the box with a number of popular validation and parsing libraries, including any library conforming to [Standard Schema](https://standardschema.dev). The below are some examples of usage with validators which we officially maintain support for.
|
|
138
163
|
|
|
139
|
-
|
|
164
|
+
### With [Zod](https://github.com/colinhacks/zod)
|
|
140
165
|
|
|
141
|
-
|
|
166
|
+
Zod is our default recommendation, it has a strong ecosystem which makes it a great choice to use in multiple parts of your codebase. If you have no opinion of your own and want a powerful library which won't limit future needs, Zod is a great choice.
|
|
167
|
+
|
|
168
|
+
```ts no-check
|
|
169
|
+
import { initTRPC } from '@trpc/server';
|
|
142
170
|
import { z } from 'zod';
|
|
143
|
-
import { publicProcedure } from './trpc';
|
|
144
171
|
|
|
145
|
-
const
|
|
172
|
+
export const t = initTRPC.create();
|
|
173
|
+
|
|
174
|
+
const publicProcedure = t.procedure;
|
|
175
|
+
|
|
176
|
+
export const appRouter = t.router({
|
|
177
|
+
hello: publicProcedure
|
|
178
|
+
.input(
|
|
179
|
+
z.object({
|
|
180
|
+
name: z.string(),
|
|
181
|
+
}),
|
|
182
|
+
)
|
|
183
|
+
.output(
|
|
184
|
+
z.object({
|
|
185
|
+
greeting: z.string(),
|
|
186
|
+
}),
|
|
187
|
+
)
|
|
188
|
+
.query(({ input }) => {
|
|
189
|
+
return {
|
|
190
|
+
greeting: `hello ${input.name}`,
|
|
191
|
+
};
|
|
192
|
+
}),
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
export type AppRouter = typeof appRouter;
|
|
146
196
|
```
|
|
147
197
|
|
|
148
|
-
|
|
198
|
+
### With [Yup](https://github.com/jquense/yup)
|
|
149
199
|
|
|
150
|
-
```ts
|
|
151
|
-
import {
|
|
152
|
-
import
|
|
200
|
+
```ts no-check
|
|
201
|
+
import { initTRPC } from '@trpc/server';
|
|
202
|
+
import * as yup from 'yup';
|
|
203
|
+
|
|
204
|
+
export const t = initTRPC.create();
|
|
205
|
+
|
|
206
|
+
const publicProcedure = t.procedure;
|
|
207
|
+
|
|
208
|
+
export const appRouter = t.router({
|
|
209
|
+
hello: publicProcedure
|
|
210
|
+
.input(
|
|
211
|
+
yup.object({
|
|
212
|
+
name: yup.string().required(),
|
|
213
|
+
}),
|
|
214
|
+
)
|
|
215
|
+
.output(
|
|
216
|
+
yup.object({
|
|
217
|
+
greeting: yup.string().required(),
|
|
218
|
+
}),
|
|
219
|
+
)
|
|
220
|
+
.query(({ input }) => {
|
|
221
|
+
return {
|
|
222
|
+
greeting: `hello ${input.name}`,
|
|
223
|
+
};
|
|
224
|
+
}),
|
|
225
|
+
});
|
|
153
226
|
|
|
154
|
-
|
|
155
|
-
.input(z.object({ name: z.string() }))
|
|
156
|
-
.input(z.object({ age: z.number() }));
|
|
227
|
+
export type AppRouter = typeof appRouter;
|
|
157
228
|
```
|
|
158
229
|
|
|
159
|
-
|
|
230
|
+
### With [Superstruct](https://github.com/ianstormtaylor/superstruct)
|
|
231
|
+
|
|
232
|
+
```ts no-check
|
|
233
|
+
import { initTRPC } from '@trpc/server';
|
|
234
|
+
import { object, string } from 'superstruct';
|
|
235
|
+
|
|
236
|
+
export const t = initTRPC.create();
|
|
237
|
+
|
|
238
|
+
const publicProcedure = t.procedure;
|
|
239
|
+
|
|
240
|
+
export const appRouter = t.router({
|
|
241
|
+
hello: publicProcedure
|
|
242
|
+
.input(object({ name: string() }))
|
|
243
|
+
.output(object({ greeting: string() }))
|
|
244
|
+
.query(({ input }) => {
|
|
245
|
+
return {
|
|
246
|
+
greeting: `hello ${input.name}`,
|
|
247
|
+
};
|
|
248
|
+
}),
|
|
249
|
+
});
|
|
160
250
|
|
|
161
|
-
|
|
251
|
+
export type AppRouter = typeof appRouter;
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### With [scale-ts](https://github.com/paritytech/scale-ts)
|
|
255
|
+
|
|
256
|
+
```ts no-check
|
|
257
|
+
import { initTRPC } from '@trpc/server';
|
|
258
|
+
import * as $ from 'scale-codec';
|
|
259
|
+
|
|
260
|
+
export const t = initTRPC.create();
|
|
261
|
+
|
|
262
|
+
const publicProcedure = t.procedure;
|
|
263
|
+
|
|
264
|
+
export const appRouter = t.router({
|
|
265
|
+
hello: publicProcedure
|
|
266
|
+
.input($.object($.field('name', $.str)))
|
|
267
|
+
.output($.object($.field('greeting', $.str)))
|
|
268
|
+
.query(({ input }) => {
|
|
269
|
+
return {
|
|
270
|
+
greeting: `hello ${input.name}`,
|
|
271
|
+
};
|
|
272
|
+
}),
|
|
273
|
+
});
|
|
162
274
|
|
|
163
|
-
|
|
275
|
+
export type AppRouter = typeof appRouter;
|
|
276
|
+
```
|
|
164
277
|
|
|
165
|
-
|
|
278
|
+
### With [Typia](https://typia.io/docs/utilization/trpc/)
|
|
166
279
|
|
|
167
280
|
```ts
|
|
168
|
-
import {
|
|
169
|
-
import
|
|
281
|
+
import { initTRPC } from '@trpc/server';
|
|
282
|
+
import typia from 'typia';
|
|
283
|
+
import { v4 } from 'uuid';
|
|
284
|
+
import { IBbsArticle } from '../structures/IBbsArticle';
|
|
285
|
+
|
|
286
|
+
const t = initTRPC.create();
|
|
287
|
+
|
|
288
|
+
const publicProcedure = t.procedure;
|
|
170
289
|
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
|
|
290
|
+
export const appRouter = t.router({
|
|
291
|
+
store: publicProcedure
|
|
292
|
+
.input(typia.createAssert<IBbsArticle.IStore>())
|
|
293
|
+
.output(typia.createAssert<IBbsArticle>())
|
|
294
|
+
.query(({ input }) => {
|
|
295
|
+
return {
|
|
296
|
+
id: v4(),
|
|
297
|
+
writer: input.writer,
|
|
298
|
+
title: input.title,
|
|
299
|
+
body: input.body,
|
|
300
|
+
created_at: new Date().toString(),
|
|
301
|
+
};
|
|
302
|
+
}),
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
export type AppRouter = typeof appRouter;
|
|
174
306
|
```
|
|
175
307
|
|
|
176
|
-
|
|
308
|
+
### With [ArkType](https://github.com/arktypeio/arktype#trpc)
|
|
177
309
|
|
|
178
310
|
```ts
|
|
179
|
-
import {
|
|
180
|
-
import {
|
|
311
|
+
import { initTRPC } from '@trpc/server';
|
|
312
|
+
import { type } from 'arktype';
|
|
313
|
+
|
|
314
|
+
export const t = initTRPC.create();
|
|
315
|
+
|
|
316
|
+
const publicProcedure = t.procedure;
|
|
317
|
+
|
|
318
|
+
export const appRouter = t.router({
|
|
319
|
+
hello: publicProcedure.input(type({ name: 'string' })).query((opts) => {
|
|
320
|
+
return {
|
|
321
|
+
greeting: `hello ${opts.input.name}`,
|
|
322
|
+
};
|
|
323
|
+
}),
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
export type AppRouter = typeof appRouter;
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### With [effect](https://effect.website/docs/schema/introduction/)
|
|
330
|
+
|
|
331
|
+
```ts no-check
|
|
332
|
+
import { initTRPC } from '@trpc/server';
|
|
333
|
+
import { Schema } from 'effect';
|
|
334
|
+
|
|
335
|
+
export const t = initTRPC.create();
|
|
336
|
+
|
|
337
|
+
const publicProcedure = t.procedure;
|
|
338
|
+
|
|
339
|
+
export const appRouter = t.router({
|
|
340
|
+
hello: publicProcedure
|
|
341
|
+
.input(Schema.standardSchemaV1(Schema.Struct({ name: Schema.String })))
|
|
342
|
+
.output(Schema.standardSchemaV1(Schema.Struct({ greeting: Schema.String })))
|
|
343
|
+
.query(({ input }) => {
|
|
344
|
+
return {
|
|
345
|
+
greeting: `hello ${input.name}`,
|
|
346
|
+
};
|
|
347
|
+
}),
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
export type AppRouter = typeof appRouter;
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
### With [Valibot](https://github.com/fabian-hiller/valibot)
|
|
355
|
+
|
|
356
|
+
```ts no-check
|
|
357
|
+
import { initTRPC } from '@trpc/server';
|
|
358
|
+
import * as v from 'valibot';
|
|
359
|
+
|
|
360
|
+
export const t = initTRPC.create();
|
|
361
|
+
|
|
362
|
+
const publicProcedure = t.procedure;
|
|
363
|
+
|
|
364
|
+
export const appRouter = t.router({
|
|
365
|
+
hello: publicProcedure
|
|
366
|
+
.input(v.object({ name: v.string() }))
|
|
367
|
+
.output(v.object({ greeting: v.string() }))
|
|
368
|
+
.query(({ input }) => {
|
|
369
|
+
return {
|
|
370
|
+
greeting: `hello ${input.name}`,
|
|
371
|
+
};
|
|
372
|
+
}),
|
|
373
|
+
});
|
|
181
374
|
|
|
182
|
-
|
|
183
|
-
.output(z.object({ id: z.string() }))
|
|
184
|
-
.query(() => ({ id: '123' }));
|
|
375
|
+
export type AppRouter = typeof appRouter;
|
|
185
376
|
```
|
|
186
377
|
|
|
187
|
-
|
|
378
|
+
### With [Sury](https://github.com/DZakh/sury)
|
|
188
379
|
|
|
189
|
-
|
|
380
|
+
```ts no-check
|
|
381
|
+
import { initTRPC } from '@trpc/server';
|
|
382
|
+
import * as S from 'sury';
|
|
190
383
|
|
|
191
|
-
|
|
384
|
+
export const t = initTRPC.create();
|
|
192
385
|
|
|
193
|
-
|
|
386
|
+
const publicProcedure = t.procedure;
|
|
387
|
+
|
|
388
|
+
export const appRouter = t.router({
|
|
389
|
+
hello: publicProcedure
|
|
390
|
+
.input(S.schema({ name: S.string }))
|
|
391
|
+
.output(S.schema({ greeting: S.string }))
|
|
392
|
+
.query(({ input }) => {
|
|
393
|
+
return {
|
|
394
|
+
greeting: `hello ${input.name}`,
|
|
395
|
+
};
|
|
396
|
+
}),
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
export type AppRouter = typeof appRouter;
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
### With [@robolex/sure](https://github.com/robolex-app/public_ts)
|
|
403
|
+
|
|
404
|
+
You're able to define your own Error types and error throwing function if necessary.
|
|
405
|
+
As a convenience `@robolex/sure` provides [sure/src/err.ts](https://github.com/robolex-app/public_ts/blob/main/packages/sure/src/err.ts):
|
|
194
406
|
|
|
195
407
|
```ts
|
|
196
|
-
|
|
197
|
-
|
|
408
|
+
// sure/src/err.ts
|
|
409
|
+
export const err = (schema: any) => (input: any) => {
|
|
410
|
+
const [good, result] = schema(input);
|
|
411
|
+
if (good) return result;
|
|
412
|
+
throw result;
|
|
413
|
+
};
|
|
414
|
+
```
|
|
198
415
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
416
|
+
```ts
|
|
417
|
+
import { err, object, string } from '@robolex/sure';
|
|
418
|
+
import { initTRPC } from '@trpc/server';
|
|
419
|
+
|
|
420
|
+
export const t = initTRPC.create();
|
|
421
|
+
|
|
422
|
+
const publicProcedure = t.procedure;
|
|
423
|
+
|
|
424
|
+
export const appRouter = t.router({
|
|
425
|
+
hello: publicProcedure
|
|
426
|
+
.input(
|
|
427
|
+
err(
|
|
428
|
+
object({
|
|
429
|
+
name: string,
|
|
430
|
+
}),
|
|
431
|
+
),
|
|
432
|
+
)
|
|
433
|
+
.output(
|
|
434
|
+
err(
|
|
435
|
+
object({
|
|
436
|
+
greeting: string,
|
|
437
|
+
}),
|
|
438
|
+
),
|
|
439
|
+
)
|
|
440
|
+
.query(({ input }) => {
|
|
441
|
+
// ^?
|
|
442
|
+
return {
|
|
443
|
+
greeting: `hello ${input.name}`,
|
|
444
|
+
};
|
|
445
|
+
}),
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
export type AppRouter = typeof appRouter;
|
|
204
449
|
```
|
|
205
450
|
|
|
206
|
-
|
|
451
|
+
### With [TypeBox](https://github.com/sinclairzx81/typebox)
|
|
207
452
|
|
|
208
453
|
```ts
|
|
209
|
-
import {
|
|
210
|
-
import {
|
|
454
|
+
import { Type } from '@sinclair/typebox';
|
|
455
|
+
import { initTRPC } from '@trpc/server';
|
|
456
|
+
import { wrap } from '@typeschema/typebox';
|
|
211
457
|
|
|
212
|
-
const
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
458
|
+
export const t = initTRPC.create();
|
|
459
|
+
|
|
460
|
+
const publicProcedure = t.procedure;
|
|
461
|
+
|
|
462
|
+
export const appRouter = t.router({
|
|
463
|
+
hello: publicProcedure
|
|
464
|
+
.input(wrap(Type.Object({ name: Type.String() })))
|
|
465
|
+
.output(wrap(Type.Object({ greeting: Type.String() })))
|
|
466
|
+
.query(({ input }) => {
|
|
467
|
+
return {
|
|
468
|
+
greeting: `hello ${input.name}`,
|
|
469
|
+
};
|
|
470
|
+
}),
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
export type AppRouter = typeof appRouter;
|
|
217
474
|
```
|
|
218
475
|
|
|
219
|
-
|
|
476
|
+
## Contributing your own Validator Library
|
|
477
|
+
|
|
478
|
+
If you work on a validator library which supports tRPC usage, please feel free to open a PR for this page with equivalent usage to the other examples here, and a link to your docs.
|
|
220
479
|
|
|
221
|
-
|
|
480
|
+
Integration with tRPC in most cases is as simple as meeting one of several existing type interfaces. Conforming to [Standard Schema](https://standardschema.dev) is recommended, but in some cases we may accept a PR to add a new supported interface. Feel free to open an issue for discussion. You can check the existing supported interfaces and functions for parsing/validation [in code](https://github.com/trpc/trpc/blob/main/packages/server/src/unstable-core-do-not-import/parser.ts).
|
|
222
481
|
|
|
223
|
-
##
|
|
482
|
+
## Related skills
|
|
224
483
|
|
|
225
|
-
- `server-setup`
|
|
226
|
-
- `error-handling` -- how validation errors surface as BAD_REQUEST
|
|
227
|
-
- `error-handling` -- errorFormatter to expose Zod field errors
|
|
228
|
-
- `middlewares` -- use input chaining with middleware base procedures
|
|
484
|
+
- `server-setup` (https://trpc.io/docs/server/overview): `npx @tanstack/intent@latest load @trpc/server#server-setup`
|
|
@@ -11,7 +11,8 @@ import { splitSetCookieString } from '../../vendor/cookie-es/set-cookie/split';
|
|
|
11
11
|
export type LambdaEvent = APIGatewayProxyEvent | APIGatewayProxyEventV2;
|
|
12
12
|
|
|
13
13
|
export type APIGatewayResult =
|
|
14
|
-
|
|
14
|
+
| APIGatewayProxyResult
|
|
15
|
+
| APIGatewayProxyStructuredResultV2;
|
|
15
16
|
|
|
16
17
|
function determinePayloadFormat(event: LambdaEvent): string {
|
|
17
18
|
// https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
|
|
@@ -64,7 +65,7 @@ const v1Processor: Processor<APIGatewayProxyEvent> = {
|
|
|
64
65
|
}
|
|
65
66
|
const matches = event.resource.matchAll(/\{(.*?)\}/g);
|
|
66
67
|
for (const match of matches) {
|
|
67
|
-
//
|
|
68
|
+
// oxlint-disable-next-line typescript/no-non-null-assertion
|
|
68
69
|
const group = match[1]!;
|
|
69
70
|
if (group.includes('+') && event.pathParameters) {
|
|
70
71
|
return event.pathParameters[group.replace('+', '')] ?? '';
|
|
@@ -152,7 +153,7 @@ const v2Processor: Processor<APIGatewayProxyEventV2> = {
|
|
|
152
153
|
getTRPCPath: (event) => {
|
|
153
154
|
const matches = event.routeKey.matchAll(/\{(.*?)\}/g);
|
|
154
155
|
for (const match of matches) {
|
|
155
|
-
//
|
|
156
|
+
// oxlint-disable-next-line typescript/no-non-null-assertion
|
|
156
157
|
const group = match[1]!;
|
|
157
158
|
if (group.includes('+') && event.pathParameters) {
|
|
158
159
|
return event.pathParameters[group.replace('+', '')] ?? '';
|
|
@@ -47,7 +47,8 @@ export type AWSLambdaCreateContextFn<
|
|
|
47
47
|
context,
|
|
48
48
|
info,
|
|
49
49
|
}: CreateAWSLambdaContextOptions<TEvent>) =>
|
|
50
|
-
|
|
50
|
+
| inferRouterContext<TRouter>
|
|
51
|
+
| Promise<inferRouterContext<TRouter>>;
|
|
51
52
|
|
|
52
53
|
export function awsLambdaRequestHandler<
|
|
53
54
|
TRouter extends AnyRouter,
|
package/src/adapters/express.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import type * as express from 'express';
|
|
11
11
|
import type { AnyRouter } from '../@trpc/server';
|
|
12
|
-
//
|
|
12
|
+
// oxlint-disable-next-line no-restricted-imports
|
|
13
13
|
import { run } from '../unstable-core-do-not-import';
|
|
14
14
|
import type {
|
|
15
15
|
NodeHTTPCreateContextFnOptions,
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import type { CreateContextCallback } from '../../@trpc/server';
|
|
2
2
|
import { getTRPCErrorFromUnknown, TRPCError } from '../../@trpc/server';
|
|
3
|
-
//
|
|
3
|
+
// oxlint-disable-next-line no-restricted-imports
|
|
4
4
|
import { formDataToObject } from '../../unstable-core-do-not-import';
|
|
5
5
|
// FIXME: fix lint rule, this is ok
|
|
6
|
-
//
|
|
6
|
+
// oxlint-disable-next-line no-restricted-imports
|
|
7
7
|
import type { ErrorHandlerOptions } from '../../unstable-core-do-not-import/procedure';
|
|
8
8
|
// FIXME: fix lint rule, this is ok
|
|
9
|
-
//
|
|
9
|
+
// oxlint-disable-next-line no-restricted-imports
|
|
10
10
|
import type { CallerOverride } from '../../unstable-core-do-not-import/procedureBuilder';
|
|
11
11
|
// FIXME: fix lint rule, this is ok
|
|
12
|
-
//
|
|
12
|
+
// oxlint-disable-next-line no-restricted-imports
|
|
13
13
|
import type {
|
|
14
14
|
MaybePromise,
|
|
15
15
|
Simplify,
|