Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ctx.sessionKey #1

Merged
merged 2 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export interface SessionFlavor<S> {
*/
get session(): S;
set session(session: S | undefined);
/**
* Used by the session plugin to store the current session key, if it
* exists.
*/
sessionKey: string | undefined;
}
/**
* A lazy session flavor is a context flavor that holds a promise of some
Expand Down Expand Up @@ -166,6 +171,15 @@ type MultiSessionOptionsRecord<
[K in keyof S]: SessionOptions<S[K], C>;
};

function setSessionKey(ctx: Context, key?: string) {
Object.defineProperty(ctx, "sessionKey", {
value: key,
writable: false,
configurable: false,
enumerable: true,
});
}

/**
* Session middleware provides a persistent data storage for your bot. You can
* use it to let your bot remember any data you want, for example the messages
Expand Down Expand Up @@ -233,6 +247,7 @@ function strictSingleSession<S, C extends Context>(
initial,
);
const key = await getSessionKey(ctx);
setSessionKey(ctx, key);
await propSession.init(key, { custom, lazy: false });
await next(); // no catch: do not write back if middleware throws
await propSession.finish();
Expand All @@ -257,6 +272,7 @@ function strictMultiSession<S, C extends Context>(
initial,
);
const key = await getSessionKey(ctx);
setSessionKey(ctx, key);
await s.init(key, { custom, lazy: false });
return s;
}));
Expand Down Expand Up @@ -314,6 +330,7 @@ export function lazySession<S, C extends Context>(
initial,
);
const key = await getSessionKey(ctx);
setSessionKey(ctx, key);
await propSession.init(key, { custom, lazy: true });
await next(); // no catch: do not write back if middleware throws
await propSession.finish();
Expand Down
56 changes: 56 additions & 0 deletions test/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,23 @@ describe("session", () => {
assertEquals(storage.write.calls.length, 4);
assertEquals(storage.delete.calls.length, 1);
});

it("should set ctx.sessionKey", async () => {
const storage = {
read: () => Promise.resolve(),
write: () => {},
delete: () => {},
};

const initial = () => ({});
type C = Context & SessionFlavor<never>;
const composer = new Composer<C>();
const ctx = { chatId: 42 } as C;
composer.use(session({ storage, initial }));

await composer.middleware()(ctx, next);
assertEquals(ctx.sessionKey, "42");
});
});

describe("multi session", () => {
Expand Down Expand Up @@ -471,6 +488,34 @@ describe("multi session", () => {
assertEquals(storage1.write.calls.length, 4);
assertEquals(storage1.delete.calls.length, 1);
});

it("should set ctx.sessionKey", async () => {
const val = {};
const storage = {
read: () => Promise.resolve(val),
write: () => {},
delete: () => {},
};

const initial = () => ({});
type SessionData = {
prop0: Record<string, number>;
prop1: Record<string, number>;
};
type C = Context & SessionFlavor<SessionData>;
const composer = new Composer<C>();
const ctx = { chatId: 42 } as C;
composer.use(
session({
type: "multi",
prop0: { storage, initial },
prop1: { storage, initial },
}),
);

await composer.middleware()(ctx, next);
assertEquals(ctx.sessionKey, "42");
});
});

describe("lazy session", () => {
Expand Down Expand Up @@ -757,6 +802,17 @@ describe("lazy session", () => {
assert(done);
await p;
});

it("should set ctx.sessionKey", async () => {
type C = Context & SessionFlavor<never>;
const composer = new Composer<C>();
const ctx = { chatId: 42 } as C;
composer.use(lazySession());

await composer.middleware()(ctx, next);

assertEquals(ctx.sessionKey, "42");
});
});

describe("enhanceStorage", () => {
Expand Down