103 lines
3.1 KiB
TypeScript
103 lines
3.1 KiB
TypeScript
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios';
|
|
import { API_URL } from '../utils/config';
|
|
import { getToken, removeToken, removeUser } from '../utils/storage';
|
|
|
|
export interface ApiError {
|
|
message: string;
|
|
status?: number;
|
|
code?: string;
|
|
}
|
|
|
|
class ApiService {
|
|
private axiosInstance: AxiosInstance;
|
|
|
|
constructor() {
|
|
this.axiosInstance = axios.create({
|
|
baseURL: API_URL,
|
|
timeout: 30000, // 30 seconds timeout
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
|
|
// Request interceptor: Add Authorization header with JWT
|
|
this.axiosInstance.interceptors.request.use(
|
|
(config) => {
|
|
const token = getToken();
|
|
if (token) {
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
return config;
|
|
},
|
|
(error) => {
|
|
console.error('Request interceptor error:', error);
|
|
return Promise.reject(this.handleError(error));
|
|
}
|
|
);
|
|
|
|
// Response interceptor: Handle errors
|
|
this.axiosInstance.interceptors.response.use(
|
|
(response) => response,
|
|
(error: AxiosError) => {
|
|
if (error.response?.status === 401) {
|
|
// Clear tokens and redirect to login
|
|
console.warn('Unauthorized request, redirecting to login');
|
|
removeToken();
|
|
removeUser();
|
|
window.location.href = '/login';
|
|
}
|
|
return Promise.reject(this.handleError(error));
|
|
}
|
|
);
|
|
}
|
|
|
|
private handleError(error: AxiosError): ApiError {
|
|
if (error.response) {
|
|
// Server responded with error
|
|
const message = (error.response.data as any)?.message ||
|
|
(error.response.data as any)?.error ||
|
|
error.message ||
|
|
'Ein Fehler ist aufgetreten';
|
|
return {
|
|
message,
|
|
status: error.response.status,
|
|
code: error.code,
|
|
};
|
|
} else if (error.request) {
|
|
// Request was made but no response received
|
|
return {
|
|
message: 'Keine Antwort vom Server. Bitte überprüfen Sie Ihre Internetverbindung.',
|
|
code: error.code,
|
|
};
|
|
} else {
|
|
// Something else happened
|
|
return {
|
|
message: error.message || 'Ein unerwarteter Fehler ist aufgetreten',
|
|
code: error.code,
|
|
};
|
|
}
|
|
}
|
|
|
|
async get<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
|
return this.axiosInstance.get<T>(url, config);
|
|
}
|
|
|
|
async post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
|
return this.axiosInstance.post<T>(url, data, config);
|
|
}
|
|
|
|
async put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
|
return this.axiosInstance.put<T>(url, data, config);
|
|
}
|
|
|
|
async delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
|
return this.axiosInstance.delete<T>(url, config);
|
|
}
|
|
|
|
async patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
|
return this.axiosInstance.patch<T>(url, data, config);
|
|
}
|
|
}
|
|
|
|
export const api = new ApiService();
|