elysiajs / elysia

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

The plugin with scoped settings is not functioning correctly. #559

Closed hagishi closed 2 months ago

hagishi commented 7 months ago

What version of Elysia.JS is running?

1.0.5

What platform is your computer?

Darwin 23.1.0 arm64 arm

What steps can reproduce the bug?

When a plugin with "scoped" setting is referenced in multiple routers, the type can be referenced in the latter router, but becomes undefined at runtime when executed.

it("scoped run", async () => {
  const plugin = new Elysia({ name: "plugin" }).derive({ as: "scoped" }, () => {
    return { id: 1 };
  });

  const a = new Elysia().use(plugin).get("/foo", (c) => {
     return { id: c.id, name: "foo" };
  });

  const b = new Elysia().use(plugin).get("/bar", (c) => {
     return { id: c.id, name: "bar" };
  });

  console.log((await treaty(a).foo.get()).data);
  // show:  { id: 1, name: 'foo' }
  console.log((await treaty(b).bar.get()).data);
  // show: { name: 'bar' }
});

What is the expected behavior?

A plugin with the "scoped" setting is expected to be referenced at runtime within the scope it's defined.

What do you see instead?

The type can be referenced but becomes undefined at runtime.

Additional information

No response

CodyTseng commented 6 months ago

I executed your test cases and obtained the expected results.

{
  id: 1,
  name: "foo",
}
{
  id: 1,
  name: "bar",
}
SaltyAom commented 2 months ago

Closing as unable to reproduce. Thanks.