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

Export types CommandMiddleware, HearsMiddleware, etc #154

Merged
merged 5 commits into from
May 19, 2022

Conversation

IlyaSemenov
Copy link
Contributor

@IlyaSemenov IlyaSemenov commented Jan 14, 2022

With this PR, it's possible to decompose command handlers into separate functions/modules.

Example:

// bot.ts
import { Bot, Context } from "grammy"

import { commands } from "./commands"

export type BotContext = Context & {}

const bot = new Bot<BotContext>()
bot.use(commands)
bot.use(/* ... */)
// commands/index.ts
import { Composer } from "grammy"

import { BotContext } from "../bot"

import { start_command } from "./start"
import { other_command } from "./other"

export const commands = new Composer<BotContext>()

commands.command("start", start_command)
commands.command("other", other_command)
// ...possibly 20 more commands
// no need to include all implementations in a single file or use dummy composers
// commands/start.ts
import { CommandMiddleware } from "grammy"

import { BotContext } from "../bot"

export const start_command: CommandMiddleware<BotContext> = async (ctx) => {
  console.log(ctx.match) // properly typed
}

Closes #100.

@KnorpelSenf
Copy link
Member

Thanks for working on #100! Would you mind exposing the other types in this PR, too?

@KnorpelSenf KnorpelSenf added this to In progress in Coding May 1, 2022
@KnorpelSenf
Copy link
Member

@IlyaSemenov could you have another look? I fixed it up.

@KnorpelSenf KnorpelSenf changed the title Export CommandMiddleware, HearsMiddleware Export types CommandMiddleware, HearsMiddleware, etc May 14, 2022
@KnorpelSenf KnorpelSenf moved this from In progress to Review in progress in Coding May 14, 2022
Coding automation moved this from Review in progress to Reviewer approved May 19, 2022
@KnorpelSenf KnorpelSenf merged commit 2eda2a5 into grammyjs:main May 19, 2022
Coding automation moved this from Reviewer approved to Done May 19, 2022
@KnorpelSenf
Copy link
Member

@all-contributors add @IlyaSemenov for code!

@allcontributors
Copy link
Contributor

@KnorpelSenf

I've put up a pull request to add @IlyaSemenov! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

feat: Expose narrowed context and middleware types
3 participants