Files
dupe/client/src/utils/api.js
2025-11-02 00:15:06 +03:00

55 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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();
}