import axios, { AxiosRequestConfig } from 'axios'; import { auth } from 'src/lib/firebase'; // config import { HOST_API } from 'src/config-global'; // ---------------------------------------------------------------------- const axiosInstance = axios.create({ baseURL: HOST_API }); axiosInstance.interceptors.request.use( async (config) => { try { const currentUser = auth.currentUser; if (currentUser) { const token = await currentUser.getIdToken(); // eslint-disable-next-line no-param-reassign config.headers.Authorization = `Bearer ${token}`; } return config; } catch (error) { return Promise.reject(error); } }, (error) => Promise.reject(error) ); axiosInstance.interceptors.response.use( (res) => res, (error) => Promise.reject((error.response && error.response.data) || 'Something went wrong') ); export default axiosInstance; // ---------------------------------------------------------------------- export const fetcher = async (args: string | [string, AxiosRequestConfig]): Promise => { const [url, config] = Array.isArray(args) ? args : [args]; const res = await axiosInstance.get(url, { ...config }); return res.data; }; // ---------------------------------------------------------------------- export const endpoints = { invoice: '/api/invoices', customer: '/api/customers', tenant: '/api/tenant', service: '/api/services', employee: '/api/employees', };