Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 31 additions & 27 deletions src/apis/services/httpMethod.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
import axiosInstance, { setAuthToken } from '@/lib/axiosInstance';
import { handleHttpError } from '@/utils/handleHttpError';

export async function GET<T>(url: string, token?: string): Promise<T> {
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';

interface RequestOptionsProps {
method: HttpMethod;
url: string;
data?: unknown;
token?: string;
}

async function request<T>({
method,
url,
data,
token,
}: RequestOptionsProps): Promise<T> {
try {
if (token) {
setAuthToken(token);
}
const response = await axiosInstance.get(url);
return response.data;
} catch (error) {
throw new Error(error instanceof Error ? error.message : String(error));
}
}

export async function POST<T, U>(url: string, data?: U): Promise<T> {
try {
const response = await axiosInstance.post(url, data);
return response.data;
} catch (error) {
throw new Error(error instanceof Error ? error.message : String(error));
}
}
const response = await axiosInstance.request<T>({
method,
url,
data,
});

export async function PUT<T, U>(url: string, data: U): Promise<T> {
try {
const response = await axiosInstance.put(url, data);
return response.data;
} catch (error) {
throw new Error(error instanceof Error ? error.message : String(error));
handleHttpError(error);
throw error;
}
}

export async function DELETE<T>(url: string): Promise<T> {
try {
const response = await axiosInstance.delete(url);
return response.data;
} catch (error) {
throw new Error(error instanceof Error ? error.message : String(error));
}
}
export const API = {
get: <T>(url: string, token?: string) =>
request<T>({ method: 'GET', url, token }),
post: <T, U>(url: string, data?: U) =>
request<T>({ method: 'POST', url, data }),
put: <T, U>(url: string, data: U) => request<T>({ method: 'PUT', url, data }),
delete: <T>(url: string) => request<T>({ method: 'DELETE', url }),
};
7 changes: 4 additions & 3 deletions src/hooks/apis/Auth/useChangePassword.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useMutation, UseMutationResult } from '@tanstack/react-query';
import { AxiosError, AxiosResponse } from 'axios';

import { API } from '@/apis/services/httpMethod';
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';
import { notify } from '@/store/useToastStore';
import { ChangePasswordRequest } from '@/types/Auth/ChangePasswordRequest';
import { PUT } from '@/apis/services/httpMethod';
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';

