Files
myworkspace/LinkSyncServer/static/js/main.js
DavidSaylor 77b076c7d7 feat: add web UI with login, CRUD, admin, and API key management
- Add login page with JWT authentication
- Add dashboard with stats and quick actions
- Add links management page (full CRUD with search)
- Add collections management page
- Add API key management page with copy-to-clipboard
- Add admin user management page (admin only)
- Fix UUID type mismatches across all endpoints
- Add updated_at column to api_keys and audit_log in schema.sql
- Fix DB_PASSWORD default in docker-compose.yml
- Add PyJWT to requirements.txt
- Fix API docs URL (/docs instead of /api/docs)
- Improve JS error handling (show actual messages)
- Rewrite conftest.py with proper DB lifecycle management
- Add 42 new integration tests (84 total, all passing)
  - test_admin.py: 15 tests for admin endpoints
  - test_auth_extended.py: 9 tests for API key CRUD
  - test_tags.py: 12 tests for tag endpoints
  - test_sync.py: 6 tests for sync endpoints
2026-05-21 07:21:49 -05:00

130 lines
4.4 KiB
JavaScript

document.addEventListener("DOMContentLoaded", function () {
const apiBase = "/api";
async function apiFetch(endpoint, options = {}) {
const token = localStorage.getItem("token");
const headers = {
"Content-Type": "application/json",
...options.headers,
};
if (token) {
headers["Authorization"] = `Bearer ${token}`;
}
const response = await fetch(`${apiBase}${endpoint}`, {
...options,
headers,
});
if (!response.ok) {
let errorMsg = `HTTP ${response.status}`;
try {
const error = await response.json();
if (typeof error.detail === 'string') {
errorMsg = error.detail;
} else if (Array.isArray(error.detail)) {
errorMsg = error.detail.map(d => d.msg || d).join(', ');
} else if (error.detail && typeof error.detail === 'object') {
errorMsg = error.detail.detail || error.detail.msg || JSON.stringify(error.detail);
}
} catch {
// ignore parse error, use default
}
throw new Error(errorMsg);
}
return response.json();
}
window.LinkSync = {
apiFetch,
async getLinks(params = {}) {
const qs = new URLSearchParams(params).toString();
return apiFetch(`/links/?${qs}`);
},
async createLink(data) {
return apiFetch("/links/", {
method: "POST",
body: JSON.stringify(data),
});
},
async updateLink(id, data) {
return apiFetch(`/links/${id}`, {
method: "PUT",
body: JSON.stringify(data),
});
},
async deleteLink(id) {
return apiFetch(`/links/${id}`, { method: "DELETE" });
},
async getCollections() {
return apiFetch("/collections/");
},
async createCollection(data) {
return apiFetch("/collections/", {
method: "POST",
body: JSON.stringify(data),
});
},
async updateCollection(id, data) {
return apiFetch(`/collections/${id}`, {
method: "PUT",
body: JSON.stringify(data),
});
},
async deleteCollection(id) {
return apiFetch(`/collections/${id}`, { method: "DELETE" });
},
async executeQuery(expression, limit = 20) {
return apiFetch(`/queries/execute?expression=${encodeURIComponent(expression)}&limit=${limit}`);
},
async login(username, password) {
const formData = new URLSearchParams();
formData.append("username", username);
formData.append("password", password);
const response = await fetch(`${apiBase}/auth/login`, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: formData.toString(),
});
if (!response.ok) throw new Error("Login failed");
const data = await response.json();
localStorage.setItem("token", data.access_token);
return data;
},
async getMe() {
return apiFetch("/auth/me");
},
logout() {
localStorage.removeItem("token");
localStorage.removeItem("user");
},
async getUsers() {
return apiFetch("/admin/users");
},
async createUser(data) {
return apiFetch("/admin/users", {
method: "POST",
body: JSON.stringify(data),
});
},
async updateUser(id, data) {
return apiFetch(`/admin/users/${id}`, {
method: "PUT",
body: JSON.stringify(data),
});
},
async deleteUser(id) {
return apiFetch(`/admin/users/${id}`, { method: "DELETE" });
},
async getApiKeys() {
return apiFetch("/auth/api-keys");
},
async createApiKey(name) {
return apiFetch(`/auth/api-key?name=${encodeURIComponent(name)}`, {
method: "POST",
});
},
async deleteApiKey(id) {
return apiFetch(`/auth/api-key/${id}`, { method: "DELETE" });
},
};
});