import axios, {AxiosResponse, InternalAxiosRequestConfig} from "axios";
const instance = axios.create({
    baseURL: import.meta.env.VITE_URL === 'AUTO' ? window.location.protocol + '//' + window.location.hostname + ":" + window.location.port : import.meta.env.VITE_URL
});
const responseInterceptors = (response: AxiosResponse<any, any>): AxiosResponse<any, any> => {
    if (response.headers){
        if (response.headers['token']){
            window.localStorage.setItem("token", response.headers['token']);
        }
    }
    return response;
}
instance.interceptors.response.use(responseInterceptors)
const requestInterceptors = (request: InternalAxiosRequestConfig<any>): InternalAxiosRequestConfig<any> => {
    let token = window.localStorage.getItem("token");
    request.headers.set("Access-Token",token, false);
    return request;
}
instance.interceptors.request.use(requestInterceptors);

export default instance;