Skip to content

Commit

Permalink
feat(scripts): oh yeah updated generate-interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
nitreojs committed Oct 1, 2023
1 parent 4ae31f8 commit 2902493
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions scripts/generate-interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { stripIndent, stripIndents } from 'common-tags'

import * as Types from './types'

// const SCHEMA_URL = `${__dirname}/custom.min.json`
// const SCHEMA_URL = resolve(__dirname, 'custom.min.json')
const SCHEMA_URL = 'https://ark0f.github.io/tg-bot-api/custom.min.json'
const CURRENCIES_URL = 'https://core.telegram.org/bots/payments/currencies.json'

Expand Down Expand Up @@ -112,6 +112,10 @@ class InterfaceService {
type = `SoftString<${typeName}>`
}

if (field.name === 'parse_mode') {
type = 'PossibleParseMode'
}

// INFO: any [TelegramInputFile] must be replaced with [MediaInput]
if (type === 'TelegramInputFile | string') {
type = 'MediaInput'
Expand All @@ -132,6 +136,25 @@ class InterfaceService {
type = 'ReplyMarkupUnion'
}

// INFO: additional enums for IDE suggestions...
// TODO: probably autogenerate some of these from types into enums?

if (field.name === 'type' && iface.name === 'MessageEntity') {
type += ' | Enums.MessageEntityType'
}

if (field.name === 'status' && iface.name.startsWith('ChatMember')) {
type += ' | Enums.ChatMemberStatus.' + uppercaseFirst((field as Types.SchemaObjectString).default!)
}

if (field.name === 'type' && iface.name === 'Sticker') {
type += ' | Enums.StickerType'
}

if (field.name === 'type' && iface.name.startsWith('BotCommandScope')) {
type += ' | Enums.BotCommandScopeType.' + uppercaseFirst(undoSnakeCase((field as Types.SchemaObjectString).default!))
}

const description = InterfaceService.generateDescription(field.description, 2)
const property = `${description}\n${tab(field.name)}${field.required ? '' : '?'}: ${type}`

Expand Down Expand Up @@ -232,6 +255,21 @@ class MethodService {
returnType = 'Interfaces.Currency'
}

// INFO: additional enums for IDE suggestions...
// TODO: probably autogenerate some of these from types into enums?

if (field.name === 'emoji' && method.name === 'sendDice') {
returnType += ' | Enums.DiceEmoji'
}

if (field.name === 'action' && method.name === 'sendChatAction') {
returnType += ' | Enums.ChatAction'
}

if (field.name === 'sticker_format') {
returnType += ' | Enums.StickerFormat'
}

const property = `${description}\n${tab(field.name)}${field.required ? '' : '?'}: ${returnType}`

response.fields.push(property)
Expand Down Expand Up @@ -348,6 +386,8 @@ class GenerationService {
return stripIndent`
import { Readable } from 'stream' // INFO: for Interfaces.InputFile
import * as Enums from '../types/enums'
import { SoftString } from '../types/types'
import { MediaInput } from '../common/media-source'
Expand All @@ -367,6 +407,8 @@ class GenerationService {
return stripIndent`
import * as Interfaces from './telegram-interfaces'
import * as Enums from '../types/enums'
import { SoftString } from '../types/types'
import { MediaInput } from '../common/media-source'
Expand All @@ -377,7 +419,7 @@ class GenerationService {
static generateAdditionalTypes () {
return stripIndent`
export type InputFile = string | Record<string, any> | Buffer | Readable
export type PossibleParseMode = SoftString<'HTML' | 'Markdown' | 'MarkdownV2'>
export type PossibleParseMode = SoftString<'HTML' | 'Markdown' | 'MarkdownV2'> | Enums.ParseMode
export type ReplyMarkupUnion =
| TelegramInlineKeyboardMarkup | TelegramReplyKeyboardMarkup | TelegramReplyKeyboardRemove | TelegramForceReply
| Keyboard | KeyboardBuilder | InlineKeyboard | InlineKeyboardBuilder | ForceReply | RemoveKeyboard
Expand Down

0 comments on commit 2902493

Please sign in to comment.