Initial commit
This commit is contained in:
95
frontend/src/api/client.ts
Normal file
95
frontend/src/api/client.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import axios from 'axios';
|
||||
import type {
|
||||
LoginRequest,
|
||||
RegisterRequest,
|
||||
Token,
|
||||
User,
|
||||
UserCreate,
|
||||
UserUpdatePayload,
|
||||
} from '../types';
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: '/api/v1',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
// Add auth token to requests
|
||||
api.interceptors.request.use((config) => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
// Auth endpoints
|
||||
export const authAPI = {
|
||||
login: async (data: LoginRequest): Promise<Token> => {
|
||||
const response = await api.post<Token>('/auth/login', data);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
register: async (data: RegisterRequest): Promise<User> => {
|
||||
const response = await api.post<User>('/auth/register', data);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
getCurrentUser: async (): Promise<User> => {
|
||||
const response = await api.get<User>('/auth/me');
|
||||
return response.data;
|
||||
},
|
||||
};
|
||||
|
||||
// Settings endpoints
|
||||
export const settingsAPI = {
|
||||
getTheme: async (): Promise<Record<string, string>> => {
|
||||
const response = await api.get<Record<string, string>>('/settings/theme');
|
||||
return response.data;
|
||||
},
|
||||
|
||||
updateTheme: async (data: Record<string, string>): Promise<Record<string, string>> => {
|
||||
const response = await api.put<Record<string, string>>('/settings/theme', data);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
getModules: async (): Promise<Record<string, boolean | string>> => {
|
||||
const response = await api.get<Record<string, boolean | string>>('/settings/modules');
|
||||
return response.data;
|
||||
},
|
||||
|
||||
updateModules: async (data: Record<string, boolean>): Promise<Record<string, boolean>> => {
|
||||
const response = await api.put<Record<string, boolean>>('/settings/modules', data);
|
||||
return response.data;
|
||||
},
|
||||
};
|
||||
|
||||
// Users endpoints
|
||||
export const usersAPI = {
|
||||
list: async (): Promise<User[]> => {
|
||||
const response = await api.get<User[]>('/users');
|
||||
return response.data;
|
||||
},
|
||||
|
||||
get: async (id: string): Promise<User> => {
|
||||
const response = await api.get<User>(`/users/${id}`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
create: async (data: UserCreate): Promise<User> => {
|
||||
const response = await api.post<User>('/users', data);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
update: async (id: string, data: UserUpdatePayload): Promise<User> => {
|
||||
const response = await api.put<User>(`/users/${id}`, data);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
delete: async (id: string): Promise<void> => {
|
||||
await api.delete(`/users/${id}`);
|
||||
},
|
||||
};
|
||||
|
||||
export default api;
|
||||
Reference in New Issue
Block a user