Skip to content

Commit

Permalink
feat: @tanstack/react-query 설정 추가 및 예시 쿼리 추가 (#7)
Browse files Browse the repository at this point in the history
* feat: @tanstack/react-query 설정 추가 및 예시 쿼리 추가

* fix: main, types 수정

* chore: lint 에러 수정

* fix: 병합 잘못된 것 수정
  • Loading branch information
alstn2468 authored Jan 21, 2024
1 parent 39415e2 commit c975959
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 67 deletions.
135 changes: 106 additions & 29 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified .yarn/install-state.gz
Binary file not shown.
9 changes: 4 additions & 5 deletions apps/admin/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Hello, QueryClientProvider, ReactQueryDevtools } from '@boolti/api';
import { QueryClientProvider } from '@boolti/api';

const App = () => {
// const { data } = useHelloQuery();
// console.log(data?.hello)
return (
<QueryClientProvider>
<h1>
{Hello()}
<ReactQueryDevtools />
</h1>
<h1>Hello World</h1>
</QueryClientProvider>
);
};
Expand Down
9 changes: 5 additions & 4 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
"private": true,
"version": "0.0.0",
"type": "module",
"main": "src/index.tsx",
"types": "src/index.tsx",
"main": "src/index.ts",
"types": "src/index.ts",
"scripts": {
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0"
},
"dependencies": {
"@lukemorales/query-key-factory": "^1.3.2",
"@tanstack/react-query": "^5.17.15",
"@tanstack/react-query-devtools": "^5.17.18",
"@tanstack/query-core": "^4.32.6",
"@tanstack/react-query": "^4.32.6",
"@tanstack/react-query-devtools": "^4.32.6",
"ky": "^1.2.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
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>
);
}
34 changes: 34 additions & 0 deletions packages/api/src/fetcher.ts
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)),
};
2 changes: 2 additions & 0 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { useHelloQuery } from './useHelloQuery';
export { QueryClientProvider } from './QueryClientProvider';
13 changes: 13 additions & 0 deletions packages/api/src/queryKey.ts
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'),
},
});
10 changes: 10 additions & 0 deletions packages/api/src/useHelloQuery.ts
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,
});
}
3 changes: 3 additions & 0 deletions packages/api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": "@boolti/typescript-config/vite.json",
"compilerOptions": {
"moduleResolution": "node",
},
"include": [
"src"
]
Expand Down
Loading

0 comments on commit c975959

Please sign in to comment.