Skip to content

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

Closed
@alvaro-cuesta

Description

@alvaro-cuesta

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpendingShould be resolved in next release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions