import { fallbackContacts, fallbackDocuments, fallbackEvents, fallbackFuelPrices, fallbackNews, fallbackNotices, type ContactItem, type DocumentItem, type EventItem, type FuelPrice, type NewsItem, type Notice, } from './fallback-data'; type CollectionName = 'news' | 'events' | 'notices' | 'fuel_prices' | 'documents' | 'contacts'; const defaultSortByCollection: Partial> = { news: '-publish_date', events: '-start_datetime', notices: '-priority', fuel_prices: '-last_updated', documents: '-uploaded_at', contacts: 'order', }; declare const process: { env: Record; }; const directusUrl = process.env.DIRECTUS_URL ?? 'http://directus:8055'; const directusToken = process.env.DIRECTUS_ADMIN_TOKEN; async function readCollection(collection: CollectionName): Promise { const endpoint = new URL(`/items/${collection}`, directusUrl); endpoint.searchParams.set('limit', '100'); const sort = defaultSortByCollection[collection]; if (sort) { endpoint.searchParams.set('sort', sort); } try { const headers: Record = {}; if (directusToken) { headers['Authorization'] = `Bearer ${directusToken}`; } const response = await fetch(endpoint, { headers: Object.keys(headers).length > 0 ? headers : undefined, }); if (!response.ok) { throw new Error(`Directus responded with ${response.status}`); } const payload = (await response.json()) as { data?: T[] }; return payload.data ?? []; } catch { return fallbackFor(collection) as T[]; } } function fallbackFor(collection: CollectionName) { switch (collection) { case 'news': return fallbackNews; case 'events': return fallbackEvents; case 'notices': return fallbackNotices; case 'fuel_prices': return fallbackFuelPrices; case 'documents': return fallbackDocuments; case 'contacts': return fallbackContacts; } } export const getNews = () => readCollection('news'); export const getEvents = () => readCollection('events'); export const getNotices = () => readCollection('notices'); export const getFuelPrices = () => readCollection('fuel_prices'); export const getDocuments = () => readCollection('documents'); export const getContacts = () => readCollection('contacts');