/** * Proxy für newsdata.io API. * Endpoint: https://newsdata.io/api/1/news?apikey=...&language=...&category=... * Pagination: counter = wievieltes Widget dieser Art (0 = erste Seite, 1 = zweite, …), damit News nicht doppelt gezeigt werden. */ const NEWS_BASE = 'https://newsdata.io/api/1/news'; /** * @param {object} options * @param {number} options.counter - 0 = erste Seite, 1 = zweite, … (für Pagination/nextPage) * @param {string} [options.language] - z. B. de, en * @param {string} [options.category] - z. B. top, technology * @returns {Promise<{ results: Array, nextPage: string|null }>} */ async function fetchNewsPage({ counter, language = 'de', category = 'top', nextPageToken = null }) { const apiKey = process.env.NEWSDATA_IO_API_KEY; if (!apiKey || !apiKey.trim()) { throw new Error('NEWSDATA_IO_API_KEY is not set in .env'); } const params = new URLSearchParams(); params.set('apikey', apiKey.trim()); params.set('language', String(language)); params.set('category', String(category)); if (nextPageToken) params.set('page', nextPageToken); const url = `${NEWS_BASE}?${params.toString()}`; const res = await fetch(url); if (!res.ok) { const text = await res.text(); throw new Error(`newsdata.io: ${res.status} ${text.slice(0, 200)}`); } const data = await res.json(); return { results: data.results ?? [], nextPage: data.nextPage ?? null }; } /** * Liefert den N-ten Artikel (counter = 0, 1, 2, …) für das N-te News-Widget. * Lädt ggf. mehrere Seiten, bis genug Artikel vorhanden sind; jede Widget-Instanz bekommt einen anderen Artikel. * * @param {object} options * @param {number} options.counter - Index des Artikels (0 = erster, 1 = zweiter, …) * @param {string} [options.language] * @param {string} [options.category] * @returns {Promise<{ results: Array, nextPage: string|null }>} */ async function getNews({ counter = 0, language = 'de', category = 'top' }) { const neededIndex = Math.max(0, counter); const collected = []; let nextPageToken = null; while (collected.length <= neededIndex) { const page = await fetchNewsPage({ language, category, nextPageToken: nextPageToken || undefined }); const items = page.results ?? []; collected.push(...items); nextPageToken = page.nextPage ?? null; if (items.length === 0 || !nextPageToken) break; } const single = collected[neededIndex] ? [collected[neededIndex]] : []; return { results: single, nextPage: null }; } export default { getNews };