From 29a6f3781fa654effc3461e90f63dd9ad2f265e6 Mon Sep 17 00:00:00 2001 From: webwarrior-ws Date: Fri, 21 Feb 2025 14:47:00 +0100 Subject: [PATCH] bot/middleware: convert to TS (#632) Convert bot/middleware to TypeScript. --- bot/middleware/{commands.js => commands.ts} | 8 +++++--- bot/middleware/index.js | 15 --------------- bot/middleware/index.ts | 11 +++++++++++ bot/middleware/{stage.js => stage.ts} | 13 +++++++------ bot/middleware/{user.js => user.ts} | 14 ++++++-------- 5 files changed, 29 insertions(+), 32 deletions(-) rename bot/middleware/{commands.js => commands.ts} (86%) delete mode 100644 bot/middleware/index.js create mode 100644 bot/middleware/index.ts rename bot/middleware/{stage.js => stage.ts} (77%) rename bot/middleware/{user.js => user.ts} (53%) diff --git a/bot/middleware/commands.js b/bot/middleware/commands.ts similarity index 86% rename from bot/middleware/commands.js rename to bot/middleware/commands.ts index 06479f55..75e68b63 100644 --- a/bot/middleware/commands.js +++ b/bot/middleware/commands.ts @@ -1,4 +1,6 @@ -const commandArgs = () => (ctx, next) => { +import { CommunityContext } from "../modules/community/communityContext"; + +const commandArgs = () => (ctx: CommunityContext, next: () => void) => { if (ctx.message && ctx.message.text) { const text = ctx.message.text; if (text.startsWith('/')) { @@ -12,7 +14,7 @@ const commandArgs = () => (ctx, next) => { command = match[1]; } let next_arg = ['', '', match[2]]; - while ((next_arg = re_next_arg.exec(next_arg[2]))) { + while ((next_arg = re_next_arg.exec(next_arg[2])!)) { let quoted_arg = next_arg[1]; let unquoted_arg = ''; while (quoted_arg.length > 0) { @@ -49,4 +51,4 @@ const commandArgs = () => (ctx, next) => { return next(); }; -module.exports = commandArgs; +export default commandArgs; diff --git a/bot/middleware/index.js b/bot/middleware/index.js deleted file mode 100644 index 67626ac4..00000000 --- a/bot/middleware/index.js +++ /dev/null @@ -1,15 +0,0 @@ -const commandArgsMiddleware = require('./commands'); -const { stageMiddleware } = require('./stage'); -const { - userMiddleware, - adminMiddleware, - superAdminMiddleware, -} = require('./user'); - -module.exports = { - commandArgsMiddleware, - stageMiddleware, - userMiddleware, - adminMiddleware, - superAdminMiddleware, -}; diff --git a/bot/middleware/index.ts b/bot/middleware/index.ts new file mode 100644 index 00000000..152556ac --- /dev/null +++ b/bot/middleware/index.ts @@ -0,0 +1,11 @@ +import commandArgsMiddleware from './commands'; +import { stageMiddleware } from './stage'; +import { userMiddleware, adminMiddleware, superAdminMiddleware } from './user'; + +export { + commandArgsMiddleware, + stageMiddleware, + userMiddleware, + adminMiddleware, + superAdminMiddleware, +}; diff --git a/bot/middleware/stage.js b/bot/middleware/stage.ts similarity index 77% rename from bot/middleware/stage.js rename to bot/middleware/stage.ts index 145d46c1..a1f90ce0 100644 --- a/bot/middleware/stage.js +++ b/bot/middleware/stage.ts @@ -1,15 +1,16 @@ // @ts-check -const { Scenes } = require('telegraf'); -const CommunityModule = require('../modules/community'); +import { Scenes } from 'telegraf'; +import * as CommunityModule from '../modules/community'; const OrdersModule = require('../modules/orders'); -const UserModule = require('../modules/user'); +import * as UserModule from '../modules/user'; +import { CommunityContext } from '../modules/community/communityContext'; const { addInvoiceWizard, addFiatAmountWizard, addInvoicePHIWizard, } = require('../scenes'); -exports.stageMiddleware = () => { +export const stageMiddleware = () => { const scenes = [ addInvoiceWizard, addFiatAmountWizard, @@ -34,8 +35,8 @@ exports.stageMiddleware = () => { return stage.middleware(); }; -function addGenericCommands(scene) { - scene.command('exit', async ctx => { +function addGenericCommands(scene: Scenes.WizardScene) { + scene.command('exit', async (ctx) => { await ctx.scene.leave(); const text = ctx.i18n.t('wizard_exit'); await ctx.reply(text); diff --git a/bot/middleware/user.js b/bot/middleware/user.ts similarity index 53% rename from bot/middleware/user.js rename to bot/middleware/user.ts index 68098982..03ce55ac 100644 --- a/bot/middleware/user.js +++ b/bot/middleware/user.ts @@ -1,10 +1,8 @@ -const { - validateUser, - validateAdmin, - validateSuperAdmin, -} = require('../validations'); +import { MainContext } from "../start"; -exports.userMiddleware = async (ctx, next) => { +import { validateUser, validateAdmin, validateSuperAdmin } from '../validations'; + +export const userMiddleware = async (ctx: MainContext, next: () => void) => { const user = await validateUser(ctx, false); if (!user) return false; ctx.i18n.locale(user.lang); @@ -13,7 +11,7 @@ exports.userMiddleware = async (ctx, next) => { next(); }; -exports.adminMiddleware = async (ctx, next) => { +export const adminMiddleware = async (ctx: MainContext, next: () => void) => { const admin = await validateAdmin(ctx); if (!admin) return false; ctx.i18n.locale(admin.lang); @@ -22,7 +20,7 @@ exports.adminMiddleware = async (ctx, next) => { next(); }; -exports.superAdminMiddleware = async (ctx, next) => { +export const superAdminMiddleware = async (ctx: MainContext, next: () => void) => { const admin = await validateSuperAdmin(ctx); if (!admin) return false; ctx.i18n.locale(admin.lang);