Skip to content

Commit

Permalink
Merge pull request #1404 from woowacourse/refactor/1403-axios
Browse files Browse the repository at this point in the history
Refactor/#1403 api axios 사용하는 파일 리팩터링
  • Loading branch information
hae-on authored Jul 14, 2023
2 parents 674b324 + 7e949e9 commit 9dd8c05
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 35 deletions.
10 changes: 5 additions & 5 deletions frontend/src/apis/comment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createAxiosInstance } from '../utils/axiosInstance';
import { CommentListResponse, CommentRequest } from '../models/Comment';

const instanceWithoutToken = createAxiosInstance();
const customAxios = createAxiosInstance();

export const getComments = async (studylogId: number): Promise<CommentListResponse> => {
const response = await instanceWithoutToken.get(`/studylogs/${studylogId}/comments`);
const response = await customAxios.get(`/studylogs/${studylogId}/comments`);

return response.data;
};
Expand All @@ -15,7 +15,7 @@ export const createCommentRequest = ({
}: {
studylogId: number;
body: CommentRequest;
}) => instanceWithoutToken.post(`/studylogs/${studylogId}/comments`, body);
}) => customAxios.post(`/studylogs/${studylogId}/comments`, body);

export const editComment = ({
studylogId,
Expand All @@ -25,12 +25,12 @@ export const editComment = ({
studylogId: number;
commentId: number;
body: CommentRequest;
}) => instanceWithoutToken.put(`/studylogs/${studylogId}/comments/${commentId}`, body);
}) => customAxios.put(`/studylogs/${studylogId}/comments/${commentId}`, body);

export const deleteComment = ({
studylogId,
commentId,
}: {
studylogId: number;
commentId: number;
}) => instanceWithoutToken.delete(`/studylogs/${studylogId}/comments/${commentId}`);
}) => customAxios.delete(`/studylogs/${studylogId}/comments/${commentId}`);
4 changes: 2 additions & 2 deletions frontend/src/apis/curriculum.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createAxiosInstance } from '../utils/axiosInstance';
import { CurriculumListResponse } from '../models/Keywords';

const instanceWithoutToken = createAxiosInstance();
const customAxios = createAxiosInstance();

export const getCurriculums = async () => {
const response = await instanceWithoutToken.get<CurriculumListResponse>(`/curriculums`);
const response = await customAxios.get<CurriculumListResponse>(`/curriculums`);

return response.data;
};
12 changes: 5 additions & 7 deletions frontend/src/apis/keywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,32 @@ import {
QuizListByKeywordRequest,
} from '../models/Keywords';

const instanceWithoutToken = createAxiosInstance();
const customAxios = createAxiosInstance();

