Skip to content

Commit b92ebfe

Browse files
committed
feat: add build script and implement API fetcher with React Query integration
1 parent 027f5e8 commit b92ebfe

File tree

7 files changed

+158
-1
lines changed

7 files changed

+158
-1
lines changed

packages/api-base/build.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import esbuild from 'esbuild';
2+
import pkg from './package.json' with { type: 'json' };
3+
4+
const sharedConfig = {
5+
entryPoints: ['./src/index.ts'],
6+
bundle: true,
7+
write: true,
8+
treeShaking: true,
9+
minify: true,
10+
sourcemap: false,
11+
target: ['es2020'],
12+
external: [
13+
'react',
14+
'react-dom',
15+
...Object.keys(pkg.dependencies || {}),
16+
...Object.keys(pkg.peerDependencies || {}),
17+
],
18+
jsx: 'automatic',
19+
jsxImportSource: 'react',
20+
};
21+
22+
esbuild
23+
.build({
24+
...sharedConfig,
25+
outfile: './dist/index.js',
26+
format: 'esm',
27+
platform: 'neutral',
28+
})
29+
.catch(() => process.exit(1));
30+
31+
esbuild
32+
.build({
33+
...sharedConfig,
34+
outfile: './dist/index.cjs',
35+
format: 'cjs',
36+
platform: 'neutral',
37+
})
38+
.catch(() => process.exit(1));

packages/api-base/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
"react-dom": "^19.1.1"
3535
},
3636
"dependencies": {
37+
"@tanstack/react-query": "^5.84.1",
38+
"@tanstack/react-query-devtools": "^5.84.1",
3739
"ky": "^1.8.2"
3840
},
3941
"devDependencies": {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use client';
2+
3+
import {
4+
QueryClientProvider as BaseQueryClientProvider,
5+
QueryClient,
6+
type QueryClientConfig,
7+
} from '@tanstack/react-query';
8+
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
9+
import { type PropsWithChildren, useState } from 'react';
10+
11+
type Props = PropsWithChildren<{
12+
config?: QueryClientConfig;
13+
}>;
14+
15+
const defaultConfig: QueryClientConfig = {
16+
defaultOptions: {
17+
queries: {
18+
refetchOnWindowFocus: false,
19+
refetchInterval: false,
20+
staleTime: 1000 * 60,
21+
retry: 0,
22+
},
23+
},
24+
};
25+
26+
export const QueryClientProvider = ({ children, config }: Props) => {
27+
const mergedConfig = { ...defaultConfig, ...config };
28+
const [queryClient] = useState(() => new QueryClient(mergedConfig));
29+
30+
return (
31+
<BaseQueryClientProvider client={queryClient}>
32+
{children}
33+
<ReactQueryDevtools initialIsOpen={false} />
34+
</BaseQueryClientProvider>
35+
);
36+
};

packages/api-base/src/fetcher.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { Options, ResponsePromise } from 'ky';
2+
import ky from 'ky';
3+
4+
const API_URL = typeof window === 'undefined' ? 'https://api.hufscheer.com/' : '/api';
5+
6+
const defaultOption: Options = {
7+
retry: 0,
8+
timeout: 30_000,
9+
};
10+
11+
export const instance = ky.create({
12+
prefixUrl: API_URL,
13+
headers: { 'content-type': 'application/json' },
14+
...defaultOption,
15+
});
16+
17+
export async function resultify<T>(response: ResponsePromise) {
18+
return await response.json<T>();
19+
}
20+
21+
export const fetcher = {
22+
get: <T>(pathname: string, options?: Options) => resultify<T>(instance.get(pathname, options)),
23+
post: <T>(pathname: string, options?: Options) => resultify<T>(instance.post(pathname, options)),
24+
put: <T>(pathname: string, options?: Options) => resultify<T>(instance.put(pathname, options)),
25+
patch: <T>(pathname: string, options?: Options) =>
26+
resultify<T>(instance.patch(pathname, options)),
27+
delete: <T>(pathname: string, options?: Options) =>
28+
resultify<T>(instance.delete(pathname, options)),
29+
};

packages/api-base/src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export {
2+
dehydrate,
3+
HydrationBoundary,
4+
MutationCache,
5+
QueryCache,
6+
type UseSuspenseQueryResult,
7+
useMutation,
8+
useQuery,
9+
useQueryClient,
10+
useSuspenseQuery,
11+
} from '@tanstack/react-query';
12+
13+
export * from './fetcher';
14+
export * from './QueryClientProvider';

packages/icons/src/child.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type ChildProps = {
66
};
77

88
const Child = ({ children, ...props }: ChildProps) => {
9-
const child: React.ReactNode = Children.only(children);
9+
const child: ReactNode = Children.only(children);
1010

1111
return isValidElement(child) ? cloneElement(child, props) : null;
1212
};

pnpm-lock.yaml

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)