diff --git a/src/lib/cms.ts b/src/lib/cms.ts new file mode 100644 index 0000000..c6aac22 --- /dev/null +++ b/src/lib/cms.ts @@ -0,0 +1,44 @@ +// src/lib/cms.ts — server-side Vula CMS fetch helpers (do not import on client) +const VULA_CMS_URL = process.env.VULA_CMS_URL || 'https://ide.vulai.co.za' +const VULA_CMS_PROJECT_ID = process.env.VULA_CMS_PROJECT_ID || '33794467-33ff-448d-a769-45d80bb2b543' + +export interface CmsItem { + id: string + collection: string + title: string | null + data: Record + status: string + sortOrder: number + scheduledAt: string | null + expiresAt: string | null + publishedAt: string | null + createdAt: string +} + +async function fetchCollection(collection: string, limit = 20): Promise { + if (!VULA_CMS_PROJECT_ID) return [] + try { + const url = `${VULA_CMS_URL}/api/public/cms/${VULA_CMS_PROJECT_ID}/${collection}?status=published&limit=${limit}` + const res = await fetch(url, { next: { revalidate: 60 } }) + if (!res.ok) return [] + const json = await res.json() as { items?: CmsItem[] } + return json.items ?? [] + } catch { + return [] + } +} + +export const getCampaigns = () => fetchCollection('campaigns') +export const getSpecials = () => fetchCollection('specials') +export const getBusinessHours = () => fetchCollection('business_hours', 7) +export const getPromotions = () => fetchCollection('promotions') +export const getFeaturedProductIds = async (): Promise => { + const items = await fetchCollection('featured_products', 10) + return items.flatMap(i => { + const ids = i.data.product_ids + return Array.isArray(ids) ? ids as string[] : [] + }) +} +export const getBlogPosts = (limit = 10) => fetchCollection('posts', limit) +export const getAdvertBanners = () => fetchCollection('advert_banners') +export const getProductCarousels = () => fetchCollection('product_carousels')