import { getPayload } from "payload"; import configPromise from "@payload-config"; export async function getAllPosts() { if (!process.env.DATABASE_URI && !process.env.POSTGRES_URI) { console.warn( "⚠️ Bypassing Payload fetch during Next.js build: DATABASE_URI is missing.", ); return []; } try { const payload = await getPayload({ config: configPromise }); const { docs } = await payload.find({ collection: "posts", limit: 1000, sort: "-date", where: { and: [ { _status: { equals: "published", }, }, { date: { less_than_equal: new Date(), }, }, ], }, }); return docs.map((doc) => ({ title: doc.title as string, description: doc.description as string, date: doc.date as string, tags: (doc.tags || []).map((t) => typeof t === "object" && t !== null ? t.tag : t, ) as string[], slug: doc.slug as string, thumbnail: (doc.featuredImage && typeof doc.featuredImage === "object" && doc.featuredImage.url ? doc.featuredImage.url : "") || "", body: { code: "" as string }, lexicalContent: doc.content || null, })); } catch (error) { console.warn( "⚠️ Bypassing Payload fetch during build: Database connection refused.", error, ); return []; } }