|
@@ -1,44 +1,104 @@
|
|
|
|
|
+import assert from "assert";
|
|
|
|
|
+const API = process.env.NEXT_PUBLIC_API || "/api";
|
|
|
|
|
+
|
|
|
export enum ApiMethods {
|
|
export enum ApiMethods {
|
|
|
GET = "GET",
|
|
GET = "GET",
|
|
|
POST = "POST",
|
|
POST = "POST",
|
|
|
PUT = "PUT",
|
|
PUT = "PUT",
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-export enum ApiPaths {
|
|
|
|
|
- SignUp = "signup",
|
|
|
|
|
- Login = "login",
|
|
|
|
|
- Settings = "user",
|
|
|
|
|
|
|
+export enum ApiResulState {
|
|
|
|
|
+ SUCCESS,
|
|
|
|
|
+ ERROR,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-type FetchApiProps<T> = {
|
|
|
|
|
- method: ApiMethods;
|
|
|
|
|
- path: ApiPaths;
|
|
|
|
|
- query?: string;
|
|
|
|
|
- body: T;
|
|
|
|
|
|
|
+type SuccessApiResult = {
|
|
|
|
|
+ state: ApiResulState.SUCCESS;
|
|
|
|
|
+ message: string;
|
|
|
|
|
+ user: User;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-type FetchApiResult = { error?: string; message?: string };
|
|
|
|
|
|
|
+type FailureApiResult = { state: ApiResulState.ERROR; error: string };
|
|
|
|
|
+
|
|
|
|
|
+export type FetchApiResult = SuccessApiResult | FailureApiResult;
|
|
|
|
|
+
|
|
|
|
|
+type FetchApi = <T>(
|
|
|
|
|
+ path: string,
|
|
|
|
|
+ method: ApiMethods,
|
|
|
|
|
+ body: T
|
|
|
|
|
+) => Promise<FetchApiResult>;
|
|
|
|
|
|
|
|
-const fetchApi = async <T>({
|
|
|
|
|
- method,
|
|
|
|
|
- path,
|
|
|
|
|
- query,
|
|
|
|
|
- body,
|
|
|
|
|
-}: FetchApiProps<T>): Promise<FetchApiResult> => {
|
|
|
|
|
- const response = await fetch(`/api/${path}/${query || ""}`, {
|
|
|
|
|
|
|
+const fetchApi: FetchApi = async (path, method, body) => {
|
|
|
|
|
+ const response = await fetch(`${API}${path}`, {
|
|
|
method: method,
|
|
method: method,
|
|
|
body: JSON.stringify(body),
|
|
body: JSON.stringify(body),
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- const { message } = await response.json();
|
|
|
|
|
|
|
+ const { message, ...result } = await response.json();
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
if (response.ok) {
|
|
|
- return { message };
|
|
|
|
|
|
|
+ return { state: ApiResulState.SUCCESS, message, user: result };
|
|
|
} else {
|
|
} else {
|
|
|
return {
|
|
return {
|
|
|
|
|
+ state: ApiResulState.ERROR,
|
|
|
error: message.replace("Incorect", "Incorrect"),
|
|
error: message.replace("Incorect", "Incorrect"),
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-export default fetchApi;
|
|
|
|
|
|
|
+export interface FetchApiInterface<T> {
|
|
|
|
|
+ (body: T): Promise<FetchApiResult>;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export const login: FetchApiInterface<
|
|
|
|
|
+ Pick<User, "email" | "password">
|
|
|
|
|
+> = async (body) => {
|
|
|
|
|
+ const response = await fetchApi("/login", ApiMethods.POST, body);
|
|
|
|
|
+ switch (response.state) {
|
|
|
|
|
+ case ApiResulState.SUCCESS: {
|
|
|
|
|
+ const user = await fetchUserData(body.email);
|
|
|
|
|
+ return {
|
|
|
|
|
+ ...response,
|
|
|
|
|
+ user: user,
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+ default:
|
|
|
|
|
+ return response;
|
|
|
|
|
+ }
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+export const register: FetchApiInterface<
|
|
|
|
|
+ Pick<User, "email" | "password">
|
|
|
|
|
+> = async (body) => {
|
|
|
|
|
+ return fetchApi("/register", ApiMethods.POST, body);
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+export const updateUser: FetchApiInterface<
|
|
|
|
|
+ Partial<User> & Pick<User, "id">
|
|
|
|
|
+> = async ({ id, ...body }) => {
|
|
|
|
|
+ return fetchApi(`/user/${JSON.stringify(id)}`, ApiMethods.PUT, body);
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+const fetchUsers = async (): Promise<Array<User>> => {
|
|
|
|
|
+ const response = await fetch("/api/users");
|
|
|
|
|
+ if (response.ok) {
|
|
|
|
|
+ return response.json();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return Promise.resolve([]);
|
|
|
|
|
+ }
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+const fetchUserData = async (email: string): Promise<User> => {
|
|
|
|
|
+ const users = await fetchUsers();
|
|
|
|
|
+ const user = users.find((user) => user.email === email);
|
|
|
|
|
+ assert(user);
|
|
|
|
|
+ return user;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+const apiPaths = {
|
|
|
|
|
+ login,
|
|
|
|
|
+ register,
|
|
|
|
|
+ updateUser,
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+export default apiPaths;
|