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,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