interface ErrorResponse {
message: string;
Expand All @@ -18,7 +19,7 @@ export const useChangePassword = (
> => {
return useMutation({
mutationFn: (data: ChangePasswordRequest) =>
PUT<AxiosResponse, ChangePasswordRequest>(
API.put<AxiosResponse, ChangePasswordRequest>(
API_ENDPOINTS.AUTH.PASSWORD,
data,
),
Expand Down
7 changes: 4 additions & 3 deletions src/hooks/apis/Auth/useFollowCountQuery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AxiosError } from 'axios';
import { useQuery, UseQueryOptions } from '@tanstack/react-query';
import { GET } from '@/apis/services/httpMethod';
import { AxiosError } from 'axios';

import { API } from '@/apis/services/httpMethod';
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';
import { QUERY_KEYS } from '@/constants/QueryKeys';

Expand All @@ -17,7 +18,7 @@ export interface FollowCountResponse {

const followCountOptions: UseQueryOptions<FollowCountResponse, AxiosError> = {
queryKey: [QUERY_KEYS.FOLLOW_COUNT],
queryFn: () => GET<FollowCountResponse>(API_ENDPOINTS.AUTH.FOLLOW_COUNT),
queryFn: () => API.get<FollowCountResponse>(API_ENDPOINTS.AUTH.FOLLOW_COUNT),
};

export const useFollowCountQuery = () => {
Expand Down
11 changes: 6 additions & 5 deletions src/hooks/apis/Auth/useModifyProfilePic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import {
useQueryClient,
} from '@tanstack/react-query';
import { AxiosError, AxiosResponse } from 'axios';
import { ModifyProfilePicRequest } from '@/types/Auth/ModifyProfilePicRequest';
import { notify } from '@/store/useToastStore';
import { QUERY_KEYS } from '@/constants/QueryKeys';
import { PUT } from '@/apis/services/httpMethod';

import { API } from '@/apis/services/httpMethod';
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';
import { QUERY_KEYS } from '@/constants/QueryKeys';
import { notify } from '@/store/useToastStore';
import { ModifyProfilePicRequest } from '@/types/Auth/ModifyProfilePicRequest';

export const useModifyProfilePic = (): UseMutationResult<
AxiosResponse,
Expand All @@ -19,7 +20,7 @@ export const useModifyProfilePic = (): UseMutationResult<

return useMutation({
mutationFn: (data: ModifyProfilePicRequest) =>
PUT<AxiosResponse, ModifyProfilePicRequest>(
API.put<AxiosResponse, ModifyProfilePicRequest>(
API_ENDPOINTS.AUTH.PROFILE_PIC,
data,
),
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/apis/Auth/useUserProfileQuery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useQuery, UseQueryOptions } from '@tanstack/react-query';
import { AxiosError } from 'axios';
import { GET } from '@/apis/services/httpMethod';

import { API } from '@/apis/services/httpMethod';
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';
import { QUERY_KEYS } from '@/constants/QueryKeys';
import { UserProfileResponse } from '@/types/response';
Expand All @@ -10,7 +11,7 @@ const userProfileOptions = (
): UseQueryOptions<UserProfileResponse, AxiosError> => ({
queryKey: [QUERY_KEYS.USER_PROFILE, userId],
queryFn: () =>
GET<UserProfileResponse>(API_ENDPOINTS.AUTH.USER_PROFILE(userId)),
API.get<UserProfileResponse>(API_ENDPOINTS.AUTH.USER_PROFILE(userId)),
});

export const useUserProfileQuery = (userId: number) => {
Expand Down
8 changes: 5 additions & 3 deletions src/hooks/apis/Auth/useWithdrawal.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useRouter } from 'next/navigation';
import { DELETE } from '@/apis/services/httpMethod';
import { WithdrawalResponse } from '@/types/response';

import { API } from '@/apis/services/httpMethod';
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';
import { notify } from '@/store/useToastStore';
import { WithdrawalResponse } from '@/types/response';

export const useWithdrawal = () => {
const queryClient = useQueryClient();
const router = useRouter();

return useMutation({
mutationFn: () => DELETE<WithdrawalResponse>(API_ENDPOINTS.AUTH.WITHDRAWAL),
mutationFn: () =>
API.delete<WithdrawalResponse>(API_ENDPOINTS.AUTH.WITHDRAWAL),
onSuccess: () => {
notify('success', '회원탈퇴에 성공하였습니다.', 3000);
document.cookie = 'token=; max-age=0; path=/;';
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/apis/Comment/useCreateCommentQuery.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';

import { POST } from '@/apis/services/httpMethod';
import { API } from '@/apis/services/httpMethod';
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';
import { TOAST_MESSAGES } from '@/constants/Messages';
import { QUERY_KEYS } from '@/constants/QueryKeys';
import { notify } from '@/store/useToastStore';
import { CommentResponse, GetCommentRequest } from '@/types/Comment';
import { TOAST_MESSAGES } from '@/constants/Messages';

export const useCreateComment = () => {
const queryClient = useQueryClient();

return useMutation({
mutationFn: (postData: GetCommentRequest) =>
POST<CommentResponse, GetCommentRequest>(
API.post<CommentResponse, GetCommentRequest>(
API_ENDPOINTS.COMMENT.CREATE,
postData,
),
Expand Down
9 changes: 5 additions & 4 deletions src/hooks/apis/Comment/useDeleteCommentQuery.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { notify } from '@/store/useToastStore';
import { DELETE } from '@/apis/services/httpMethod';

import { API } from '@/apis/services/httpMethod';
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';
import { QUERY_KEYS } from '@/constants/QueryKeys';
import { TOAST_MESSAGES } from '@/constants/Messages';
import { QUERY_KEYS } from '@/constants/QueryKeys';
import { notify } from '@/store/useToastStore';
import { CommentResponse } from '@/types/Comment';

export const useDeleteComment = () => {
const queryClient = useQueryClient();

return useMutation({
mutationFn: (commentId: number) =>
DELETE<CommentResponse>(API_ENDPOINTS.COMMENT.DELETE(commentId)),
API.delete<CommentResponse>(API_ENDPOINTS.COMMENT.DELETE(commentId)),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.COMPLETE_DETAIL] });
notify('success', TOAST_MESSAGES.COMMENT_DELETE_SUCCESS, 3000);
Expand Down
9 changes: 5 additions & 4 deletions src/hooks/apis/Comment/usePutCommentQuery.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { notify } from '@/store/useToastStore';
import { PUT } from '@/apis/services/httpMethod';

import { API } from '@/apis/services/httpMethod';
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';
import { CommentResponse, PutCommentRequest } from '@/types/Comment';
import { TOAST_MESSAGES } from '@/constants/Messages';
import { QUERY_KEYS } from '@/constants/QueryKeys';
import { notify } from '@/store/useToastStore';
import { CommentResponse, PutCommentRequest } from '@/types/Comment';

interface PutCommentVariables {
data: PutCommentRequest;
Expand All @@ -16,7 +17,7 @@ export const usePutComment = () => {

return useMutation<CommentResponse, Error, PutCommentVariables>({
mutationFn: ({ data, commentId }) =>
PUT<CommentResponse, PutCommentRequest>(
API.put<CommentResponse, PutCommentRequest>(
API_ENDPOINTS.COMMENT.PUT(commentId),
data,
),
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/apis/Complete/useGetCompleteDetailQuery.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useQuery, UseQueryOptions } from '@tanstack/react-query';
import { AxiosError } from 'axios';

import { GET } from '@/apis/services/httpMethod';
import { API } from '@/apis/services/httpMethod';
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';
import { QUERY_KEYS } from '@/constants/QueryKeys';
import { GetCompleteDetailResponse } from '@/types/Completes';
Expand All @@ -11,7 +11,7 @@ const GetCompleteDetailOptions = (
): UseQueryOptions<GetCompleteDetailResponse, AxiosError> => ({
queryKey: [QUERY_KEYS.COMPLETE_DETAIL, completeId],
queryFn: () =>
GET<GetCompleteDetailResponse>(
API.get<GetCompleteDetailResponse>(
API_ENDPOINTS.TODOS.GET_CERTIFIED_TODO(completeId),
),
});
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/apis/Dashboard/useRecentTodosQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '@tanstack/react-query';
import { AxiosError } from 'axios';

import { GET } from '@/apis/services/httpMethod';
import { API } from '@/apis/services/httpMethod';
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';
import { QUERY_KEYS } from '@/constants/QueryKeys';
import { RecentTodosResponse } from '@/types/Dashboard';
Expand All @@ -14,7 +14,7 @@ export const recentTodosOptions = (
): UseSuspenseQueryOptions<RecentTodosResponse, AxiosError> => ({
queryKey: [QUERY_KEYS.RECENT_TODOS],
queryFn: () =>
GET<RecentTodosResponse>(
API.get<RecentTodosResponse>(
`${API_ENDPOINTS.TODOS.GET_ALL}?lastTodoId=0&size=3`,
token,
),
Expand Down
7 changes: 5 additions & 2 deletions src/hooks/apis/Dashboard/useTodayProgressQuery.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useQuery, UseQueryOptions } from '@tanstack/react-query';
import { AxiosError } from 'axios';

import { GET } from '@/apis/services/httpMethod';
import { API } from '@/apis/services/httpMethod';
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';
import { QUERY_KEYS } from '@/constants/QueryKeys';
import { TodayProgressResponse } from '@/types/Dashboard';
Expand All @@ -11,7 +11,10 @@ export const todayProgressOptions = (
): UseQueryOptions<TodayProgressResponse, AxiosError> => ({
queryKey: [QUERY_KEYS.TODAY_PROGRESS],
queryFn: () =>
GET<TodayProgressResponse>(API_ENDPOINTS.TODOS.GET_TODAY_PROGRESS, token),
API.get<TodayProgressResponse>(
API_ENDPOINTS.TODOS.GET_TODAY_PROGRESS,
token,
),
});

export const useTodayProgressQuery = () => {
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/apis/Dashboard/useTodosOfGoalsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '@tanstack/react-query';
import { AxiosError } from 'axios';

import { GET } from '@/apis/services/httpMethod';
import { API } from '@/apis/services/httpMethod';
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';
import { QUERY_KEYS } from '@/constants/QueryKeys';
import { TodosOfGoalsResponse } from '@/types/Dashboard';
Expand All @@ -17,7 +17,7 @@ export const todosOfGoalsOptions = (): UseInfiniteQueryOptions<
> => ({
queryKey: [QUERY_KEYS.TODOS_OF_GOALS],
queryFn: ({ pageParam = 0 }) =>
GET<TodosOfGoalsResponse>(
API.get<TodosOfGoalsResponse>(
`${API_ENDPOINTS.TODOS.GET_GOALS}?lastGoalId=${pageParam}&size=5`,
),
getNextPageParam: (lastPage) => {
Expand Down
9 changes: 5 additions & 4 deletions src/hooks/apis/Follow/useAssignFollowMutation.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { POST } from '@/apis/services/httpMethod';

import { API } from '@/apis/services/httpMethod';
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';
import { AssignFollowResponse, UserProfileResponse } from '@/types/response';
import { notify } from '@/store/useToastStore';
import { QUERY_KEYS } from '@/constants/QueryKeys';
import { notify } from '@/store/useToastStore';
import { AssignFollowResponse, UserProfileResponse } from '@/types/response';

interface FollowId {
userId: number;
Expand All @@ -14,7 +15,7 @@ export const useAssignFollowMutation = () => {

return useMutation({
mutationFn: (userId: number) =>
POST<AssignFollowResponse, FollowId>(
API.post<AssignFollowResponse, FollowId>(
API_ENDPOINTS.FOLLOW.ASSIGN_FOLLOW(userId),
),
onMutate: async (userId) => {
Expand Down
11 changes: 7 additions & 4 deletions src/hooks/apis/Follow/useDeleteFollowMutation.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { DELETE } from '@/apis/services/httpMethod';

import { API } from '@/apis/services/httpMethod';
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';
import { DeleteFollowResponse, UserProfileResponse } from '@/types/response';
import { notify } from '@/store/useToastStore';
import { QUERY_KEYS } from '@/constants/QueryKeys';
import { notify } from '@/store/useToastStore';
import { DeleteFollowResponse, UserProfileResponse } from '@/types/response';

export const useDeleteFollowMutation = () => {
const queryClient = useQueryClient();

return useMutation({
mutationFn: (userId: number) =>
DELETE<DeleteFollowResponse>(API_ENDPOINTS.FOLLOW.DELETE_FOLLOW(userId)),
API.delete<DeleteFollowResponse>(
API_ENDPOINTS.FOLLOW.DELETE_FOLLOW(userId),
),
onMutate: async (userId) => {
const previousData = queryClient.getQueriesData<UserProfileResponse>({
queryKey: [QUERY_KEYS.USER_PROFILE, userId],
Expand Down
13 changes: 8 additions & 5 deletions src/hooks/apis/Follows/useDeleteFollowsQuery.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { notify } from '@/store/useToastStore';
import { DeleteFollowsResponse } from '@/types/Follows';
import { DELETE } from '@/apis/services/httpMethod';

import { API } from '@/apis/services/httpMethod';
import { API_ENDPOINTS } from '@/constants/ApiEndpoints';
import { QUERY_KEYS } from '@/constants/QueryKeys';
import { TOAST_MESSAGES } from '@/constants/Messages';
import { QUERY_KEYS } from '@/constants/QueryKeys';
import { notify } from '@/store/useToastStore';
import { DeleteFollowsResponse } from '@/types/Follows';

export const useDeleteFollow = () => {
const queryClient = useQueryClient();

return useMutation({
mutationFn: (followerId: number) =>
DELETE<DeleteFollowsResponse>(API_ENDPOINTS.FOLLOW.DELETE(followerId)),
API.delete<DeleteFollowsResponse>(
API_ENDPOINTS.FOLLOW.DELETE(followerId),
),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.FOLLOW_COUNT] });
notify('success', TOAST_MESSAGES.DELETE_FOLLOW_SUCCESS, 3000);
Expand Down
Loading
Loading