55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
const apiBase = import.meta.env.VITE_API;
|
||
export const API = apiBase || window.location.origin;
|
||
|
||
// 🔐 Ortak kimlik doğrulama başlığı (token varsa ekler)
|
||
export function authHeaders() {
|
||
const token = localStorage.getItem("token");
|
||
return token ? { Authorization: `Bearer ${token}` } : {};
|
||
}
|
||
|
||
// 🔧 Yardımcı fetch (otomatik token ekler, hata durumunda logout)
|
||
export async function apiFetch(path, options = {}) {
|
||
const headers = { ...(options.headers || {}), ...authHeaders() };
|
||
const res = await fetch(`${API}${path}`, { ...options, headers });
|
||
|
||
// Token süresi dolmuşsa veya yanlışsa kullanıcıyı çıkışa yönlendir
|
||
if (res.status === 401) {
|
||
localStorage.removeItem("token");
|
||
window.location.reload();
|
||
}
|
||
return res;
|
||
}
|
||
|
||
// 🗑️ Çöp API'leri
|
||
export async function getTrashItems() {
|
||
const res = await apiFetch("/api/trash");
|
||
return res.json();
|
||
}
|
||
|
||
export async function restoreFromTrash(trashName) {
|
||
const res = await apiFetch("/api/trash/restore", {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({ trashName })
|
||
});
|
||
return res.json();
|
||
}
|
||
|
||
export async function deleteFromTrash(trashName) {
|
||
const res = await apiFetch(`/api/trash`, {
|
||
method: "DELETE",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({ trashName })
|
||
});
|
||
return res.json();
|
||
}
|
||
|
||
export async function renameFolder(path, newName) {
|
||
const res = await apiFetch("/api/folder", {
|
||
method: "PATCH",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({ path, newName })
|
||
});
|
||
return res.json();
|
||
}
|