| 6 | const cache = new Map(); |
| 7 | |
| 8 | async function fetchCached(path) { |
| 9 | const hit = cache.get(path); |
| 10 | if (hit && Date.now() - hit.at < CACHE_TTL_MS) return hit.data; |
| 11 | |
| 12 | const controller = new AbortController(); |
| 13 | const timeoutId = setTimeout(() => controller.abort(), 20000); |
| 14 | const response = await fetch(`${API_BASE_URL}${path}`, { |
| 15 | headers: getAuthHeaders(), |
| 16 | signal: controller.signal, |
| 17 | }).finally(() => clearTimeout(timeoutId)); |
| 18 | |
| 19 | if (!response.ok) { |
| 20 | throw new Error(`Failed to fetch ${path}: ${response.status}`); |
| 21 | } |
| 22 | const data = await response.json(); |
| 23 | cache.set(path, { data, at: Date.now() }); |
| 24 | return data; |
| 25 | } |
| 26 | |
| 27 | export const getImageModels = () => fetchCached("/image/models"); |
| 28 | export const getTextModels = () => fetchCached("/text/models"); |