1- import type {
2- Session ,
3- SessionData ,
4- SessionStorage ,
5- } from "@remix-run/server-runtime" ;
61import type { Context } from "hono" ;
2+ import type { Session , SessionData , SessionStorage } from "react-router" ;
73
84import { createMiddleware } from "hono/factory" ;
95
10- const sessionStorageSymbol = Symbol ( ) ;
11- const sessionSymbol = Symbol ( ) ;
6+ type Env = {
7+ Variables : Record < symbol , unknown > ;
8+ } ;
9+
10+ const sessionStorageKey = Symbol ( ) ;
11+ const sessionKey = Symbol ( ) ;
1212
1313export function session < Data = SessionData , FlashData = Data > ( options : {
1414 autoCommit ?: boolean ;
1515 createSessionStorage ( c : Context ) : SessionStorage < Data , FlashData > ;
1616} ) {
17- return createMiddleware ( async ( c , next ) => {
17+ return createMiddleware < Env > ( async ( c , next ) => {
1818 let sessionStorage = options . createSessionStorage ( c ) ;
1919
20- c . set ( sessionStorageSymbol , sessionStorage ) ;
20+ c . set ( sessionStorageKey , sessionStorage ) ;
2121
2222 // If autoCommit is disabled, we just create the SessionStorage and make it
2323 // available with c.get(sessionStorageSymbol), then call next() and
@@ -32,7 +32,7 @@ export function session<Data = SessionData, FlashData = Data>(options: {
3232 ) ;
3333
3434 // And make it available with c.get(sessionSymbol).
35- c . set ( sessionSymbol , session ) ;
35+ c . set ( sessionKey , session ) ;
3636
3737 // Then we call next() to let the rest of the middlewares run.
3838 await next ( ) ;
@@ -45,19 +45,19 @@ export function session<Data = SessionData, FlashData = Data>(options: {
4545}
4646
4747export function getSessionStorage < Data = SessionData , FlashData = Data > (
48- c : Context ,
48+ c : Context < Env > ,
4949) : SessionStorage < Data , FlashData > {
50- let sessionStorage = c . get ( sessionStorageSymbol ) ;
50+ let sessionStorage = c . get ( sessionStorageKey ) ;
5151 if ( ! sessionStorage ) {
5252 throw new Error ( "A session middleware was not set." ) ;
5353 }
5454 return sessionStorage as SessionStorage < Data , FlashData > ;
5555}
5656
5757export function getSession < Data = SessionData , FlashData = Data > (
58- c : Context ,
58+ c : Context < Env > ,
5959) : Session < Data , FlashData > {
60- let session = c . get ( sessionSymbol ) ;
60+ let session = c . get ( sessionKey ) ;
6161 if ( ! session ) {
6262 throw new Error ( "A session middleware was not set." ) ;
6363 }
0 commit comments