refactor(telegram): tighten api result typings

This commit is contained in:
Ayaan Zaidi
2026-03-28 09:48:06 +05:30
parent 9905f39e9d
commit 4a014083ff
2 changed files with 8 additions and 8 deletions

View File

@@ -11,6 +11,7 @@ import { makeProxyFetch } from "./proxy.js";
type TelegramApiOk<T> = { ok: true; result: T };
type TelegramApiErr = { ok: false; description?: string };
type TelegramGroupMembershipAuditData = Omit<TelegramGroupMembershipAudit, "elapsedMs">;
type TelegramChatMemberResult = { status?: string };
export async function auditTelegramGroupMembershipImpl(
params: AuditTelegramGroupMembershipParams,
@@ -27,7 +28,7 @@ export async function auditTelegramGroupMembershipImpl(
try {
const url = `${base}/getChatMember?chat_id=${encodeURIComponent(chatId)}&user_id=${encodeURIComponent(String(params.botId))}`;
const res = await fetchWithTimeout(url, {}, params.timeoutMs, fetcher);
const json = (await res.json()) as TelegramApiOk<{ status?: string }> | TelegramApiErr;
const json = (await res.json()) as TelegramApiOk<TelegramChatMemberResult> | TelegramApiErr;
if (!res.ok || !isRecord(json) || !json.ok) {
const desc =
isRecord(json) && !json.ok && typeof json.description === "string"
@@ -43,9 +44,8 @@ export async function auditTelegramGroupMembershipImpl(
});
continue;
}
const status = isRecord((json as TelegramApiOk<unknown>).result)
? ((json as TelegramApiOk<{ status?: string }>).result.status ?? null)
: null;
const status =
isRecord(json.result) && typeof json.result.status === "string" ? json.result.status : null;
const ok = status === "creator" || status === "administrator" || status === "member";
groups.push({
chatId,

View File

@@ -21,6 +21,8 @@ type TelegramSendMessageDraft = (
},
) => Promise<unknown>;
type TelegramSendMessageParams = Parameters<Bot["api"]["sendMessage"]>[2];
/**
* Keep draft-id allocation shared across bundled chunks so concurrent preview
* lanes do not accidentally reuse draft ids when code-split entries coexist.
@@ -179,7 +181,7 @@ export function createTelegramDraftStream(params: {
: replyParams;
const usedThreadParams =
"message_thread_id" in (sendParams ?? {}) &&
typeof (sendParams as { message_thread_id?: unknown }).message_thread_id === "number";
typeof sendParams?.message_thread_id === "number";
try {
return {
sent: await params.api.sendMessage(chatId, sendArgs.renderedText, sendParams),
@@ -189,9 +191,7 @@ export function createTelegramDraftStream(params: {
if (!usedThreadParams || !THREAD_NOT_FOUND_RE.test(String(err))) {
throw err;
}
const threadlessParams = {
...(sendParams as Record<string, unknown>),
};
const threadlessParams: TelegramSendMessageParams = { ...(sendParams ?? {}) };
delete threadlessParams.message_thread_id;
params.warn?.(sendArgs.fallbackWarnMessage);
return {