Skip to content
rest-rpc
Esc
↑↓navigate↵open⌘Jpreview
On this page

Context

Define a shared context for route handlers and middleware

Route Context

Route handlers and middleware share a mutable context object per request. Use context.set() to store values and context.get() to read them. Use route.$context<T>() to strongly type the context for set of routes or a single route.

const authenticatedRoute = route.$context<{
	user: { id: string; name: string };
}>();

Global Context

To avoid having to specify a context type for every route, you can define a default context type for you application globally.

declare module "@rest-rpc/express" {
	interface DefaultContext {
		env: {
			DATABASE_URL: string;
		};
	}
}

Was this page helpful?