elysiajs / elysia

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

`aot` does not recognize the use of `ctx.body` within a `try catch` #658

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?

// test.test.ts
import { describe, it, expect } from "bun:test";
import { Elysia, type Context } from "elysia";

describe("AOT", () => {
  it("???", async () => {
    class Controller {
      static async handle(ctx: Context) {
        try {
          // @ts-ignore
          const { token } = ctx.body;
          return token;
        } catch {
          return "nope";
        }
      }
    }

    const app = new Elysia({ aot: true }).post("/", Controller.handle);

    const response = await app.handle(
      new Request("http://localhost/", {
        method: "POST",
        body: JSON.stringify({ token: "yay" }),
        headers: { "Content-Type": "application/json" },
      })
    );

    expect(await response.text()).toEqual("yay");
  });
});

What is the expected behavior?

Pass all tests

What do you see instead?

Expected: "yay"
Received: "nope"

Additional information

workaround:

new Elysia({ aot: false })

reported on discord

bogeychan commented 1 month ago

Looks like its related to this: https://github.com/elysiajs/elysia/issues/656