diff options
| author | Maze Winther <mazewinther@gmail.com> | 2026-03-24 14:18:39 +0100 |
|---|---|---|
| committer | Maze Winther <mazewinther@gmail.com> | 2026-03-24 14:18:39 +0100 |
| commit | f2eed05d36846cd771458b20e77840e907084bc5 (patch) | |
| tree | d3a790c4c3d7fee6cf927832a5de2a71c8afbca0 /apps/web/src/lib | |
| parent | 26d523ebad3dfdcb682db049899ada84ac739ed7 (diff) | |
| download | opencut-f2eed05d36846cd771458b20e77840e907084bc5.tar.gz | |
fix blog build when marble cms isn't available
Diffstat (limited to 'apps/web/src/lib')
| -rw-r--r-- | apps/web/src/lib/blog/query.ts | 70 |
1 files changed, 63 insertions, 7 deletions
diff --git a/apps/web/src/lib/blog/query.ts b/apps/web/src/lib/blog/query.ts index e6d68bb6..f814eeaf 100644 --- a/apps/web/src/lib/blog/query.ts +++ b/apps/web/src/lib/blog/query.ts @@ -1,6 +1,7 @@ import type { MarbleAuthorList, MarbleCategoryList, + Pagination, MarblePost, MarblePostList, MarbleTagList, @@ -16,43 +17,98 @@ const url = process.env.NEXT_PUBLIC_MARBLE_API_URL ?? "https://api.marblecms.com"; const key = process.env.MARBLE_WORKSPACE_KEY ?? "cmd4iw9mm0006l804kwqv0k46"; +const EMPTY_PAGINATION: Pagination = { + limit: 0, + currpage: 1, + nextPage: null, + prevPage: null, + totalItems: 0, + totalPages: 0, +}; + +const EMPTY_POSTS: MarblePostList = { + posts: [], + pagination: EMPTY_PAGINATION, +}; + +const EMPTY_TAGS: MarbleTagList = { + tags: [], + pagination: EMPTY_PAGINATION, +}; + +const EMPTY_CATEGORIES: MarbleCategoryList = { + categories: [], + pagination: EMPTY_PAGINATION, +}; + +const EMPTY_AUTHORS: MarbleAuthorList = { + authors: [], + pagination: EMPTY_PAGINATION, +}; + +function isMarbleConfigured() { + return !["", "placeholder", "build-placeholder", "your_workspace_key_here"].includes(key); +} + async function fetchFromMarble<T>({ endpoint, + fallback, }: { endpoint: string; + fallback: T; }): Promise<T> { + if (!isMarbleConfigured()) { + return fallback; + } + try { const response = await fetch(`${url}/${key}/${endpoint}`); if (!response.ok) { - throw new Error( + console.warn( `Failed to fetch ${endpoint}: ${response.status} ${response.statusText}`, ); + return fallback; } return (await response.json()) as T; } catch (error) { console.error(`Error fetching ${endpoint}:`, error); - throw error; + return fallback; } } export async function getPosts() { - return fetchFromMarble<MarblePostList>({ endpoint: "posts" }); + return fetchFromMarble<MarblePostList>({ + endpoint: "posts", + fallback: EMPTY_POSTS, + }); } export async function getTags() { - return fetchFromMarble<MarbleTagList>({ endpoint: "tags" }); + return fetchFromMarble<MarbleTagList>({ + endpoint: "tags", + fallback: EMPTY_TAGS, + }); } export async function getSinglePost({ slug }: { slug: string }) { - return fetchFromMarble<MarblePost>({ endpoint: `posts/${slug}` }); + return fetchFromMarble<MarblePost | null>({ + endpoint: `posts/${slug}`, + fallback: null, + }); } export async function getCategories() { - return fetchFromMarble<MarbleCategoryList>({ endpoint: "categories" }); + return fetchFromMarble<MarbleCategoryList>({ + endpoint: "categories", + fallback: EMPTY_CATEGORIES, + }); } export async function getAuthors() { - return fetchFromMarble<MarbleAuthorList>({ endpoint: "authors" }); + return fetchFromMarble<MarbleAuthorList>({ + endpoint: "authors", + fallback: EMPTY_AUTHORS, + }); } export async function processHtmlContent({ |
