refactor: centralize WhatsApp config merging
This commit is contained in:
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { ClawdbotConfig } from "../config/config.js";
|
||||
import {
|
||||
mergeWhatsAppConfig,
|
||||
setWhatsAppAllowFrom,
|
||||
setWhatsAppDmPolicy,
|
||||
setWhatsAppSelfChatMode,
|
||||
@@ -54,4 +55,27 @@ describe("onboard-providers WhatsApp setters", () => {
|
||||
expect(next.whatsapp?.selfChatMode).toBe(true);
|
||||
expect(next.whatsapp?.allowFrom).toEqual(["+15555550123"]);
|
||||
});
|
||||
|
||||
it("merges WhatsApp config without clobbering fields", () => {
|
||||
const cfg: ClawdbotConfig = {
|
||||
whatsapp: {
|
||||
dmPolicy: "pairing",
|
||||
allowFrom: ["*"],
|
||||
},
|
||||
};
|
||||
|
||||
const merged = mergeWhatsAppConfig(cfg, {
|
||||
dmPolicy: "open",
|
||||
allowFrom: undefined,
|
||||
});
|
||||
const cleared = mergeWhatsAppConfig(
|
||||
cfg,
|
||||
{ allowFrom: undefined },
|
||||
{ unsetOnUndefined: ["allowFrom"] },
|
||||
);
|
||||
|
||||
expect(merged.whatsapp?.dmPolicy).toBe("open");
|
||||
expect(merged.whatsapp?.allowFrom).toEqual(["*"]);
|
||||
expect(cleared.whatsapp?.allowFrom).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import type { ClawdbotConfig } from "../config/config.js";
|
||||
import type { DmPolicy, WhatsAppConfig } from "../config/types.js";
|
||||
import { mergeWhatsAppConfig } from "../config/merge-config.js";
|
||||
import type { DmPolicy } from "../config/types.js";
|
||||
import {
|
||||
listDiscordAccountIds,
|
||||
resolveDefaultDiscordAccountId,
|
||||
@@ -249,20 +250,7 @@ async function noteSlackTokenHelp(
|
||||
);
|
||||
}
|
||||
|
||||
export function mergeWhatsAppConfig(
|
||||
cfg: ClawdbotConfig,
|
||||
patch: Partial<WhatsAppConfig>,
|
||||
): ClawdbotConfig {
|
||||
const base = cfg.whatsapp ?? {};
|
||||
return {
|
||||
...cfg,
|
||||
whatsapp: {
|
||||
selfChatMode: base.selfChatMode,
|
||||
...base,
|
||||
...patch,
|
||||
},
|
||||
};
|
||||
}
|
||||
export { mergeWhatsAppConfig };
|
||||
|
||||
export function setWhatsAppDmPolicy(
|
||||
cfg: ClawdbotConfig,
|
||||
@@ -275,7 +263,11 @@ export function setWhatsAppAllowFrom(
|
||||
cfg: ClawdbotConfig,
|
||||
allowFrom?: string[],
|
||||
): ClawdbotConfig {
|
||||
return mergeWhatsAppConfig(cfg, { allowFrom });
|
||||
return mergeWhatsAppConfig(
|
||||
cfg,
|
||||
{ allowFrom },
|
||||
{ unsetOnUndefined: ["allowFrom"] },
|
||||
);
|
||||
}
|
||||
|
||||
function setMessagesResponsePrefix(
|
||||
|
||||
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