MCPcopy Create free account
hub / github.com/pollinations/pollinations / fetchCached

Function fetchCached

packages/mcp/src/utils/models.js:8–25  ·  view source on GitHub ↗
(path)

Source from the content-addressed store, hash-verified

6const cache = new Map();
7
8async 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
27export const getImageModels = () => fetchCached("/image/models");
28export const getTextModels = () => fetchCached("/text/models");

Callers 3

getImageModelsFunction · 0.85
getTextModelsFunction · 0.85
getAudioModelsFunction · 0.85

Calls 6

getAuthHeadersFunction · 0.90
jsonMethod · 0.80
getMethod · 0.65
abortMethod · 0.65
setMethod · 0.65
fetchFunction · 0.50

Tested by

no test coverage detected