51 lines
1 KiB
TypeScript
51 lines
1 KiB
TypeScript
|
|
import axios, { AxiosRequestConfig } from 'axios';
|
|
import {config} from "@/config";
|
|
|
|
const baseURL = config.BUSINESS_URL + config.BUSINESS_PREFIX;
|
|
|
|
interface Request {
|
|
endpoint: string;
|
|
query?: Record<string, any>;
|
|
config?: Record<string, any>;
|
|
}
|
|
|
|
const makeQuery = (reqQuery: Record<string, any>) => {
|
|
|
|
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<string, any>): Promise<any> => (
|
|
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
|
|
}
|
|
}
|