Skip to content

Commit

Permalink
fix: private, public axios 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
wildcatco committed Aug 21, 2023
1 parent c7058a9 commit ab1c2b2
Show file tree
Hide file tree
Showing 20 changed files with 51 additions and 43 deletions.
8 changes: 6 additions & 2 deletions src/common/libs/axios.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import axios from 'axios';
import { getBearerToken } from '@/common/utils/localStorageManager';

export const axiosInstance = axios.create({
export const axiosPublic = axios.create({
baseURL: import.meta.env.VITE_SERVER_API,
});

axiosInstance.interceptors.request.use((config) => {
export const axiosPrivate = axios.create({
baseURL: import.meta.env.VITE_SERVER_API,
});

axiosPrivate.interceptors.request.use((config) => {
const token = getBearerToken();
if (token) {
config.headers.Authorization = `Bearer ${token}`;
Expand Down
4 changes: 2 additions & 2 deletions src/features/auth/queries/useAcceptTerms.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useMutation } from '@tanstack/react-query';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';
import { AcceptTermsRequest, AcceptTermsResponse } from './dto/accept-terms';

async function acceptTerms(data: AcceptTermsRequest) {
const res = await axiosInstance.patch<AcceptTermsResponse>(
const res = await axiosPrivate.patch<AcceptTermsResponse>(
'/user/acceptance-of-terms',
data,
);
Expand Down
4 changes: 2 additions & 2 deletions src/features/comments/queries/useAddComment.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';
import { GetCommentsResponse } from '@/features/comments/queries/dto/get-comments';
import { useUser } from '@/features/user/queries';
import { AddCommentRequest, AddCommentResponse } from './dto/add-comment';

async function addComment(data: AddCommentRequest) {
const res = await axiosInstance.post<AddCommentResponse>('/worry-reply', {
const res = await axiosPrivate.post<AddCommentResponse>('/worry-reply', {
...data,
mentionUserNickname: undefined,
});
Expand Down
4 changes: 2 additions & 2 deletions src/features/comments/queries/useDeleteComment.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';
import { DeleteCommentResponse } from './dto/delete-comment';

async function deleteComment(id: number) {
const res = await axiosInstance.patch<DeleteCommentResponse>(
const res = await axiosPrivate.patch<DeleteCommentResponse>(
`/worry-reply/${id}/soft-delete`,
);
return res.data;
Expand Down
4 changes: 2 additions & 2 deletions src/features/comments/queries/useGetComments.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useQuery } from '@tanstack/react-query';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';
import { GetCommentsResponse } from './dto/get-comments';

async function getComments(postId: number) {
const res = await axiosInstance.get<GetCommentsResponse>('/worry-replies', {
const res = await axiosPrivate.get<GetCommentsResponse>('/worry-replies', {
params: {
worryId: postId,
},
Expand Down
4 changes: 2 additions & 2 deletions src/features/posts/queries/useAddPost.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';
import { AddPostRequest, AddPostResponse } from './dto/add-post';

async function addPost(data: AddPostRequest) {
const res = await axiosInstance.post<AddPostResponse>('/worry', data);
const res = await axiosPrivate.post<AddPostResponse>('/worry', data);
return res.data;
}

Expand Down
4 changes: 2 additions & 2 deletions src/features/posts/queries/useDeletePost.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';
import { DeletePostResponse } from './dto/delete-post';

async function deletePost(id: number) {
const res = await axiosInstance.patch<DeletePostResponse>(
const res = await axiosPrivate.patch<DeletePostResponse>(
`/worry/${id}/soft-delete`,
);
return res.data;
Expand Down
4 changes: 2 additions & 2 deletions src/features/posts/queries/useEditPost.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';
import { EditPostRequest, EditPostResponse } from './dto/edit-post';

async function editPost(data: EditPostRequest) {
const res = await axiosInstance.patch<EditPostResponse>(
const res = await axiosPrivate.patch<EditPostResponse>(
`/worries/${data.id}`,
data,
);
Expand Down
4 changes: 2 additions & 2 deletions src/features/posts/queries/useGetPost.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useQuery } from '@tanstack/react-query';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';
import { GetPostResponse } from './dto/get-post';

async function getPost(postId?: number) {
const res = await axiosInstance.get<GetPostResponse>(`/worries/${postId}`);
const res = await axiosPrivate.get<GetPostResponse>(`/worries/${postId}`);
return res.data;
}

Expand Down
4 changes: 3 additions & 1 deletion src/features/posts/queries/useGetPosts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useInfiniteQuery } from '@tanstack/react-query';
import { useEffect } from 'react';
import { useInView } from 'react-intersection-observer';
import { POST_TYPE } from '@/common/constants/post-type';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate, axiosPublic } from '@/common/libs/axios';
import { Post } from '@/features/posts/types/post';
import { GetPostsResponse } from './dto/get-posts';

Expand All @@ -17,6 +17,8 @@ async function getPosts({
participatingUserId,
pageParam,
}: getPostsArgs) {
const axiosInstance =
authorId || participatingUserId ? axiosPrivate : axiosPublic;
const res = await axiosInstance.get<GetPostsResponse>('/worries', {
params: {
sort: 'DESC',
Expand Down
4 changes: 2 additions & 2 deletions src/features/report/queries/useReportComment.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useMutation } from '@tanstack/react-query';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';
import {
ReportCommentRequest,
ReportCommentResponse,
} from './dto/report-comment';

async function reportComment(data: ReportCommentRequest) {
const res = await axiosInstance.post<ReportCommentResponse>(
const res = await axiosPrivate.post<ReportCommentResponse>(
'/worry-reply-report',
data,
);
Expand Down
4 changes: 2 additions & 2 deletions src/features/report/queries/useReportPost.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useMutation } from '@tanstack/react-query';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';
import { ReportPostRequest, ReportPostResponse } from './dto/report-post';

async function reportPost(data: ReportPostRequest) {
const res = await axiosInstance.post<ReportPostResponse>(
const res = await axiosPrivate.post<ReportPostResponse>(
'/worry-report',
data,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AxiosError } from 'axios';
import { useCallback, useEffect, useState } from 'react';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';
import { parseDateString } from '@/common/utils/date/parseDateString';
import { validateBirthDate } from '@/common/utils/validations/birth-date';
import { validateNickname } from '@/common/utils/validations/nickname';
Expand Down Expand Up @@ -53,7 +53,7 @@ export function NicknameAgeGenderForm({
let error = validateNickname(nickname);
if (!error) {
try {
await axiosInstance.get(`/user/${user?.id}/validate-nickname`, {
await axiosPrivate.get(`/user/${user?.id}/validate-nickname`, {
params: { nickname },
});
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions src/features/user/queries/useEditProfile.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';
import { EditProfileRequest, EditProfileResponse } from './dto/edit-profile';

async function editProfile(data: EditProfileRequest) {
const res = await axiosInstance.patch<EditProfileResponse>('/user', data);
const res = await axiosPrivate.patch<EditProfileResponse>('/user', data);
return res.data;
}

Expand Down
4 changes: 2 additions & 2 deletions src/features/user/queries/useUnregister.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useMutation } from '@tanstack/react-query';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';

async function unregister(data: { userId: number; reason: string }) {
const res = await axiosInstance.patch(`/user/${data.userId}/soft-delete`, {
const res = await axiosPrivate.patch(`/user/${data.userId}/soft-delete`, {
reason: data.reason,
});
return res.data;
Expand Down
14 changes: 8 additions & 6 deletions src/features/user/queries/useUser.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import { useQuery } from '@tanstack/react-query';
import { useQuery, useQueryClient } from '@tanstack/react-query';

Check failure on line 1 in src/features/user/queries/useUser.tsx

View workflow job for this annotation

GitHub Actions / ci-cd

'useQueryClient' is defined but never used
import { useEffect, useMemo } from 'react';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';
import { User } from '@/features/user/types';
import { userLocalStorage } from '@/features/user/utils/user-local-storage';
import { GetUserResponse } from './dto/get-user';

async function getUser() {
const res = await axiosInstance.get<GetUserResponse>('/user');
const res = await axiosPrivate.get<GetUserResponse>('/user');
return res.data;
}

export function useUser() {
const { data, isLoading, isError } = useQuery({
// @ts-ignore

Check failure on line 15 in src/features/user/queries/useUser.tsx

View workflow job for this annotation

GitHub Actions / ci-cd

Do not use "@ts-ignore" because it alters compilation errors
queryKey: ['user'],
queryFn: getUser,
refetchOnMount: false,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
initialData: userLocalStorage.getUser(),
retry: false,
});

const user: User | null = useMemo(
() =>
data
data && !isError
? {
id: data.id,
joinStatus: data.joinStatus,
Expand All @@ -46,7 +47,7 @@ export function useUser() {
image: data.profileImageUrl,
}
: null,
[data],
[data, isError],
);

useEffect(() => {
Expand All @@ -60,6 +61,7 @@ export function useUser() {
useEffect(() => {
if (isError) {
userLocalStorage.removeUser();
localStorage.removeItem('token');
}
}, [isError]);

Expand Down
4 changes: 2 additions & 2 deletions src/features/vote/queries/useChooseOption.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';
import { ChooseOptionRequest, ChooseOptionResponse } from './dto/choose-option';

async function chooseOption(data: ChooseOptionRequest) {
const res = await axiosInstance.post<ChooseOptionResponse>(
const res = await axiosPrivate.post<ChooseOptionResponse>(
'/user-worry-choice',
{
userId: data.userId,
Expand Down
4 changes: 2 additions & 2 deletions src/features/vote/queries/useGetChoiceOptions.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useSuspenseQuery } from '@suspensive/react-query';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';
import { GetChoiceOptionsResponse } from './dto/get-choice-options';

async function getChoiceOptions(postId: number) {
const res = await axiosInstance.get<GetChoiceOptionsResponse>(
const res = await axiosPrivate.get<GetChoiceOptionsResponse>(
'/worry-choices',
{
params: {
Expand Down
4 changes: 2 additions & 2 deletions src/features/vote/queries/useGetMyChoice.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useQuery } from '@tanstack/react-query';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';
import { GetMyChoiceResponse } from './dto/get-my-choice';

async function getMyChoice(postId?: number, userId?: number) {
const res = await axiosInstance.get<GetMyChoiceResponse>(
const res = await axiosPrivate.get<GetMyChoiceResponse>(
'/user-worry-choice',
{
params: {
Expand Down
4 changes: 2 additions & 2 deletions src/features/vote/queries/useGetUsersChoices.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useSuspenseQuery } from '@suspensive/react-query';
import { axiosInstance } from '@/common/libs/axios';
import { axiosPrivate } from '@/common/libs/axios';
import { GetUsersChoices } from './dto/get-users-choices';

async function getUserChoices(postId?: number) {
const res = await axiosInstance.get<GetUsersChoices>('/user-worry-choice', {
const res = await axiosPrivate.get<GetUsersChoices>('/user-worry-choice', {
params: {
worryId: postId,
},
Expand Down

0 comments on commit ab1c2b2

Please sign in to comment.