Skip to content

Commit bebdc71

Browse files
committed
replace symbols with a string key
1 parent 9eb35f2 commit bebdc71

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

src/session.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import type { Session, SessionData, SessionStorage } from "react-router";
33

44
import { createMiddleware } from "hono/factory";
55

6-
const sessionStorageSymbol = Symbol().toString();
7-
const sessionSymbol = Symbol().toString();
6+
const sessionStorageKey = "sessionStorage";
7+
const sessionKey = "session";
88

99
export function session<Data = SessionData, FlashData = Data>(options: {
1010
autoCommit?: boolean;
@@ -13,7 +13,7 @@ export function session<Data = SessionData, FlashData = Data>(options: {
1313
return createMiddleware(async (c, next) => {
1414
let sessionStorage = options.createSessionStorage(c);
1515

16-
c.set(sessionStorageSymbol, sessionStorage);
16+
c.set(sessionStorageKey, sessionStorage);
1717

1818
// If autoCommit is disabled, we just create the SessionStorage and make it
1919
// available with c.get(sessionStorageSymbol), then call next() and
@@ -28,7 +28,7 @@ export function session<Data = SessionData, FlashData = Data>(options: {
2828
);
2929

3030
// And make it available with c.get(sessionSymbol).
31-
c.set(sessionSymbol, session);
31+
c.set(sessionKey, session);
3232

3333
// Then we call next() to let the rest of the middlewares run.
3434
await next();
@@ -43,7 +43,7 @@ export function session<Data = SessionData, FlashData = Data>(options: {
4343
export function getSessionStorage<Data = SessionData, FlashData = Data>(
4444
c: Context,
4545
): SessionStorage<Data, FlashData> {
46-
let sessionStorage = c.get(sessionStorageSymbol);
46+
let sessionStorage = c.get(sessionStorageKey);
4747
if (!sessionStorage) {
4848
throw new Error("A session middleware was not set.");
4949
}
@@ -53,7 +53,7 @@ export function getSessionStorage<Data = SessionData, FlashData = Data>(
5353
export function getSession<Data = SessionData, FlashData = Data>(
5454
c: Context,
5555
): Session<Data, FlashData> {
56-
let session = c.get(sessionSymbol);
56+
let session = c.get(sessionKey);
5757
if (!session) {
5858
throw new Error("A session middleware was not set.");
5959
}

test/session.test.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ describe(session.name, () => {
6262
await middleware(c, next);
6363

6464
expect(createSessionStorage).toHaveBeenCalledOnce();
65-
expect(c.set).toHaveBeenNthCalledWith(
66-
1,
67-
expect.any(String),
68-
sessionStorage,
69-
);
65+
expect(c.set).toHaveBeenNthCalledWith(1, "sessionStorage", sessionStorage);
7066
expect(next).toHaveBeenCalledOnce();
7167
expect(spy.getSession).not.toBeCalled();
7268
});
@@ -82,20 +78,12 @@ describe(session.name, () => {
8278
await middleware(c, next);
8379

8480
expect(createSessionStorage).toHaveBeenCalledOnce();
85-
expect(c.set).toHaveBeenNthCalledWith(
86-
1,
87-
expect.any(String),
88-
sessionStorage,
89-
);
81+
expect(c.set).toHaveBeenNthCalledWith(1, "sessionStorage", sessionStorage);
9082
expect(spy.getSession).toHaveBeenCalledOnce();
9183

9284
let sessionInContext = await sessionStorage.getSession();
9385

94-
expect(c.set).toHaveBeenNthCalledWith(
95-
2,
96-
expect.any(String),
97-
sessionInContext,
98-
);
86+
expect(c.set).toHaveBeenNthCalledWith(2, "session", sessionInContext);
9987
expect(next).toHaveBeenCalledOnce();
10088
expect(c.header).toHaveBeenLastCalledWith(
10189
"set-cookie",
@@ -121,7 +109,7 @@ describe(getSessionStorage.name, () => {
121109
"A session middleware was not set.",
122110
);
123111

124-
expect(c.get).toHaveBeenCalledWith(expect.any(String));
112+
expect(c.get).toHaveBeenCalledWith("sessionStorage");
125113
});
126114

127115
test("returns session storage", async () => {
@@ -147,7 +135,7 @@ describe(getSession.name, () => {
147135
"A session middleware was not set.",
148136
);
149137

150-
expect(c.get).toHaveBeenCalledWith(expect.any(String));
138+
expect(c.get).toHaveBeenCalledWith("session");
151139
});
152140

153141
test("returns session", async () => {

0 commit comments

Comments
 (0)