Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scoped: true: type leaks, lifecycle hooks don't go off, and state never gets initialized #127

Closed
ethanniser opened this issue Sep 11, 2023 · 2 comments

Comments

@ethanniser
Copy link

ethanniser commented Sep 11, 2023

I'm trying to used scoped: true and im running into some strange behavior

  1. useing an Elysia instace with scoped: true, doesn't remove the types from that instance, even though they shouldn’t be accessible

  2. store values in Elysia instaces with scoped: true seem to be wrongfully initiazed as undefined

  3. scoped lifecycle hooks don't seem to be working

import { Elysia } from "elysia";

const scoped = new Elysia({
  name: "scoped",
  scoped: true,
})
  .state("count", 0)
  .get("/scoped", ({ store }) => {
    console.log("store:", store);
    store.count += 1;
    return store.count;
  })
  .onRequest(() => {
    console.log("this should only trigger for /scoped");
  });

const app = new Elysia()
  .use(scoped)
  .get("/", ({ store }) => {
    console.log("store (should be empty):", store);
    console.log("this should not be possible:", store.count);
    // ^^ doesn't type error
    return "Hello World!";
  })
  .onRequest(({ request }) => {
    console.log(`request: ${request.method} ${request.url}`);
  })
  .listen(8080);

console.log(
  `🦊 Elysia is running at http://${app.server?.hostname}:${app.server?.port}`
);
🦊 Elysia is running at http://localhost:8080

request: GET http://localhost:8080/
store (should be empty): {}
this should not be possible: undefined

request: GET http://localhost:8080/scoped
store: {}

request: GET http://localhost:8080/scoped
store: {
  count: NaN
}

request: GET http://localhost:8080/
store (should be empty): {
  count: NaN
}
this should not be possible: NaN
@SaltyAom
Copy link
Member

SaltyAom commented Sep 12, 2023

Ok, seems like there's some area I haven't cover yet. I'll handle that in 0.7.
As a workaround, does wrapping the scope inside a guard hook should fix the problem.

@SaltyAom
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants