canina/frontend/application/lib/services/api.ts

29 lines
748 B
TypeScript

import axios from 'axios';
const baseURL = typeof window !== 'undefined' ? '/api' : (process.env.NEXT_PUBLIC_API_URL || (process.env.NODE_ENV === 'development' ? 'http://localhost:4001/api' : 'https://apicanina.parsaaghayi.ir/api'));
const api = axios.create({
baseURL,
timeout: 10000,
headers: {
'Content-Type': 'application/json',
},
});
// Interceptor to add auth token in the future
api.interceptors.request.use(
(config) => {
let token = null;
if (typeof window !== 'undefined') {
token = localStorage.getItem('accessToken');
}
if (token && config.headers) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
},
(error) => Promise.reject(error)
);
export default api;