初始化
This commit is contained in:
13
client-temp/src/api/auth.ts
Normal file
13
client-temp/src/api/auth.ts
Normal 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;
|
||||
};
|
||||
15
client-temp/src/api/axios.ts
Normal file
15
client-temp/src/api/axios.ts
Normal 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);
|
||||
}
|
||||
);
|
||||
21
client-temp/src/api/todo.ts
Normal file
21
client-temp/src/api/todo.ts
Normal 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}`);
|
||||
};
|
||||
Reference in New Issue
Block a user