elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
9.09k stars 193 forks source link

`ctx.query` doesn't work in some cases with `aot: true` #656

Closed bogeychan closed 1 month ago

bogeychan commented 1 month ago

What version of Elysia.JS is running?

1.0.22

What platform is your computer?

WSL Ubuntu

What steps can reproduce the bug?

import { Elysia, type Context } from 'elysia'

const checker = {
    check: async (ctx: Context, name: string, state?: string) => {
        return typeof state !== 'undefined'
    }
}

new Elysia()
    .derive((ctx) => {
        const { name } = ctx.params

        return {
            check: async () => {
                const { state } = ctx.query

                if (!(await checker.check(ctx, name, state ?? ctx.query.state))) {
                    throw new Error('State mismatch')
                }
            }
        }
    })
    .get('/:name', async (ctx) => {
        await ctx.check()
        return 'yay'
    })
    .listen(8080)

Open http://localhost:8080/yay?state=123

What is the expected behavior?

Respond with yay (no error thrown)

What do you see instead?

{"name":"Error","message":"State mismatch"}

Additional information

AOT does not recognize:

only this seems to work:

const state = ctx.query.state


const { state } = ctx.query and const { query: { state } } = ctx used to work back in 1.0.3:

SaltyAom commented 1 month ago

346134b published under 1.0.23, including test case. Let me know if it works on your end.

doro running

bogeychan commented 1 month ago

Works for me, thnx

68747470733a2f2f6d656469612e74656e6f722e636f6d2f613061742d33646e447a6b414141414d2f646f726f2d646f726f7468792e676966