-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: @tanstack/react-query 설정 추가 및 예시 쿼리 추가 (#7)
* feat: @tanstack/react-query 설정 추가 및 예시 쿼리 추가 * fix: main, types 수정 * chore: lint 에러 수정 * fix: 병합 잘못된 것 수정
- Loading branch information
Showing
11 changed files
with
262 additions
and
67 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
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
13 changes: 7 additions & 6 deletions
13
packages/api/src/index.tsx → packages/api/src/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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import { QueryClientProvider as BaseQueryClientProvider, QueryClient } from '@tanstack/react-query'; | ||
import { useState } from 'react'; | ||
export { ReactQueryDevtools } from '@tanstack/react-query-devtools'; | ||
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'; | ||
|
||
export function QueryClientProvider({ children }: React.PropsWithChildren) { | ||
const [queryClient] = useState(() => new QueryClient()); | ||
return <BaseQueryClientProvider client={queryClient}>{children}</BaseQueryClientProvider>; | ||
} | ||
|
||
export function Hello() { | ||
return 'World'; | ||
return ( | ||
<BaseQueryClientProvider client={queryClient}> | ||
{children} | ||
<ReactQueryDevtools /> | ||
</BaseQueryClientProvider> | ||
); | ||
} |
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 @@ | ||
import ky, { Options, ResponsePromise } from 'ky'; | ||
|
||
// TODO 환경 변수로 API 베이스 설정 | ||
const API_URL = ''; | ||
|
||
export const instance = ky.create({ | ||
prefixUrl: API_URL, | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
hooks: { | ||
// TODO 인증 관련 헤더 검증 로직 추가 | ||
beforeRequest: [], | ||
// TODO 서버 에러 처리 | ||
beforeError: [], | ||
}, | ||
}); | ||
|
||
export async function resultify<T>(response: ResponsePromise) { | ||
try { | ||
// TODO 바디가 없는 경우 어떻게 할지 논의 필요 | ||
return await response.json<T>(); | ||
} catch (e) { | ||
console.error('[fetcher.ts] resultify에서 JSON 파싱을 하는 도중 오류 발생'); | ||
throw e; | ||
} | ||
} | ||
|
||
export const fetcher = { | ||
get: <T>(pathname: string, options?: Options) => resultify<T>(ky.get(pathname, options)), | ||
post: <T>(pathname: string, options?: Options) => resultify<T>(ky.post(pathname, options)), | ||
put: <T>(pathname: string, options?: Options) => resultify<T>(ky.put(pathname, options)), | ||
delete: <T>(pathname: string, options?: Options) => resultify<T>(ky.delete(pathname, options)), | ||
}; |
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 { useHelloQuery } from './useHelloQuery'; | ||
export { QueryClientProvider } from './QueryClientProvider'; |
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,13 @@ | ||
import { createQueryKeys } from '@lukemorales/query-key-factory'; | ||
import { fetcher } from './fetcher'; | ||
|
||
export interface Hello { | ||
hello: string; | ||
} | ||
|
||
export const queryKey = createQueryKeys('boolti', { | ||
hello: { | ||
queryKey: null, | ||
queryFn: () => fetcher.get<Hello>('/hello'), | ||
}, | ||
}); |
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,10 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { queryKey } from './queryKey'; | ||
import { TypedUseQueryOptions } from '@lukemorales/query-key-factory'; | ||
|
||
export function useHelloQuery(options?: TypedUseQueryOptions<typeof queryKey.hello>) { | ||
return useQuery({ | ||
...options, | ||
...queryKey.hello, | ||
}); | ||
} |
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
Oops, something went wrong.