import { getBackendUrl } from './config'; import type { MergedArticle, Tag, TrackedEventPublic, Category, Weather, StockTicker, BookmarksFeed, Poe2Data, WidgetsEnabled } from './types'; async function get(path: string, fetchFn: typeof fetch = fetch): Promise { // credentials: 'include' so the private-access cookie (see lib/privateAccess.ts) // is sent cross-origin to the backend, revealing private categories/articles to // anyone who's logged in — without it every request would look unauthenticated. const res = await fetchFn(`${getBackendUrl()}${path}`, { credentials: 'include' }); if (!res.ok) throw new Error(`Request failed: ${path} (${res.status})`); return res.json(); } export interface FeedParams { category?: string; geo?: string; eventId?: string; tag?: string; before?: string; limit?: number; } export function getFeed(params: FeedParams = {}, fetchFn?: typeof fetch): Promise { const qs = new URLSearchParams( Object.fromEntries(Object.entries(params).filter(([, v]) => v !== undefined).map(([k, v]) => [k, String(v)])) ).toString(); return get(`/api/feed${qs ? `?${qs}` : ''}`, fetchFn); } export function getArticle(id: string, fetchFn?: typeof fetch): Promise { return get(`/api/article/${id}`, fetchFn); } export function getTags(fetchFn?: typeof fetch): Promise { return get('/api/tags', fetchFn); } export function getTagBySlug(slug: string, fetchFn?: typeof fetch): Promise { return get(`/api/tag/${slug}`, fetchFn); } export function getEvents(fetchFn?: typeof fetch): Promise { return get('/api/events', fetchFn); } export function getCategories(fetchFn?: typeof fetch): Promise { return get('/api/categories', fetchFn); } export function getWeather(fetchFn?: typeof fetch): Promise { return get('/api/widget/weather', fetchFn); } export function getStocks(fetchFn?: typeof fetch): Promise { return get('/api/widget/stocks', fetchFn); } export function getBookmarks(fetchFn?: typeof fetch): Promise { return get('/api/widget/bookmarks', fetchFn); } export function getPoe2(fetchFn?: typeof fetch): Promise { return get('/api/widget/poe2', fetchFn); } export function getWidgetsEnabled(fetchFn?: typeof fetch): Promise { return get('/api/widgets', fetchFn); }