refactor(extensions): reuse shared helper primitives

This commit is contained in:
Peter Steinberger
2026-03-07 10:40:57 +00:00
parent 3c71e2bd48
commit 1aa77e4603
58 changed files with 1567 additions and 2195 deletions

View File

@@ -40,37 +40,47 @@ function resolveSendContext(options: ZaloSendOptions): {
return { token, fetcher: resolveZaloProxyFetch(proxy) };
}
function resolveValidatedSendContext(
chatId: string,
options: ZaloSendOptions,
): { ok: true; chatId: string; token: string; fetcher?: ZaloFetch } | { ok: false; error: string } {
const { token, fetcher } = resolveSendContext(options);
if (!token) {
return { ok: false, error: "No Zalo bot token configured" };
}
const trimmedChatId = chatId?.trim();
if (!trimmedChatId) {
return { ok: false, error: "No chat_id provided" };
}
return { ok: true, chatId: trimmedChatId, token, fetcher };
}
export async function sendMessageZalo(
chatId: string,
text: string,
options: ZaloSendOptions = {},
): Promise<ZaloSendResult> {
const { token, fetcher } = resolveSendContext(options);
if (!token) {
return { ok: false, error: "No Zalo bot token configured" };
}
if (!chatId?.trim()) {
return { ok: false, error: "No chat_id provided" };
const context = resolveValidatedSendContext(chatId, options);
if (!context.ok) {
return { ok: false, error: context.error };
}
if (options.mediaUrl) {
return sendPhotoZalo(chatId, options.mediaUrl, {
return sendPhotoZalo(context.chatId, options.mediaUrl, {
...options,
token,
token: context.token,
caption: text || options.caption,
});
}
try {
const response = await sendMessage(
token,
context.token,
{
chat_id: chatId.trim(),
chat_id: context.chatId,
text: text.slice(0, 2000),
},
fetcher,
context.fetcher,
);
if (response.ok && response.result) {
@@ -88,14 +98,9 @@ export async function sendPhotoZalo(
photoUrl: string,
options: ZaloSendOptions = {},
): Promise<ZaloSendResult> {
const { token, fetcher } = resolveSendContext(options);
if (!token) {
return { ok: false, error: "No Zalo bot token configured" };
}
if (!chatId?.trim()) {
return { ok: false, error: "No chat_id provided" };
const context = resolveValidatedSendContext(chatId, options);
if (!context.ok) {
return { ok: false, error: context.error };
}
if (!photoUrl?.trim()) {
@@ -104,13 +109,13 @@ export async function sendPhotoZalo(
try {
const response = await sendPhoto(
token,
context.token,
{
chat_id: chatId.trim(),
chat_id: context.chatId,
photo: photoUrl.trim(),
caption: options.caption?.slice(0, 2000),
},
fetcher,
context.fetcher,
);
if (response.ok && response.result) {