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

View file

@ -1,5 +1,5 @@
import { assertNever, MtArgumentError, MtTypeAssertionError } from '@mtcute/core' import { assertNever, MtArgumentError } from '@mtcute/core'
import { assertTypeIs } from '@mtcute/core/utils' import { assertTypeIs, assertTypeIsNot } from '@mtcute/core/utils'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { InputMediaLike, InputPeerLike, MessageMedia, Photo, RawDocument } from '../../types' import { InputMediaLike, InputPeerLike, MessageMedia, Photo, RawDocument } from '../../types'
@ -61,9 +61,7 @@ export async function uploadMedia(
media: normMedia, media: normMedia,
}) })
if (res._ === 'messageMediaEmpty') { assertTypeIsNot('uploadMedia', res, 'messageMediaEmpty')
throw new MtTypeAssertionError('uploadMedia', 'not messageMediaEmpty', 'messageMediaEmpty')
}
switch (normMedia._) { switch (normMedia._) {
case 'inputMediaUploadedPhoto': 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 { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
@ -69,9 +69,7 @@ export async function* getInviteLinks(
const last = res.invites[res.invites.length - 1] const last = res.invites[res.invites.length - 1]
if (last._ === 'chatInvitePublicJoinRequests') { assertTypeIsNot('getInviteLinks', last, 'chatInvitePublicJoinRequests')
throw new MtTypeAssertionError('getInviteLinks', 'chatInviteExported', last._)
}
offsetDate = last.date offsetDate = last.date
offsetLink = last.link offsetLink = last.link

View file

@ -1,6 +1,6 @@
import Long from 'long' import Long from 'long'
import { MtTypeAssertionError } from '@mtcute/core' import { assertTypeIsNot } from '@mtcute/core/utils'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { InputPeerLike, Message, PeersIndex } from '../../types' import { InputPeerLike, Message, PeersIndex } from '../../types'
@ -105,9 +105,7 @@ export async function* getHistory(
hash: Long.ZERO, hash: Long.ZERO,
}) })
if (res._ === 'messages.messagesNotModified') { assertTypeIsNot('getHistory', res, 'messages.messagesNotModified')
throw new MtTypeAssertionError('messages.getHistory', '!messages.messagesNotModified', res._)
}
const peers = PeersIndex.from(res) 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 { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
@ -64,9 +65,7 @@ export async function getMessagesUnsafe(
id: ids, id: ids,
}) })
if (res._ === 'messages.messagesNotModified') { assertTypeIsNot('getMessagesUnsafe', res, 'messages.messagesNotModified')
throw new MtTypeAssertionError('getMessages', '!messages.messagesNotModified', res._)
}
const peers = PeersIndex.from(res) 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 { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
@ -74,9 +75,7 @@ export async function getMessages(
}, },
) )
if (res._ === 'messages.messagesNotModified') { assertTypeIsNot('getMessages', res, 'messages.messagesNotModified')
throw new MtTypeAssertionError('getMessages', '!messages.messagesNotModified', res._)
}
const peers = PeersIndex.from(res) 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 { TelegramClient } from '../../client'
import { InputPeerLike, Message, PeersIndex } from '../../types' import { InputPeerLike, Message, PeersIndex } from '../../types'
@ -48,9 +49,7 @@ export async function getScheduledMessages(
id: messageIds as number[], id: messageIds as number[],
}) })
if (res._ === 'messages.messagesNotModified') { assertTypeIsNot('getScheduledMessages', res, 'messages.messagesNotModified')
throw new MtTypeAssertionError('getMessages', '!messages.messagesNotModified', res._)
}
const peers = PeersIndex.from(res) 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 { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
@ -58,7 +58,7 @@ export async function* searchGlobal(
let offsetId = 0 let offsetId = 0
for (;;) { for (;;) {
const res = await this.call({ const res: tl.RpcCallReturn['messages.searchGlobal'] = await this.call({
_: 'messages.searchGlobal', _: 'messages.searchGlobal',
q: params.query || '', q: params.query || '',
filter: params.filter || SearchFilters.Empty, filter: params.filter || SearchFilters.Empty,
@ -70,9 +70,7 @@ export async function* searchGlobal(
limit: Math.min(limit, total - current), limit: Math.min(limit, total - current),
}) })
if (res._ === 'messages.messagesNotModified') { assertTypeIsNot('searchGlobal', res, 'messages.messagesNotModified')
throw new MtTypeAssertionError('messages.searchGlobal', '!messages.messagesNotModified', res._)
}
const peers = PeersIndex.from(res) const peers = PeersIndex.from(res)

View file

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

View file

@ -1,4 +1,5 @@
import { assertNever, getMarkedPeerId, MtArgumentError, MtTypeAssertionError, toggleChannelIdMark } from '@mtcute/core' import { assertNever, getMarkedPeerId, MtArgumentError, MtTypeAssertionError, toggleChannelIdMark } from '@mtcute/core'
import { assertTypeIsNot } from '@mtcute/core/utils'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
@ -103,9 +104,7 @@ export class Message {
*/ */
readonly isScheduled = false, readonly isScheduled = false,
) { ) {
if (raw._ === 'messageEmpty') { assertTypeIsNot('Message#ctor', raw, 'messageEmpty')
throw new MtTypeAssertionError('Message#ctor', 'not messageEmpty', 'messageEmpty')
}
this.raw = raw 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 { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
@ -39,9 +39,7 @@ export class ChatInviteLink {
raw: tl.RawChatInviteExported raw: tl.RawChatInviteExported
constructor(readonly client: TelegramClient, raw: tl.TypeExportedChatInvite, readonly _peers?: PeersIndex) { constructor(readonly client: TelegramClient, raw: tl.TypeExportedChatInvite, readonly _peers?: PeersIndex) {
if (raw._ === 'chatInvitePublicJoinRequests') { assertTypeIsNot('ChatInviteLink', raw, 'chatInvitePublicJoinRequests')
throw new MtTypeAssertionError('ChatInviteLink', 'chatInviteExported', raw._)
}
this.raw = raw 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['_']>( export function mtpAssertTypeIs<T extends mtp.TlObject, K extends T['_']>(
context: string, context: string,
obj: T, obj: T,