98 lines
2.5 KiB
JavaScript
98 lines
2.5 KiB
JavaScript
const API_BASE = "http://192.168.1.3:3000";
|
|
|
|
// ------------------------------
|
|
// AUTH
|
|
// ------------------------------
|
|
|
|
export async function login(email, password) {
|
|
const res = await fetch(`${API_BASE}/auth/login`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ email, password })
|
|
});
|
|
|
|
const data = await res.json();
|
|
if (!res.ok) throw new Error(data.error || "Errore login");
|
|
return data.token;
|
|
}
|
|
|
|
export async function register(email, password) {
|
|
const res = await fetch(`${API_BASE}/auth/register`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ email, password })
|
|
});
|
|
|
|
const data = await res.json();
|
|
if (!res.ok) throw new Error(data.error || "Errore registrazione");
|
|
return data;
|
|
}
|
|
|
|
// ------------------------------
|
|
// LINKS
|
|
// ------------------------------
|
|
|
|
export async function getLinks(token) {
|
|
const res = await fetch(`${API_BASE}/links`, {
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
Accept: "application/json"
|
|
}
|
|
});
|
|
|
|
const data = await res.json();
|
|
if (!res.ok) throw new Error(data.error || "Errore caricamento link");
|
|
return data;
|
|
}
|
|
|
|
export async function createLink(token, { name, url, iconFile }) {
|
|
const formData = new FormData();
|
|
formData.append("name", name);
|
|
formData.append("url", url);
|
|
if (iconFile) formData.append("icon", iconFile);
|
|
|
|
const res = await fetch(`${API_BASE}/links`, {
|
|
method: "POST",
|
|
headers: {
|
|
Authorization: `Bearer ${token}`
|
|
},
|
|
body: formData
|
|
});
|
|
|
|
const data = await res.json();
|
|
if (!res.ok) throw new Error(data.error || "Errore creazione link");
|
|
return data;
|
|
}
|
|
|
|
export async function deleteLink(token, id) {
|
|
const res = await fetch(`${API_BASE}/links/${id}`, {
|
|
method: "DELETE",
|
|
headers: {
|
|
Authorization: `Bearer ${token}`
|
|
}
|
|
});
|
|
|
|
const data = await res.json();
|
|
if (!res.ok) throw new Error(data.error || "Errore eliminazione link");
|
|
return data;
|
|
}
|
|
|
|
export async function updateLink(token, id, { name, url, iconFile }) {
|
|
const formData = new FormData();
|
|
if (name) formData.append("name", name);
|
|
if (url) formData.append("url", url);
|
|
if (iconFile) formData.append("icon", iconFile);
|
|
|
|
const res = await fetch(`${API_BASE}/links/${id}`, {
|
|
method: "PUT",
|
|
headers: {
|
|
Authorization: `Bearer ${token}`
|
|
},
|
|
body: formData
|
|
});
|
|
|
|
const data = await res.json();
|
|
if (!res.ok) throw new Error(data.error || "Errore aggiornamento link");
|
|
return data;
|
|
}
|
|
|