Skip to content

Commit

Permalink
🔧 fix: undefined route context on aot=false
Browse files Browse the repository at this point in the history
It looks like the `route` context is returning `undefined` if the
`aot=false`.

This commit try to fix it by passing the `route` to
`createDynamicHandler`

Fix: [#898](#898)
  • Loading branch information
hisamafahri committed Jan 30, 2025
1 parent 1ca0ef1 commit b731518
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/dynamic-handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const createDynamicHandler = (app: AnyElysia) => {
set,
// @ts-expect-error
store: app.singleton.store,
route: app._route,
request,
path,
qi,
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ export default class Elysia<
private dependencies: Record<string, Checksum[]> = {}

_routes: Routes = {} as any
_route: string = ""

_types = {
Prefix: '' as BasePath,
Expand Down Expand Up @@ -680,7 +681,7 @@ export default class Elysia<
validator,
hooks,
content: localHook?.type as string,
handle
handle,
})

if (this.config.strictPath === false)
Expand All @@ -699,6 +700,7 @@ export default class Elysia<
hooks: hooks,
compile: handle
})
this._route = path

return
}
Expand Down
56 changes: 56 additions & 0 deletions test/core/context.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { describe, expect, it } from 'bun:test'
import { Elysia } from '../../src'
import { req } from '../utils'

describe('context', () => {
describe('return route', () => {
it('on aot=true', async () => {
const app = new Elysia().get('/hi/:id', ({ route }) => route)
const res = await app.handle(req('/hi/123')).then((x) => x.text())
expect(res).toBe('/hi/:id')
})

it('on aot=false', async () => {
const app = new Elysia({ aot: false }).get(
'/hi/:id',
({ route }) => route
)
const res = await app.handle(req('/hi/123')).then((x) => x.text())
expect(res).toBe('/hi/:id')
})
})

describe('early return on macros with route data', () => {
it('on aot=true', async () => {
const app = new Elysia()
.macro({
test: {
beforeHandle({ route }) {
return route
}
}
})
.get('/hi/:id', () => 'should not returned', {
test: true
})
const res = await app.handle(req('/hi/123')).then((x) => x.text())
expect(res).toBe('/hi/:id')
})

it('on aot=false', async () => {
const app = new Elysia({ aot: false })
.macro({
test: {
beforeHandle({ route }) {
return route
}
}
})
.get('/hi/:id', () => 'should not returned', {
test: true
})
const res = await app.handle(req('/hi/123')).then((x) => x.text())
expect(res).toBe('/hi/:id')
})
})
})

0 comments on commit b731518

Please sign in to comment.