Spaces:
Sleeping
Sleeping
Download src/utils/api.ts from vooom/NewsAnalyze: direct link, hf CLI and curl.
- Browser
- Download file 1.9 kB
-
https://huggingface.co/spaces/vooom/NewsAnalyze/resolve/main/src/utils/api.ts
- Command line
-
hf download hf://spaces/vooom/NewsAnalyze/src/utils/api.ts
-
curl -L -o api.ts https://huggingface.co/spaces/vooom/NewsAnalyze/resolve/main/src/utils/api.ts
1.9 kB
| import { auth } from "../config/firebase.js"; | |
| import { getBackendUrl } from "./backend_url.js"; | |
| export { getBackendUrl }; | |
| export function getLlmApiKey(): string { | |
| return localStorage.getItem("GEO_STRAT_LLM_API_KEY") || ""; | |
| } | |
| export function getUnslothApiKey(): string { | |
| return localStorage.getItem("GEO_STRAT_UNSLOTH_KEY") || ""; | |
| } | |
| export function getHfToken(): string { | |
| return localStorage.getItem("GEO_STRAT_HF_TOKEN") || ""; | |
| } | |
| export function getGNewsApiKey(): string { | |
| return localStorage.getItem("GEO_STRAT_GNEWS_KEY") || ""; | |
| } | |
| export function getLlmProvider(): string { | |
| return localStorage.getItem("GEO_STRAT_LLM_PROVIDER") || "cascade_auto"; // 'cascade_auto', 'gemini', 'unsloth', 'hf_token', 'hf_free' | |
| } | |
| export async function fetchWithAuth(endpoint: string, options: RequestInit = {}): Promise<Response> { | |
| const baseUrl = getBackendUrl(); | |
| const url = endpoint.startsWith("http") ? endpoint : `${baseUrl}${endpoint}`; | |
| const headers = new Headers(options.headers || {}); | |
| headers.set("Content-Type", "application/json"); | |
| // Attach LLM Keys & Provider | |
| const customKey = getLlmApiKey(); | |
| const unslothKey = getUnslothApiKey(); | |
| const hfToken = getHfToken(); | |
| const gnewsKey = getGNewsApiKey(); | |
| const provider = getLlmProvider(); | |
| if (customKey) { | |
| headers.set("X-LLM-API-Key", customKey); | |
| } | |
| if (unslothKey) { | |
| headers.set("X-Unsloth-Key", unslothKey); | |
| } | |
| if (hfToken) { | |
| headers.set("X-HF-Token", hfToken); | |
| } | |
| if (gnewsKey) { | |
| headers.set("X-GNews-Key", gnewsKey); | |
| } | |
| headers.set("X-LLM-Provider", provider); | |
| // Attach Firebase Auth Token if signed in | |
| if (auth.currentUser) { | |
| try { | |
| const idToken = await auth.currentUser.getIdToken(); | |
| headers.set("Authorization", `Bearer ${idToken}`); | |
| } catch (_e) { | |
| // Ignore token fetch error | |
| } | |
| } | |
| return fetch(url, { | |
| ...options, | |
| headers, | |
| }); | |
| } | |