-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
apiClient 변경 및 기존 customFetch 함수 제거 #929
Changes from all commits
e5af0d5
61b5bd3
8192cc6
bd20e68
182341a
433ae91
a89d255
2cb566d
8b75c4d
ceb82d1
c718c2a
2e18300
1518d28
5efa301
e20477b
7caab2a
cf71479
f748d07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 에러 반환 함수 만들어주셨군요👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { END_POINTS } from '@/routes'; | ||
|
||
// TODO: errorCode magic number | ||
export const getErrorMessage = (errorCode: number, instance: string) => { | ||
if (errorCode === 1202 && instance === END_POINTS.CHECK_NAME) { | ||
return '중복된 아이디입니다'; | ||
} | ||
|
||
return 'APIError 입니다'; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,22 @@ | ||
import { END_POINTS } from '@/routes'; | ||
import type { LoginRequest, SignupRequest } from '@/types'; | ||
import { MemberInfo } from '@/types'; | ||
|
||
import { apiClient } from './ApiClient'; | ||
import { customFetch } from './customFetch'; | ||
import { apiClient } from './config'; | ||
|
||
const API_URL = process.env.REACT_APP_API_URL || 'https://default-url.com'; | ||
export const postSignup = async (signupInfo: SignupRequest) => await apiClient.post(END_POINTS.SIGNUP, signupInfo); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 코드가 확연히 줄었네요 매우 좋습니다~! 👍👍👍 |
||
|
||
export const SIGNUP_API_URL = `${API_URL}${END_POINTS.SIGNUP}`; | ||
export const LOGIN_API_URL = `${API_URL}${END_POINTS.LOGIN}`; | ||
export const LOGOUT_API_URL = `${API_URL}${END_POINTS.LOGOUT}`; | ||
export const LOGIN_STATE_API_URL = `${API_URL}${END_POINTS.LOGIN_CHECK}`; | ||
export const CHECK_NAME_API_URL = `${API_URL}${END_POINTS.CHECK_NAME}`; | ||
export const postLogin = async (loginInfo: LoginRequest) => { | ||
const response = await apiClient.post(END_POINTS.LOGIN, loginInfo); | ||
|
||
export const postSignup = async (signupInfo: SignupRequest) => | ||
await customFetch({ | ||
method: 'POST', | ||
url: `${SIGNUP_API_URL}`, | ||
body: JSON.stringify(signupInfo), | ||
}); | ||
|
||
export const postLogin = async (loginInfo: LoginRequest): Promise<MemberInfo> => { | ||
const response = await customFetch<MemberInfo>({ | ||
method: 'POST', | ||
url: `${LOGIN_API_URL}`, | ||
body: JSON.stringify(loginInfo), | ||
}); | ||
|
||
if ('memberId' in response) { | ||
return response; | ||
} | ||
|
||
if (response.status >= 400) { | ||
return { memberId: undefined, name: undefined }; | ||
} | ||
|
||
throw new Error(response.detail); | ||
return await response.json(); | ||
}; | ||
|
||
export const postLogout = async () => { | ||
const response = await customFetch<unknown>({ | ||
method: 'POST', | ||
url: `${LOGOUT_API_URL}`, | ||
}); | ||
|
||
return response; | ||
}; | ||
export const postLogout = async () => await apiClient.post(END_POINTS.LOGOUT, {}); | ||
|
||
export const getLoginState = async () => apiClient.get(END_POINTS.LOGIN_CHECK); | ||
|
||
Comment on lines
16
to
17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 파일에 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 요청에 대한 응답이 없고, 사용하지 않기 때문입니다. 혹시 일관성을 위해 구조를 통일해야할까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 흠,,, 이건 개발자 취향일거 같긴하지만,,, 전 일관성과 통일감을 위해 통일하는 편이긴합니다... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 평소 통일성을 위해 일관된 반환값을 선호하는 편이지만, 지금 이 경우에는 애초에 서버에서 오는 반환값이 없기 때문에 json()이 아예 불가능한 상황이라고 생각하였습니다. 그래서 지금 이대로도 괜찮을 것 같아요..! |
||
export const checkName = async (name: string) => { | ||
const params = new URLSearchParams({ name }); | ||
const url = `${CHECK_NAME_API_URL}?${params}`; | ||
|
||
const response = await fetch(url, { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
credentials: 'include', | ||
}); | ||
|
||
if (response.status === 409) { | ||
throw new Error('중복된 아이디입니다.'); | ||
} | ||
|
||
if (!response.ok) { | ||
throw new Error('서버 에러가 발생했습니다.'); | ||
} | ||
|
||
return {}; | ||
await apiClient.get(`${END_POINTS.CHECK_NAME}?${params.toString()}`); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,25 @@ | ||
import { HttpResponse } from 'msw'; | ||
|
||
import { END_POINTS } from '@/routes'; | ||
import type { | ||
CategoryListResponse, | ||
CategoryUploadRequest, | ||
CategoryEditRequest, | ||
CategoryDeleteRequest, | ||
CustomError, | ||
Category, | ||
} from '@/types'; | ||
|
||
import { customFetch } from './customFetch'; | ||
import type { CategoryUploadRequest, CategoryEditRequest, CategoryDeleteRequest } from '@/types'; | ||
|
||
const API_URL = process.env.REACT_APP_API_URL || 'https://default-url.com'; | ||
|
||
export const CATEGORY_API_URL = `${API_URL}${END_POINTS.CATEGORIES}`; | ||
import { apiClient } from './config'; | ||
|
||
export const getCategoryList = async (memberId: number) => { | ||
const url = `${CATEGORY_API_URL}?memberId=${memberId}`; | ||
|
||
const response = await customFetch<CategoryListResponse>({ | ||
url, | ||
const queryParams = new URLSearchParams({ | ||
memberId: memberId.toString(), | ||
}); | ||
const response = await apiClient.get(`${END_POINTS.CATEGORIES}?${queryParams.toString()}`); | ||
|
||
if ('categories' in response) { | ||
return response; | ||
} | ||
|
||
throw new Error(response.detail); | ||
return await response.json(); | ||
}; | ||
|
||
export const postCategory = async (newCategory: CategoryUploadRequest): Promise<Category | CustomError> => | ||
await customFetch({ | ||
method: 'POST', | ||
url: `${CATEGORY_API_URL}`, | ||
body: JSON.stringify(newCategory), | ||
}); | ||
export const postCategory = async (newCategory: CategoryUploadRequest) => { | ||
const response = await apiClient.post(`${END_POINTS.CATEGORIES}`, newCategory); | ||
|
||
export const editCategory = async ({ id, name }: CategoryEditRequest) => | ||
await customFetch({ | ||
method: 'PUT', | ||
url: `${CATEGORY_API_URL}/${id}`, | ||
body: JSON.stringify({ name }), | ||
}); | ||
|
||
export const deleteCategory = async ({ id }: CategoryDeleteRequest) => { | ||
const response = await customFetch<HttpResponse>({ | ||
method: 'DELETE', | ||
url: `${CATEGORY_API_URL}/${id}`, | ||
}); | ||
return await response.json(); | ||
}; | ||
|
||
if (typeof response === 'object' && response !== null && 'status' in response) { | ||
throw response as CustomError; | ||
} | ||
export const editCategory = async ({ id, name }: CategoryEditRequest) => | ||
await apiClient.put(`${END_POINTS.CATEGORIES}/${id}`, { name }); | ||
|
||
return response; | ||
}; | ||
export const deleteCategory = async ({ id }: CategoryDeleteRequest) => | ||
await apiClient.delete(`${END_POINTS.CATEGORIES}/${id}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ApiClient } from './ApiClient'; | ||
|
||
export const API_URL = process.env.REACT_APP_API_URL ?? 'https://default-url.com'; | ||
|
||
const httpHeader = { | ||
'Content-Type': 'application/json', | ||
}; | ||
const httpCredentials = 'include'; | ||
|
||
export const apiClient = new ApiClient(API_URL, httpHeader, httpCredentials); |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,7 @@ | ||
import { HttpResponse } from 'msw'; | ||
|
||
import { END_POINTS } from '@/routes'; | ||
import { LikeDeleteRequest, LikePostRequest } from '@/types'; | ||
|
||
import { customFetch } from './customFetch'; | ||
|
||
const API_URL = process.env.REACT_APP_API_URL || 'https://default-url.com'; | ||
|
||
export const LIKE_API_URL = `${API_URL}${END_POINTS.LIKES}`; | ||
|
||
export const postLike = async ({ templateId }: LikePostRequest) => { | ||
const response = await customFetch<HttpResponse>({ | ||
method: 'POST', | ||
url: `${LIKE_API_URL}/${templateId}`, | ||
}); | ||
|
||
return response; | ||
}; | ||
import { apiClient } from './config'; | ||
|
||
export const deleteLike = async ({ templateId }: LikeDeleteRequest) => { | ||
const response = await customFetch<HttpResponse>({ | ||
method: 'DELETE', | ||
url: `${LIKE_API_URL}/${templateId}`, | ||
}); | ||
export const postLike = async (templateId: number) => await apiClient.post(`${END_POINTS.LIKES}/${templateId}`, {}); | ||
Comment on lines
-12
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 부분에서 |
||
|
||
return response; | ||
}; | ||
export const deleteLike = async (templateId: number) => await apiClient.delete(`${END_POINTS.LIKES}/${templateId}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,9 @@ | ||
import { END_POINTS } from '@/routes'; | ||
import { GetMemberNameResponse } from '@/types'; | ||
|
||
import { customFetch } from './customFetch'; | ||
|
||
const API_URL = process.env.REACT_APP_API_URL || 'https://default-url.com'; | ||
|
||
export const MEMBER_API_URL = `${API_URL}${END_POINTS.MEMBERS}`; | ||
import { apiClient } from './config'; | ||
|
||
export const getMemberName = async (memberId: number) => { | ||
const url = `${MEMBER_API_URL}/${memberId}/name`; | ||
|
||
const response = await customFetch<GetMemberNameResponse>({ | ||
url, | ||
}); | ||
|
||
if ('name' in response) { | ||
return response; | ||
} | ||
const response = await apiClient.get(`${END_POINTS.MEMBERS}/${memberId}/name`); | ||
|
||
throw new Error(response.detail); | ||
return await response.json(); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
절대 경로 다 고쳤는데,,,, 또르륵 ㅜ