feat: assertTypeIsNot

closes MTQ-64
This commit is contained in:
alina 🌸 2023-09-24 02:40:41 +03:00
parent befbceaf8a
commit e771e592fd
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
12 changed files with 39 additions and 50 deletions

View file

@ -1,4 +1,5 @@
import { getMarkedPeerId, MtTypeAssertionError } from '@mtcute/core'
import { getMarkedPeerId } from '@mtcute/core'
import { assertTypeIsNot } from '@mtcute/core/utils'
import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client'
@ -9,13 +10,7 @@ export function _parseDialogs(
this: TelegramClient,
res: tl.messages.TypeDialogs | tl.messages.TypePeerDialogs,
): Dialog[] {
if (res._ === 'messages.dialogsNotModified') {
throw new MtTypeAssertionError(
'messages.getPeerDialogs',
'!messages.dialogsNotModified',
'messages.dialogsNotModified',
)
}
assertTypeIsNot('parseDialogs', res, 'messages.dialogsNotModified')
const peers = PeersIndex.from(res)

View file

@ -1,5 +1,5 @@
import { assertNever, MtArgumentError, MtTypeAssertionError } from '@mtcute/core'
import { assertTypeIs } from '@mtcute/core/utils'
import { assertNever, MtArgumentError } from '@mtcute/core'
import { assertTypeIs, assertTypeIsNot } from '@mtcute/core/utils'
import { TelegramClient } from '../../client'
import { InputMediaLike, InputPeerLike, MessageMedia, Photo, RawDocument } from '../../types'
@ -61,9 +61,7 @@ export async function uploadMedia(
media: normMedia,
})
if (res._ === 'messageMediaEmpty') {
throw new MtTypeAssertionError('uploadMedia', 'not messageMediaEmpty', 'messageMediaEmpty')
}
assertTypeIsNot('uploadMedia', res, 'messageMediaEmpty')
switch (normMedia._) {
case 'inputMediaUploadedPhoto':

View file

@ -1,4 +1,4 @@
import { MtTypeAssertionError } from '@mtcute/core'
import { assertTypeIsNot } from '@mtcute/core/utils'
import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client'
@ -69,9 +69,7 @@ export async function* getInviteLinks(
const last = res.invites[res.invites.length - 1]
if (last._ === 'chatInvitePublicJoinRequests') {
throw new MtTypeAssertionError('getInviteLinks', 'chatInviteExported', last._)
}
assertTypeIsNot('getInviteLinks', last, 'chatInvitePublicJoinRequests')
offsetDate = last.date
offsetLink = last.link

View file

@ -1,6 +1,6 @@
import Long from 'long'
import { MtTypeAssertionError } from '@mtcute/core'
import { assertTypeIsNot } from '@mtcute/core/utils'
import { TelegramClient } from '../../client'
import { InputPeerLike, Message, PeersIndex } from '../../types'
@ -105,9 +105,7 @@ export async function* getHistory(
hash: Long.ZERO,
})
if (res._ === 'messages.messagesNotModified') {
throw new MtTypeAssertionError('messages.getHistory', '!messages.messagesNotModified', res._)
}
assertTypeIsNot('getHistory', res, 'messages.messagesNotModified')
const peers = PeersIndex.from(res)

View file

@ -1,4 +1,5 @@
import { MaybeArray, MtTypeAssertionError } from '@mtcute/core'
import { MaybeArray } from '@mtcute/core'
import { assertTypeIsNot } from '@mtcute/core/utils'
import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client'
@ -64,9 +65,7 @@ export async function getMessagesUnsafe(
id: ids,
})
if (res._ === 'messages.messagesNotModified') {
throw new MtTypeAssertionError('getMessages', '!messages.messagesNotModified', res._)
}
assertTypeIsNot('getMessagesUnsafe', res, 'messages.messagesNotModified')
const peers = PeersIndex.from(res)

View file

@ -1,4 +1,5 @@
import { MaybeArray, MtTypeAssertionError } from '@mtcute/core'
import { MaybeArray } from '@mtcute/core'
import { assertTypeIsNot } from '@mtcute/core/utils'
import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client'
@ -74,9 +75,7 @@ export async function getMessages(
},
)
if (res._ === 'messages.messagesNotModified') {
throw new MtTypeAssertionError('getMessages', '!messages.messagesNotModified', res._)
}
assertTypeIsNot('getMessages', res, 'messages.messagesNotModified')
const peers = PeersIndex.from(res)

View file

