UNPKG

@microagents/server-hackernews

Version:

MCP server for Hacker News API integration

86 lines 2.44 kB
import fetch from "node-fetch"; const API_BASE_URL = "https://hacker-news.firebaseio.com/v0"; /** * Client for the official Hacker News API */ export class HackerNewsAPI { /** * Fetch an item by ID */ async getItem(id) { const response = await fetch(`${API_BASE_URL}/item/${id}.json`); return response.json(); } /** * Fetch multiple items by ID */ async getItems(ids) { return Promise.all(ids.map((id) => this.getItem(id))); } /** * Fetch top stories */ async getTopStories(limit = 30) { const response = await fetch(`${API_BASE_URL}/topstories.json`); const ids = (await response.json()); return ids.slice(0, limit); } /** * Fetch new stories */ async getNewStories(limit = 30) { const response = await fetch(`${API_BASE_URL}/newstories.json`); const ids = (await response.json()); return ids.slice(0, limit); } /** * Fetch best stories */ async getBestStories(limit = 30) { const response = await fetch(`${API_BASE_URL}/beststories.json`); const ids = (await response.json()); return ids.slice(0, limit); } /** * Fetch ask stories */ async getAskStories(limit = 30) { const response = await fetch(`${API_BASE_URL}/askstories.json`); const ids = (await response.json()); return ids.slice(0, limit); } /** * Fetch show stories */ async getShowStories(limit = 30) { const response = await fetch(`${API_BASE_URL}/showstories.json`); const ids = (await response.json()); return ids.slice(0, limit); } /** * Fetch job stories */ async getJobStories(limit = 30) { const response = await fetch(`${API_BASE_URL}/jobstories.json`); const ids = (await response.json()); return ids.slice(0, limit); } /** * Fetch a user by ID */ async getUser(id) { const response = await fetch(`${API_BASE_URL}/user/${id}.json`); return response.json(); } /** * Fetch the maximum item ID */ async getMaxItemId() { const response = await fetch(`${API_BASE_URL}/maxitem.json`); const result = (await response.json()); return result; } } // Export a singleton instance export const hnApi = new HackerNewsAPI(); //# sourceMappingURL=hn.js.map