import axios from "axios";
import cookie from "js-cookie";

const api = axios.create({
    baseURL: import.meta.env.VITE_SERVER_HOST,
});

api.interceptors.request.use((req) => {
    const session = cookie.get("session");
    if (session) {
        req.headers.Authorization = session;
    }
    return req;
});

api.interceptors.response.use(
    (res) => res,
    (err) => {
        return Promise.reject({
            ...err,
            message: err.response?.data?.message || err.message,
        });
    },
);

export default api;
