mirror of
https://github.com/thiloho/archtika.git
synced 2025-11-22 10:51:36 +01:00
Create fetch utility function
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import type { Actions, PageServerLoad } from "./$types";
|
||||
import { API_BASE_PREFIX } from "$lib/server/utils";
|
||||
import type { Article, ArticleInput, DocsCategory } from "$lib/db-schema";
|
||||
import { apiRequest } from "$lib/server/utils";
|
||||
import type { Article, DocsCategory } from "$lib/db-schema";
|
||||
|
||||
export const load: PageServerLoad = async ({ params, fetch, cookies, url, parent, locals }) => {
|
||||
export const load: PageServerLoad = async ({ params, fetch, url, parent, locals }) => {
|
||||
const searchQuery = url.searchParams.get("article_search_query");
|
||||
const filterBy = url.searchParams.get("article_filter");
|
||||
|
||||
@@ -34,28 +35,22 @@ export const load: PageServerLoad = async ({ params, fetch, cookies, url, parent
|
||||
|
||||
const constructedFetchUrl = `${baseFetchUrl}&${parameters.toString()}`;
|
||||
|
||||
const totalArticlesData = await fetch(baseFetchUrl, {
|
||||
method: "HEAD",
|
||||
const totalArticles = await apiRequest(fetch, baseFetchUrl, "HEAD", {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${cookies.get("session_token")}`,
|
||||
Prefer: "count=exact"
|
||||
}
|
||||
},
|
||||
returnData: true
|
||||
});
|
||||
|
||||
const totalArticleCount = Number(
|
||||
totalArticlesData.headers.get("content-range")?.split("/").at(-1)
|
||||
totalArticles.data.headers.get("content-range")?.split("/").at(-1)
|
||||
);
|
||||
|
||||
const articlesData = await fetch(constructedFetchUrl, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${cookies.get("session_token")}`
|
||||
}
|
||||
});
|
||||
|
||||
const articles: (Article & { docs_category: DocsCategory | null })[] = await articlesData.json();
|
||||
const articles: (Article & { docs_category: DocsCategory | null })[] = (
|
||||
await apiRequest(fetch, constructedFetchUrl, "GET", {
|
||||
returnData: true
|
||||
})
|
||||
).data;
|
||||
|
||||
return {
|
||||
totalArticleCount,
|
||||
@@ -66,44 +61,22 @@ export const load: PageServerLoad = async ({ params, fetch, cookies, url, parent
|
||||
};
|
||||
|
||||
export const actions: Actions = {
|
||||
createArticle: async ({ request, fetch, cookies, params }) => {
|
||||
createArticle: async ({ request, fetch, params }) => {
|
||||
const data = await request.formData();
|
||||
|
||||
const res = await fetch(`${API_BASE_PREFIX}/article`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${cookies.get("session_token")}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
return await apiRequest(fetch, `${API_BASE_PREFIX}/article`, "POST", {
|
||||
body: {
|
||||
website_id: params.websiteId,
|
||||
title: data.get("title") as string
|
||||
} satisfies ArticleInput)
|
||||
title: data.get("title")
|
||||
},
|
||||
successMessage: "Successfully created article"
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const response = await res.json();
|
||||
return { success: false, message: response.message };
|
||||
}
|
||||
|
||||
return { success: true, message: "Successfully created article" };
|
||||
},
|
||||
deleteArticle: async ({ request, fetch, cookies }) => {
|
||||
deleteArticle: async ({ request, fetch }) => {
|
||||
const data = await request.formData();
|
||||
|
||||
const res = await fetch(`${API_BASE_PREFIX}/article?id=eq.${data.get("id")}`, {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${cookies.get("session_token")}`
|
||||
}
|
||||
return await apiRequest(fetch, `${API_BASE_PREFIX}/article?id=eq.${data.get("id")}`, "DELETE", {
|
||||
successMessage: "Successfully deleted article"
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const response = await res.json();
|
||||
return { success: false, message: response.message };
|
||||
}
|
||||
|
||||
return { success: true, message: "Successfully deleted article" };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,30 +1,28 @@
|
||||
import type { Actions, PageServerLoad } from "./$types";
|
||||
import { API_BASE_PREFIX } from "$lib/server/utils";
|
||||
import { API_BASE_PREFIX, apiRequest } from "$lib/server/utils";
|
||||
import type { Article, DocsCategory } from "$lib/db-schema";
|
||||
|
||||
export const load: PageServerLoad = async ({ parent, params, cookies, fetch }) => {
|
||||
const articleData = await fetch(`${API_BASE_PREFIX}/article?id=eq.${params.articleId}`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${cookies.get("session_token")}`,
|
||||
Accept: "application/vnd.pgrst.object+json"
|
||||
}
|
||||
});
|
||||
|
||||
const categoryData = await fetch(
|
||||
`${API_BASE_PREFIX}/docs_category?website_id=eq.${params.websiteId}&order=category_weight.desc`,
|
||||
{
|
||||
method: "GET",
|
||||
export const load: PageServerLoad = async ({ parent, params, fetch }) => {
|
||||
const article: Article = (
|
||||
await apiRequest(fetch, `${API_BASE_PREFIX}/article?id=eq.${params.articleId}`, "GET", {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${cookies.get("session_token")}`
|
||||
}
|
||||
}
|
||||
);
|
||||
Accept: "application/vnd.pgrst.object+json"
|
||||
},
|
||||
returnData: true
|
||||
})
|
||||
).data;
|
||||
|
||||
const categories: DocsCategory[] = (
|
||||
await apiRequest(
|
||||
fetch,
|
||||
`${API_BASE_PREFIX}/docs_category?website_id=eq.${params.websiteId}&order=category_weight.desc`,
|
||||
"GET",
|
||||
{
|
||||
returnData: true
|
||||
}
|
||||
)
|
||||
).data;
|
||||
|
||||
const article: Article = await articleData.json();
|
||||
const categories: DocsCategory[] = await categoryData.json();
|
||||
const { website } = await parent();
|
||||
|
||||
return { website, article, categories, API_BASE_PREFIX };
|
||||
@@ -47,66 +45,50 @@ export const actions: Actions = {
|
||||
headers["X-Original-Filename"] = coverFile.name;
|
||||
}
|
||||
|
||||
const uploadedImageData = await fetch(`${API_BASE_PREFIX}/rpc/upload_file`, {
|
||||
method: "POST",
|
||||
const uploadedImage = await apiRequest(fetch, `${API_BASE_PREFIX}/rpc/upload_file`, "POST", {
|
||||
headers,
|
||||
body: coverFile ? await coverFile.arrayBuffer() : null
|
||||
body: coverFile ? await coverFile.arrayBuffer() : null,
|
||||
returnData: true
|
||||
});
|
||||
|
||||
const uploadedImage = await uploadedImageData.json();
|
||||
|
||||
if (!uploadedImageData.ok && (coverFile?.size ?? 0 > 0)) {
|
||||
if (!uploadedImage.success && (coverFile?.size ?? 0 > 0)) {
|
||||
return { success: false, message: uploadedImage.message };
|
||||
}
|
||||
|
||||
const res = await fetch(`${API_BASE_PREFIX}/article?id=eq.${params.articleId}`, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${cookies.get("session_token")}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
title: data.get("title"),
|
||||
meta_description: data.get("description"),
|
||||
meta_author: data.get("author"),
|
||||
cover_image: uploadedImage.file_id,
|
||||
publication_date: data.get("publication-date"),
|
||||
main_content: data.get("main-content"),
|
||||
category: data.get("category"),
|
||||
article_weight: data.get("article-weight") ? data.get("article-weight") : null
|
||||
})
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const response = await res.json();
|
||||
return { success: false, message: response.message };
|
||||
}
|
||||
|
||||
return { success: true, message: "Successfully updated article" };
|
||||
return await apiRequest(
|
||||
fetch,
|
||||
`${API_BASE_PREFIX}/article?id=eq.${params.articleId}`,
|
||||
"PATCH",
|
||||
{
|
||||
body: {
|
||||
title: data.get("title"),
|
||||
meta_description: data.get("description"),
|
||||
meta_author: data.get("author"),
|
||||
cover_image: uploadedImage.data?.file_id,
|
||||
publication_date: data.get("publication-date"),
|
||||
main_content: data.get("main-content"),
|
||||
category: data.get("category"),
|
||||
article_weight: data.get("article-weight") ? data.get("article-weight") : null
|
||||
},
|
||||
successMessage: "Successfully updated article"
|
||||
}
|
||||
);
|
||||
},
|
||||
pasteImage: async ({ request, fetch, cookies, params }) => {
|
||||
pasteImage: async ({ request, fetch, params }) => {
|
||||
const data = await request.formData();
|
||||
const file = data.get("file") as File;
|
||||
|
||||
const fileData = await fetch(`${API_BASE_PREFIX}/rpc/upload_file`, {
|
||||
method: "POST",
|
||||
return await apiRequest(fetch, `${API_BASE_PREFIX}/rpc/upload_file`, "POST", {
|
||||
headers: {
|
||||
"Content-Type": "application/octet-stream",
|
||||
Authorization: `Bearer ${cookies.get("session_token")}`,
|
||||
Accept: "application/vnd.pgrst.object+json",
|
||||
"X-Website-Id": params.websiteId,
|
||||
"X-Mimetype": file.type,
|
||||
"X-Original-Filename": file.name
|
||||
},
|
||||
body: await file.arrayBuffer()
|
||||
body: await file.arrayBuffer(),
|
||||
successMessage: "Successfully uploaded image",
|
||||
returnData: true
|
||||
});
|
||||
|
||||
const fileJSON = await fileData.json();
|
||||
|
||||
if (!fileData.ok) {
|
||||
return { success: false, message: fileJSON.message };
|
||||
}
|
||||
|
||||
return { success: true, message: "Successfully uploaded image", fileId: fileJSON.file_id };
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user