first run after refactor of back and frontends
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
class ApiService {
|
||||
constructor() {
|
||||
this.baseUrl = `http://${window.location.hostname}:24024`;
|
||||
this.token = localStorage.getItem('dashy_token');
|
||||
this.subscribers = new Set();
|
||||
}
|
||||
|
||||
subscribe(fn) {
|
||||
this.subscribers.add(fn);
|
||||
return () => this.subscribers.delete(fn);
|
||||
}
|
||||
|
||||
notify() {
|
||||
for (const fn of this.subscribers) fn();
|
||||
}
|
||||
|
||||
async request(endpoint, options = {}) {
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers,
|
||||
};
|
||||
if (this.token) {
|
||||
headers['Authorization'] = `Bearer ${this.token}`;
|
||||
}
|
||||
|
||||
const response = await fetch(`${this.baseUrl}${endpoint}`, {
|
||||
...options,
|
||||
headers,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
this.logout();
|
||||
}
|
||||
throw new Error(`API Error: ${response.statusText}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async login(id, password) {
|
||||
const response = await fetch(`${this.baseUrl}/token`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ id, password }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Login failed');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
this.token = data.access_token;
|
||||
localStorage.setItem('dashy_token', this.token);
|
||||
this.notify();
|
||||
return data;
|
||||
}
|
||||
|
||||
logout() {
|
||||
this.token = null;
|
||||
localStorage.removeItem('dashy_token');
|
||||
this.notify();
|
||||
}
|
||||
|
||||
async getUsers() {
|
||||
return this.request('/users');
|
||||
}
|
||||
|
||||
async getTasks() {
|
||||
return this.request('/tasks');
|
||||
}
|
||||
|
||||
async getAudit() {
|
||||
return this.request('/audit');
|
||||
}
|
||||
|
||||
async createTask(taskData) {
|
||||
const data = await this.request('/tasks', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(taskData),
|
||||
});
|
||||
this.notify();
|
||||
return data;
|
||||
}
|
||||
|
||||
async updateTask(id, updates) {
|
||||
const data = await this.request(`/tasks/${id}`, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify(updates),
|
||||
});
|
||||
this.notify();
|
||||
return data;
|
||||
}
|
||||
|
||||
async addAudit(auditData) {
|
||||
const data = await this.request('/audit', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(auditData),
|
||||
});
|
||||
this.notify();
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
window.api = new ApiService();
|
||||
Reference in New Issue
Block a user