Skip to content

v0.40.0 — request decorators

Choose a tag to compare

@tshafer tshafer released this 11 Jul 06:30
· 120 commits to main since this release

Attach named, computed values to the current request — request.user / tenant / locale — resolved lazily and memoized for the life of the request. Inspired by Fastify's decorators, but without the footguns.

  • decorateRequest(name, resolver) registers a resolver (sync or async).
  • decorated(name) reads it — computed on first access, cached for the rest of the request.
  • setRequestValue(name, value) sets it imperatively (e.g. from auth middleware), skipping the resolver.
  • hasRequestDecorator(name) checks registration.

The per-request memo is keyed off the Hono context via a WeakMap, so nothing leaks between requests and there's no null-placeholder / onRequest-hook dance. (Decorating the application is already the service container's job.)

241 tests; type-checked doc examples. See docs/decorators.md.