import axios from 'axios';

import { backendBaseUrl } from '../config/env';
import { authStore } from '../state/auth/authStore';

export const apiClient = axios.create({
  baseURL: `${backendBaseUrl}/api`,
  timeout: 60000,
  maxRedirects: 5,
  validateStatus: status => status >= 200 && status < 600,
});

apiClient.interceptors.request.use(config => {
  const token = authStore.getToken();
  const username = authStore.getUsername();

  if (token) {
    config.headers = config.headers || {};
    config.headers['authcode'] = token;
  }
  if (username) {
    config.headers = config.headers || {};
    config.headers['userid'] = username;
  }

  return config;
});

