Files
frontend/src/api/users.js

43 lines
1.4 KiB
JavaScript

const API_URL = import.meta.env.VITE_EFC_API_URL || 'http://localhost:8000';
import { fetchWithAuth, postWithAuth, putWithAuth, deleteWithAuth } from '../fetchWithAuth';
import { extractApiError } from './apiError';
async function handleResponse(response) {
if (response.status === 401) throw new Error('SESSION_EXPIRED');
if (!response.ok) throw new Error(await extractApiError(response));
const contentType = response.headers.get('content-type');
if (!contentType || !contentType.includes('application/json')) return null;
return response.json();
}
export async function fetchUsers() {
const res = await fetchWithAuth(`${API_URL}/user/users/`);
return handleResponse(res);
}
export async function createUser(userData) {
const res = await postWithAuth(`${API_URL}/user/users/`, userData);
return handleResponse(res);
}
export async function updateUser(id, userData) {
const res = await putWithAuth(`${API_URL}/user/users/${id}/`, userData);
return handleResponse(res);
}
export async function deleteUser(id) {
const res = await deleteWithAuth(`${API_URL}/user/users/${id}/`);
return handleResponse(res);
}
export async function getCurrentUser(token) {
const res = await fetch(`${API_URL}/user/users/me/`, {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
});
return handleResponse(res);
}