erp-platform/company/src/services/api/config.ts
2025-06-20 17:55:55 +03:00

47 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import axios from 'axios';
// API Base URL
export const API_BASE_URL = import.meta.env.VITE_API_URL;
// Axios instance
export const apiClient = axios.create({
baseURL: API_BASE_URL,
headers: {
'Content-Type': 'application/json',
},
});
// Request interceptor - Add auth token and tenant header
apiClient.interceptors.request.use(
(config) => {
const token = localStorage.getItem('access_token');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
// UI uygulaması gibi tenant header'ı ekle
const tenantId = localStorage.getItem('tenant_id');
if (tenantId) {
config.headers['__tenant'] = tenantId;
}
return config;
},
(error) => {
return Promise.reject(error);
}
);
// Response interceptor - Handle errors
apiClient.interceptors.response.use(
(response) => response,
async (error) => {
if (error.response?.status === 401) {
// Token expired or invalid
localStorage.removeItem('access_token');
localStorage.removeItem('current_user');
window.location.href = '/login';
}
return Promise.reject(error);
}
);