@ -1,4 +1,5 @@
import { MaybeArray, MtTypeAssertionError } from '@mtcute/core'
import { MaybeArray } from '@mtcute/core'
import { assertTypeIsNot } from '@mtcute/core/utils'
import { TelegramClient } from '../../client'
import { InputPeerLike, Message, PeersIndex } from '../../types'
@ -48,9 +49,7 @@ export async function getScheduledMessages(
id: messageIds as number[],
})
if (res._ === 'messages.messagesNotModified') {
throw new MtTypeAssertionError('getMessages', '!messages.messagesNotModified', res._)
}
assertTypeIsNot('getScheduledMessages', res, 'messages.messagesNotModified')
const peers = PeersIndex.from(res)

View file

@ -1,4 +1,4 @@
import { MtTypeAssertionError } from '@mtcute/core'
import { assertTypeIsNot } from '@mtcute/core/utils'
import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client'
@ -58,7 +58,7 @@ export async function* searchGlobal(
let offsetId = 0
for (;;) {
const res = await this.call({
const res: tl.RpcCallReturn['messages.searchGlobal'] = await this.call({
_: 'messages.searchGlobal',
q: params.query || '',
filter: params.filter || SearchFilters.Empty,
@ -70,9 +70,7 @@ export async function* searchGlobal(
limit: Math.min(limit, total - current),
})
if (res._ === 'messages.messagesNotModified') {
throw new MtTypeAssertionError('messages.searchGlobal', '!messages.messagesNotModified', res._)
}
assertTypeIsNot('searchGlobal', res, 'messages.messagesNotModified')
const peers = PeersIndex.from(res)

View file

@ -1,6 +1,6 @@
import Long from 'long'
import { MtTypeAssertionError } from '@mtcute/core'
import { assertTypeIsNot } from '@mtcute/core/utils'
import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client'
@ -126,7 +126,7 @@ export async function* searchMessages(
const fromUser = (params.fromUser ? await this.resolvePeer(params.fromUser) : null) || undefined
for (;;) {
const res = await this.call({
const res: tl.RpcCallReturn['messages.search'] = await this.call({
_: 'messages.search',
peer,
q: params.query || '',
@ -142,9 +142,7 @@ export async function* searchMessages(
hash: Long.ZERO,
})
if (res._ === 'messages.messagesNotModified') {
throw new MtTypeAssertionError('messages.search', '!messages.messagesNotModified', res._)
}
assertTypeIsNot('searchMessages', res, 'messages.messagesNotModified')
// for successive chunks, we need to reset the offset
offset = 0

View file

@ -1,4 +1,5 @@
import { assertNever, getMarkedPeerId, MtArgumentError, MtTypeAssertionError, toggleChannelIdMark } from '@mtcute/core'
import { assertTypeIsNot } from '@mtcute/core/utils'
import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client'
@ -103,9 +104,7 @@ export class Message {
*/
readonly isScheduled = false,
) {
if (raw._ === 'messageEmpty') {
throw new MtTypeAssertionError('Message#ctor', 'not messageEmpty', 'messageEmpty')
}
assertTypeIsNot('Message#ctor', raw, 'messageEmpty')
this.raw = raw
}

View file

@ -1,4 +1,4 @@
import { MtTypeAssertionError } from '@mtcute/core'
import { assertTypeIsNot } from '@mtcute/core/utils'
import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client'
@ -39,9 +39,7 @@ export class ChatInviteLink {
raw: tl.RawChatInviteExported
constructor(readonly client: TelegramClient, raw: tl.TypeExportedChatInvite, readonly _peers?: PeersIndex) {
if (raw._ === 'chatInvitePublicJoinRequests') {
throw new MtTypeAssertionError('ChatInviteLink', 'chatInviteExported', raw._)
}
assertTypeIsNot('ChatInviteLink', raw, 'chatInvitePublicJoinRequests')
this.raw = raw
}

View file

@ -60,6 +60,16 @@ export function assertTypeIs<T extends tl.TlObject, K extends T['_']>(
}
}
export function assertTypeIsNot<T extends tl.TlObject, K extends T['_']>(
context: string,
obj: T,
expectedNot: K,
): asserts obj is Exclude<T, tl.FindByName<T, K>> {
if (obj._ === expectedNot) {
throw new MtTypeAssertionError(context, 'not ' + expectedNot, obj._)
}
}
export function mtpAssertTypeIs<T extends mtp.TlObject, K extends T['_']>(
context: string,
obj: T,