File size: 1,256 Bytes
311fa35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
const STORAGE_KEY = "openreels.apiKey.v1";

/**
 * Optional API key for the OpenReels server (`OPENREELS_API_KEY`).
 *
 * Stored in this browser's localStorage so the UI can call an authenticated
 * server. It is not a provider key and is never sent with the job payload.
 */
export function loadApiKey(): string {
  try {
    return (localStorage.getItem(STORAGE_KEY) ?? "").trim();
  } catch {
    return "";
  }
}

export function saveApiKey(value: string): void {
  const key = value.trim();
  try {
    if (key) {
      localStorage.setItem(STORAGE_KEY, key);
    } else {
      localStorage.removeItem(STORAGE_KEY);
    }
  } catch {}
}

export function clearApiKey(): void {
  try {
    localStorage.removeItem(STORAGE_KEY);
  } catch {}
}

/** Headers for fetch-based API calls. */
export function apiKeyHeaders(): Record<string, string> {
  const key = loadApiKey();
  return key ? { "X-API-Key": key } : {};
}

/**
 * Add `?api_key=` to a URL for EventSource, media, and download links that
 * cannot set request headers.
 */
export function withApiKey(url: string): string {
  const key = loadApiKey();
  if (!key) return url;
  const separator = url.includes("?") ? "&" : "?";
  return `${url}${separator}api_key=${encodeURIComponent(key)}`;
}