Skip to content

Commit bbd83a9

Browse files
committed
🎉 feat: mount trace
1 parent 7199fc5 commit bbd83a9

File tree

4 files changed

+97
-10
lines changed

4 files changed

+97
-10
lines changed

example/a.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import { Elysia, t } from '../src'
22

33
const a = (request: Request) => new Response(request.url)
44

5-
const app = new Elysia({ systemRouter: false })
6-
.trace((a) => {
7-
a.onHandle(() => {
8-
// @ts-expect-error private property
9-
a.context.url
10-
})
5+
let url = ''
6+
let hasWrap = false
7+
8+
const app = new Elysia()
9+
.wrap((fn) => {
10+
console.log('A')
11+
12+
return fn
1113
})
12-
.get('/', () => 'ok')
14+
.mount('/', () => new Response('OK'))
1315
.listen(3000)
1416

1517
fetch('http://localhost:3000/a')

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "elysia",
33
"description": "Ergonomic Framework for Human",
4-
"version": "1.3.0-exp.68",
4+
"version": "1.3.0-exp.69",
55
"author": {
66
"name": "saltyAom",
77
"url": "https://github.com/SaltyAom",

src/adapter/bun/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,14 @@ export const BunAdapter: ElysiaAdapter = {
127127

128128
if (method === 'ALL') {
129129
if (!(`WS_${route.path}` in tree))
130-
routes[route.path] =
131-
route.hooks?.config?.mount ?? route.handler
130+
routes[route.path] = route.hooks?.config?.mount
131+
? route.hooks.trace ||
132+
app.event.trace ||
133+
// @ts-expect-error private property
134+
app.extender.higherOrderFunctions
135+
? createBunRouteHandler(app, route)
136+
: route.hooks.mount || route.handler
137+
: route.handler
132138

133139
continue
134140
}

test/bun/router.test.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,83 @@ describe('Bun router', () => {
160160
expect(url).toBe(`http://localhost:${app.server!.port}/`)
161161
expect(hasRequestId).toBe(true)
162162
})
163+
164+
it('handle wrap in mount', async () => {
165+
let url = ''
166+
let hasWrap = false
167+
168+
const app = new Elysia()
169+
.wrap((fn) => {
170+
hasWrap = true
171+
172+
return fn
173+
})
174+
.mount('/', (request) => new Response((url = request.url)))
175+
.listen(0)
176+
177+
await fetch(`http://localhost:${app.server!.port}/`)
178+
179+
expect(url).toBe(`http://localhost:${app.server!.port}/`)
180+
expect(hasWrap).toBe(true)
181+
})
182+
183+
it('handle trace url with wrap', async () => {
184+
let url = ''
185+
let hasRequestId = false
186+
let hasWrap = false
187+
188+
const app = new Elysia()
189+
.wrap((fn) => {
190+
hasWrap = true
191+
192+
return fn
193+
})
194+
.trace((a) => {
195+
a.onHandle(() => {
196+
// @ts-expect-error private property
197+
url = a.context.url
198+
199+
// @ts-expect-error private property
200+
hasRequestId = !!a.context[ELYSIA_REQUEST_ID]
201+
})
202+
})
203+
.get('/', () => 'ok')
204+
.listen(0)
205+
206+
await fetch(`http://localhost:${app.server!.port}/`)
207+
208+
expect(url).toBe(`http://localhost:${app.server!.port}/`)
209+
expect(hasWrap).toBe(true)
210+
expect(hasRequestId).toBe(true)
211+
})
212+
213+
it('handle mount', async () => {
214+
let url = ''
215+
let hasRequestId = false
216+
let hasWrap = false
217+
218+
const app = new Elysia()
219+
.wrap((fn) => {
220+
hasWrap = true
221+
222+
return fn
223+
})
224+
.trace((a) => {
225+
a.onHandle(() => {
226+
// @ts-expect-error private property
227+
url = a.context.url
228+
229+
// @ts-expect-error private property
230+
hasRequestId = !!a.context[ELYSIA_REQUEST_ID]
231+
})
232+
})
233+
.get('/', () => 'ok')
234+
.listen(0)
235+
236+
await fetch(`http://localhost:${app.server!.port}/`)
237+
238+
expect(url).toBe(`http://localhost:${app.server!.port}/`)
239+
expect(hasWrap).toBe(true)
240+
expect(hasRequestId).toBe(true)
241+
})
163242
})

0 commit comments

Comments
 (0)