export const getKeyword = async ({ sessionId, keywordId }: KeywordRequest) => {
const response = await instanceWithoutToken.get<KeywordResponse>(
const response = await customAxios.get<KeywordResponse>(
`/sessions/${sessionId}/keywords/${keywordId}`
);

return response.data;
};

export const getTopKeywordList = async (sessionId: number) => {
const response = await instanceWithoutToken.get<TopKeywordListResponse>(
`/sessions/${sessionId}/keywords`
);
const response = await customAxios.get<TopKeywordListResponse>(`/sessions/${sessionId}/keywords`);

return response.data;
};

export const getChildKeywordList = async ({ sessionId, keywordId }: ChildKeywordListRequest) => {
const response = await instanceWithoutToken.get<SubKeywordListResponse>(
const response = await customAxios.get<SubKeywordListResponse>(
`/sessions/${sessionId}/keywords/${keywordId}/children`
);

return response.data;
};

export const getQuizListByKeyword = async ({ sessionId, keywordId }: QuizListByKeywordRequest) => {
const response = await instanceWithoutToken.get<QuizListResponse>(
const response = await customAxios.get<QuizListResponse>(
`/sessions/${sessionId}/keywords/${keywordId}/quizs`
);

Expand Down
12 changes: 6 additions & 6 deletions frontend/src/apis/levellogs.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { createAxiosInstance } from '../utils/axiosInstance';
import { LevellogRequest } from '../models/Levellogs';

const instanceWithoutToken = createAxiosInstance();
const customAxios = createAxiosInstance();

export const requestGetLevellogs = async (currPage: number) => {
const params = currPage !== 1 ? `?page=${currPage}` : '';

const { data } = await instanceWithoutToken.get(`/levellogs${params}`);
const { data } = await customAxios.get(`/levellogs${params}`);

return data;
};

export const createNewLevellogRequest = (body: LevellogRequest) =>
instanceWithoutToken.post(`/levellogs`, body);
customAxios.post(`/levellogs`, body);

export const requestGetLevellog = async (id) => {
const { data } = await instanceWithoutToken.get(`/levellogs/${id}`);
const { data } = await customAxios.get(`/levellogs/${id}`);

return data;
};

export const requestDeleteLevellog = async (id) => {
instanceWithoutToken.delete(`/levellogs/${id}`);
customAxios.delete(`/levellogs/${id}`);
};

export const requestEditLevellog = (id, body: LevellogRequest) =>
instanceWithoutToken.put(`/levellogs/${id}`, body);
customAxios.put(`/levellogs/${id}`, body);
27 changes: 12 additions & 15 deletions frontend/src/apis/studylogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import {
} from '../models/Studylogs';
import { createAxiosInstance } from '../utils/axiosInstance';

const instanceWithoutToken = createAxiosInstance();
const instanceWithToken = (accessToken) => createAxiosInstance({ accessToken });
const customAxios = (accessToken?: string) => createAxiosInstance({ accessToken });

export const requestGetPopularStudylogs = ({ accessToken }: { accessToken?: string }) => {
if (accessToken) {
return instanceWithToken(accessToken).get('/studylogs/popular');
return customAxios(accessToken).get('/studylogs/popular');
}
return instanceWithoutToken.get('/studylogs/popular');
return customAxios().get('/studylogs/popular');
};

export type StudylogQuery =
Expand All @@ -44,7 +43,7 @@ export const requestGetStudylogs = ({
query?: StudylogQuery;
accessToken?: string;
}) => {
const instance = accessToken ? instanceWithToken(accessToken) : instanceWithoutToken;
const instance = accessToken ? customAxios(accessToken) : customAxios();

if (!query) {
return instance.get('/studylogs');
Expand All @@ -70,14 +69,13 @@ export const requestGetStudylogs = ({

export type ResponseError = { code: number; messsage: string };

export const requestGetTags = (): Promise<AxiosResponse<Tag[]>> =>
instanceWithoutToken.get('/tags');
export const requestGetTags = (): Promise<AxiosResponse<Tag[]>> => customAxios().get('/tags');

export const requestGetMissions = ({ accessToken }): Promise<AxiosResponse<Mission[]>> =>
instanceWithToken(accessToken).get('/missions/mine');
customAxios(accessToken).get('/missions/mine');

export const requestGetSessions = ({ accessToken }): Promise<AxiosResponse<Session[]>> =>
instanceWithToken(accessToken).get('/sessions/mine');
customAxios(accessToken).get('/sessions/mine');

export const requestGetStudylog = ({
id,
Expand All @@ -86,7 +84,7 @@ export const requestGetStudylog = ({
id: string;
accessToken: string;
}): AxiosPromise<AxiosResponse<Studylog>> =>
instanceWithToken(accessToken).get<AxiosResponse<Studylog>>(`/studylogs/${id}`);
customAxios(accessToken).get<AxiosResponse<Studylog>>(`/studylogs/${id}`);

/** 작성 및 수정 **/
export const requestPostStudylog = ({
Expand All @@ -95,7 +93,7 @@ export const requestPostStudylog = ({
}: {
accessToken: string;
data: StudylogForm;
}): AxiosPromise<AxiosResponse<null>> => instanceWithToken(accessToken).post('/studylogs', data);
}): AxiosPromise<AxiosResponse<null>> => customAxios(accessToken).post('/studylogs', data);

export const requestEditStudylog = ({
id,
Expand All @@ -105,15 +103,14 @@ export const requestEditStudylog = ({
id: string;
accessToken: string;
data: StudylogForm;
}): AxiosPromise<AxiosResponse<null>> =>
instanceWithToken(accessToken).put(`/studylogs/${id}`, data);
}): AxiosPromise<AxiosResponse<null>> => customAxios(accessToken).put(`/studylogs/${id}`, data);

/** 임시 저장 **/
export const requestGetTempSavedStudylog = async () => {
const { data } = await instanceWithoutToken.get<Nullable<TempSavedStudyLog>>('/studylogs/temp');
const { data } = await customAxios().get<Nullable<TempSavedStudyLog>>('/studylogs/temp');

return data;
};

export const requestPostTempSavedStudylog = (data: TempSavedStudyLogForm) =>
instanceWithoutToken.put('/studylogs/temp', data);
customAxios().put('/studylogs/temp', data);

0 comments on commit 9dd8c05

Please sign in to comment.