исправил клиент, пути в функциях, добавил переменную окружения
This commit is contained in:
1 parent
8aa8e04077
commit
1699873ea8
8 files changed
+59
-19
No files matched your search
+41
-7
@@ -1,9 +1,43 @@
|
||||
import axios from "axios";
|
||||
import axios, { AxiosInstance } from 'axios';
|
||||
|
||||
export const API_BASE = "";
|
||||
const client = axios.create({
|
||||
baseURL: API_BASE,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
class ApiClient {
|
||||
private axiosInstance: AxiosInstance;
|
||||
|
||||
export default client;
|
||||
constructor(baseURL: string = import.meta.env.VITE_API_URL) {
|
||||
this.axiosInstance = axios.create({
|
||||
baseURL,
|
||||
timeout: 10000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
// Интерцептор для обработки ошибок
|
||||
this.axiosInstance.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response?.status === 404) {
|
||||
console.error('Resource not found:', error.config?.url);
|
||||
} else if (error.response?.status === 500) {
|
||||
console.error('Server error:', error.response?.data);
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
async get<T>(url: string): Promise<{ data: T }> {
|
||||
const response = await this.axiosInstance.get<T>(url);
|
||||
return { data: response.data };
|
||||
}
|
||||
|
||||
async post<T>(
|
||||
url: string,
|
||||
data?: any,
|
||||
): Promise<{ data: T }> {
|
||||
const response = await this.axiosInstance.post<T>(url, data);
|
||||
return { data: response.data };
|
||||
}
|
||||
}
|
||||
|
||||
export const client = new ApiClient('/api');
|
||||
Reference in new issue
Block a user