feat(i18n): removed immediate dependency on client to allow for outside usage

This commit is contained in:
teidesu 2022-08-29 18:54:43 +03:00
parent 689533c549
commit 5019d7b596
2 changed files with 24 additions and 39 deletions

View file

@ -11,8 +11,5 @@
"coverage": "nyc npm run test", "coverage": "nyc npm run test",
"build": "tsc", "build": "tsc",
"docs": "typedoc" "docs": "typedoc"
},
"dependencies": {
"@mtcute/client": "workspace:^1.0.0"
} }
} }

View file

@ -1,21 +1,11 @@
import { import type { ParsedUpdate } from '@mtcute/client'
ParsedUpdate,
assertNever,
User,
Message,
DeleteMessageUpdate,
ChatMemberUpdate,
InlineQuery,
ChosenInlineResult,
CallbackQuery,
PollUpdate,
PollVoteUpdate,
UserStatusUpdate,
BotStoppedUpdate,
BotChatJoinRequestUpdate,
} from '@mtcute/client'
import { I18nValue } from './types' import { I18nValue } from './types'
let client: any
try {
client = require('@mtcute/client')
} catch (e) {}
export function createI18nStringsIndex( export function createI18nStringsIndex(
strings: Record<string, any> strings: Record<string, any>
): Record<string, I18nValue> { ): Record<string, I18nValue> {
@ -41,27 +31,25 @@ export function createI18nStringsIndex(
export function extractLanguageFromUpdate( export function extractLanguageFromUpdate(
update: ParsedUpdate['data'] update: ParsedUpdate['data']
): string | null | undefined { ): string | null | undefined {
switch (update.constructor) { if (!client) {
case Message: throw new Error(
'@mtcute/client is not installed, you must provide your own adapter'
)
}
const upd = update as any
switch (upd.constructor) {
case client.Message:
// if sender is Chat it will just be undefined // if sender is Chat it will just be undefined
return ((update as Message).sender as User).language return upd.sender.language
case ChatMemberUpdate: case client.ChatMemberUpdate:
case InlineQuery: case client.InlineQuery:
case ChosenInlineResult: case client.ChosenInlineResult:
case CallbackQuery: case client.CallbackQuery:
case PollVoteUpdate: case client.PollVoteUpdate:
case BotStoppedUpdate: case client.BotStoppedUpdate:
case BotChatJoinRequestUpdate: case client.BotChatJoinRequestUpdate:
return ( return upd.user.language
update as
| ChatMemberUpdate
| InlineQuery
| ChosenInlineResult
| CallbackQuery
| PollVoteUpdate
| BotStoppedUpdate
| BotChatJoinRequestUpdate
).user.language
} }
return null return null