Skip to content

Commit

Permalink
fix: use @grammyjs/types and assume more types
Browse files Browse the repository at this point in the history
TypeScript can assume a lot of function return types which will be
more accurate that way
  • Loading branch information
EdJoPaTo committed Oct 20, 2021
1 parent ab62cb8 commit 62ae5b6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
"engines": {
"node": ">=12"
},
"dependencies": {
"@grammyjs/types": "^2.2.6"
},
"peerDependencies": {
"grammy": "^1.3.0"
},
Expand All @@ -47,7 +50,7 @@
"grammy": "^1.3.0",
"nyc": "^15.0.0",
"ts-node": "^10.0.0",
"typescript": "^4.2.0",
"typescript": "^4.4.4",
"xo": "^0.45.0"
},
"files": [
Expand Down
3 changes: 2 additions & 1 deletion source/body.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {InputFile, LabeledPrice, Location, ParseMode, Venue} from 'grammy/out/platform'
import {InputFile} from 'grammy'
import {LabeledPrice, Location, ParseMode, Venue} from '@grammyjs/types'

import {hasTruthyKey, isObject} from './generic-types'

Expand Down
2 changes: 1 addition & 1 deletion source/keyboard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {InlineKeyboardButton as TelegramInlineKeyboardButton} from 'grammy/out/platform'
import {InlineKeyboardButton as TelegramInlineKeyboardButton} from '@grammyjs/types'

import {ConstOrContextPathFunc, ContextPathFunc, filterNonNullable} from './generic-types'
import {combinePath} from './path'
Expand Down
3 changes: 1 addition & 2 deletions source/menu-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Composer, Context as BaseContext} from 'grammy'
import {Message} from 'grammy/out/platform'

import {ActionFunc} from './action-hive'
import {combineTrigger, createRootMenuTrigger, combinePath} from './path'
Expand Down Expand Up @@ -56,7 +55,7 @@ export class MenuMiddleware<Context extends BaseContext> {
* const menuMiddleware = new MenuMiddleware('/', menuTemplate)
* bot.command('start', async ctx => menuMiddleware.replyToContext(ctx))
*/
async replyToContext(context: Context, path = this.rootTrigger): Promise<Message> {
async replyToContext(context: Context, path = this.rootTrigger) {
if (typeof path === 'function') {
// Happens when a JS User does this as next is the second argument and not a string:
// ctx.command('start', menuMiddleware.replyToContext)
Expand Down
46 changes: 25 additions & 21 deletions source/send-menu.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Context as BaseContext, Api} from 'grammy'
import {InputMedia, Message} from 'grammy/out/platform'
import {Message} from '@grammyjs/types'

import {Body, TextBody, MediaBody, LocationBody, isMediaBody, isLocationBody, isTextBody, getBodyText, isVenueBody, VenueBody, isInvoiceBody} from './body'
import {ensurePathMenu} from './path'
Expand Down Expand Up @@ -30,7 +30,7 @@ export type EditMessageIntoMenuFunction<Context> = (chatId: number | string, mes
* @param path path of the menu
* @param other optional additional options
*/
export async function replyMenuToContext<Context extends BaseContext>(menu: MenuLike<Context>, context: Context, path: string, other?: Readonly<Record<string, unknown>>): Promise<Message> {
export async function replyMenuToContext<Context extends BaseContext>(menu: MenuLike<Context>, context: Context, path: string, other?: Readonly<Record<string, unknown>>) {
ensurePathMenu(path)
const body = await menu.renderBody(context, path)
const keyboard = await menu.renderKeyboard(context, path)
Expand All @@ -44,7 +44,7 @@ export async function replyMenuToContext<Context extends BaseContext>(menu: Menu
* @param path path of the menu
* @param other optional additional options
*/
export async function editMenuOnContext<Context extends BaseContext>(menu: MenuLike<Context>, context: Context, path: string, other: Readonly<Record<string, unknown>> = {}): Promise<Message | boolean> {
export async function editMenuOnContext<Context extends BaseContext>(menu: MenuLike<Context>, context: Context, path: string, other: Readonly<Record<string, unknown>> = {}) {
ensurePathMenu(path)
const body = await menu.renderBody(context, path)
const keyboard = await menu.renderKeyboard(context, path)
Expand All @@ -56,14 +56,15 @@ export async function editMenuOnContext<Context extends BaseContext>(menu: MenuL

if (isMediaBody(body)) {
if ('animation' in message || 'audio' in message || 'document' in message || 'photo' in message || 'video' in message) {
const media: InputMedia = {
type: body.type,
media: body.media,
caption: body.text,
parse_mode: body.parse_mode,
}

return context.editMessageMedia(media, createGenericOther(keyboard, other))
return context.editMessageMedia(
{
type: body.type,
media: body.media,
caption: body.text,
parse_mode: body.parse_mode,
},
createGenericOther(keyboard, other)
)
// eslint-disable-next-line promise/prefer-await-to-then
.catch(catchMessageNotModified)
}
Expand Down Expand Up @@ -108,7 +109,7 @@ export async function deleteMenuFromContext<Context extends BaseContext>(context
* @param path path of the menu
* @param other optional additional options
*/
export async function resendMenuToContext<Context extends BaseContext>(menu: MenuLike<Context>, context: Context, path: string, other: Readonly<Record<string, unknown>> = {}): Promise<Message> {
export async function resendMenuToContext<Context extends BaseContext>(menu: MenuLike<Context>, context: Context, path: string, other: Readonly<Record<string, unknown>> = {}) {
const [menuMessage] = await Promise.all([
replyMenuToContext(menu, context, path, other),
deleteMenuFromContext(context),
Expand All @@ -125,7 +126,7 @@ function catchMessageNotModified(error: unknown): false {
throw error
}

async function replyRenderedMenuPartsToContext<Context extends BaseContext>(body: Body, keyboard: InlineKeyboard, context: Context, other: Readonly<Record<string, unknown>> = {}): Promise<Message> {
async function replyRenderedMenuPartsToContext<Context extends BaseContext>(body: Body, keyboard: InlineKeyboard, context: Context, other: Readonly<Record<string, unknown>> = {}) {
if (isMediaBody(body)) {
const mediaOther = createSendMediaOther(body, keyboard, other)

Expand Down Expand Up @@ -233,14 +234,17 @@ export function generateEditMessageIntoMenuFunction<Context>(telegram: Readonly<
const keyboard = await menu.renderKeyboard(context, path)

if (isMediaBody(body)) {
const media: InputMedia = {
type: body.type,
media: body.media,
caption: body.text,
parse_mode: body.parse_mode,
}

return telegram.editMessageMedia(chatId, messageId, media, createGenericOther(keyboard, other))
return telegram.editMessageMedia(
chatId,
messageId,
{
type: body.type,
media: body.media,
caption: body.text,
parse_mode: body.parse_mode,
},
createGenericOther(keyboard, other),
)
}

if (isLocationBody(body)) {
Expand Down

0 comments on commit 62ae5b6

Please sign in to comment.