Skip to content

Commit 520c495

Browse files
authored
Merge pull request #1040 from hisamafahri/feat/undefined-route
fix: undefined `route` context on `aot=false`
2 parents 0e2e134 + b731518 commit 520c495

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

src/dynamic-handle.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export const createDynamicHandler = (app: AnyElysia) => {
6363
set,
6464
// @ts-expect-error
6565
store: app.singleton.store,
66+
route: app._route,
6667
request,
6768
path,
6869
qi,

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ export default class Elysia<
195195
private dependencies: Record<string, Checksum[]> = {}
196196

197197
_routes: Routes = {} as any
198+
_route: string = ""
198199

199200
_types = {
200201
Prefix: '' as BasePath,
@@ -688,7 +689,7 @@ export default class Elysia<
688689
validator,
689690
hooks,
690691
content: localHook?.type as string,
691-
handle
692+
handle,
692693
})
693694

694695
if (this.config.strictPath === false)
@@ -706,6 +707,7 @@ export default class Elysia<
706707
handler: handle,
707708
hooks
708709
})
710+
this._route = path
709711

710712
return
711713
}

test/core/context.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { describe, expect, it } from 'bun:test'
2+
import { Elysia } from '../../src'
3+
import { req } from '../utils'
4+
5+
describe('context', () => {
6+
describe('return route', () => {
7+
it('on aot=true', async () => {
8+
const app = new Elysia().get('/hi/:id', ({ route }) => route)
9+
const res = await app.handle(req('/hi/123')).then((x) => x.text())
10+
expect(res).toBe('/hi/:id')
11+
})
12+
13+
it('on aot=false', async () => {
14+
const app = new Elysia({ aot: false }).get(
15+
'/hi/:id',
16+
({ route }) => route
17+
)
18+
const res = await app.handle(req('/hi/123')).then((x) => x.text())
19+
expect(res).toBe('/hi/:id')
20+
})
21+
})
22+
23+
describe('early return on macros with route data', () => {
24+
it('on aot=true', async () => {
25+
const app = new Elysia()
26+
.macro({
27+
test: {
28+
beforeHandle({ route }) {
29+
return route
30+
}
31+
}
32+
})
33+
.get('/hi/:id', () => 'should not returned', {
34+
test: true
35+
})
36+
const res = await app.handle(req('/hi/123')).then((x) => x.text())
37+
expect(res).toBe('/hi/:id')
38+
})
39+
40+
it('on aot=false', async () => {
41+
const app = new Elysia({ aot: false })
42+
.macro({
43+
test: {
44+
beforeHandle({ route }) {
45+
return route
46+
}
47+
}
48+
})
49+
.get('/hi/:id', () => 'should not returned', {
50+
test: true
51+
})
52+
const res = await app.handle(req('/hi/123')).then((x) => x.text())
53+
expect(res).toBe('/hi/:id')
54+
})
55+
})
56+
})

0 commit comments

Comments
 (0)