This commit is contained in:
Vula Builder
2026-07-29 18:15:19 +00:00
parent b7270774dc
commit d4609c9615
+44
View File
@@ -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 || '3e5f5ceb-fd52-413d-83d5-f566bbd091a6'
export interface CmsItem {
id: string
collection: string
title: string | null
data: Record<string, unknown>
status: string
sortOrder: number
scheduledAt: string | null
expiresAt: string | null
publishedAt: string | null
createdAt: string
}
async function fetchCollection(collection: string, limit = 20): Promise<CmsItem[]> {
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<string[]> => {
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')