@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,66 +1,226 @@
|
|
|
1
1
|
---
|
|
2
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'
|
|
3
|
+
description: Deploy tRPC on WinterCG-compliant edge runtimes with fetchRequestHandler() from @trpc/server/adapters/fetch. Supports Cloudflare Workers, Deno Deploy, Vercel Edge Runtime, Astro, Remix, SolidStart. FetchCreateContextFnOptions provides req (Request) and resHeaders (Headers) for context creation. The endpoint option must match the URL path prefix where the handler is mounted.
|
|
12
4
|
requires:
|
|
13
5
|
- server-setup
|
|
14
6
|
sources:
|
|
15
|
-
- www/docs/server/adapters/fetch.
|
|
16
|
-
|
|
17
|
-
|
|
7
|
+
- www/v11/docs/server/adapters/fetch.md
|
|
8
|
+
metadata:
|
|
9
|
+
type: core
|
|
10
|
+
library: trpc
|
|
11
|
+
library_version: 11.23.0
|
|
18
12
|
---
|
|
19
13
|
|
|
20
|
-
|
|
14
|
+
<!-- Generated from www/v11/docs/server/adapters/fetch.md by www/build/skills.ts. Edit the docs instead. -->
|
|
21
15
|
|
|
22
|
-
|
|
16
|
+
# Fetch / Edge Runtimes Adapter
|
|
23
17
|
|
|
24
|
-
|
|
25
|
-
|
|
18
|
+
From https://trpc.io/docs/server/adapters/fetch
|
|
19
|
+
|
|
20
|
+
You can create a tRPC server within any edge runtime that follow the [WinterCG](https://wintercg.org/), specifically the [Minimum Common Web Platform API](https://min-common-api.proposal.wintertc.org/) specification.
|
|
21
|
+
|
|
22
|
+
Some of these runtimes include, but are not limited to:
|
|
23
|
+
|
|
24
|
+
- Cloudflare Workers
|
|
25
|
+
- Deno Deploy
|
|
26
|
+
- Vercel Edge Runtime (& Next.js Edge Runtime)
|
|
27
|
+
|
|
28
|
+
This also makes it easy to integrate into frameworks that use the web platform APIs to represent requests and responses, such as:
|
|
29
|
+
|
|
30
|
+
- Astro (SSR mode)
|
|
31
|
+
- Remix
|
|
32
|
+
- SolidStart
|
|
33
|
+
|
|
34
|
+
## Example apps
|
|
35
|
+
|
|
36
|
+
<table>
|
|
37
|
+
<thead>
|
|
38
|
+
<tr>
|
|
39
|
+
<th>Description</th>
|
|
40
|
+
<th>Links</th>
|
|
41
|
+
</tr>
|
|
42
|
+
</thead>
|
|
43
|
+
<tbody>
|
|
44
|
+
<tr>
|
|
45
|
+
<td>Cloudflare Workers example</td>
|
|
46
|
+
<td>
|
|
47
|
+
<a href="https://github.com/trpc/trpc/tree/main/examples/cloudflare-workers">
|
|
48
|
+
Source
|
|
49
|
+
</a>
|
|
50
|
+
</td>
|
|
51
|
+
</tr>
|
|
52
|
+
<tr>
|
|
53
|
+
<td>Deno Deploy example</td>
|
|
54
|
+
<td>
|
|
55
|
+
<a href="https://github.com/trpc/trpc/tree/main/examples/deno-deploy">
|
|
56
|
+
Source
|
|
57
|
+
</a>
|
|
58
|
+
</td>
|
|
59
|
+
</tr>
|
|
60
|
+
<tr>
|
|
61
|
+
<td>Next.js Edge Runtime example</td>
|
|
62
|
+
<td>
|
|
63
|
+
<a href="https://github.com/trpc/trpc/tree/main/examples/next-edge-runtime">
|
|
64
|
+
Source
|
|
65
|
+
</a>
|
|
66
|
+
</td>
|
|
67
|
+
</tr>
|
|
68
|
+
<tr>
|
|
69
|
+
<td>Vercel Edge Runtime example</td>
|
|
70
|
+
<td>
|
|
71
|
+
<a href="https://github.com/trpc/trpc/tree/main/examples/vercel-edge-runtime">
|
|
72
|
+
Source
|
|
73
|
+
</a>
|
|
74
|
+
</td>
|
|
75
|
+
</tr>
|
|
76
|
+
</tbody>
|
|
77
|
+
</table>
|
|
78
|
+
|
|
79
|
+
## How to use tRPC server with an edge runtime
|
|
80
|
+
|
|
81
|
+
tRPC provides a [fetch adapter](https://trpc.io/docs/server/adapters/fetch) that uses the native [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) and [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) APIs as input and output. The tRPC-specific code is the same across all runtimes, the only difference being how the response is returned.
|
|
82
|
+
|
|
83
|
+
tRPC includes an adapter for the native [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) out of the box. This adapter lets you convert your tRPC router into a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) handler that returns [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) objects.
|
|
84
|
+
|
|
85
|
+
## Required Web APIs
|
|
86
|
+
|
|
87
|
+
tRPC server uses the following Fetch APIs:
|
|
88
|
+
|
|
89
|
+
- `Request`, `Response`
|
|
90
|
+
- `fetch`
|
|
91
|
+
- `Headers`
|
|
92
|
+
- `URL`
|
|
93
|
+
|
|
94
|
+
If your runtime supports these APIs, you can [use tRPC server](https://trpc.io/docs/server/adapters/fetch#how-to-use-trpc-server-with-an-edge-runtime).
|
|
95
|
+
|
|
96
|
+
> [!TIP]
|
|
97
|
+
> Fun fact: that also means you can use a tRPC server in your browser!
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
## Common setup
|
|
101
|
+
|
|
102
|
+
### Install dependencies
|
|
103
|
+
|
|
104
|
+
> [!TIP]
|
|
105
|
+
> You can skip this step if you use Deno Deploy.
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
npm install @trpc/server @trpc/client zod
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
> [Zod](https://github.com/colinhacks/zod) isn't a required dependency, but it's used in the sample router below.
|
|
113
|
+
|
|
114
|
+
### Create the router
|
|
115
|
+
|
|
116
|
+
First of all you need a [router](https://trpc.io/docs/server/routers) to handle your queries, mutations and subscriptions.
|
|
117
|
+
|
|
118
|
+
A sample router is given below, save it in a file named `router.ts`.
|
|
119
|
+
|
|
120
|
+
<details>
|
|
121
|
+
<summary>router.ts</summary>
|
|
122
|
+
|
|
123
|
+
```ts no-check title="router.ts"
|
|
26
124
|
import { initTRPC } from '@trpc/server';
|
|
27
|
-
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
|
|
28
|
-
import type { FetchCreateContextFnOptions } from '@trpc/server/adapters/fetch';
|
|
29
125
|
import { z } from 'zod';
|
|
126
|
+
import type { Context } from './context';
|
|
30
127
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const t = initTRPC.context<Context>().create();
|
|
128
|
+
type User = {
|
|
129
|
+
id: string;
|
|
130
|
+
name: string;
|
|
131
|
+
bio?: string;
|
|
132
|
+
};
|
|
38
133
|
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
134
|
+
const users: Record<string, User> = {};
|
|
135
|
+
|
|
136
|
+
export const t = initTRPC.context<Context>().create();
|
|
137
|
+
|
|
138
|
+
export const appRouter = t.router({
|
|
139
|
+
getUserById: t.procedure.input(z.string()).query((opts) => {
|
|
140
|
+
return users[opts.input]; // input type is string
|
|
141
|
+
}),
|
|
142
|
+
createUser: t.procedure
|
|
143
|
+
// validate input with Zod
|
|
144
|
+
.input(
|
|
145
|
+
z.object({
|
|
146
|
+
name: z.string().min(3),
|
|
147
|
+
bio: z.string().max(142).optional(),
|
|
148
|
+
}),
|
|
149
|
+
)
|
|
150
|
+
.mutation((opts) => {
|
|
151
|
+
const id = Date.now().toString();
|
|
152
|
+
const user: User = { id, ...opts.input };
|
|
153
|
+
users[user.id] = user;
|
|
154
|
+
return user;
|
|
155
|
+
}),
|
|
43
156
|
});
|
|
44
157
|
|
|
158
|
+
// export type definition of API
|
|
45
159
|
export type AppRouter = typeof appRouter;
|
|
160
|
+
```
|
|
46
161
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
162
|
+
</details>
|
|
163
|
+
|
|
164
|
+
If your router file starts getting too big, split your router into several subrouters each implemented in its own file. Then [merge them](https://trpc.io/docs/server/merging-routers) into a single root `appRouter`.
|
|
165
|
+
|
|
166
|
+
### Create the context
|
|
167
|
+
|
|
168
|
+
Then you need a [context](https://trpc.io/docs/server/context) that will be created for each request.
|
|
169
|
+
|
|
170
|
+
A sample context is given below, save it in a file named `context.ts`:
|
|
171
|
+
|
|
172
|
+
<details>
|
|
173
|
+
<summary>context.ts</summary>
|
|
174
|
+
|
|
175
|
+
```ts no-check title="context.ts"
|
|
176
|
+
import type { FetchCreateContextFnOptions } from '@trpc/server/adapters/fetch';
|
|
177
|
+
|
|
178
|
+
export function createContext({
|
|
179
|
+
req,
|
|
180
|
+
resHeaders,
|
|
181
|
+
}: FetchCreateContextFnOptions) {
|
|
182
|
+
const user = { name: req.headers.get('username') ?? 'anonymous' };
|
|
183
|
+
return { req, resHeaders, user };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export type Context = Awaited<ReturnType<typeof createContext>>;
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
</details>
|
|
190
|
+
|
|
191
|
+
Headers you set on `resHeaders`, like `Set-Cookie`, are added to the response.
|
|
192
|
+
|
|
193
|
+
`fetchRequestHandler` takes an `endpoint`, the URL path the handler is mounted on, plus the [options every adapter takes](https://trpc.io/docs/server/adapters#common-options), like `onError` and `maxBatchSize`.
|
|
194
|
+
|
|
195
|
+
## Runtime-specific setup
|
|
196
|
+
|
|
197
|
+
### Astro
|
|
198
|
+
|
|
199
|
+
```ts no-check title="src/pages/trpc/[trpc].ts"
|
|
200
|
+
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
|
|
201
|
+
import type { APIRoute } from 'astro';
|
|
202
|
+
import { createContext } from '../../server/context';
|
|
203
|
+
import { appRouter } from '../../server/router';
|
|
204
|
+
|
|
205
|
+
export const ALL: APIRoute = (opts) => {
|
|
206
|
+
return fetchRequestHandler({
|
|
207
|
+
endpoint: '/trpc',
|
|
208
|
+
req: opts.request,
|
|
209
|
+
router: appRouter,
|
|
210
|
+
createContext,
|
|
211
|
+
});
|
|
56
212
|
};
|
|
57
213
|
```
|
|
58
214
|
|
|
59
|
-
|
|
215
|
+
### Cloudflare Worker
|
|
60
216
|
|
|
61
|
-
|
|
217
|
+
> [!NOTE]
|
|
218
|
+
> You need the [Wrangler CLI](https://developers.cloudflare.com/workers/wrangler/) to run Cloudflare Workers.
|
|
62
219
|
|
|
63
|
-
|
|
220
|
+
|
|
221
|
+
#### Create Cloudflare Worker
|
|
222
|
+
|
|
223
|
+
```ts no-check title="server.ts"
|
|
64
224
|
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
|
|
65
225
|
import { createContext } from './context';
|
|
66
226
|
import { appRouter } from './router';
|
|
@@ -77,34 +237,138 @@ export default {
|
|
|
77
237
|
};
|
|
78
238
|
```
|
|
79
239
|
|
|
80
|
-
|
|
240
|
+
Run `wrangler dev server.ts` and your endpoints will be available via HTTP!
|
|
81
241
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
import { createContext } from '../../server/context';
|
|
87
|
-
import { appRouter } from '../../server/router';
|
|
242
|
+
| Endpoint | HTTP URI |
|
|
243
|
+
| ------------ | -------------------------------------------------------------------------------------------------------------- |
|
|
244
|
+
| `getUser` | `GET http://localhost:8787/trpc/getUserById?input=INPUT` <br/><br/>where `INPUT` is a URI-encoded JSON string. |
|
|
245
|
+
| `createUser` | `POST http://localhost:8787/trpc/createUser` <br/><br/>with `req.body` of type `User` |
|
|
88
246
|
|
|
89
|
-
|
|
247
|
+
### Deno Oak
|
|
248
|
+
|
|
249
|
+
> [!NOTE]
|
|
250
|
+
> This assumes you have Deno installed and setup. Refer to their [getting started guide](https://deno.com/manual/getting_started/installation) for more information.
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
#### Update the imports in `router.ts`
|
|
254
|
+
|
|
255
|
+
```ts title="router.ts"
|
|
256
|
+
import { initTRPC } from 'npm:@trpc/server';
|
|
257
|
+
import { z } from 'npm:zod';
|
|
258
|
+
import { Context } from './context.ts';
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
#### Update the imports in `context.ts`
|
|
262
|
+
|
|
263
|
+
```ts title="context.ts"
|
|
264
|
+
import { FetchCreateContextFnOptions } from 'npm:@trpc/server/adapters/fetch';
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
#### Use `fetchRequestHandler` with Oak in `app.ts`
|
|
268
|
+
|
|
269
|
+
```ts title="app.ts"
|
|
270
|
+
import { Application, Router } from 'https://deno.land/x/oak/mod.ts';
|
|
271
|
+
import { fetchRequestHandler } from 'npm:@trpc/server/adapters/fetch';
|
|
272
|
+
import { createContext } from './context.ts';
|
|
273
|
+
import { appRouter } from './router.ts';
|
|
274
|
+
|
|
275
|
+
const app = new Application();
|
|
276
|
+
const router = new Router();
|
|
277
|
+
|
|
278
|
+
router.all('/trpc/(.*)', async (ctx) => {
|
|
279
|
+
const res = await fetchRequestHandler({
|
|
280
|
+
endpoint: '/trpc',
|
|
281
|
+
req: new Request(ctx.request.url, {
|
|
282
|
+
headers: ctx.request.headers,
|
|
283
|
+
body:
|
|
284
|
+
ctx.request.method !== 'GET' && ctx.request.method !== 'HEAD'
|
|
285
|
+
? ctx.request.body({ type: 'stream' }).value
|
|
286
|
+
: void 0,
|
|
287
|
+
method: ctx.request.method,
|
|
288
|
+
}),
|
|
289
|
+
router: appRouter,
|
|
290
|
+
createContext,
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
ctx.response.status = res.status;
|
|
294
|
+
ctx.response.headers = res.headers;
|
|
295
|
+
ctx.response.body = res.body;
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
app.use(router.routes());
|
|
299
|
+
app.use(router.allowedMethods());
|
|
300
|
+
|
|
301
|
+
await app.listen({ port: 3000 });
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### Deno Deploy
|
|
305
|
+
|
|
306
|
+
> [!NOTE]
|
|
307
|
+
> This assumes you have Deno installed and setup. Refer to their [getting started guide](https://deno.com/manual/getting_started/installation) for more information.
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
> [!TIP]
|
|
311
|
+
> See our example [Deno Deploy app](https://github.com/trpc/trpc/tree/main/examples/deno-deploy) for a working example.
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
#### Update the imports in `router.ts`
|
|
315
|
+
|
|
316
|
+
```ts title="router.ts"
|
|
317
|
+
import { initTRPC } from 'npm:@trpc/server';
|
|
318
|
+
import { z } from 'npm:zod';
|
|
319
|
+
import { Context } from './context.ts';
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
#### Update the imports in `context.ts`
|
|
323
|
+
|
|
324
|
+
```ts title="context.ts"
|
|
325
|
+
import { FetchCreateContextFnOptions } from 'npm:@trpc/server/adapters/fetch';
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
#### Create Deno Deploy Function
|
|
329
|
+
|
|
330
|
+
```ts title="server.ts"
|
|
331
|
+
import { fetchRequestHandler } from 'npm:@trpc/server/adapters/fetch';
|
|
332
|
+
import { createContext } from './context.ts';
|
|
333
|
+
import { appRouter } from './router.ts';
|
|
334
|
+
|
|
335
|
+
function handler(request) {
|
|
90
336
|
return fetchRequestHandler({
|
|
91
337
|
endpoint: '/trpc',
|
|
92
|
-
req:
|
|
338
|
+
req: request,
|
|
93
339
|
router: appRouter,
|
|
94
340
|
createContext,
|
|
95
341
|
});
|
|
96
|
-
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
Deno.serve(handler);
|
|
97
345
|
```
|
|
98
346
|
|
|
347
|
+
Run `deno run --allow-net=:8000 --allow-env ./server.ts` and your endpoints will be available via HTTP!
|
|
348
|
+
|
|
349
|
+
| Endpoint | HTTP URI |
|
|
350
|
+
| ------------ | -------------------------------------------------------------------------------------------------------------- |
|
|
351
|
+
| `getUser` | `GET http://localhost:8000/trpc/getUserById?input=INPUT` <br/><br/>where `INPUT` is a URI-encoded JSON string. |
|
|
352
|
+
| `createUser` | `POST http://localhost:8000/trpc/createUser` <br/><br/>with `req.body` of type `User` |
|
|
353
|
+
|
|
354
|
+
### Next.js Edge Runtime
|
|
355
|
+
|
|
356
|
+
See a full example [here](https://github.com/trpc/trpc/tree/main/examples/next-edge-runtime).
|
|
357
|
+
|
|
99
358
|
### Remix
|
|
100
359
|
|
|
101
|
-
```ts
|
|
102
|
-
// app/routes/trpc.$trpc.ts
|
|
360
|
+
```ts no-check title="app/routes/trpc.$trpc.ts"
|
|
103
361
|
import type { ActionFunctionArgs, LoaderFunctionArgs } from '@remix-run/node';
|
|
104
362
|
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
|
|
105
363
|
import { createContext } from '~/server/context';
|
|
106
364
|
import { appRouter } from '~/server/router';
|
|
107
365
|
|
|
366
|
+
export const loader = async (args: LoaderFunctionArgs) => {
|
|
367
|
+
return handleRequest(args);
|
|
368
|
+
};
|
|
369
|
+
export const action = async (args: ActionFunctionArgs) => {
|
|
370
|
+
return handleRequest(args);
|
|
371
|
+
};
|
|
108
372
|
function handleRequest(args: LoaderFunctionArgs | ActionFunctionArgs) {
|
|
109
373
|
return fetchRequestHandler({
|
|
110
374
|
endpoint: '/trpc',
|
|
@@ -113,87 +377,100 @@ function handleRequest(args: LoaderFunctionArgs | ActionFunctionArgs) {
|
|
|
113
377
|
createContext,
|
|
114
378
|
});
|
|
115
379
|
}
|
|
116
|
-
|
|
117
|
-
export const loader = async (args: LoaderFunctionArgs) => handleRequest(args);
|
|
118
|
-
export const action = async (args: ActionFunctionArgs) => handleRequest(args);
|
|
119
380
|
```
|
|
120
381
|
|
|
121
|
-
###
|
|
382
|
+
### SolidStart
|
|
122
383
|
|
|
123
|
-
```ts
|
|
124
|
-
import { fetchRequestHandler } from '
|
|
125
|
-
import {
|
|
126
|
-
import {
|
|
384
|
+
```ts no-check title="src/routes/api/trpc/[trpc].ts"
|
|
385
|
+
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
|
|
386
|
+
import type { APIEvent } from '@solidjs/start/server';
|
|
387
|
+
import { createContext } from '../../server/context';
|
|
388
|
+
import { appRouter } from '../../server/router';
|
|
127
389
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
endpoint: '/trpc',
|
|
131
|
-
req: request,
|
|
390
|
+
const handler = (event: APIEvent) =>
|
|
391
|
+
fetchRequestHandler({
|
|
392
|
+
endpoint: '/api/trpc',
|
|
393
|
+
req: event.request,
|
|
132
394
|
router: appRouter,
|
|
133
395
|
createContext,
|
|
134
396
|
});
|
|
135
|
-
}
|
|
397
|
+
export { handler as GET, handler as POST };
|
|
136
398
|
```
|
|
137
399
|
|
|
138
|
-
###
|
|
400
|
+
### Vercel Edge Runtime
|
|
139
401
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
import { createContext } from './context';
|
|
143
|
-
import { appRouter } from './router';
|
|
402
|
+
> [!NOTE]
|
|
403
|
+
> See the official [Vercel Edge Runtime documentation](https://edge-runtime.vercel.app/getting-started) for more information.
|
|
144
404
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
405
|
+
|
|
406
|
+
> [!TIP]
|
|
407
|
+
> See our example [Vercel Edge Runtime app](https://github.com/trpc/trpc/tree/main/examples/vercel-edge-runtime) for a working example.
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
#### Install dependencies
|
|
411
|
+
|
|
412
|
+
<div class="code-tabs" data-tabs="package-manager">
|
|
413
|
+
<div data-tab="npm" data-active>
|
|
414
|
+
|
|
415
|
+
```sh
|
|
416
|
+
npm install -g edge-runtime
|
|
156
417
|
```
|
|
157
418
|
|
|
158
|
-
|
|
419
|
+
</div>
|
|
420
|
+
<div data-tab="yarn">
|
|
159
421
|
|
|
160
|
-
|
|
422
|
+
```sh
|
|
423
|
+
yarn global add edge-runtime
|
|
424
|
+
```
|
|
161
425
|
|
|
162
|
-
|
|
426
|
+
</div>
|
|
427
|
+
<div data-tab="pnpm">
|
|
163
428
|
|
|
164
|
-
|
|
429
|
+
```sh
|
|
430
|
+
pnpm add -g edge-runtime
|
|
431
|
+
```
|
|
165
432
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
router: appRouter,
|
|
172
|
-
createContext,
|
|
173
|
-
});
|
|
433
|
+
</div>
|
|
434
|
+
<div data-tab="bun">
|
|
435
|
+
|
|
436
|
+
```sh
|
|
437
|
+
bun add -g edge-runtime
|
|
174
438
|
```
|
|
175
439
|
|
|
176
|
-
|
|
440
|
+
</div>
|
|
441
|
+
</div>
|
|
442
|
+
|
|
443
|
+
#### Create Edge Runtime Function
|
|
444
|
+
|
|
445
|
+
```ts no-check title="server.ts"
|
|
446
|
+
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
|
|
447
|
+
import { createContext } from './context';
|
|
448
|
+
import { appRouter } from './router';
|
|
177
449
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
450
|
+
// Vercel Edge Runtime uses Service Worker-style addEventListener
|
|
451
|
+
addEventListener('fetch', (event: any) => {
|
|
452
|
+
return event.respondWith(
|
|
453
|
+
fetchRequestHandler({
|
|
454
|
+
endpoint: '/trpc',
|
|
455
|
+
req: event.request,
|
|
456
|
+
router: appRouter,
|
|
457
|
+
createContext,
|
|
458
|
+
}),
|
|
459
|
+
);
|
|
185
460
|
});
|
|
186
461
|
```
|
|
187
462
|
|
|
188
|
-
|
|
463
|
+
Run `edge-runtime --listen server.ts --port 3000` and your endpoints will be available via HTTP!
|
|
464
|
+
|
|
465
|
+
| Endpoint | HTTP URI |
|
|
466
|
+
| ------------ | -------------------------------------------------------------------------------------------------------------- |
|
|
467
|
+
| `getUser` | `GET http://localhost:3000/trpc/getUserById?input=INPUT` <br/><br/>where `INPUT` is a URI-encoded JSON string. |
|
|
468
|
+
| `createUser` | `POST http://localhost:3000/trpc/createUser` <br/><br/>with `req.body` of type `User` |
|
|
469
|
+
|
|
470
|
+
## Common mistakes
|
|
189
471
|
|
|
190
|
-
|
|
472
|
+
- **An `endpoint` that doesn't match the route.** tRPC removes `endpoint` from the start of the URL path to find the procedure, so a handler that serves `/api/trpc/*` needs `endpoint: '/api/trpc'`. With the wrong `endpoint`, every call returns `404 NOT_FOUND`.
|
|
191
473
|
|
|
192
|
-
##
|
|
474
|
+
## Related skills
|
|
193
475
|
|
|
194
|
-
-
|
|
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
|
|
476
|
+
- `server-setup` (https://trpc.io/docs/server/overview): `npx @tanstack/intent@latest load @trpc/server#server-setup`
|