const PAYLOAD_URL = process.env.PAYLOAD_URL || "https://payload.mk360.de"; const TENANT_SLUG = "einfach-produktiv"; export type BlogPost = { id: number; title: string; slug: string; category: string; readTime: number; excerpt: string; thumbnail: string | null; }; type PayloadPost = { id: number; title: string; slug: string; category: { name: string } | number | null; readTime: number; excerpt: string; thumbnail: { url: string } | number | null; }; export async function getBlogPosts(limit = 3): Promise { const params = new URLSearchParams({ "where[tenant.slug][equals]": TENANT_SLUG, sort: "-publishedAt", depth: "2", limit: String(limit), }); const res = await fetch(`${PAYLOAD_URL}/api/posts?${params}`, { next: { revalidate: 60 }, }); if (!res.ok) { console.error(`getBlogPosts: Payload returned ${res.status} ${res.statusText}`); return []; } const data: { docs?: PayloadPost[] } = await res.json(); const docs = Array.isArray(data.docs) ? data.docs : []; return docs.map((post) => ({ id: post.id, title: post.title, slug: post.slug, category: typeof post.category === "object" && post.category ? post.category.name : "", readTime: post.readTime, excerpt: post.excerpt, thumbnail: typeof post.thumbnail === "object" && post.thumbnail ? post.thumbnail.url : null, })); }