feat: update

This commit is contained in:
yanghao05
2025-04-17 09:36:09 +08:00
parent e03564631a
commit 0d0887b4fb
7 changed files with 234 additions and 376 deletions

View File

@@ -0,0 +1,48 @@
import { useAuthStore } from '@/stores/auth';
import axios from 'axios';
// Create axios instance with default config
const client = axios.create({
baseURL: '/v1',
timeout: 10000,
headers: {
'Content-Type': 'application/json',
}
});
// Request interceptor
client.interceptors.request.use(
config => {
const authStore = useAuthStore();
if (authStore.isAuthenticated && authStore.token) {
config.headers.Authorization = `Bearer ${authStore.token}`;
}
return config;
},
error => {
return Promise.reject(error);
}
);
// Response interceptor
client.interceptors.response.use(
response => {
return response
},
error => {
// Handle HTTP errors here
if (error.response) {
// Server responded with error status
console.error('API Error:', error.response.status, error.response.data);
} else if (error.request) {
// Request made but no response received
console.error('API Error: No response received', error.request);
} else {
// Something else happened
console.error('API Error:', error.message);
}
return Promise.reject(error);
}
);
export default client;