summaryrefslogtreecommitdiff
path: root/apps/web/src/lib
diff options
context:
space:
mode:
authorDominik Koch <dominik@koch-bautechnik.de>2025-07-21 23:45:02 +0200
committerDominik Koch <dominik@koch-bautechnik.de>2025-07-21 23:45:02 +0200
commit5ef6e2bc97d5b0def83f4ee78a515d2b216a6ed3 (patch)
tree9479a6528703e4a644552e594bedc97b7ed036a6 /apps/web/src/lib
parent7ea4b316265bf0d4387f9dd0f76550a685bcb959 (diff)
downloadopencut-5ef6e2bc97d5b0def83f4ee78a515d2b216a6ed3.tar.gz
fix: implement coderabbit suggestions
They actually good wtf
Diffstat (limited to 'apps/web/src/lib')
-rw-r--r--apps/web/src/lib/blog-query.ts77
1 files changed, 33 insertions, 44 deletions
diff --git a/apps/web/src/lib/blog-query.ts b/apps/web/src/lib/blog-query.ts
index 71ee7341..55fe25ab 100644
--- a/apps/web/src/lib/blog-query.ts
+++ b/apps/web/src/lib/blog-query.ts
@@ -4,62 +4,51 @@ import rehypeParse from "rehype-parse";
import rehypeStringify from "rehype-stringify";
import rehypeSlug from "rehype-slug";
import rehypeAutolinkHeadings from "rehype-autolink-headings";
+import rehypeSanitize from "rehype-sanitize";
const url = process.env.MARBLE_API_URL;
const key = process.env.MARBLE_WORKSPACE_KEY;
-export async function getPosts() {
- try {
- const raw = await fetch(`${url}/${key}/posts`);
- const data: MarblePostList = await raw.json();
- return data;
- } catch (error) {
- console.log(error);
+async function fetchFromMarble<T>(endpoint: string): Promise<T> {
+ if (!url || !key) {
+ throw new Error('Missing required environment variables');
+ }
+
+ try {
+ const response = await fetch(`${url}/${key}/${endpoint}`);
+ if (!response.ok) {
+ throw new Error(`Failed to fetch ${endpoint}: ${response.status} ${response.statusText}`);
+ }
+ return await response.json() as T;
+ } catch (error) {
+ console.error(`Error fetching ${endpoint}:`, error);
+ throw error;
+ }
}
-}
-
-export async function getTags() {
- try {
- const raw = await fetch(`${url}/${key}/tags`);
- const data: MarbleTagList = await raw.json();
- return data;
- } catch (error) {
- console.log(error);
+
+ export async function getPosts() {
+ return fetchFromMarble<MarblePostList>('posts');
}
-}
-
-export async function getSinglePost(slug: string) {
- try {
- const raw = await fetch(`${url}/${key}/posts/${slug}`);
- const data: MarblePost = await raw.json();
- return data;
- } catch (error) {
- console.log(error);
+
+ export async function getTags() {
+ return fetchFromMarble<MarbleTagList>('tags');
}
-}
-
-export async function getCategories() {
- try {
- const raw = await fetch(`${url}/${key}/categories`);
- const data: MarbleCategoryList = await raw.json();
- return data;
- } catch (error) {
- console.log(error);
+
+ export async function getSinglePost(slug: string) {
+ return fetchFromMarble<MarblePost>(`posts/${slug}`);
}
-}
-
-export async function getAuthors() {
- try {
- const raw = await fetch(`${url}/${key}/authors`);
- const data: MarbleAuthorList = await raw.json();
- return data;
- } catch (error) {
- console.log(error);
+
+ export async function getCategories() {
+ return fetchFromMarble<MarbleCategoryList>('categories');
+ }
+
+ export async function getAuthors() {
+ return fetchFromMarble<MarbleAuthorList>('authors');
}
-}
export async function processHtmlContent(html: string): Promise<string> {
const processor = unified()
+ .use(rehypeSanitize)
.use(rehypeParse, { fragment: true })
.use(rehypeSlug)
.use(rehypeAutolinkHeadings, { behavior: "append" })