refactor(client): made builder methods modify existing object instead of spreading, and also improved docs

This commit is contained in:
teidesu 2021-05-16 13:44:59 +03:00
parent 803daecdf3
commit d75071c284
4 changed files with 324 additions and 195 deletions

View file

@ -5,7 +5,6 @@ import {
InputMediaGeo,
InputMediaGeoLive,
InputMediaVenue,
Venue,
} from '../../media'
/**
@ -111,73 +110,99 @@ export type InputInlineMessage =
| InputInlineMessageGame
export namespace BotInlineMessage {
/**
* Create a text inline message
*
* @param text Message text
* @param params
*/
export function text(
text: string,
params?: Omit<InputInlineMessageText, 'type' | 'text'>
params: Omit<InputInlineMessageText, 'type' | 'text'> = {}
): InputInlineMessageText {
return {
type: 'text',
text,
...(params || {}),
}
const ret = params as tl.Mutable<InputInlineMessageText>
ret.type = 'text'
ret.text = text
return ret
}
/**
* Create an inline message containing
* media from the result
*/
export function media(
params?: Omit<InputInlineMessageMedia, 'type'>
params: Omit<InputInlineMessageMedia, 'type'> = {}
): InputInlineMessageMedia {
return {
type: 'media',
...(params || {}),
}
const ret = params as tl.Mutable<InputInlineMessageMedia>
ret.type = 'media'
return ret
}
/**
* Create an inline message containing a geolocation
*
* @param latitude Latitude of the location
* @param longitude Longitude of the location
* @param params Additional parameters
*/
export function geo(
latitude: number,
longitude: number,
params?: Omit<InputInlineMessageGeo, 'type' | 'latitude' | 'longitude'>
params: Omit<InputInlineMessageGeo, 'type' | 'latitude' | 'longitude'> = {}
): InputInlineMessageGeo {
return {
type: 'geo',
latitude,
longitude,
...(params || {}),
}
const ret = params as tl.Mutable<InputInlineMessageGeo>
ret.type = 'geo'
ret.latitude = latitude
ret.longitude = longitude
return ret
}
/**
* Create an inline message containing a live geolocation
*
* @param latitude Latitude of the current location
* @param longitude Longitude of the current location
* @param params Additional parameters
*/
export function geoLive(
latitude: number,
longitude: number,
params?: Omit<
params: Omit<
InputInlineMessageGeoLive,
'type' | 'latitude' | 'longitude'
>
> = {}
): InputInlineMessageGeoLive {
return {
type: 'geo_live',
latitude,
longitude,
...(params || {}),
}
const ret = params as tl.Mutable<InputInlineMessageGeoLive>
ret.type = 'geo_live'
ret.latitude = latitude
ret.longitude = longitude
return ret
}
/**
* Create an inline message containing a venue
*/
export function venue(
params: Omit<InputInlineMessageVenue, 'type'>
): InputInlineMessageVenue {
return {
type: 'venue',
...params,
}
const ret = params as tl.Mutable<InputInlineMessageVenue>
ret.type = 'venue'
return ret
}
/**
* Create an inline message containing a game
* from the inline result
*/
export function game(
params: Omit<InputInlineMessageGame, 'type'>
): InputInlineMessageGame {
return {
type: 'game',
...params,
}
const ret = params as tl.Mutable<InputInlineMessageGame>
ret.type = 'game'
return ret
}
/** @internal */
export async function _convertToTl(
client: TelegramClient,
obj: InputInlineMessage,

View file

@ -1,5 +1,5 @@
import { tl } from '@mtcute/tl'
import { BotInlineMessage, InputInlineMessage } from './input-inline-message'
import { BotInlineMessage, InputInlineMessage, InputInlineMessageGame } from './input-inline-message'
import { TelegramClient } from '../../../client'
import { fileIdToInputDocument, fileIdToInputPhoto } from '@mtcute/file-id'
import { extractFileName } from '../../../utils/file-utils'
@ -480,82 +480,123 @@ export type InputInlineResult =
| InputInlineResultContact
export namespace BotInline {
/**
* Create an inline result containing an article
*
* @param id Inline result ID
* @param params Article
*/
export function article(
id: string,
params: Omit<InputInlineResultArticle, 'type' | 'id'>
): InputInlineResultArticle {
return {
id,
type: 'article',
...params,
}
const ret = params as tl.Mutable<InputInlineResultArticle>
ret.id = id
ret.type = 'article'
return ret
}
/**
* Create an inline result containing a GIF
*
* @param id Inline result ID
* @param media GIF animation
* @param params Additional parameters
*/
export function gif(
id: string,
media: string | tl.RawInputWebDocument | tl.RawInputDocument,
params: Omit<InputInlineResultGif, 'type' | 'id' | 'media'>
params: Omit<InputInlineResultGif, 'type' | 'id' | 'media'> = {}
): InputInlineResultGif {
return {
id,
media,
type: 'gif',
...params,
}
const ret = params as tl.Mutable<InputInlineResultGif>
ret.id = id
ret.type = 'gif'
ret.media = media
return ret
}
/**
* Create an inline result containing a video
*
* @param id Inline result ID
* @param media Video
* @param params Additional parameters
*/
export function video(
id: string,
media: string | tl.RawInputWebDocument | tl.RawInputDocument,
params: Omit<InputInlineResultVideo, 'type' | 'id' | 'media'>
): InputInlineResultVideo {
return {
id,
type: 'video',
media,
...params,
}
const ret = params as tl.Mutable<InputInlineResultVideo>
ret.id = id
ret.type = 'video'
ret.media = media
return ret
}
/**
* Create an inline result containing an audio file
*
* @param id Inline result ID
* @param media Audio file
* @param params Additional parameters
*/
export function audio(
id: string,
media: string | tl.RawInputWebDocument | tl.RawInputDocument,
params: Omit<InputInlineResultAudio, 'type' | 'id' | 'media'>
): InputInlineResultAudio {
return {
id,
type: 'audio',
media,
...params,
}
const ret = params as tl.Mutable<InputInlineResultAudio>
ret.id = id
ret.type = 'audio'
ret.media = media
return ret
}
/**
* Create an inline result containing a voice note
*
* @param id Inline result ID
* @param media Voice note
* @param params Additional parameters
*/
export function voice(
id: string,
media: string | tl.RawInputWebDocument | tl.RawInputDocument,
params: Omit<InputInlineResultVoice, 'type' | 'id' | 'media'>
): InputInlineResultVoice {
return {
id,
type: 'voice',
media,
...params,
}
const ret = params as tl.Mutable<InputInlineResultVoice>
ret.id = id
ret.type = 'voice'
ret.media = media
return ret
}
/**
* Create an inline result containing a photo
*
* @param id Inline result ID
* @param media Photo
* @param params Additional parameters
*/
export function photo(
id: string,
media: string | tl.RawInputWebDocument | tl.RawInputPhoto,
params?: Omit<InputInlineResultPhoto, 'type' | 'id' | 'media'>
params: Omit<InputInlineResultPhoto, 'type' | 'id' | 'media'> = {}
): InputInlineResultPhoto {
return {
id,
type: 'photo',
media,
...(params || {}),
}
const ret = params as tl.Mutable<InputInlineResultPhoto>
ret.id = id
ret.type = 'photo'
ret.media = media
return ret
}
/**
* Create an inline result containing a sticker
*
* @param id Inline result ID
* @param media Sticker
*/
export function sticker(
id: string,
media: string | tl.RawInputDocument
@ -567,67 +608,100 @@ export namespace BotInline {
}
}
/**
* Create an inline result containing a document
* (only PDF and ZIP are supported)
*
* @param id Inline result ID
* @param media Document
* @param params Additional parameters
*/
export function file(
id: string,
media: string | tl.RawInputWebDocument | tl.RawInputDocument,
params: Omit<InputInlineResultFile, 'type' | 'id' | 'media'>
): InputInlineResultFile {
return {
id,
type: 'file',
media,
...params,
}
const ret = params as tl.Mutable<InputInlineResultFile>
ret.id = id
ret.type = 'file'
ret.media = media
return ret
}
/**
* Create an inline result containing a geolocation
*
* @param id Inline result ID
* @param latitude Latitude of the location
* @param longitude Longitude of the location
* @param params Additional parameters
*/
export function geo(
id: string,
latitude: number,
longitude: number,
params: Omit<InputInlineResultGeo, 'type' | 'latitude' | 'longitude'>
): InputInlineResultGeo {
return {
type: 'geo',
latitude,
longitude,
...params,
}
const ret = params as tl.Mutable<InputInlineResultGeo>
ret.id = id
ret.type = 'geo'
ret.latitude = latitude
ret.longitude = longitude
return ret
}
/**
* Create an inline result containing a venue
*
* @param id Inline result ID
* @param params Venue parameters
*/
export function venue(
id: string,
params: Omit<InputInlineResultVenue, 'type' | 'id'>
): InputInlineResultVenue {
return {
id,
type: 'venue',
...params,
}
const ret = params as tl.Mutable<InputInlineResultVenue>
ret.id = id
ret.type = 'venue'
return ret
}
/**
* Create an inline result containing a contact
*
* @param id Inline result ID
* @param params Contact parameters
*/
export function contact(
id: string,
params: Omit<InputInlineResultContact, 'type' | 'id'>
): InputInlineResultContact {
return {
id,
type: 'contact',
...params,
}
const ret = params as tl.Mutable<InputInlineResultContact>
ret.id = id
ret.type = 'contact'
return ret
}
/**
* Create an inline result containing a game
*
* @param id Inline result ID
* @param shortName Short name of the game
* @param params Additional parameters
*/
export function game(
id: string,
shortName: string,
params?: Omit<InputInlineResultGame, 'type' | 'id' | 'shortName'>
params: Omit<InputInlineResultGame, 'type' | 'id' | 'shortName'> = {}
): InputInlineResultGame {
return {
id,
type: 'game',
shortName,
...(params || {})
}
const ret = params as tl.Mutable<InputInlineResultGame>
ret.id = id
ret.type = 'game'
ret.shortName = shortName
return ret
}
/** @internal */
export async function _convertToTl(
client: TelegramClient,
obj: InputInlineResult,

View file

@ -81,11 +81,10 @@ export namespace BotKeyboard {
buttons: tl.TypeKeyboardButton[][],
params: Omit<ReplyKeyboardMarkup, 'type' | 'buttons'> = {}
): ReplyKeyboardMarkup {
return {
type: 'reply',
buttons,
...params,
}
const ret = params as tl.Mutable<ReplyKeyboardMarkup>
ret.type = 'reply'
ret.buttons = buttons
return ret
}
/**
@ -109,10 +108,9 @@ export namespace BotKeyboard {
export function forceReply(
params: Omit<ReplyKeyboardForceReply, 'type'> = {}
): ReplyKeyboardForceReply {
return {
type: 'force_reply',
...params,
}
const ret = params as tl.Mutable<ReplyKeyboardForceReply>
ret.type = 'force_reply'
return ret
}
/**

View file

@ -2,6 +2,7 @@ import { InputFileLike } from '../files'
import { tl } from '@mtcute/tl'
import { Venue } from './venue'
import { MaybeArray } from '@mtcute/core'
import { InputInlineResultGame } from '../bots'
interface BaseInputMedia {
/**
@ -574,145 +575,166 @@ export namespace InputMedia {
/**
* Create an animation to be sent
*
* @param file Animation
* @param params Additional parameters
*/
export function animation(
file: InputFileLike,
params?: OmitTypeAndFile<InputMediaVideo>
params: OmitTypeAndFile<InputMediaVideo> = {}
): InputMediaVideo {
return {
type: 'video',
file,
isAnimated: true,
...(params || {}),
}
const ret = params as tl.Mutable<InputMediaVideo>
ret.type = 'video'
ret.file = file
ret.isAnimated = true
return ret
}
/**
* Create an audio to be sent
*
* @param file Audio file
* @param params Additional parameters
*/
export function audio(
file: InputFileLike,
params?: OmitTypeAndFile<InputMediaAudio>
params: OmitTypeAndFile<InputMediaAudio> = {}
): InputMediaAudio {
return {
type: 'audio',
file,
...(params || {}),
}
const ret = params as tl.Mutable<InputMediaAudio>
ret.type = 'audio'
ret.file = file
return ret
}
/**
* Create an document to be sent
*
* @param file Document
* @param params Additional parameters
*/
export function document(
file: InputFileLike,
params?: OmitTypeAndFile<InputMediaDocument>
params: OmitTypeAndFile<InputMediaDocument> = {}
): InputMediaDocument {
return {
type: 'document',
file,
...(params || {}),
}
const ret = params as tl.Mutable<InputMediaDocument>
ret.type = 'document'
ret.file = file
return ret
}
/**
* Create an photo to be sent
*
* @param file Photo
* @param params Additional parameters
*/
export function photo(
file: InputFileLike,
params?: OmitTypeAndFile<InputMediaPhoto>
params: OmitTypeAndFile<InputMediaPhoto> = {}
): InputMediaPhoto {
return {
type: 'photo',
file,
...(params || {}),
}
const ret = params as tl.Mutable<InputMediaPhoto>
ret.type = 'photo'
ret.file = file
return ret
}
/**
* Create an video to be sent
*
* @param file Video
* @param params Additional parameters
*/
export function video(
file: InputFileLike,
params?: OmitTypeAndFile<InputMediaVideo>
params: OmitTypeAndFile<InputMediaVideo> = {}
): InputMediaVideo {
return {
type: 'video',
file,
...(params || {}),
}
const ret = params as tl.Mutable<InputMediaVideo>
ret.type = 'video'
ret.file = file
return ret
}
/**
* Create a voice message to be sent
* Create a voice note to be sent
*
* @param file Voice note
* @param params Additional parameters
*/
export function voice(
file: InputFileLike,
params?: OmitTypeAndFile<InputMediaVoice>
params: OmitTypeAndFile<InputMediaVoice> = {}
): InputMediaVoice {
return {
type: 'voice',
file,
...(params || {}),
}
const ret = params as tl.Mutable<InputMediaVoice>
ret.type = 'voice'
ret.file = file
return ret
}
/**
* Create a sticker to be sent
*
* @param file Sticker
* @param params Additional parameters
*/
export function sticker(
file: InputFileLike,
params?: OmitTypeAndFile<InputMediaSticker>
params: OmitTypeAndFile<InputMediaSticker> = {}
): InputMediaSticker {
return {
type: 'sticker',
file,
...(params || {}),
}
const ret = params as tl.Mutable<InputMediaSticker>
ret.type = 'sticker'
ret.file = file
return ret
}
/**
* Create a venue to be sent
*
* @param params Venue parameters
*/
export function venue(
params: OmitTypeAndFile<InputMediaVenue>
): InputMediaVenue {
return {
type: 'venue',
...params,
}
const ret = params as tl.Mutable<InputMediaVenue>
ret.type = 'venue'
return ret
}
/**
* Create a geolocation to be sent
*
* @param latitude Latitude of the location
* @param longitude Longitude of the location
* @param params Additional parameters
*/
export function geo(
latitude: number,
longitude: number,
params?: OmitTypeAndFile<InputMediaGeo, 'latitude' | 'longitude'>
params: OmitTypeAndFile<InputMediaGeo, 'latitude' | 'longitude'> = {}
): InputMediaGeo {
return {
type: 'geo',
latitude,
longitude,
...(params || {}),
}
const ret = params as tl.Mutable<InputMediaGeo>
ret.type = 'geo'
ret.latitude = latitude
ret.longitude = longitude
return ret
}
/**
* Create a live geolocation to be sent
*
* @param latitude Latitude of the current location
* @param longitude Longitude of the current location
* @param params Additional parameters
*/
export function geoLive(
latitude: number,
longitude: number,
params?: OmitTypeAndFile<InputMediaGeoLive, 'latitude' | 'longitude'>
params: OmitTypeAndFile<InputMediaGeoLive, 'latitude' | 'longitude'> = {}
): InputMediaGeoLive {
return {
type: 'geo_live',
latitude,
longitude,
...(params || {}),
}
const ret = params as tl.Mutable<InputMediaGeoLive>
ret.type = 'geo_live'
ret.latitude = latitude
ret.longitude = longitude
return ret
}
/**
@ -720,6 +742,8 @@ export namespace InputMedia {
*
* For convenience, known dice emojis are available
* as static members of {@link Dice}.
*
* @param emoji Emoji representing the dice
*/
export function dice(emoji: string): InputMediaDice {
return {
@ -730,18 +754,21 @@ export namespace InputMedia {
/**
* Create a contact to be sent
*
* @param params Contact parameters
*/
export function contact(
params: OmitTypeAndFile<InputMediaContact>
): InputMediaContact {
return {
type: 'contact',
...params,
}
const ret = params as tl.Mutable<InputMediaContact>
ret.type = 'contact'
return ret
}
/**
* Create a game to be sent
*
* @param game Game short name or TL object representing one
*/
export function game(game: string | tl.TypeInputGame): InputMediaGame {
return {
@ -752,38 +779,41 @@ export namespace InputMedia {
/**
* Create an invoice to be sent
*
* @param params Invoice parameters
*/
export function invoice(
params: OmitTypeAndFile<InputMediaInvoice>
): InputMediaInvoice {
return {
type: 'invoice',
...params,
}
const ret = params as tl.Mutable<InputMediaInvoice>
ret.type = 'invoice'
return ret
}
/**
* Create a poll to be sent
*
* @param params Poll parameters
*/
export function poll(
params: OmitTypeAndFile<InputMediaPoll>
): InputMediaPoll {
return {
type: 'poll',
...params,
}
const ret = params as tl.Mutable<InputMediaPoll>
ret.type = 'poll'
return ret
}
/**
* Create a quiz to be sent
*
* @param params Quiz parameters
*/
export function quiz(
params: OmitTypeAndFile<InputMediaQuiz>
): InputMediaQuiz {
return {
type: 'quiz',
...params,
}
const ret = params as tl.Mutable<InputMediaQuiz>
ret.type = 'quiz'
return ret
}
/**
@ -793,15 +823,17 @@ export namespace InputMedia {
* Photo type is only inferred for reused files,
* newly uploaded photos with `auto` will be
* uploaded as a document
*
* @param file The media file
* @param params Additional parameters
*/
export function auto(
file: InputFileLike,
params?: OmitTypeAndFile<InputMediaAuto>
params: OmitTypeAndFile<InputMediaAuto> = {}
): InputMediaAuto {
return {
type: 'auto',
file,
...(params || {}),
}
const ret = params as tl.Mutable<InputMediaAuto>
ret.type = 'auto'
ret.file = file
return ret
}
}