-
Notifications
You must be signed in to change notification settings - Fork 1
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
[Api] axios, react-query setting #26
Changes from 23 commits
e153b8e
d162b53
a74fdd3
4420a2d
e3791cf
dd6a627
cd1348d
650d752
6b8c207
452a93e
2a0e6ff
79be16f
0379029
d0be44d
8afdcdc
b844861
02d0069
9fe6bb7
e499925
68d522f
2c7c056
0196a91
52fe772
18e4d3b
13ee8e1
eee6740
4c98a2d
b135342
a9a9270
84b0f03
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
CLIENT_API_URL = 'client-api-url' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./src"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./post"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./useGetPostDetail"; | ||
export * from "./useGetPostList"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { postUrl, get, PostDetailType, postQueryKeys } from "client"; | ||
|
||
export const useGetPostDetail = (seq: number) => { | ||
const query = useQuery<PostDetailType>(postQueryKeys.getPostDetail(seq), () => | ||
get(postUrl.postDetail(seq)) | ||
); | ||
|
||
return query; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { | ||
PostCategoryType, | ||
PostListType, | ||
get, | ||
postQueryKeys, | ||
postUrl, | ||
} from "client"; | ||
|
||
export const useGetPostList = ( | ||
category: PostCategoryType, | ||
pageNumber: number | ||
) => { | ||
const query = useQuery<PostListType>( | ||
postQueryKeys.getPostList(category, pageNumber), | ||
() => get(postUrl.postList(category, pageNumber)), | ||
{ | ||
cacheTime: Infinity, | ||
staleTime: Infinity, | ||
} | ||
); | ||
|
||
return query; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./api"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./hooks"; | ||
export * from "./libs"; | ||
export * from "./types"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { instance } from "./instance"; | ||
|
||
export const get = async (...args: Parameters<typeof instance.get>) => { | ||
try { | ||
const { data } = await instance.get(...args); | ||
return data; | ||
} catch (error) { | ||
return error; | ||
} | ||
}; | ||
|
||
export const post = async <T>(...args: Parameters<typeof instance.post>) => { | ||
try { | ||
await instance.post<T>(...args); | ||
} catch (error) { | ||
return error; | ||
} | ||
}; | ||
|
||
export const patch = async <T>(...args: Parameters<typeof instance.patch>) => { | ||
try { | ||
await instance.patch<T>(...args); | ||
} catch (error) { | ||
return error; | ||
} | ||
}; | ||
|
||
export const del = async <T>(...args: Parameters<typeof instance.delete>) => { | ||
try { | ||
await instance.delete<T>(...args); | ||
} catch (error) { | ||
return error; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./client"; | ||
export * from "./instance"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import axios from "axios"; | ||
|
||
export const instance = axios.create({ | ||
baseURL: "/api/client", | ||
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. dev와 Prod의 base url이 동일한가요? 그리고 instance라는 네이밍은 너무 포괄적이군요 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.
baseUrl 관리는 해당 설정은 클라이언트(프론트)의 주소 (ex. localhost:3000/api/client)로 요청을 보내도록 작성한 코드입니다.
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.
https://axios-http.com/kr/docs/instance 공식 문서나 여러 소스 코드들을 살펴보았을 때, instance라는 네이밍이 일반적인 듯 하여 이렇게 작성했습니다! 좀 더 의도가 명확한 네이밍 (ex. clientInstance) 으로 변경하는게 아무래도 직관적인 듯 하여 수정하겠습니다! 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. |
||
withCredentials: true, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { PostCategoryType } from "../types"; | ||
|
||
hyeongrok7874 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
export const postUrl = { | ||
postList: (category: PostCategoryType, pageNumber: number) => | ||
`/post?category=${category}&pageNumber=${pageNumber}`, | ||
postDetail: (postSeq: number) => `/post/${postSeq}`, | ||
} as const; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./clientUrlController"; | ||
export * from "./api"; | ||
export * from "./queryKeys"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { PostCategoryType } from "client"; | ||
|
||
export const postQueryKeys = { | ||
getPostList: (category: PostCategoryType, pageNumber: number) => [ | ||
"post", | ||
"list", | ||
category, | ||
pageNumber, | ||
], | ||
getPostDetail: (postSeq: number) => ["post", "detail", postSeq], | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./post"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export type PostCategoryType = "NOTICE" | "FAMILY_NEWSLETTER" | "EVENT_GALLERY"; | ||
|
||
export interface PostDetailType { | ||
postTitle: string; | ||
postWriter: string; | ||
postContent: string; | ||
createdAt: string; | ||
fileInfo: FileInfoType[]; | ||
} | ||
|
||
interface FileInfoType { | ||
fileUrl: string; | ||
fileName: string; | ||
fileExtension: string; | ||
} | ||
|
||
export interface PostListType { | ||
content: ContentType[]; | ||
} | ||
|
||
export interface ContentType { | ||
postSeq: number; | ||
postTitle: string; | ||
postWriter: string; | ||
createdAt: string; | ||
thumbnailUrl: string; | ||
fileIsExist: boolean; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./src"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./libs"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./queryClient"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { QueryClient } from "@tanstack/react-query"; | ||
|
||
export const queryClient = new QueryClient({ | ||
// defaultOptions: {}, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "api", | ||
"version": "0.0.0", | ||
"main": "./index.ts", | ||
"dependencies": { | ||
"@types/node": "20.1.4", | ||
"axios": "^1.4.0" | ||
}, | ||
"devDependencies": { | ||
"tsconfig": "workspace:^" | ||
}, | ||
"peerDependencies": { | ||
"@tanstack/react-query": "^4.29.7" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "tsconfig/base.json", | ||
"compilerOptions": { | ||
"rootDir": ".", | ||
"baseUrl": "." | ||
}, | ||
"include": ["**/*.ts", "**/*.tsx"], | ||
"exclude": ["node_modules"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./src"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./styles"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./GlobalStyle"; |
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.
layout 에서 provider 컴포넌트를 사용하는게 일반적인 컨벤션인가요?
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.
react query - nextjs13
위 공식 문서 참고하시면 root layout에서 provider를 사용하는 것을 확인 하실 수 있습니다
단지 위 공식 문서와 제 방식의 차이점은
const [queryClient] = React.useState(() => new QueryClient())
이런 형태로 선언하여,setter 함수를 선언하지 않음으로써 참조 동일성을 유지하고, 방식으로 작성되어 있습니다.
제가 이해한 공식문서에서 QueryClientProvider를 커스텀한 이유는 아래와 같습니다.
커스텀 QueryClientProvider
모듈에만 추가하기 위함현재 제 구성은 <Hydrate> 사용을 고려하지 못한 구성인 듯 하여 공식 문서대로 수정을 할 예정입니다.
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.
4c98a2d
추후 <Hydrate> 사용을 위한 방식으로 수정 완료했습니다!