hattipjs / hattip

Like Express, but for the future
MIT License
1.27k stars 16 forks source link

global SessionData type in session middleware #39

Closed benatkin closed 1 year ago

benatkin commented 1 year ago

The SessionData type in session middleware is a global type with instructions to override in the README.

What if there are different session middlewares? For instance I might use one session middleware to log in and another one for a logged in user.

Perhaps a Generic could be used instead? Like session<MySessionData>()?

cyco130 commented 1 year ago

Currently the session middleware uses a hard-coded key (session) in the context object, so adding more than one session is not currently possible. I agree it should be implemented and I had a vague plan to do it. I'll have a look soon.

Until then, you can use parts of a single session object for different purposes as a workaround. E.g. you can add different keys to SessionData for different purposes.

benatkin commented 1 year ago

Thanks.

Having a hard coded key in the context object might be outdated on the frontend and perhaps an alternative on the backend would be good as well. I think it would need to work like Vue Provide/Inject or React Contexts and Zustand.

Hattip might already be friendly to leaving the context object alone. Perhaps a separate package that creates the session context key and one that can be used to manually create one that uses the provide/inject pattern or a custom context key would be nice.

cyco130 commented 1 year ago

Hattip might already be friendly to leaving the context object alone.

Sharing information between middleware and request handlers is exactly what the context object is for. It is the container for the provide/inject pattern, i.e., it is the equivalent of React context, for example.

HatTip's middleware system is modeled after Express and Koa and deliberately kept simple. We rejected a more functional DI approach to keep things simple. You can always build a more structured system on top of it (like NestJS is built on Express) but it's out of scope for HatTip.