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