import axios from "axios";
import store from "@/store";

const httpPrefix = "http";

const instance = axios.create({
  baseURL: "/api"
});

instance.interceptors.request.use(
  async (config) => {
    if (config.url.startsWith(httpPrefix)) {
      config.baseURL = "";
    }

    if (store.state.user.token) {
      config.headers.authorization = store.state.user.token;
    } else {
      delete config.headers.authorization;
    }
    return config;
  },
  (error) => {
    console.error(error);
  }
);

export {
  instance as axios
};
