mcp-hacker-news
Version:
Hacker News MCP Server
31 lines • 1.21 kB
JavaScript
import { getCached, setCache } from "./helpers.js";
// *****************************************************
// *********** Hacker News MCP fetch actions ***********
// *****************************************************
// Function to fetch from API
export async function fetchFromAPI(endpoint) {
const cacheKey = endpoint;
const cached = getCached(cacheKey);
if (cached)
return cached;
try {
const response = await fetch(`https://hacker-news.firebaseio.com/v0${endpoint}.json`);
if (!response.ok)
throw new Error(`HTTP ${response.status}`);
const data = await response.json();
setCache(cacheKey, data);
return data;
}
catch (error) {
console.error(`Error fetching ${endpoint}:`, error);
return null;
}
}
// Function to fetch multiple items
export async function fetchMultipleItems(ids, maxItems = 30) {
const limitedIds = ids.slice(0, maxItems);
const promises = limitedIds.map((id) => fetchFromAPI(`/item/${id}`));
const results = await Promise.all(promises);
return results.filter((item) => item !== null && !item.deleted && !item.dead);
}
//# sourceMappingURL=fetch-actions.js.map