refactor: centralize WhatsApp config merging
This commit is contained in:
35
src/config/merge-config.ts
Normal file
35
src/config/merge-config.ts
Normal 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),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user