Spaces:
Running
Running
File size: 1,014 Bytes
374bf33 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | const STORAGE_KEY = "anon_id";
function fallbackId(): string {
return `anon_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
}
export function getOrCreateClientAnonId(): string {
if (typeof window === "undefined") return "";
try {
const existing = window.localStorage.getItem(STORAGE_KEY);
if (existing) return existing;
const created = typeof crypto !== "undefined" && "randomUUID" in crypto ? crypto.randomUUID() : fallbackId();
window.localStorage.setItem(STORAGE_KEY, created);
return created;
} catch {
return "";
}
}
export function withAnonHeaders(init: RequestInit = {}): RequestInit {
const headers = new Headers(init.headers || {});
const anonId = getOrCreateClientAnonId();
if (anonId) {
headers.set("x-anon-id", anonId);
}
return {
...init,
headers,
credentials: "include",
};
}
export function fetchWithAnon(input: RequestInfo | URL, init: RequestInit = {}) {
return fetch(input, withAnonHeaders(init));
}
|