Skip to content

Commit

Permalink
feat: react hooks support customHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
suhaotian committed Sep 29, 2024
1 parent 7dbad27 commit 198b3f9
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 37 deletions.
2 changes: 1 addition & 1 deletion examples/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"reflect-metadata": "^0.2.1",
"socket.io": "^4.8.0",
"typeorm": "^0.3.20",
"kysely": "^0.27.2",
"kysely": "^0.27.4",
"typeorm-cursor-pagination": "^0.10.1",
"utf-8-validate": "^6.0.3",
"ws": "^8.13.0",
Expand Down
4 changes: 2 additions & 2 deletions examples/server/packages/fe-sdk-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"axios": "^1.7.7",
"xior": "^0.6.1",
"change-case": "^4.1.2",
"kysely": "^0.26.3",
"@tanstack/react-query": "^5.22.2",
"kysely": "^0.27.4",
"@tanstack/react-query": "^5.56.2",
"i18next": "^23.10.1",
"intl-pluralrules": "^2.0.1"
},
Expand Down
3 changes: 3 additions & 0 deletions examples/web/pages/xior/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { QueryTodoRes } from 'fe-sdk-demo/lib/apiconf-refs';
import { TodoStatus } from 'fe-sdk-demo/lib/modules/todo/Todo.entity';
import { AddTodo, QueryTodo } from 'fe-sdk-demo/lib/user-api';
import { useQueryTodo } from 'fe-sdk-demo/lib/user-api-hooks';
import Head from 'next/head';
import { useState, useEffect } from 'react';
// eslint-disable-next-line import/namespace
Expand All @@ -35,6 +36,7 @@ const apiURL = baseURL + `api/${apiType}`;
export default function Home() {
const [handlerName, setHanlderName] = useState('');
const [result, setResult] = useState<QueryTodoRes>();
const { data } = useQueryTodo(handlerName ? {} : undefined);

useEffect(() => {
const io = SocketIO(socketURL, {
Expand Down Expand Up @@ -80,6 +82,7 @@ export default function Home() {
};
}, []);

console.log(data);
return (
<div className={styles.container}>
<Head>
Expand Down
2 changes: 2 additions & 0 deletions packages/tsdk/fe-sdk-template/src/gen-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ let handler = (
return Promise.reject(new NoHandlerError(`Call \`setHandler\` first`));
};

export type Handler = typeof handler;

/**
* @example
* ```ts
Expand Down
19 changes: 12 additions & 7 deletions packages/tsdk/src/sync-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export async function syncAPI() {
}
`
}
import { Handler } from './gen-api';
`;
let dataHookImportStr = ``;
let dataHookBodyStr = isReactQuery
Expand Down Expand Up @@ -173,15 +174,16 @@ export async function syncAPI() {
* @category ${category}
*/
export function use${name}(
payload: ${name}Req | undefined,
payload?: ${name}Req,
options?: SWRConfiguration<${name}Res | undefined>,
requestConfig?: AxiosRequestConfig<${name}Req>
requestConfig?: AxiosRequestConfig<${name}Req>,
customHandler?: Handler,
) {
return useSWR(
() => ({ url: ${name}.config.path, arg: payload }),
({ arg }) => {
if (typeof arg === 'undefined') return undefined;
return ${name}(arg, requestConfig);
return ${name}(arg, requestConfig, customHandler);
},
options
);
Expand All @@ -201,11 +203,12 @@ return useSWR(
${name}Req
>,
requestConfig?: AxiosRequestConfig<${name}Req>,
customHandler?: Handler,
) {
return useSWRMutation(
${name}.config.path,
(url, { arg }: { arg: ${name}Req }) => {
return ${name}(arg, requestConfig);
return ${name}(arg, requestConfig, customHandler);
},
options
);
Expand All @@ -224,10 +227,11 @@ return useSWR(
* @category ${category}
*/
export function use${name}(
payload: ${name}Req | undefined,
payload?: ${name}Req,
options?: UndefinedInitialDataOptions<${name}Res | undefined, Error>,
queryClient?: QueryClient,
requestConfig?: AxiosRequestConfig<${name}Req>,
customHandler?: Handler,
) {
return useQuery(
{
Expand All @@ -237,7 +241,7 @@ return useSWR(
if (typeof payload === 'undefined') {
return undefined;
}
return ${name}(payload, requestConfig);
return ${name}(payload, requestConfig, customHandler);
},
},
queryClient || _queryClient
Expand All @@ -258,12 +262,13 @@ return useSWR(
>,
queryClient?: QueryClient,
requestConfig?: AxiosRequestConfig<${name}Req>,
customHandler?: Handler,
) {
return useMutation(
{
...(options || {}),
mutationFn(payload) {
return ${name}(payload, requestConfig);
return ${name}(payload, requestConfig, customHandler);
},
},
queryClient || _queryClient
Expand Down
4 changes: 2 additions & 2 deletions packages/tsdk/src/sync-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ async function reconfigPkg() {
: [config.entityLibName || 'typeorm']
)?.find((item) => item === 'kysely')
) {
pkgContent.dependencies.kysely = '^0.26.3';
pkgContent.dependencies.kysely = '^0.27.4';
}
const dataHookLib = config.dataHookLib?.toLowerCase();
if (dataHookLib === 'swr') {
pkgContent.dependencies.swr = '^2.2.5';
} else if (dataHookLib === 'reactquery') {
pkgContent.dependencies['@tanstack/react-query'] = '^5.22.2';
pkgContent.dependencies['@tanstack/react-query'] = '^5.56.2';
}

if (config.dependencies) {
Expand Down
40 changes: 17 additions & 23 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion website/pages/docs/quick-start/react-query.en-US.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Add `@tanstack/react-query` to **next-app/package.json**:
},
"dependencies": {
...
"@tanstack/react-query": "^5.8.4"
"@tanstack/react-query": "^5.56.2"
},
...
}
Expand Down
2 changes: 1 addition & 1 deletion website/pages/docs/quick-start/react-query.zh-CN.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { Callout } from 'nextra/components';
},
"dependencies": {
...
"@tanstack/react-query": "^5.8.4"
"@tanstack/react-query": "^5.56.2"
},
...
}
Expand Down

0 comments on commit 198b3f9

Please sign in to comment.