-
Notifications
You must be signed in to change notification settings - Fork 46
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
[우재현] Sprint10 #317
The head ref may contain hidden characters: "Next-\uC6B0\uC7AC\uD604-sprint10"
[우재현] Sprint10 #317
Changes from all commits
cec16db
6db2616
d689db9
1b4525c
e5dc536
cb60cfc
73fcca3
204ee68
a644810
6a4a468
0e0e376
840b87b
a392885
053dd4c
bdc4f1c
7fc9edb
a6b3c23
c8b3642
608d74d
3ad4d85
5e4c6b1
e136570
95f7937
407db67
cbcc1c3
da2a0a2
fa30cd0
26a90e6
37a25ae
1f1fb91
8277087
2926a1d
70c75e1
3c559ba
51ad049
9e37071
27ab728
6d3722f
f44da17
7b63704
eaa5c6a
5ce6d0c
774a70f
6023f74
c5c2007
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { ArticleList } from "@/types/Article.type"; | ||
import { Article, ArticleList } from "@/types/Article.type"; | ||
import axios from "./axios"; | ||
|
||
interface GetArticleListParams { | ||
|
@@ -27,5 +27,33 @@ async function getArticleList({ | |
return response.data; | ||
} | ||
|
||
export { getArticleList }; | ||
async function getArticle({ id }: { id: number }): Promise<Article> { | ||
const response = await axios.get(`/articles/${id}`); | ||
return response.data; | ||
} | ||
|
||
interface PostArticle { | ||
image?: string | undefined; | ||
content: string; | ||
title: string; | ||
} | ||
|
||
async function postArticle({ | ||
image, | ||
content, | ||
title, | ||
}: PostArticle): Promise<Article> { | ||
const response = await axios({ | ||
method: "post", | ||
url: "/articles", | ||
data: { | ||
content, | ||
title, | ||
image, | ||
}, | ||
}); | ||
return response.data; | ||
Comment on lines
+46
to
+54
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. axios 사용할 때 현재 처럼 설정 값을 객체 통해 지정하고 있는데, 아래 처럼 메서드를 직접 사용하는 방식이 보다 간결하고 명확해서 변경하면 좋겠습니다. 😸
|
||
} | ||
|
||
export { getArticleList, getArticle, postArticle }; | ||
export type { GetArticleListParams, OrderBy }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { User } from "@/types/User.type"; | ||
import axios from "./axios"; | ||
|
||
interface SignUpParams { | ||
email: string; | ||
nickname: string; | ||
password: string; | ||
passwordConfirmation: string; | ||
} | ||
|
||
interface SignInParams { | ||
email: string; | ||
password: string; | ||
} | ||
|
||
interface AuthResponse { | ||
accessToken: string; | ||
refreshToken: string; | ||
user: User; | ||
} | ||
|
||
async function postSignUp({ | ||
email, | ||
nickname, | ||
password, | ||
passwordConfirmation, | ||
}: SignUpParams): Promise<AuthResponse> { | ||
const response = await axios({ | ||
method: "post", | ||
url: "/auth/signUp", | ||
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. 이러한 URL은 contants 디렉토리 내 api.ts 파일을 하나 만들어서 상수로 관리하면 좋겠어요.
|
||
data: { | ||
email, | ||
nickname, | ||
password, | ||
passwordConfirmation, | ||
}, | ||
}); | ||
return response.data; | ||
} | ||
|
||
async function postSignIn({ | ||
email, | ||
password, | ||
}: SignInParams): Promise<AuthResponse> { | ||
const response = await axios({ | ||
method: "post", | ||
url: "/auth/signIn", | ||
data: { | ||
email, | ||
password, | ||
}, | ||
}); | ||
return response.data; | ||
} | ||
|
||
async function postRefresh(refreshToken: string) { | ||
const response = await axios({ | ||
method: "post", | ||
url: "/auth/refresh-token", | ||
data: { | ||
refreshToken, | ||
}, | ||
}); | ||
return response.data; | ||
} | ||
|
||
export { postSignUp, postSignIn, postRefresh }; | ||
export type { SignUpParams, SignInParams, AuthResponse }; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import axios from "axios"; | ||
|
||
const BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL; | ||
|
||
const instance = axios.create({ | ||
baseURL: BASE_URL, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
let interceptorId: any; | ||
|
||
export const setInstanceHeaders = (token?: string) => { | ||
const value = token ? `Bearer ${token}` : undefined; | ||
// 기존 인터셉터 제거 | ||
if (interceptorId !== undefined) { | ||
instance.interceptors.request.eject(interceptorId); | ||
} | ||
|
||
// 새로운 인터셉터 추가 | ||
interceptorId = instance.interceptors.request.use((config) => { | ||
config.headers["Authorization"] = value; | ||
return config; | ||
}); | ||
Comment on lines
+16
to
+25
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. 오호 interceptors 아이디어 좋습니다. 👍 |
||
}; | ||
export default instance; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import axios from "./axios"; | ||
|
||
interface GetCommentListByArticleId { | ||
articleId: number; | ||
limit: number; | ||
cursor?: number; | ||
} | ||
|
||
async function getCommentListByArticleId({ | ||
articleId, | ||
limit, | ||
cursor, | ||
}: GetCommentListByArticleId) { | ||
const response = await axios.get(`/articles/${articleId}/comments`, { | ||
params: { | ||
limit, | ||
cursor, | ||
}, | ||
}); | ||
return response.data; | ||
} | ||
Comment on lines
+3
to
+21
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. 여기서 GetCommentListByArticleId 값이 지역 타입이기도 하고, 타입이라 예상되지만 이처럼 PascalCase 로 네이밍이 되게되면 혼란이 야기될 수 있어서 타입 혹은 인터페이스 뒤에 props 붙이는 방법도 있습니다. 즉 GetCommentListByArticleIdProps 이처럼요. |
||
|
||
interface PostCommentByArticleId { | ||
articleId: number; | ||
content: string; | ||
} | ||
|
||
async function postCommentByArticleId({ | ||
articleId, | ||
content, | ||
}: PostCommentByArticleId) { | ||
const response = await axios({ | ||
method: "post", | ||
url: `/articles/${articleId}/comments`, | ||
data: { | ||
content, | ||
}, | ||
}); | ||
return response.data; | ||
} | ||
|
||
export { getCommentListByArticleId, postCommentByArticleId }; | ||
export type { GetCommentListByArticleId, PostCommentByArticleId }; | ||
Comment on lines
+42
to
+43
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. 정답은 없지만, get / post / delete / update 등을 함수명에 추가하는 방법은 좋습니다. 현업 사례로 기준을 잡고 말씀드리면 API를 요청하는 부분은 fetchOOOO 통일하곤 해요. 즉 fetchArticles 가 되겠습니다. 또한 아래처럼 도메인 중심 네이밍 방법도 있는데요. 선택하셔서 수정하면 지금 보다 더 좋은 네이밍과 유연한 설계가 될 것 같아요.
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import axios from "./axios"; | ||
|
||
async function postImage(file: File): Promise<string> { | ||
const response = await axios.post("/images/upload", { | ||
data: { image: file }, | ||
}); | ||
return response.data.url; | ||
} | ||
|
||
export { postImage }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ReactNode } from "react"; | ||
|
||
export default function Container({ children }: { children: ReactNode }) { | ||
return <div className="container">{children}</div>; | ||
} |
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.
아래처럼 응답 타입에 제너릭을 추가할 수 있습니다.