import axios from 'axios';

const server = axios.create({
    baseURL: 'http://localhost:5000/api', // fallback only
    headers: {
        "Access-Control-Allow-Origin": "*",
    }
});

export const initApi = (baseURL) => {
    server.defaults.baseURL = baseURL;
};

server.interceptors.request.use((config) => {
    const token = localStorage.getItem('token');
    if (token) {
        config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
});

export default () => server;

export const getBaseUrl = () => server.defaults.baseURL;