Skip to content

Commit

Permalink
refactor: userservice class
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Curtis committed Aug 16, 2024
1 parent a5cb540 commit e0831ce
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 32 deletions.
31 changes: 18 additions & 13 deletions src/hooks/useAuthHook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useLocalStorage } from "./useLocalStorage";
import { UserService } from "../services";
import { UserData, Response } from "../interfaces";
import { useNavigate } from "react-router-dom";
import { AxiosResponse } from "axios";

export const useAuthHook = () => {
const [user, setUser] = useLocalStorage("user", "");
Expand All @@ -11,7 +10,17 @@ export const useAuthHook = () => {

const api = new UserService(VITE_API_ENDPOINT);

const authenticate = async (): Promise<AxiosResponse | Response | void> => {
const getErrorResponse = (errorMessage: string) => {
return {
status: 500,
data: {
msg: errorMessage,
user: null,
},
};
};

const authenticate = async (): Promise<Response> => {
try {
const response = await api.authenticate();

Expand All @@ -21,13 +30,11 @@ export const useAuthHook = () => {

return response;
} catch (error: any) {
console.log(error.message);
return getErrorResponse(error.message);
}
};

const signIn = async (
userData: UserData
): Promise<AxiosResponse | Response | void> => {
const signIn = async (userData: UserData): Promise<Response> => {
try {
const response = await api.login(userData);

Expand All @@ -39,11 +46,11 @@ export const useAuthHook = () => {

return response;
} catch (error: any) {
console.log(error.message);
return getErrorResponse(error.message);
}
};

const signOut = async (): Promise<AxiosResponse | Response | void> => {
const signOut = async (): Promise<Response> => {
try {
const response = await api.logout();

Expand All @@ -52,13 +59,11 @@ export const useAuthHook = () => {

return response;
} catch (error: any) {
console.log(error.message);
return getErrorResponse(error.message);
}
};

const register = async (
userData: UserData
): Promise<AxiosResponse | Response | void> => {
const register = async (userData: UserData): Promise<Response> => {
try {
const response = await api.register(userData);

Expand All @@ -70,7 +75,7 @@ export const useAuthHook = () => {

return response;
} catch (error: any) {
console.log(error.message);
return getErrorResponse(error.message);
}
};

Expand Down
64 changes: 45 additions & 19 deletions src/services/UserService.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import axios from "axios";
import axios, { AxiosInstance } from "axios";
import { UserData, Response } from "../interfaces";

export class UserService {
#instance;
#instance: AxiosInstance;

constructor(baseUrl: string) {
this.#instance = axios.create({
baseURL: baseUrl,
});
}

getInstance() {
getInstance(): AxiosInstance {
return this.#instance;
}

generateErrorResponse(error: any): Response {
generateErrorResponse(errorMessage: string): Response {
return {
status: 500,
data: {
msg: error.message,
user: null
msg: errorMessage,
user: null,
},
};
}
Expand All @@ -30,10 +30,16 @@ export class UserService {
withCredentials: true,
validateStatus: (status) => status < 500,
});
return response;

} catch (error) {
return this.generateErrorResponse(error);

return {
status: response.status,
data: {
msg: response.data.msg,
user: response.data.user,
},
};
} catch (error: any) {
return this.generateErrorResponse(error.message);
}
}

Expand All @@ -47,9 +53,16 @@ export class UserService {
validateStatus: (status) => status < 500,
}
);
return response;
} catch (error) {
return this.generateErrorResponse(error);

return {
status: response.status,
data: {
msg: response.data.msg,
user: null,
},
};
} catch (error: any) {
return this.generateErrorResponse(error.message);
}
}

Expand All @@ -58,9 +71,16 @@ export class UserService {
const response = await this.#instance.post("/register", userData, {
validateStatus: (status) => status < 500,
});
return response;
} catch (error) {
return this.generateErrorResponse(error);

return {
status: response.status,
data: {
msg: response.data.msg,
user: response.data.user,
}
};
} catch (error: any) {
return this.generateErrorResponse(error.message);
}
}

Expand All @@ -71,9 +91,15 @@ export class UserService {
{},
{ withCredentials: true, validateStatus: (status) => status < 500 }
);
return response;
} catch (error) {
return this.generateErrorResponse(error);
return {
status: response.status,
data: {
msg: response.data.msg,
user: response.data.user,
}
};
} catch (error: any) {
return this.generateErrorResponse(error.message);
}
}
}

0 comments on commit e0831ce

Please sign in to comment.