-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): Tanstack Query 세팅 및 API 공통 메서드 작성 (#57)
* feat: Tanstack Query 및 api 메서드 정의 * feat: Tanstack Query 및 api 메서드 정의 * chore: 코드리뷰 반영 * chore: 코드리뷰 반영
- Loading branch information
Showing
18 changed files
with
3,284 additions
and
3,773 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,3 @@ | ||
'use client'; | ||
|
||
import { FirstDomainExampleScreen } from '@sambad/web-domains/first-domain'; | ||
import { useEffect } from 'react'; | ||
|
||
import styles from './page.module.css'; | ||
|
||
export default function Home() { | ||
useEffect(() => { | ||
console.log('test-main'); | ||
}, []); | ||
|
||
return ( | ||
<main className={styles.main}> | ||
<FirstDomainExampleScreen /> | ||
</main> | ||
); | ||
} | ||
export default FirstDomainExampleScreen; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import axios from 'axios'; | ||
const baseURL = `/api`; | ||
|
||
const axiosInstance = axios.create({ | ||
baseURL: baseURL, | ||
}); | ||
|
||
export class Http { | ||
static async GET<Response = unknown>(...args: Parameters<typeof axiosInstance.get>) { | ||
const response = await axiosInstance.get<Response>(...args); | ||
return response.data; | ||
} | ||
|
||
static async POST<Request = unknown, Response = unknown>(...args: Parameters<typeof axiosInstance.post>) { | ||
const response = await axiosInstance.post<Request, Response>(...args); | ||
return response; | ||
} | ||
|
||
static async PUT<Request = unknown, Response = unknown>(...args: Parameters<typeof axiosInstance.put>) { | ||
const response = await axiosInstance.put<Request, Response>(...args); | ||
return response; | ||
} | ||
|
||
static async DELETE<Response = unknown>(...args: Parameters<typeof axiosInstance.delete>) { | ||
const response = await axiosInstance.delete<Response>(...args); | ||
return response.data; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/web-domains/src/common/apis/queries/useHealthCheckQuery.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { QueryClient, useQuery } from '@tanstack/react-query'; | ||
|
||
import { Http } from '../base.api'; | ||
|
||
const ping = () => Http.GET<any>('/actuator/health'); | ||
|
||
export const useHealthCheckQuery = () => { | ||
const { data } = useQuery({ | ||
queryKey: ['health'], | ||
queryFn: ping, | ||
}); | ||
|
||
return { data }; | ||
}; | ||
|
||
export const useHealthCheckPrefetchQuery = async () => { | ||
const queryClient = new QueryClient(); | ||
|
||
const prefetchedData = await queryClient.prefetchQuery({ | ||
queryKey: ['health'], | ||
queryFn: ping, | ||
}); | ||
|
||
return { | ||
queryClient, | ||
prefetchedData, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use client'; | ||
|
||
import { PropsWithChildren } from 'react'; | ||
|
||
import { QueryClientProvider } from './QueryClientProvider'; | ||
|
||
export const Providers = ({ children }: PropsWithChildren) => { | ||
return <QueryClientProvider>{children}</QueryClientProvider>; | ||
}; |
34 changes: 34 additions & 0 deletions
34
packages/web-domains/src/common/contexts/QueryClientProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use client'; | ||
import { isServer, QueryClient, QueryClientProvider as TanstackProvider } from '@tanstack/react-query'; | ||
import { PropsWithChildren } from 'react'; | ||
|
||
const makeQueryClient = () => { | ||
return new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
staleTime: 60 * 1000, | ||
refetchOnWindowFocus: false, | ||
retry: 1, | ||
}, | ||
}, | ||
}); | ||
}; | ||
|
||
let browserQueryClient: QueryClient | undefined = undefined; | ||
|
||
const getQueryClient = () => { | ||
if (isServer) { | ||
return makeQueryClient(); | ||
} | ||
|
||
if (!browserQueryClient) { | ||
browserQueryClient = makeQueryClient(); | ||
} | ||
return browserQueryClient; | ||
}; | ||
|
||
export const QueryClientProvider = ({ children }: PropsWithChildren) => { | ||
const queryClient = getQueryClient(); | ||
|
||
return <TanstackProvider client={queryClient}>{children}</TanstackProvider>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { QueryClientProvider } from './contexts/QueryClientProvider'; | ||
export { Providers } from './contexts/Providers'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...web-domains/src/first-domain/features/services/useFirstFeatureOfFirstDomainTestService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 11 additions & 5 deletions
16
packages/web-domains/src/first-domain/screens/FirstDomainExampleScreen.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
import { HydrationBoundary, dehydrate } from '@tanstack/react-query'; | ||
|
||
import { useHealthCheckPrefetchQuery } from '../../common/apis/queries/useHealthCheckQuery'; | ||
import { FirstFeatureOfFirstDomainTestContainer } from '../features/containers/FirstFeatureOfFirstDomainTestContainer'; | ||
|
||
export const FirstDomainExampleScreen = () => { | ||
export const FirstDomainExampleScreen = async () => { | ||
const { queryClient } = await useHealthCheckPrefetchQuery(); | ||
return ( | ||
<div> | ||
<h1>도메인 화면을 전체 담당 하는 컴포넌트입니다.</h1> | ||
<FirstFeatureOfFirstDomainTestContainer /> | ||
</div> | ||
<HydrationBoundary state={dehydrate(queryClient)}> | ||
<div> | ||
<h1>도메인 화면을 전체 담당 하는 컴포넌트입니다.</h1> | ||
<FirstFeatureOfFirstDomainTestContainer /> | ||
</div> | ||
</HydrationBoundary> | ||
); | ||
}; |
Oops, something went wrong.