Skip to content

Commit d7580f8

Browse files
committed
🎉 feat: update to match use migration
1 parent accc9d2 commit d7580f8

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

docs/blog/integrate-trpc-with-elysia.md

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,20 @@ export type Router = typeof router
9898
9999
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.
100100
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)`
102102
```typescript
103103
import { Elysia } from 'elysia'
104104
import { cors } from '@elysiajs/cors' // [!code ++]
105-
import '@elysiajs/trpc' // [!code ++]
105+
import { trpc } '@elysiajs/trpc' // [!code ++]
106106

107107
import { router } from './trpc' // [!code ++]
108108
109109
const app = new Elysia()
110110
.use(cors()) // [!code ++]
111111
.get('/', () => 'Hello Elysia')
112-
.trpc(router) // [!code ++]
112+
.use( // [!code ++]
113+
trpc(router) // [!code ++]
114+
) // [!code ++]
113115
.listen(3000)
114116

115117
console.log(`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`)
@@ -120,7 +122,7 @@ And that's it! 🎉
120122
That's all it takes to integrate tRPC with Elysia, making tRPC run on Bun.
121123

122124
## 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`.
124126

125127
For example, adding `createContext` into tRPC server:
126128
```typescript
@@ -156,9 +158,11 @@ import { router, createContext } from './trpc' // [!code ++]
156158
const app = new Elysia()
157159
.use(cors())
158160
.get('/', () => 'Hello Elysia')
159-
.trpc(router, {
160-
createContext // [!code ++]
161-
})
161+
.use(
162+
trpc(router, { // [!code ++]
163+
createContext // [!code ++]
164+
}) // [!code ++]
165+
)
162166
.listen(3000)
163167

164168
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`:
168172
```typescript
169173
import { Elysia } from 'elysia'
170174
import { cors } from '@elysiajs/cors'
171-
import '@elysiajs/trpc'
175+
import { trpc } from '@elysiajs/trpc'
172176

173177
import { router, createContext } from './trpc'
174178

175179
const app = new Elysia()
176180
.use(cors())
177181
.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+
)
182188
.listen(3000)
183189

184190
console.log(`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`)
@@ -233,16 +239,15 @@ export type Router = typeof router
233239
234240
And then we register:
235241
```typescript
236-
import { Elysia } from 'elysia'
242+
import { Elysia, ws } from 'elysia'
237243
import { cors } from '@elysiajs/cors'
238-
import { websocket } from '@elysiajs/websocket' // [!code ++]
239244
import '@elysiajs/trpc'
240245

241246
import { router, createContext } from './trpc'
242247

243248
const app = new Elysia()
244249
.use(cors())
245-
.use(websocket()) // [!code ++]
250+
.use(ws()) // [!code ++]
246251
.get('/', () => 'Hello Elysia')
247252
.trpc(router, {
248253
createContext
@@ -267,20 +272,21 @@ This means that you can use Express-like syntax to create RESTful API with full-
267272
To get started, let's export the app type.
268273

269274
```typescript
270-
import { Elysia } from 'elysia'
275+
import { Elysia, ws } from 'elysia'
271276
import { cors } from '@elysiajs/cors'
272-
import { websocket } from '@elysiajs/websocket'
273-
import '@elysiajs/trpc'
277+
import { trpc } from '@elysiajs/trpc'
274278

275279
import { router, createContext } from './trpc'
276280

277281
const app = new Elysia()
278282
.use(cors())
279-
.use(websocket())
283+
.use(ws())
280284
.get('/', () => 'Hello Elysia')
281-
.trpc(router, {
282-
createContext
283-
})
285+
.use(
286+
trpc(router, {
287+
createContext
288+
})
289+
)
284290
.listen(3000)
285291

286292
export type App = typeof app // [!code ++]

0 commit comments

Comments
 (0)