From fdaab0ef6c99d8ad6f0225190e4ed05694ada1e6 Mon Sep 17 00:00:00 2001 From: deptyped Date: Tue, 23 Jul 2024 15:38:26 +0300 Subject: [PATCH] Wrap session plugin --- src/bot/index.ts | 12 +++--------- src/bot/middlewares/session.ts | 12 ++++++++++++ 2 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 src/bot/middlewares/session.ts diff --git a/src/bot/index.ts b/src/bot/index.ts index 74dd6fa2..2cfb9797 100644 --- a/src/bot/index.ts +++ b/src/bot/index.ts @@ -2,7 +2,7 @@ import { autoChatAction } from '@grammyjs/auto-chat-action' import { hydrate } from '@grammyjs/hydrate' import { hydrateReply, parseMode } from '@grammyjs/parse-mode' import type { BotConfig, StorageAdapter } from 'grammy' -import { Bot as TelegramBot, session } from 'grammy' +import { Bot as TelegramBot } from 'grammy' import { sequentialize } from '@grammyjs/runner' import { welcomeFeature } from './features/welcome.js' import { adminFeature } from './features/admin.js' @@ -10,6 +10,7 @@ import { languageFeature } from './features/language.js' import { unhandledFeature } from './features/unhandled.js' import { errorHandler } from './handlers/error.js' import { updateLogger } from './middlewares/update-logger.js' +import { session } from './middlewares/session.js' import type { Context, SessionData } from '#root/bot/context.js' import { createContextConstructor } from '#root/bot/context.js' import { i18n, isMultipleLocales } from '#root/bot/i18n.js' @@ -53,14 +54,7 @@ export function createBot(token: string, dependencies: Dependencies, options: Op protectedBot.use(autoChatAction(bot.api)) protectedBot.use(hydrateReply) protectedBot.use(hydrate()) - protectedBot.filter( - ctx => getSessionKey(ctx) !== 'undefined', - session({ - getSessionKey, - storage: options.botSessionStorage, - initial: () => ({}), - }), - ) + protectedBot.use(session({ getSessionKey, storage: options.botSessionStorage })) protectedBot.use(i18n) // Handlers diff --git a/src/bot/middlewares/session.ts b/src/bot/middlewares/session.ts new file mode 100644 index 00000000..f05d75e4 --- /dev/null +++ b/src/bot/middlewares/session.ts @@ -0,0 +1,12 @@ +import { type Middleware, type SessionOptions, session as createSession } from 'grammy' +import type { Context, SessionData } from '#root/bot/context.js' + +type Options = Pick, 'getSessionKey' | 'storage'> + +export function session(options: Options): Middleware { + return createSession({ + getSessionKey: options.getSessionKey, + storage: options.storage, + initial: () => ({}), + }) +}