refactor: share zalo send context validation

This commit is contained in:
Peter Steinberger
2026-03-14 00:52:39 +00:00
parent d55fa78e40
commit 827b166bbc

View File

@@ -77,15 +77,30 @@ function resolveValidatedSendContext(
return { ok: true, chatId: trimmedChatId, token, fetcher }; return { ok: true, chatId: trimmedChatId, token, fetcher };
} }
function resolveSendContextOrFailure(
chatId: string,
options: ZaloSendOptions,
):
| { context: { chatId: string; token: string; fetcher?: ZaloFetch } }
| { failure: ZaloSendResult } {
const context = resolveValidatedSendContext(chatId, options);
return context.ok
? { context }
: {
failure: { ok: false, error: context.error },
};
}
export async function sendMessageZalo( export async function sendMessageZalo(
chatId: string, chatId: string,
text: string, text: string,
options: ZaloSendOptions = {}, options: ZaloSendOptions = {},
): Promise<ZaloSendResult> { ): Promise<ZaloSendResult> {
const context = resolveValidatedSendContext(chatId, options); const resolved = resolveSendContextOrFailure(chatId, options);
if (!context.ok) { if ("failure" in resolved) {
return { ok: false, error: context.error }; return resolved.failure;
} }
const { context } = resolved;
if (options.mediaUrl) { if (options.mediaUrl) {
return sendPhotoZalo(context.chatId, options.mediaUrl, { return sendPhotoZalo(context.chatId, options.mediaUrl, {
@@ -112,10 +127,11 @@ export async function sendPhotoZalo(
photoUrl: string, photoUrl: string,
options: ZaloSendOptions = {}, options: ZaloSendOptions = {},
): Promise<ZaloSendResult> { ): Promise<ZaloSendResult> {
const context = resolveValidatedSendContext(chatId, options); const resolved = resolveSendContextOrFailure(chatId, options);
if (!context.ok) { if ("failure" in resolved) {
return { ok: false, error: context.error }; return resolved.failure;
} }
const { context } = resolved;
if (!photoUrl?.trim()) { if (!photoUrl?.trim()) {
return { ok: false, error: "No photo URL provided" }; return { ok: false, error: "No photo URL provided" };