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

derive properties with { as: 'global' } are typed in onError context, but not present at runtime #615

Closed
alvaro-cuesta opened this issue Apr 19, 2024 · 3 comments
Labels
bug Something isn't working pending Should be resolved in next release

Comments

@alvaro-cuesta
Copy link

alvaro-cuesta commented Apr 19, 2024

What version of Elysia.JS is running?

1.0.13

What platform is your computer?

x64

What steps can reproduce the bug?

const app = new Elysia()
  .derive({ as: 'global' }, () => ({ hello: 'world' }))
  .onError(({ hello }) => `404: ${hello}`)
  .get('/', ({ hello }) => `Root: ${hello}`)
  .listen(3000)

What is the expected behavior?

  • / renders Root: world
  • /will-404 renders 404: world
    OR
    the .onError handler does not typecheck

What do you see instead?

  • / renders Root: world
  • /will-404 renders 404: undefined

Additional information

This seems to happen only when using { as: 'global' }. With 'scoped'/'local (or omitting) hello is not typed in onError (but is properly typed in e.g. handlers).

Not sure if it's the type or the runtime behavior that's wrong, but it felt natural to me to write it this way -- in particular I need to access my derived isAuthorized in onError when NOT_FOUND to avoid leaking my app structure to unauthorized users.

In my particular case { as: 'global' } is needed because I want to inject this isAuthorized globally from a plugin.

If onError (on router) isn't meant to access the derived properties (which might be the case according to the lifecycle graph, since derive happens on Transform Hook while NOT_FOUND I assume comes from Router), what's the alternative approach here, if any?

@alvaro-cuesta alvaro-cuesta added the bug Something isn't working label Apr 19, 2024
@alvaro-cuesta alvaro-cuesta changed the title as: 'global' derive is typed in onError but not present in runtime as: 'global' derive is typed in onError but not present at runtime Apr 19, 2024
@alvaro-cuesta alvaro-cuesta changed the title as: 'global' derive is typed in onError but not present at runtime derive with { as: 'global' } is typed in onError context, but not present at runtime Apr 19, 2024
@alvaro-cuesta alvaro-cuesta changed the title derive with { as: 'global' } is typed in onError context, but not present at runtime derive properties with { as: 'global' } are typed in onError context, but not present at runtime Apr 19, 2024
@SaltyAom
Copy link
Member

SaltyAom commented Apr 26, 2024

Patched on 1.0.14, please update and let me know if it works

@alvaro-cuesta
Copy link
Author

@SaltyAom doesn't seem to be fixed for me on Elysia 1.0.15.

@SaltyAom
Copy link
Member

derive shouldn't be present in onError as specified in Lifecycle.
referencing it on onError now should show a type error as 321a7c5, published under 1.0.21

@SaltyAom SaltyAom added the pending Should be resolved in next release label May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending Should be resolved in next release
Projects
None yet
Development

No branches or pull requests

2 participants