@trpc/server 11.22.2-canary.9.85bcf328a → 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,76 +1,158 @@
|
|
|
1
1
|
---
|
|
2
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'
|
|
3
|
+
description: Mount tRPC on Node.js built-in HTTP server with createHTTPServer() from @trpc/server/adapters/standalone, createHTTPHandler() for custom http.createServer, createHTTP2Handler() for HTTP/2 with TLS. Configure basePath to slice URL prefix, CORS via the cors npm package passed as middleware option. CreateHTTPContextOptions provides req and res for context creation. Pass the common adapter options (onError, maxBatchSize, maxBodySize) to createHTTPServer.
|
|
12
4
|
requires:
|
|
13
5
|
- server-setup
|
|
14
6
|
sources:
|
|
15
|
-
- www/docs/server/adapters/standalone.md
|
|
16
|
-
-
|
|
7
|
+
- www/v11/docs/server/adapters/standalone.md
|
|
8
|
+
- www/v11/docs/server/adapters.md
|
|
9
|
+
metadata:
|
|
10
|
+
type: core
|
|
11
|
+
library: trpc
|
|
12
|
+
library_version: 11.23.0
|
|
17
13
|
---
|
|
18
14
|
|
|
19
|
-
|
|
15
|
+
<!-- Generated from www/v11/docs/server/adapters/standalone.md by www/build/skills.ts. Edit the docs instead. -->
|
|
20
16
|
|
|
21
|
-
|
|
17
|
+
# Standalone Adapter
|
|
22
18
|
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
From https://trpc.io/docs/server/adapters/standalone
|
|
20
|
+
|
|
21
|
+
tRPC's Standalone Adapter is the simplest way to get a new project working. It's ideal for local development, and for server-based production environments. In essence it's just a wrapper around the standard [Node.js HTTP Server](https://nodejs.org/api/http.html) with the normal options related to tRPC.
|
|
22
|
+
|
|
23
|
+
If you have an existing API deployment like [Express](https://trpc.io/docs/server/adapters/express), [Fastify](https://trpc.io/docs/server/adapters/fastify), or [Next.js](https://trpc.io/docs/server/adapters/nextjs), which you want to integrate tRPC into, you should have a look at their respective adapters. Likewise if you have a preference to host on serverless or edge compute, we have adapters like [AWS Lambda](https://trpc.io/docs/server/adapters/aws-lambda) and [Fetch](https://trpc.io/docs/server/adapters/fetch) which may fit your needs.
|
|
24
|
+
|
|
25
|
+
It's also not uncommon, where the deployed adapter is hard to run on local machines, to have 2 entry-points in your application. You could use the Standalone Adapter for local development, and a different adapter when deployed.
|
|
26
|
+
|
|
27
|
+
## Example app
|
|
28
|
+
|
|
29
|
+
<table>
|
|
30
|
+
<thead>
|
|
31
|
+
<tr>
|
|
32
|
+
<th>Description</th>
|
|
33
|
+
<th>Links</th>
|
|
34
|
+
</tr>
|
|
35
|
+
</thead>
|
|
36
|
+
<tbody>
|
|
37
|
+
<tr>
|
|
38
|
+
<td>Standalone tRPC Server</td>
|
|
39
|
+
<td>
|
|
40
|
+
<ul>
|
|
41
|
+
<li><a href="https://stackblitz.com/github/trpc/trpc/tree/main/examples/minimal">StackBlitz</a></li>
|
|
42
|
+
<li><a href="https://github.com/trpc/trpc/blob/main/examples/minimal/src/server/index.ts">Source</a></li>
|
|
43
|
+
</ul>
|
|
44
|
+
</td>
|
|
45
|
+
</tr>
|
|
46
|
+
<tr>
|
|
47
|
+
<td>Standalone tRPC Server with CORS handling</td>
|
|
48
|
+
<td>
|
|
49
|
+
<ul>
|
|
50
|
+
<li><a href="https://stackblitz.com/github/trpc/trpc/tree/main/examples/minimal-react">StackBlitz</a></li>
|
|
51
|
+
<li><a href="https://github.com/trpc/trpc/blob/main/examples/minimal-react/server/index.ts">Source</a></li>
|
|
52
|
+
</ul>
|
|
53
|
+
</td>
|
|
54
|
+
</tr>
|
|
55
|
+
</tbody>
|
|
56
|
+
</table>
|
|
57
|
+
|
|
58
|
+
## Setting up a Standalone tRPC Server
|
|
59
|
+
|
|
60
|
+
### 1. Implement your App Router
|
|
61
|
+
|
|
62
|
+
Implement your tRPC router. For example:
|
|
63
|
+
|
|
64
|
+
```ts no-check title="appRouter.ts"
|
|
25
65
|
import { initTRPC } from '@trpc/server';
|
|
26
|
-
import { createHTTPServer } from '@trpc/server/adapters/standalone';
|
|
27
66
|
import { z } from 'zod';
|
|
28
67
|
|
|
29
|
-
const t = initTRPC.create();
|
|
30
|
-
|
|
31
|
-
const appRouter = t.router({
|
|
32
|
-
|
|
33
|
-
.input
|
|
34
|
-
|
|
68
|
+
export const t = initTRPC.create();
|
|
69
|
+
|
|
70
|
+
export const appRouter = t.router({
|
|
71
|
+
getUser: t.procedure.input(z.string()).query((opts) => {
|
|
72
|
+
return { id: opts.input, name: 'Bilbo' };
|
|
73
|
+
}),
|
|
74
|
+
createUser: t.procedure
|
|
75
|
+
.input(z.object({ name: z.string().min(5) }))
|
|
76
|
+
.mutation(async (opts) => {
|
|
77
|
+
// use your ORM of choice
|
|
78
|
+
return { id: '1', ...opts.input };
|
|
79
|
+
}),
|
|
35
80
|
});
|
|
36
81
|
|
|
82
|
+
// export type definition of API
|
|
37
83
|
export type AppRouter = typeof appRouter;
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
For more information, you can look at the [quickstart guide](https://trpc.io/docs/quickstart)
|
|
87
|
+
|
|
88
|
+
### 2. Use the Standalone adapter
|
|
89
|
+
|
|
90
|
+
The Standalone adapter runs a simple Node.js HTTP server.
|
|
91
|
+
|
|
92
|
+
```ts no-check title="server.ts"
|
|
93
|
+
import { createHTTPServer } from '@trpc/server/adapters/standalone';
|
|
94
|
+
import { appRouter } from './appRouter';
|
|
38
95
|
|
|
39
96
|
createHTTPServer({
|
|
40
97
|
router: appRouter,
|
|
41
98
|
createContext() {
|
|
42
99
|
return {};
|
|
43
100
|
},
|
|
44
|
-
|
|
101
|
+
// basePath: '/trpc/', // optional, defaults to '/'
|
|
102
|
+
}).listen(2022);
|
|
103
|
+
```
|
|
45
104
|
|
|
46
|
-
|
|
105
|
+
`createHTTPServer` accepts the [options every adapter takes](https://trpc.io/docs/server/adapters#common-options), like `onError` and `maxBatchSize`, plus `middleware`, `maxBodySize` and [`basePath`](https://trpc.io/docs/server/adapters/standalone#custom-basePath).
|
|
106
|
+
|
|
107
|
+
## Handling CORS & OPTIONS
|
|
108
|
+
|
|
109
|
+
By default the standalone server will not respond to HTTP OPTIONS requests, or set any CORS headers.
|
|
110
|
+
|
|
111
|
+
If you're not hosting in an environment which can handle this for you, like during local development, you may need to handle it.
|
|
112
|
+
|
|
113
|
+
### 1. Install cors
|
|
114
|
+
|
|
115
|
+
You can add support yourself with the popular `cors` package
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
yarn add cors
|
|
119
|
+
yarn add -D @types/cors
|
|
47
120
|
```
|
|
48
121
|
|
|
49
|
-
|
|
122
|
+
For full information on how to configure this package, [check the docs](https://github.com/expressjs/cors#readme)
|
|
123
|
+
|
|
124
|
+
### 2. Configure the Standalone server
|
|
50
125
|
|
|
51
|
-
|
|
126
|
+
This example just throws open CORS to any request, which is useful for development, but you can and should configure it more strictly in a production environment.
|
|
52
127
|
|
|
53
|
-
```ts
|
|
128
|
+
```ts no-check title="server.ts"
|
|
54
129
|
import { createHTTPServer } from '@trpc/server/adapters/standalone';
|
|
55
130
|
import cors from 'cors';
|
|
56
|
-
import {
|
|
57
|
-
import { appRouter } from './router';
|
|
131
|
+
import { appRouter } from './appRouter';
|
|
58
132
|
|
|
59
133
|
createHTTPServer({
|
|
60
|
-
middleware: cors(
|
|
134
|
+
middleware: cors(),
|
|
61
135
|
router: appRouter,
|
|
62
|
-
createContext
|
|
63
|
-
}
|
|
136
|
+
createContext() {
|
|
137
|
+
return {};
|
|
138
|
+
},
|
|
139
|
+
}).listen(3333);
|
|
64
140
|
```
|
|
65
141
|
|
|
66
|
-
|
|
142
|
+
The `middleware` option will accept any function which resembles a connect/node.js middleware, so it can be used for more than `cors` handling if you wish. It is, however, intended to be a simple escape hatch and as such won't on its own allow you to compose multiple middlewares together. If you want to do this then you could:
|
|
67
143
|
|
|
68
|
-
|
|
144
|
+
1. Use an alternate adapter with more comprehensive middleware support, like the [Express adapter](https://trpc.io/docs/server/adapters/express)
|
|
145
|
+
2. Use a solution to compose middlewares such as [connect](https://github.com/senchalabs/connect)
|
|
146
|
+
3. Extend the Standalone `createHTTPHandler` with a custom http server (see below)
|
|
69
147
|
|
|
70
|
-
|
|
148
|
+
## Adding a handler to a Custom HTTP server
|
|
149
|
+
|
|
150
|
+
`createHTTPServer` is returning an instance of Node's built-in [`http.Server`](https://nodejs.org/api/http.html#class-httpserver), which means that you have an access to all its properties and APIs. However, if `createHTTPServer` isn't enough for your use case, you can also use the standalone adapter's `createHTTPHandler` function to create your own HTTP server. For instance:
|
|
151
|
+
|
|
152
|
+
```ts no-check title="server.ts"
|
|
71
153
|
import { createServer } from 'http';
|
|
72
154
|
import { createHTTPHandler } from '@trpc/server/adapters/standalone';
|
|
73
|
-
import { appRouter } from './
|
|
155
|
+
import { appRouter } from './appRouter';
|
|
74
156
|
|
|
75
157
|
const handler = createHTTPHandler({
|
|
76
158
|
router: appRouter,
|
|
@@ -80,21 +162,23 @@ const handler = createHTTPHandler({
|
|
|
80
162
|
});
|
|
81
163
|
|
|
82
164
|
createServer((req, res) => {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
165
|
+
/**
|
|
166
|
+
* Handle the request however you like,
|
|
167
|
+
* just call the tRPC handler when you're ready
|
|
168
|
+
*/
|
|
169
|
+
|
|
88
170
|
handler(req, res);
|
|
89
|
-
}).listen(
|
|
171
|
+
}).listen(3001);
|
|
90
172
|
```
|
|
91
173
|
|
|
92
|
-
|
|
174
|
+
## Custom base path for handling requests
|
|
175
|
+
|
|
176
|
+
The Standalone adapter also supports a `basePath` option, which will slice the basePath from the beginning of the request path.
|
|
93
177
|
|
|
94
|
-
```ts
|
|
178
|
+
```ts no-check title="server.ts"
|
|
95
179
|
import { createServer } from 'http';
|
|
96
180
|
import { createHTTPHandler } from '@trpc/server/adapters/standalone';
|
|
97
|
-
import { appRouter } from './
|
|
181
|
+
import { appRouter } from './appRouter';
|
|
98
182
|
|
|
99
183
|
const handler = createHTTPHandler({
|
|
100
184
|
router: appRouter,
|
|
@@ -105,37 +189,39 @@ createServer((req, res) => {
|
|
|
105
189
|
if (req.url?.startsWith('/trpc/')) {
|
|
106
190
|
return handler(req, res);
|
|
107
191
|
}
|
|
192
|
+
// [... insert your custom logic here ...]
|
|
193
|
+
|
|
108
194
|
res.statusCode = 404;
|
|
109
195
|
res.end('Not Found');
|
|
110
|
-
}).listen(
|
|
196
|
+
}).listen(3001);
|
|
111
197
|
```
|
|
112
198
|
|
|
113
|
-
|
|
199
|
+
## HTTP2
|
|
114
200
|
|
|
115
|
-
|
|
201
|
+
The Standalone adapter also supports HTTP/2.
|
|
116
202
|
|
|
117
|
-
```ts
|
|
203
|
+
```ts no-check title="server.ts"
|
|
118
204
|
import http2 from 'http2';
|
|
119
|
-
import { readFileSync } from 'node:fs';
|
|
120
205
|
import { createHTTP2Handler } from '@trpc/server/adapters/standalone';
|
|
121
|
-
import
|
|
122
|
-
import {
|
|
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
|
-
}
|
|
206
|
+
import { appRouter } from './_app';
|
|
207
|
+
import { createContext } from './context';
|
|
130
208
|
|
|
131
209
|
const handler = createHTTP2Handler({
|
|
132
210
|
router: appRouter,
|
|
133
211
|
createContext,
|
|
212
|
+
// basePath: '/trpc/', // optional, defaults to '/'
|
|
134
213
|
});
|
|
135
214
|
|
|
136
215
|
const server = http2.createSecureServer(
|
|
137
|
-
{
|
|
216
|
+
{
|
|
217
|
+
key: '...',
|
|
218
|
+
cert: '...',
|
|
219
|
+
},
|
|
138
220
|
(req, res) => {
|
|
221
|
+
/**
|
|
222
|
+
* Handle the request however you like,
|
|
223
|
+
* just call the tRPC handler when you're ready
|
|
224
|
+
*/
|
|
139
225
|
handler(req, res);
|
|
140
226
|
},
|
|
141
227
|
);
|
|
@@ -143,56 +229,60 @@ const server = http2.createSecureServer(
|
|
|
143
229
|
server.listen(3001);
|
|
144
230
|
```
|
|
145
231
|
|
|
146
|
-
|
|
232
|
+
```ts no-check title="context.ts"
|
|
233
|
+
import { CreateHTTP2ContextOptions } from '@trpc/server/adapters/standalone';
|
|
147
234
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
235
|
+
export async function createContext(opts: CreateHTTP2ContextOptions) {
|
|
236
|
+
opts.req;
|
|
237
|
+
opts.res;
|
|
151
238
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
239
|
+
opts.info;
|
|
240
|
+
return {};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export type Context = Awaited<ReturnType<typeof createContext>>;
|
|
156
244
|
```
|
|
157
245
|
|
|
158
|
-
|
|
246
|
+
## Common mistakes
|
|
159
247
|
|
|
160
|
-
|
|
248
|
+
- **Calling the server from another origin without CORS.** The browser's preflight `OPTIONS` request gets no CORS headers, so the browser blocks the call. Pass [`middleware: cors()`](https://trpc.io/docs/server/adapters/standalone#handling-cors--options), with the origins you allow.
|
|
161
249
|
|
|
162
|
-
|
|
250
|
+
## Common options
|
|
163
251
|
|
|
164
|
-
|
|
252
|
+
Every HTTP adapter accepts these options, next to its own (like `endpoint` for Fetch, or `basePath` for Standalone):
|
|
165
253
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
254
|
+
| Option | Description |
|
|
255
|
+
| --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
256
|
+
| `router` | Your app router. Required. |
|
|
257
|
+
| `createContext` | Creates the [context](https://trpc.io/docs/server/context) for each request. It receives the adapter's own objects (e.g. `req` and `res`) plus `info`. |
|
|
258
|
+
| `onError` | Called with `{ error, type, path, input, ctx, req }` for every error, e.g. to report it. See [Error handling](https://trpc.io/docs/server/error-handling#handling-errors). |
|
|
259
|
+
| `responseMeta` | Returns `{ status, headers }` for the response, e.g. for [caching](https://trpc.io/docs/server/caching). |
|
|
260
|
+
| `allowBatching` | Set to `false` to reject batch requests. Defaults to `true`. |
|
|
261
|
+
| `maxBatchSize` | Rejects batch requests with more calls than this with a `400 Bad Request`. Set `maxItems` on your batch link to the same or a lower value. |
|
|
262
|
+
| `allowMethodOverride` | Accepts queries sent as `POST`, for clients that set `methodOverride: 'POST'` on their link. Defaults to `false`. |
|
|
174
263
|
|
|
175
|
-
|
|
264
|
+
The Node.js adapters (Standalone, Express and Next.js) also accept:
|
|
176
265
|
|
|
177
|
-
|
|
178
|
-
|
|
266
|
+
- `maxBodySize`: the largest request body in bytes; larger ones are rejected with `413 PAYLOAD_TOO_LARGE`.
|
|
267
|
+
- `middleware`: a connect-style `(req, res, next)` function that runs before tRPC, e.g. [`cors()`](https://trpc.io/docs/server/adapters/standalone#handling-cors--options).
|
|
268
|
+
|
|
269
|
+
```ts no-check title="server.ts"
|
|
270
|
+
import { createHTTPServer } from '@trpc/server/adapters/standalone';
|
|
271
|
+
import { appRouter } from './router';
|
|
179
272
|
|
|
180
273
|
createHTTPServer({
|
|
181
|
-
middleware: cors({ origin: 'http://localhost:5173' }),
|
|
182
274
|
router: appRouter,
|
|
183
|
-
createContext() {
|
|
184
|
-
|
|
275
|
+
createContext: () => ({}),
|
|
276
|
+
onError({ error, path }) {
|
|
277
|
+
console.error(`tRPC error on '${path}':`, error);
|
|
185
278
|
},
|
|
279
|
+
maxBatchSize: 10,
|
|
280
|
+
maxBodySize: 1_000_000, // 1 MB
|
|
186
281
|
}).listen(3000);
|
|
187
282
|
```
|
|
188
283
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
Source: www/docs/server/adapters/standalone.md
|
|
284
|
+
From https://trpc.io/docs/server/adapters#common-options
|
|
192
285
|
|
|
193
|
-
##
|
|
286
|
+
## Related skills
|
|
194
287
|
|
|
195
|
-
-
|
|
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
|
|
288
|
+
- `server-setup` (https://trpc.io/docs/server/overview): `npx @tanstack/intent@latest load @trpc/server#server-setup`
|