You can extend Elysia to fit your need. This is useful when you need to access extra values in a handler (e.g. a database connection).
In summary:
state
: assign value to Context.store
(a global state object of the Elysia instance)decorate
: assign value to Context
Context
is a parameter in the callback of handler.
version
is registered using state
, and accessible via Context.store.version
.getDate
is registered using decorate
, and accessible via Context.getDate
.By providing a function as a first parameters, the callback will accept current value, allowing us to remap the value to anything we like.
You can type state and decorator explicitly using TypeScript with as
:
If the explicit type doesn't type null
or undefined
, make sure to set strict
to true
in tsconfig.json
: