export const BASE_URL = ""; export const baseFetch = async ( url: string, method: string = "POST", options: any, ) => { let isSuccess = false; let error = ""; let data = null; try { const response = await fetch(BASE_URL + url, { method: method, headers: { "Content-Type": "application/json", Accept: "application/json", ...options.headers, }, body: options.body, }); if (response.ok) { isSuccess = true; } data = await response.json(); if (response.status >= 400 && response.status < 600) { isSuccess = false; throw new Error(data); } return { data, isSuccess, error }; } catch (err: any) { error = err.message; return { error, isSuccess, data }; } }; export const getToken = async ( url: string, data: { [key: string]: any }, ) => { return await baseFetch( url, "POST", { body: JSON.stringify(data), } ); }; export const post = async ( url: string, data: { [key: string]: any }, token: string ) => { return await baseFetch(url, "POST", { body: JSON.stringify(data), headers: { Authorization: "Bearer " + token, }, }); }; export const postWithoutToken = async ( url: string, data: { [key: string]: any } ) => { return await baseFetch(url, "POST", { body: JSON.stringify(data), }); }; export const get = async (url: string, token: string) => { return await baseFetch(url, "GET", { headers: { Authorization: "Bearer " + token, }, }); }; export const remove = async (url: string, token: string) => { return await baseFetch(url, "GET", { headers: { Authorization: "Bearer " + token, }, }); };