Open History to see the last commit
1import { type UnifiedSettings, UsageLimitSourceId } from "@t3tools/contracts";
2
3export function sourceIdFromUrl(
4 url: string,
5 sources: UnifiedSettings["usageLimitSources"],
6): UsageLimitSourceId {
7 const saved = Object.entries(sources).find(([, source]) => source.url === url);
8 if (saved) return UsageLimitSourceId.make(saved[0]);
9 let host = url;
10 try {
11 host = new URL(url).host;
12 } catch {}
13 const slug = host
14 .toLowerCase()
15 .replace(/[^a-z0-9.-]+/g, "-")
16 .replace(/^-+|-+$/g, "");
17 // Settings merge by ID, so adding a different hub must not replace an existing entry.
18 const base = `cliproxy-${slug || "hub"}`;
19 let id = base;
20 for (let suffix = 2; id in sources; suffix += 1) {
21 id = `${base}-${suffix}`;
22 }
23 return UsageLimitSourceId.make(id);
24}