refactor: centralize WhatsApp config merging

This commit is contained in:
Peter Steinberger
2026-01-08 06:27:13 +00:00
parent e09d44e63a
commit c9e07616c7
3 changed files with 67 additions and 16 deletions

View File

@@ -0,0 +1,35 @@
import type { ClawdbotConfig } from "./config.js";
import type { WhatsAppConfig } from "./types.js";
export type MergeSectionOptions<T> = {
unsetOnUndefined?: Array<keyof T>;
};
export function mergeConfigSection<T extends Record<string, unknown>>(
base: T | undefined,
patch: Partial<T>,
options: MergeSectionOptions<T> = {},
): T {
const next: Record<string, unknown> = { ...(base ?? {}) };
for (const [key, value] of Object.entries(patch) as [keyof T, T[keyof T]][]) {
if (value === undefined) {
if (options.unsetOnUndefined?.includes(key)) {
delete next[key as string];
}
continue;
}
next[key as string] = value as unknown;
}
return next as T;
}
export function mergeWhatsAppConfig(
cfg: ClawdbotConfig,
patch: Partial<WhatsAppConfig>,
options?: MergeSectionOptions<WhatsAppConfig>,
): ClawdbotConfig {
return {
...cfg,
whatsapp: mergeConfigSection(cfg.whatsapp, patch, options),
};
}