Files
dupe/client/src/utils/api.js

146 lines
3.9 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;
const ACCESS_TOKEN_KEY = "accessToken";
const REFRESH_TOKEN_KEY = "refreshToken";
export function getAccessToken() {
return localStorage.getItem(ACCESS_TOKEN_KEY);
}
export function getRefreshToken() {
return localStorage.getItem(REFRESH_TOKEN_KEY);
}
export function persistTokens({ accessToken, refreshToken }) {
if (accessToken) {
localStorage.setItem(ACCESS_TOKEN_KEY, accessToken);
localStorage.setItem("token", accessToken);
}
if (refreshToken) localStorage.setItem(REFRESH_TOKEN_KEY, refreshToken);
}
export function clearTokens() {
localStorage.removeItem(ACCESS_TOKEN_KEY);
localStorage.removeItem(REFRESH_TOKEN_KEY);
localStorage.removeItem("token");
}
export function withToken(url) {
const token = getAccessToken();
if (!token) return url;
const separator = url.includes("?") ? "&" : "?";
return `${url}${separator}token=${token}`;
}
// 🔐 Ortak kimlik doğrulama başlığı (token varsa ekler)
export function authHeaders() {
const token = getAccessToken();
return token ? { Authorization: `Bearer ${token}` } : {};
}
async function refreshAccessToken() {
const refreshToken = getRefreshToken();
if (!refreshToken) return null;
const res = await fetch(`${API}/api/token/refresh`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ refreshToken })
});
if (!res.ok) return null;
const { accessToken } = await res.json();
if (accessToken) {
persistTokens({ accessToken });
}
return accessToken || null;
}
// 🔧 Yardımcı fetch (otomatik token ekler, 401'de refresh dener)
export async function apiFetch(path, options = {}, retry = true) {
const headers = { ...(options.headers || {}), ...authHeaders() };
const res = await fetch(`${API}${path}`, { ...options, headers });
if (res.status === 401 && retry) {
const refreshed = await refreshAccessToken();
if (refreshed) {
const retryHeaders = { ...(options.headers || {}), ...authHeaders() };
return fetch(`${API}${path}`, { ...options, headers: retryHeaders });
}
clearTokens();
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();
}
// 👤 Profil
export async function fetchProfile() {
const res = await apiFetch("/api/profile");
return res.json();
}
export async function uploadAvatar(formData) {
const res = await apiFetch("/api/profile/avatar", {
method: "POST",
body: formData
});
return res.json();
}
export async function moveEntry(sourcePath, targetDirectory) {
const res = await apiFetch("/api/file/move", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
sourcePath,
targetDirectory
})
});
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();
}
export async function copyEntry(sourcePath, targetDirectory) {
const res = await apiFetch("/api/file/copy", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
sourcePath,
targetDirectory
})
});
return res.json();
}