初始化

This commit is contained in:
梁泽军
2025-06-16 18:12:26 +08:00
commit f1cf65e7a7
57 changed files with 1739 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
import axios from 'axios';
const API_URL = process.env.REACT_APP_API_URL || 'http://localhost:5050/api/auth';
export const register = async (username: string, password: string) => {
const res = await axios.post(`${API_URL}/register`, { username, password });
return res.data;
};
export const login = async (username: string, password: string) => {
const res = await axios.post(`${API_URL}/login`, { username, password });
return res.data;
};

View File

@@ -0,0 +1,15 @@
import axios from 'axios';
export const api = axios.create({
baseURL: import.meta.env.VITE_API_URL,
});
api.interceptors.response.use(
response => response,
error => {
if (error.response && error.response.status === 401) {
window.location.href = '/login';
}
return Promise.reject(error);
}
);

View File

@@ -0,0 +1,21 @@
import type { Todo } from '../types/todo';
import { api } from './axios';
export const getTodos = async (): Promise<Todo[]> => {
const response = await api.get('/api/todos');
return response.data;
};
export const addTodo = async (title: string, priority: Todo['priority']): Promise<Todo> => {
const response = await api.post('/api/todos', { title, priority });
return response.data;
};
export const toggleTodo = async (id: number): Promise<Todo> => {
const response = await api.patch(`/api/todos/${id}/toggle`);
return response.data;
};
export const deleteTodo = async (id: number): Promise<void> => {
await api.delete(`/api/todos/${id}`);
};