import axios, { AxiosRequestConfig } from 'axios'; import {config} from "@/config"; const baseURL = config.BUSINESS_URL + config.BUSINESS_PREFIX; interface Request { endpoint: string; query?: Record; config?: Record; } const makeQuery = (reqQuery: Record) => { let result = ''; result = '?' + Object.entries(reqQuery) .map(([ key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`) .join('&') return result; }; export const useHttp = () => { const api = axios.create({ baseURL, withCredentials: true, }); const post = async (endpoint: string, payload?: Record): Promise => ( await api.post(endpoint, payload) ) const get = async (req: Request) => { if (req.query) { req.endpoint += makeQuery(req.query); } const res = await api.get(req.endpoint, req.config); return res; }; return { get, post } }