@@ -98,18 +98,20 @@ export type Router = typeof router
98
98
99
99
Normally all we need to use tRPC is to export the type of router, but to integrate tRPC with Elysia, we need to export the instance of router too.
100
100
101
- Then in the Elysia server, we import the router and register tRPC router with ` .trpc `
101
+ Then in the Elysia server, we import the router and register tRPC router with ` .use ( trpc ) `
102
102
` ` ` typescript
103
103
import { Elysia } from ' elysia'
104
104
import { cors } from ' @elysiajs/cors' // [!code ++]
105
- import ' @elysiajs/trpc' // [!code ++]
105
+ import { trpc } ' @elysiajs/trpc' // [!code ++]
106
106
107
107
import { router } from ' ./trpc' // [!code ++]
108
108
109
109
const app = new Elysia ()
110
110
.use (cors ()) // [!code ++]
111
111
.get (' /' , () => ' Hello Elysia' )
112
- .trpc (router ) // [!code ++]
112
+ .use ( // [!code ++]
113
+ trpc (router ) // [!code ++]
114
+ ) // [!code ++]
113
115
.listen (3000 )
114
116
115
117
console .log (` 🦊 Elysia is running at ${app .server ?.hostname }:${app .server ?.port } ` )
@@ -120,7 +122,7 @@ And that's it! 🎉
120
122
That's all it takes to integrate tRPC with Elysia, making tRPC run on Bun.
121
123
122
124
## tRPC config and Context
123
- To create context, ` . trpc` can accept 2nd parameters that can configure tRPC as same as ` createHTTPServer ` .
125
+ To create context, ` trpc ` can accept 2nd parameters that can configure tRPC as same as ` createHTTPServer ` .
124
126
125
127
For example, adding ` createContext ` into tRPC server:
126
128
``` typescript
@@ -156,9 +158,11 @@ import { router, createContext } from './trpc' // [!code ++]
156
158
const app = new Elysia ()
157
159
.use (cors ())
158
160
.get (' /' , () => ' Hello Elysia' )
159
- .trpc (router , {
160
- createContext // [!code ++]
161
- })
161
+ .use (
162
+ trpc (router , { // [!code ++]
163
+ createContext // [!code ++]
164
+ }) // [!code ++]
165
+ )
162
166
.listen (3000 )
163
167
164
168
console .log (` 🦊 Elysia is running at ${app .server ?.hostname }:${app .server ?.port } ` )
@@ -168,17 +172,19 @@ And we can specify a custom endpoint of tRPC by using `endpoint`:
168
172
``` typescript
169
173
import { Elysia } from ' elysia'
170
174
import { cors } from ' @elysiajs/cors'
171
- import ' @elysiajs/trpc'
175
+ import { trpc } from ' @elysiajs/trpc'
172
176
173
177
import { router , createContext } from ' ./trpc'
174
178
175
179
const app = new Elysia ()
176
180
.use (cors ())
177
181
.get (' /' , () => ' Hello Elysia' )
178
- .trpc (router , {
179
- createContext ,
180
- endpoint: ' /v2/trpc' // [!code ++]
181
- })
182
+ .use (
183
+ trpc (router , {
184
+ createContext ,
185
+ endpoint: ' /v2/trpc' // [!code ++]
186
+ })
187
+ )
182
188
.listen (3000 )
183
189
184
190
console .log (` 🦊 Elysia is running at ${app .server ?.hostname }:${app .server ?.port } ` )
@@ -233,16 +239,15 @@ export type Router = typeof router
233
239
234
240
And then we register:
235
241
` ` ` typescript
236
- import { Elysia } from ' elysia'
242
+ import { Elysia , ws } from ' elysia'
237
243
import { cors } from ' @elysiajs/cors'
238
- import { websocket } from ' @elysiajs/websocket' // [!code ++]
239
244
import ' @elysiajs/trpc'
240
245
241
246
import { router , createContext } from ' ./trpc'
242
247
243
248
const app = new Elysia ()
244
249
.use (cors ())
245
- .use (websocket ()) // [!code ++]
250
+ .use (ws ()) // [!code ++]
246
251
.get (' /' , () => ' Hello Elysia' )
247
252
.trpc (router , {
248
253
createContext
@@ -267,20 +272,21 @@ This means that you can use Express-like syntax to create RESTful API with full-
267
272
To get started, let's export the app type.
268
273
269
274
``` typescript
270
- import { Elysia } from ' elysia'
275
+ import { Elysia , ws } from ' elysia'
271
276
import { cors } from ' @elysiajs/cors'
272
- import { websocket } from ' @elysiajs/websocket'
273
- import ' @elysiajs/trpc'
277
+ import { trpc } from ' @elysiajs/trpc'
274
278
275
279
import { router , createContext } from ' ./trpc'
276
280
277
281
const app = new Elysia ()
278
282
.use (cors ())
279
- .use (websocket ())
283
+ .use (ws ())
280
284
.get (' /' , () => ' Hello Elysia' )
281
- .trpc (router , {
282
- createContext
283
- })
285
+ .use (
286
+ trpc (router , {
287
+ createContext
288
+ })
289
+ )
284
290
.listen (3000 )
285
291
286
292
export type App = typeof app // [!code ++]
0 commit comments