| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- export enum ApiMethods {
- GET = "GET",
- POST = "POST",
- PUT = "PUT",
- }
- export enum ApiPaths {
- SignUp = "signup",
- Login = "login",
- Settings = "user",
- }
- type FetchApiProps<T> = {
- method: ApiMethods;
- path: ApiPaths;
- query?: string;
- body: T;
- };
- type FetchApiResult = { error?: string; message?: string };
- const fetchApi = async <T>({
- method,
- path,
- query,
- body,
- }: FetchApiProps<T>): Promise<FetchApiResult> => {
- 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;
|