@trpc/server 11.22.1 → 11.22.2-canary.11.4427f0bea
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.d.cts +1 -1
- package/dist/adapters/aws-lambda/index.d.mts +1 -1
- package/dist/adapters/express.d.cts +2 -2
- package/dist/adapters/express.d.mts +2 -2
- package/dist/adapters/fastify/index.d.cts +3 -3
- package/dist/adapters/fastify/index.d.mts +3 -3
- package/dist/adapters/fetch/index.d.cts +1 -1
- package/dist/adapters/fetch/index.d.mts +1 -1
- package/dist/adapters/next-app-dir.d.cts +2 -2
- package/dist/adapters/next-app-dir.d.mts +2 -2
- package/dist/adapters/next-app-dir.mjs.map +1 -1
- package/dist/adapters/next.d.cts +2 -2
- package/dist/adapters/next.d.mts +2 -2
- package/dist/adapters/node-http/index.d.cts +1 -1
- package/dist/adapters/node-http/index.d.mts +1 -1
- package/dist/adapters/standalone.d.cts +2 -2
- package/dist/adapters/standalone.d.mts +2 -2
- package/dist/adapters/ws.d.cts +2 -1
- package/dist/adapters/ws.d.mts +2 -1
- package/dist/http.d.cts +1 -1
- package/dist/http.d.mts +1 -1
- package/dist/{index-D7foMQMh.d.cts → index-CtkOmZzm.d.mts} +2 -2
- package/dist/{index-DPj2-I1U.d.mts.map → index-CtkOmZzm.d.mts.map} +1 -1
- package/dist/{index-DPj2-I1U.d.mts → index-Tpw5OCTF.d.cts} +2 -2
- package/dist/{index-D7foMQMh.d.cts.map → index-Tpw5OCTF.d.cts.map} +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/rpc.d.cts +1 -1
- package/dist/rpc.d.mts +1 -1
- package/dist/shared.d.cts +1 -1
- package/dist/shared.d.mts +1 -1
- package/dist/{unstable-core-do-not-import-BkTO4Ekx.d.cts → unstable-core-do-not-import-B8epiErP.d.cts} +28 -3
- package/dist/unstable-core-do-not-import-B8epiErP.d.cts.map +1 -0
- package/dist/{unstable-core-do-not-import-DElJf3aU.d.mts → unstable-core-do-not-import-Dbo98mkh.d.mts} +28 -3
- package/dist/unstable-core-do-not-import-Dbo98mkh.d.mts.map +1 -0
- package/dist/unstable-core-do-not-import.d.cts +2 -2
- package/dist/unstable-core-do-not-import.d.mts +2 -2
- package/dist/{ws-BR3-CM6m.d.mts → ws-DD1ukLuC.d.cts} +9 -34
- package/dist/ws-DD1ukLuC.d.cts.map +1 -0
- package/dist/ws-DJ2SSChC.mjs.map +1 -1
- package/dist/{ws-CaI8wmsh.d.cts → ws-DT37N43e.d.mts} +9 -34
- package/dist/ws-DT37N43e.d.mts.map +1 -0
- package/package.json +24 -12
- package/src/adapters/next-app-dir/notFound.ts +1 -1
- package/src/adapters/next-app-dir/redirect.ts +1 -1
- package/src/adapters/ws.ts +5 -5
- package/src/unstable-core-do-not-import/types.ts +10 -4
- package/src/unstable-core-do-not-import.ts +1 -0
- package/bin/intent.js +0 -20
- package/dist/unstable-core-do-not-import-BkTO4Ekx.d.cts.map +0 -1
- package/dist/unstable-core-do-not-import-DElJf3aU.d.mts.map +0 -1
- package/dist/ws-BR3-CM6m.d.mts.map +0 -1
- package/dist/ws-CaI8wmsh.d.cts.map +0 -1
- package/skills/adapter-aws-lambda/SKILL.md +0 -203
- package/skills/adapter-express/SKILL.md +0 -176
- package/skills/adapter-fastify/SKILL.md +0 -221
- package/skills/adapter-fetch/SKILL.md +0 -199
- package/skills/adapter-standalone/SKILL.md +0 -198
- package/skills/auth/SKILL.md +0 -342
- package/skills/caching/SKILL.md +0 -205
- package/skills/error-handling/SKILL.md +0 -253
- package/skills/middlewares/SKILL.md +0 -242
- package/skills/non-json-content-types/SKILL.md +0 -265
- package/skills/server-setup/SKILL.md +0 -378
- package/skills/server-side-calls/SKILL.md +0 -249
- package/skills/service-oriented-architecture/SKILL.md +0 -247
- package/skills/subscriptions/SKILL.md +0 -406
- package/skills/trpc-router/SKILL.md +0 -151
- package/skills/validators/SKILL.md +0 -228
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: adapter-fastify
|
|
3
|
-
description: >
|
|
4
|
-
Mount tRPC as a Fastify plugin with fastifyTRPCPlugin from
|
|
5
|
-
@trpc/server/adapters/fastify. Configure prefix, trpcOptions (router,
|
|
6
|
-
createContext, onError). Enable WebSocket subscriptions with useWSS and
|
|
7
|
-
@fastify/websocket. Set routerOptions.maxParamLength for batch requests.
|
|
8
|
-
Requires Fastify v5+. FastifyTRPCPluginOptions for type-safe onError.
|
|
9
|
-
CreateFastifyContextOptions provides req, res.
|
|
10
|
-
type: core
|
|
11
|
-
library: trpc
|
|
12
|
-
library_version: '11.16.0'
|
|
13
|
-
requires:
|
|
14
|
-
- server-setup
|
|
15
|
-
sources:
|
|
16
|
-
- www/docs/server/adapters/fastify.md
|
|
17
|
-
- examples/fastify-server/
|
|
18
|
-
---
|
|
19
|
-
|
|
20
|
-
# tRPC — Adapter: Fastify
|
|
21
|
-
|
|
22
|
-
## Setup
|
|
23
|
-
|
|
24
|
-
```ts
|
|
25
|
-
// server.ts
|
|
26
|
-
import { initTRPC } from '@trpc/server';
|
|
27
|
-
import {
|
|
28
|
-
fastifyTRPCPlugin,
|
|
29
|
-
FastifyTRPCPluginOptions,
|
|
30
|
-
} from '@trpc/server/adapters/fastify';
|
|
31
|
-
import type { CreateFastifyContextOptions } from '@trpc/server/adapters/fastify';
|
|
32
|
-
import fastify from 'fastify';
|
|
33
|
-
import { z } from 'zod';
|
|
34
|
-
|
|
35
|
-
function createContext({ req, res }: CreateFastifyContextOptions) {
|
|
36
|
-
return { req, res };
|
|
37
|
-
}
|
|
38
|
-
type Context = Awaited<ReturnType<typeof createContext>>;
|
|
39
|
-
|
|
40
|
-
const t = initTRPC.context<Context>().create();
|
|
41
|
-
|
|
42
|
-
const appRouter = t.router({
|
|
43
|
-
greet: t.procedure
|
|
44
|
-
.input(z.object({ name: z.string() }))
|
|
45
|
-
.query(({ input }) => ({ greeting: `Hello, ${input.name}!` })),
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
export type AppRouter = typeof appRouter;
|
|
49
|
-
|
|
50
|
-
const server = fastify({
|
|
51
|
-
routerOptions: {
|
|
52
|
-
maxParamLength: 5000,
|
|
53
|
-
},
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
server.register(fastifyTRPCPlugin, {
|
|
57
|
-
prefix: '/trpc',
|
|
58
|
-
trpcOptions: {
|
|
59
|
-
router: appRouter,
|
|
60
|
-
createContext,
|
|
61
|
-
onError({ path, error }) {
|
|
62
|
-
console.error(`Error in tRPC handler on path '${path}':`, error);
|
|
63
|
-
},
|
|
64
|
-
} satisfies FastifyTRPCPluginOptions<AppRouter>['trpcOptions'],
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
(async () => {
|
|
68
|
-
try {
|
|
69
|
-
await server.listen({ port: 3000 });
|
|
70
|
-
console.log('Listening on http://localhost:3000');
|
|
71
|
-
} catch (err) {
|
|
72
|
-
server.log.error(err);
|
|
73
|
-
process.exit(1);
|
|
74
|
-
}
|
|
75
|
-
})();
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
## Core Patterns
|
|
79
|
-
|
|
80
|
-
### WebSocket subscriptions with @fastify/websocket
|
|
81
|
-
|
|
82
|
-
```ts
|
|
83
|
-
import ws from '@fastify/websocket';
|
|
84
|
-
import {
|
|
85
|
-
fastifyTRPCPlugin,
|
|
86
|
-
FastifyTRPCPluginOptions,
|
|
87
|
-
} from '@trpc/server/adapters/fastify';
|
|
88
|
-
import fastify from 'fastify';
|
|
89
|
-
import { createContext } from './context';
|
|
90
|
-
import { appRouter, type AppRouter } from './router';
|
|
91
|
-
|
|
92
|
-
const server = fastify({
|
|
93
|
-
routerOptions: { maxParamLength: 5000 },
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
// Register @fastify/websocket BEFORE the tRPC plugin
|
|
97
|
-
server.register(ws);
|
|
98
|
-
|
|
99
|
-
server.register(fastifyTRPCPlugin, {
|
|
100
|
-
prefix: '/trpc',
|
|
101
|
-
useWSS: true,
|
|
102
|
-
trpcOptions: {
|
|
103
|
-
router: appRouter,
|
|
104
|
-
createContext,
|
|
105
|
-
keepAlive: {
|
|
106
|
-
enabled: true,
|
|
107
|
-
pingMs: 30000,
|
|
108
|
-
pongWaitMs: 5000,
|
|
109
|
-
},
|
|
110
|
-
} satisfies FastifyTRPCPluginOptions<AppRouter>['trpcOptions'],
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
server.listen({ port: 3000 });
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
Install: `npm install @fastify/websocket` (minimum version 3.11.0)
|
|
117
|
-
|
|
118
|
-
### Type-safe onError with satisfies
|
|
119
|
-
|
|
120
|
-
```ts
|
|
121
|
-
server.register(fastifyTRPCPlugin, {
|
|
122
|
-
prefix: '/trpc',
|
|
123
|
-
trpcOptions: {
|
|
124
|
-
router: appRouter,
|
|
125
|
-
createContext,
|
|
126
|
-
onError({ path, error, type, input }) {
|
|
127
|
-
console.error(`[${type}] ${path}:`, error.message);
|
|
128
|
-
},
|
|
129
|
-
} satisfies FastifyTRPCPluginOptions<AppRouter>['trpcOptions'],
|
|
130
|
-
});
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
Due to Fastify plugin type inference limitations, use `satisfies FastifyTRPCPluginOptions<AppRouter>['trpcOptions']` to get correct types on `onError` and other callbacks.
|
|
134
|
-
|
|
135
|
-
### Limiting batch size with maxBatchSize
|
|
136
|
-
|
|
137
|
-
```ts
|
|
138
|
-
server.register(fastifyTRPCPlugin, {
|
|
139
|
-
prefix: '/trpc',
|
|
140
|
-
trpcOptions: {
|
|
141
|
-
router: appRouter,
|
|
142
|
-
createContext,
|
|
143
|
-
maxBatchSize: 10,
|
|
144
|
-
} satisfies FastifyTRPCPluginOptions<AppRouter>['trpcOptions'],
|
|
145
|
-
});
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
Requests batching more than `maxBatchSize` operations are rejected with a `400 Bad Request` error. Set `maxItems` on your client's `httpBatchLink` to the same value to avoid exceeding the limit.
|
|
149
|
-
|
|
150
|
-
## Common Mistakes
|
|
151
|
-
|
|
152
|
-
### HIGH Registering @fastify/websocket after tRPC plugin
|
|
153
|
-
|
|
154
|
-
Wrong:
|
|
155
|
-
|
|
156
|
-
```ts
|
|
157
|
-
server.register(fastifyTRPCPlugin, {
|
|
158
|
-
useWSS: true,
|
|
159
|
-
trpcOptions: { router: appRouter, createContext },
|
|
160
|
-
});
|
|
161
|
-
server.register(ws); // too late!
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
Correct:
|
|
165
|
-
|
|
166
|
-
```ts
|
|
167
|
-
server.register(ws); // register FIRST
|
|
168
|
-
server.register(fastifyTRPCPlugin, {
|
|
169
|
-
useWSS: true,
|
|
170
|
-
trpcOptions: { router: appRouter, createContext },
|
|
171
|
-
});
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
The WebSocket plugin must be registered before the tRPC plugin. Reverse order causes WebSocket routes to not be recognized.
|
|
175
|
-
|
|
176
|
-
Source: www/docs/server/adapters/fastify.md
|
|
177
|
-
|
|
178
|
-
### HIGH Missing maxParamLength for batch requests
|
|
179
|
-
|
|
180
|
-
Wrong:
|
|
181
|
-
|
|
182
|
-
```ts
|
|
183
|
-
const server = fastify();
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
Correct:
|
|
187
|
-
|
|
188
|
-
```ts
|
|
189
|
-
const server = fastify({
|
|
190
|
-
routerOptions: { maxParamLength: 5000 },
|
|
191
|
-
});
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
Fastify defaults to `maxParamLength: 100`. Batch requests from `httpBatchLink` encode multiple procedure names in the URL path parameter, which easily exceeds 100 characters and returns a 404.
|
|
195
|
-
|
|
196
|
-
Source: www/docs/server/adapters/fastify.md
|
|
197
|
-
|
|
198
|
-
### CRITICAL Using Fastify v4 with tRPC v11
|
|
199
|
-
|
|
200
|
-
Wrong:
|
|
201
|
-
|
|
202
|
-
```json
|
|
203
|
-
{ "dependencies": { "fastify": "^4.0.0" } }
|
|
204
|
-
```
|
|
205
|
-
|
|
206
|
-
Correct:
|
|
207
|
-
|
|
208
|
-
```json
|
|
209
|
-
{ "dependencies": { "fastify": "^5.0.0" } }
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
tRPC v11 requires Fastify v5+. Fastify v4 may return empty responses without errors due to incompatible response handling.
|
|
213
|
-
|
|
214
|
-
Source: www/docs/server/adapters/fastify.md
|
|
215
|
-
|
|
216
|
-
## See Also
|
|
217
|
-
|
|
218
|
-
- **server-setup** -- `initTRPC.create()`, router/procedure definition, context
|
|
219
|
-
- **subscriptions** -- async generator subscriptions, `tracked()`, `keepAlive`
|
|
220
|
-
- **adapter-standalone** -- simpler adapter when Fastify features are not needed
|
|
221
|
-
- Fastify docs: https://fastify.dev/docs/latest/
|
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: adapter-fetch
|
|
3
|
-
description: >
|
|
4
|
-
Deploy tRPC on WinterCG-compliant edge runtimes with fetchRequestHandler() from
|
|
5
|
-
@trpc/server/adapters/fetch. Supports Cloudflare Workers, Deno Deploy, Vercel
|
|
6
|
-
Edge Runtime, Astro, Remix, SolidStart. FetchCreateContextFnOptions provides
|
|
7
|
-
req (Request) and resHeaders (Headers) for context creation. The endpoint option
|
|
8
|
-
must match the URL path prefix where the handler is mounted.
|
|
9
|
-
type: core
|
|
10
|
-
library: trpc
|
|
11
|
-
library_version: '11.16.0'
|
|
12
|
-
requires:
|
|
13
|
-
- server-setup
|
|
14
|
-
sources:
|
|
15
|
-
- www/docs/server/adapters/fetch.mdx
|
|
16
|
-
- examples/cloudflare-workers/
|
|
17
|
-
- examples/deno-deploy/
|
|
18
|
-
---
|
|
19
|
-
|
|
20
|
-
# tRPC — Adapter: Fetch / Edge Runtimes
|
|
21
|
-
|
|
22
|
-
## Setup
|
|
23
|
-
|
|
24
|
-
```ts
|
|
25
|
-
// Cloudflare Worker example
|
|
26
|
-
import { initTRPC } from '@trpc/server';
|
|
27
|
-
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
|
|
28
|
-
import type { FetchCreateContextFnOptions } from '@trpc/server/adapters/fetch';
|
|
29
|
-
import { z } from 'zod';
|
|
30
|
-
|
|
31
|
-
function createContext({ req, resHeaders }: FetchCreateContextFnOptions) {
|
|
32
|
-
const user = req.headers.get('authorization');
|
|
33
|
-
return { user, resHeaders };
|
|
34
|
-
}
|
|
35
|
-
type Context = Awaited<ReturnType<typeof createContext>>;
|
|
36
|
-
|
|
37
|
-
const t = initTRPC.context<Context>().create();
|
|
38
|
-
|
|
39
|
-
const appRouter = t.router({
|
|
40
|
-
greet: t.procedure
|
|
41
|
-
.input(z.object({ name: z.string() }))
|
|
42
|
-
.query(({ input }) => ({ greeting: `Hello, ${input.name}!` })),
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
export type AppRouter = typeof appRouter;
|
|
46
|
-
|
|
47
|
-
export default {
|
|
48
|
-
async fetch(request: Request): Promise<Response> {
|
|
49
|
-
return fetchRequestHandler({
|
|
50
|
-
endpoint: '/trpc',
|
|
51
|
-
req: request,
|
|
52
|
-
router: appRouter,
|
|
53
|
-
createContext,
|
|
54
|
-
});
|
|
55
|
-
},
|
|
56
|
-
};
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
## Core Patterns
|
|
60
|
-
|
|
61
|
-
### Cloudflare Workers
|
|
62
|
-
|
|
63
|
-
```ts
|
|
64
|
-
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
|
|
65
|
-
import { createContext } from './context';
|
|
66
|
-
import { appRouter } from './router';
|
|
67
|
-
|
|
68
|
-
export default {
|
|
69
|
-
async fetch(request: Request): Promise<Response> {
|
|
70
|
-
return fetchRequestHandler({
|
|
71
|
-
endpoint: '/trpc',
|
|
72
|
-
req: request,
|
|
73
|
-
router: appRouter,
|
|
74
|
-
createContext,
|
|
75
|
-
});
|
|
76
|
-
},
|
|
77
|
-
};
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
### Astro SSR
|
|
81
|
-
|
|
82
|
-
```ts
|
|
83
|
-
// src/pages/trpc/[trpc].ts
|
|
84
|
-
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
|
|
85
|
-
import type { APIRoute } from 'astro';
|
|
86
|
-
import { createContext } from '../../server/context';
|
|
87
|
-
import { appRouter } from '../../server/router';
|
|
88
|
-
|
|
89
|
-
export const ALL: APIRoute = (opts) => {
|
|
90
|
-
return fetchRequestHandler({
|
|
91
|
-
endpoint: '/trpc',
|
|
92
|
-
req: opts.request,
|
|
93
|
-
router: appRouter,
|
|
94
|
-
createContext,
|
|
95
|
-
});
|
|
96
|
-
};
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
### Remix
|
|
100
|
-
|
|
101
|
-
```ts
|
|
102
|
-
// app/routes/trpc.$trpc.ts
|
|
103
|
-
import type { ActionFunctionArgs, LoaderFunctionArgs } from '@remix-run/node';
|
|
104
|
-
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
|
|
105
|
-
import { createContext } from '~/server/context';
|
|
106
|
-
import { appRouter } from '~/server/router';
|
|
107
|
-
|
|
108
|
-
function handleRequest(args: LoaderFunctionArgs | ActionFunctionArgs) {
|
|
109
|
-
return fetchRequestHandler({
|
|
110
|
-
endpoint: '/trpc',
|
|
111
|
-
req: args.request,
|
|
112
|
-
router: appRouter,
|
|
113
|
-
createContext,
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export const loader = async (args: LoaderFunctionArgs) => handleRequest(args);
|
|
118
|
-
export const action = async (args: ActionFunctionArgs) => handleRequest(args);
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
### Deno Deploy
|
|
122
|
-
|
|
123
|
-
```ts
|
|
124
|
-
import { fetchRequestHandler } from 'npm:@trpc/server/adapters/fetch';
|
|
125
|
-
import { createContext } from './context.ts';
|
|
126
|
-
import { appRouter } from './router.ts';
|
|
127
|
-
|
|
128
|
-
Deno.serve((request) => {
|
|
129
|
-
return fetchRequestHandler({
|
|
130
|
-
endpoint: '/trpc',
|
|
131
|
-
req: request,
|
|
132
|
-
router: appRouter,
|
|
133
|
-
createContext,
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
### Limiting batch size with maxBatchSize
|
|
139
|
-
|
|
140
|
-
```ts
|
|
141
|
-
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
|
|
142
|
-
import { createContext } from './context';
|
|
143
|
-
import { appRouter } from './router';
|
|
144
|
-
|
|
145
|
-
export default {
|
|
146
|
-
async fetch(request: Request): Promise<Response> {
|
|
147
|
-
return fetchRequestHandler({
|
|
148
|
-
endpoint: '/trpc',
|
|
149
|
-
req: request,
|
|
150
|
-
router: appRouter,
|
|
151
|
-
createContext,
|
|
152
|
-
maxBatchSize: 10,
|
|
153
|
-
});
|
|
154
|
-
},
|
|
155
|
-
};
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
Requests batching more than `maxBatchSize` operations are rejected with a `400 Bad Request` error. Set `maxItems` on your client's `httpBatchLink` to the same value to avoid exceeding the limit.
|
|
159
|
-
|
|
160
|
-
## Common Mistakes
|
|
161
|
-
|
|
162
|
-
### HIGH Mismatched endpoint path in fetchRequestHandler
|
|
163
|
-
|
|
164
|
-
Wrong:
|
|
165
|
-
|
|
166
|
-
```ts
|
|
167
|
-
// Handler mounted at /api/trpc/[trpc] but endpoint says /trpc
|
|
168
|
-
fetchRequestHandler({
|
|
169
|
-
endpoint: '/trpc',
|
|
170
|
-
req: request,
|
|
171
|
-
router: appRouter,
|
|
172
|
-
createContext,
|
|
173
|
-
});
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
Correct:
|
|
177
|
-
|
|
178
|
-
```ts
|
|
179
|
-
// endpoint must match the actual URL path prefix
|
|
180
|
-
fetchRequestHandler({
|
|
181
|
-
endpoint: '/api/trpc',
|
|
182
|
-
req: request,
|
|
183
|
-
router: appRouter,
|
|
184
|
-
createContext,
|
|
185
|
-
});
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
The `endpoint` option tells tRPC where to strip the URL prefix to extract the procedure name. If it does not match the actual mount path, all procedures return 404 because the path parsing extracts the wrong procedure name.
|
|
189
|
-
|
|
190
|
-
Source: www/docs/server/adapters/fetch.mdx
|
|
191
|
-
|
|
192
|
-
## See Also
|
|
193
|
-
|
|
194
|
-
- **server-setup** -- `initTRPC.create()`, router/procedure definition, context
|
|
195
|
-
- **adapter-standalone** -- alternative for Node.js HTTP server
|
|
196
|
-
- **adapter-express** -- alternative when Express middleware ecosystem is needed
|
|
197
|
-
- **adapter-aws-lambda** -- alternative for AWS Lambda deployments
|
|
198
|
-
- Cloudflare Workers docs: https://developers.cloudflare.com/workers/
|
|
199
|
-
- Deno Deploy docs: https://deno.com/deploy/docs
|
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: adapter-standalone
|
|
3
|
-
description: >
|
|
4
|
-
Mount tRPC on Node.js built-in HTTP server with createHTTPServer() from
|
|
5
|
-
@trpc/server/adapters/standalone, createHTTPHandler() for custom http.createServer,
|
|
6
|
-
createHTTP2Handler() for HTTP/2 with TLS. Configure basePath to slice URL prefix,
|
|
7
|
-
CORS via the cors npm package passed as middleware option. CreateHTTPContextOptions
|
|
8
|
-
provides req and res for context creation.
|
|
9
|
-
type: core
|
|
10
|
-
library: trpc
|
|
11
|
-
library_version: '11.16.0'
|
|
12
|
-
requires:
|
|
13
|
-
- server-setup
|
|
14
|
-
sources:
|
|
15
|
-
- www/docs/server/adapters/standalone.md
|
|
16
|
-
- examples/standalone-server/src/server.ts
|
|
17
|
-
---
|
|
18
|
-
|
|
19
|
-
# tRPC — Adapter: Standalone
|
|
20
|
-
|
|
21
|
-
## Setup
|
|
22
|
-
|
|
23
|
-
```ts
|
|
24
|
-
// server.ts
|
|
25
|
-
import { initTRPC } from '@trpc/server';
|
|
26
|
-
import { createHTTPServer } from '@trpc/server/adapters/standalone';
|
|
27
|
-
import { z } from 'zod';
|
|
28
|
-
|
|
29
|
-
const t = initTRPC.create();
|
|
30
|
-
|
|
31
|
-
const appRouter = t.router({
|
|
32
|
-
greet: t.procedure
|
|
33
|
-
.input(z.object({ name: z.string() }))
|
|
34
|
-
.query(({ input }) => ({ greeting: `Hello, ${input.name}!` })),
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
export type AppRouter = typeof appRouter;
|
|
38
|
-
|
|
39
|
-
createHTTPServer({
|
|
40
|
-
router: appRouter,
|
|
41
|
-
createContext() {
|
|
42
|
-
return {};
|
|
43
|
-
},
|
|
44
|
-
}).listen(3000);
|
|
45
|
-
|
|
46
|
-
console.log('Listening on http://localhost:3000');
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
## Core Patterns
|
|
50
|
-
|
|
51
|
-
### CORS with the cors package
|
|
52
|
-
|
|
53
|
-
```ts
|
|
54
|
-
import { createHTTPServer } from '@trpc/server/adapters/standalone';
|
|
55
|
-
import cors from 'cors';
|
|
56
|
-
import { createContext } from './context';
|
|
57
|
-
import { appRouter } from './router';
|
|
58
|
-
|
|
59
|
-
createHTTPServer({
|
|
60
|
-
middleware: cors({ origin: 'http://localhost:5173' }),
|
|
61
|
-
router: appRouter,
|
|
62
|
-
createContext,
|
|
63
|
-
}).listen(3000);
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
Install CORS support: `npm install cors @types/cors`
|
|
67
|
-
|
|
68
|
-
### Custom HTTP server with createHTTPHandler
|
|
69
|
-
|
|
70
|
-
```ts
|
|
71
|
-
import { createServer } from 'http';
|
|
72
|
-
import { createHTTPHandler } from '@trpc/server/adapters/standalone';
|
|
73
|
-
import { appRouter } from './router';
|
|
74
|
-
|
|
75
|
-
const handler = createHTTPHandler({
|
|
76
|
-
router: appRouter,
|
|
77
|
-
createContext() {
|
|
78
|
-
return {};
|
|
79
|
-
},
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
createServer((req, res) => {
|
|
83
|
-
if (req.url?.startsWith('/health')) {
|
|
84
|
-
res.writeHead(200);
|
|
85
|
-
res.end('OK');
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
handler(req, res);
|
|
89
|
-
}).listen(3000);
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
### basePath for URL prefix stripping
|
|
93
|
-
|
|
94
|
-
```ts
|
|
95
|
-
import { createServer } from 'http';
|
|
96
|
-
import { createHTTPHandler } from '@trpc/server/adapters/standalone';
|
|
97
|
-
import { appRouter } from './router';
|
|
98
|
-
|
|
99
|
-
const handler = createHTTPHandler({
|
|
100
|
-
router: appRouter,
|
|
101
|
-
basePath: '/trpc/',
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
createServer((req, res) => {
|
|
105
|
-
if (req.url?.startsWith('/trpc/')) {
|
|
106
|
-
return handler(req, res);
|
|
107
|
-
}
|
|
108
|
-
res.statusCode = 404;
|
|
109
|
-
res.end('Not Found');
|
|
110
|
-
}).listen(3000);
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
The `basePath` option strips the prefix before routing, so `/trpc/greet` resolves to the `greet` procedure.
|
|
114
|
-
|
|
115
|
-
### HTTP/2 with createHTTP2Handler
|
|
116
|
-
|
|
117
|
-
```ts
|
|
118
|
-
import http2 from 'http2';
|
|
119
|
-
import { readFileSync } from 'node:fs';
|
|
120
|
-
import { createHTTP2Handler } from '@trpc/server/adapters/standalone';
|
|
121
|
-
import type { CreateHTTP2ContextOptions } from '@trpc/server/adapters/standalone';
|
|
122
|
-
import { appRouter } from './router';
|
|
123
|
-
|
|
124
|
-
const tlsKey = readFileSync('./certs/server.key');
|
|
125
|
-
const tlsCert = readFileSync('./certs/server.crt');
|
|
126
|
-
|
|
127
|
-
async function createContext(opts: CreateHTTP2ContextOptions) {
|
|
128
|
-
return {};
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const handler = createHTTP2Handler({
|
|
132
|
-
router: appRouter,
|
|
133
|
-
createContext,
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
const server = http2.createSecureServer(
|
|
137
|
-
{ key: tlsKey, cert: tlsCert },
|
|
138
|
-
(req, res) => {
|
|
139
|
-
handler(req, res);
|
|
140
|
-
},
|
|
141
|
-
);
|
|
142
|
-
|
|
143
|
-
server.listen(3001);
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
### Limiting batch size with maxBatchSize
|
|
147
|
-
|
|
148
|
-
```ts
|
|
149
|
-
import { createHTTPServer } from '@trpc/server/adapters/standalone';
|
|
150
|
-
import { appRouter } from './router';
|
|
151
|
-
|
|
152
|
-
createHTTPServer({
|
|
153
|
-
router: appRouter,
|
|
154
|
-
maxBatchSize: 10,
|
|
155
|
-
}).listen(3000);
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
Requests batching more than `maxBatchSize` operations are rejected with a `400 Bad Request` error. Set `maxItems` on your client's `httpBatchLink` to the same value to avoid exceeding the limit.
|
|
159
|
-
|
|
160
|
-
## Common Mistakes
|
|
161
|
-
|
|
162
|
-
### HIGH No CORS configuration for cross-origin requests
|
|
163
|
-
|
|
164
|
-
Wrong:
|
|
165
|
-
|
|
166
|
-
```ts
|
|
167
|
-
createHTTPServer({
|
|
168
|
-
router: appRouter,
|
|
169
|
-
createContext() {
|
|
170
|
-
return {};
|
|
171
|
-
},
|
|
172
|
-
}).listen(3000);
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
Correct:
|
|
176
|
-
|
|
177
|
-
```ts
|
|
178
|
-
import cors from 'cors';
|
|
179
|
-
|
|
180
|
-
createHTTPServer({
|
|
181
|
-
middleware: cors({ origin: 'http://localhost:5173' }),
|
|
182
|
-
router: appRouter,
|
|
183
|
-
createContext() {
|
|
184
|
-
return {};
|
|
185
|
-
},
|
|
186
|
-
}).listen(3000);
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
The standalone adapter has no CORS handling by default. Cross-origin browser requests fail silently because preflight OPTIONS requests receive no CORS headers.
|
|
190
|
-
|
|
191
|
-
Source: www/docs/server/adapters/standalone.md
|
|
192
|
-
|
|
193
|
-
## See Also
|
|
194
|
-
|
|
195
|
-
- **server-setup** -- `initTRPC.create()`, router/procedure definition, context
|
|
196
|
-
- **adapter-express** -- alternative adapter when you need Express middleware ecosystem
|
|
197
|
-
- **adapter-fetch** -- alternative adapter for edge/serverless runtimes
|
|
198
|
-
- **subscriptions** -- adding real-time subscriptions to a standalone server
|