export enum ApiMethods { GET = "GET", POST = "POST", PUT = "PUT", } export enum ApiPaths { SignUp = "signup", Login = "login", Settings = "user", } type FetchApiProps = { method: ApiMethods; path: ApiPaths; query?: string; body: T; }; type FetchApiResult = { error?: string; message?: string }; const fetchApi = async ({ method, path, query, body, }: FetchApiProps): Promise => { const response = await fetch(`/api/${path}/${query || ""}`, { method: method, body: JSON.stringify(body), }); const { message } = await response.json(); if (response.ok) { return { message }; } else { return { error: message.replace("Incorect", "Incorrect"), }; } }; export default fetchApi;