refactor: centralize target normalization
This commit is contained in:
31
src/infra/outbound/target-normalization.ts
Normal file
31
src/infra/outbound/target-normalization.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { getChannelPlugin, normalizeChannelId } from "../../channels/plugins/index.js";
|
||||
import type { ChannelId } from "../../channels/plugins/types.js";
|
||||
|
||||
export function normalizeChannelTargetInput(raw: string): string {
|
||||
return raw.trim();
|
||||
}
|
||||
|
||||
export function normalizeTargetForProvider(provider: string, raw?: string): string | undefined {
|
||||
if (!raw) return undefined;
|
||||
const providerId = normalizeChannelId(provider);
|
||||
const plugin = providerId ? getChannelPlugin(providerId) : undefined;
|
||||
const normalized =
|
||||
plugin?.messaging?.normalizeTarget?.(raw) ?? (raw.trim().toLowerCase() || undefined);
|
||||
return normalized || undefined;
|
||||
}
|
||||
|
||||
export function buildTargetResolverSignature(channel: ChannelId): string {
|
||||
const plugin = getChannelPlugin(channel);
|
||||
const hint = plugin?.messaging?.targetHint ?? "";
|
||||
const looksLike = plugin?.messaging?.looksLikeTargetId;
|
||||
const source = looksLike ? looksLike.toString() : "";
|
||||
return hashSignature(`${hint}|${source}`);
|
||||
}
|
||||
|
||||
function hashSignature(value: string): string {
|
||||
let hash = 5381;
|
||||
for (let i = 0; i < value.length; i += 1) {
|
||||
hash = ((hash << 5) + hash) ^ value.charCodeAt(i);
|
||||
}
|
||||
return (hash >>> 0).toString(36);
|
||||
}
|
||||
Reference in New Issue
Block a user