Skip to content

Allow type augmentation for Request.context#183

Open
kling90 wants to merge 2 commits into
sitevision:mainfrom
kling90:types-request-context
Open

Allow type augmentation for Request.context#183
kling90 wants to merge 2 commits into
sitevision:mainfrom
kling90:types-request-context

Conversation

@kling90

@kling90 kling90 commented Dec 17, 2025

Copy link
Copy Markdown
Contributor

The current inline type definition can't be augmented via TypeScript module augmentation. This change makes it possible to add type-safe properties to req.context when passing data from hooks to routes.

By extracting to a named Context interface you can now augment it:

import type { User } from './api/findUser';

declare module '@sitevision/api/common/router' {
  interface Context {
    user: User;
  }
}

In hooks.ts

hooks.beforeRender((req, res) => { 
  const user = findUser();
  req.context.user = user;
});

In index.ts

router.get('/', (req, res) => {
  const { user } = req.context;
});
image

Should the Session be an interface as well to make augmentation possible?

I made a PR for this suggestions since I guessed this isn't part of the definitions that get generated from JavaDoc?

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

Successfully merging this pull request may close these issues.

1 participant