From 198b3f9047e43dde6c406fd66d4dc1076711ad1b Mon Sep 17 00:00:00 2001 From: suhaotian Date: Sun, 29 Sep 2024 22:01:58 +1000 Subject: [PATCH] feat: react hooks support customHandler --- examples/server/package.json | 2 +- .../server/packages/fe-sdk-demo/package.json | 4 +- examples/web/pages/xior/page.tsx | 3 ++ packages/tsdk/fe-sdk-template/src/gen-api.ts | 2 + packages/tsdk/src/sync-api.ts | 19 +++++---- packages/tsdk/src/sync-files.ts | 4 +- pnpm-lock.yaml | 40 ++++++++----------- .../docs/quick-start/react-query.en-US.mdx | 2 +- .../docs/quick-start/react-query.zh-CN.mdx | 2 +- 9 files changed, 41 insertions(+), 37 deletions(-) diff --git a/examples/server/package.json b/examples/server/package.json index 1dcb492..fa3a41f 100644 --- a/examples/server/package.json +++ b/examples/server/package.json @@ -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", diff --git a/examples/server/packages/fe-sdk-demo/package.json b/examples/server/packages/fe-sdk-demo/package.json index d7f855f..05dfdc6 100644 --- a/examples/server/packages/fe-sdk-demo/package.json +++ b/examples/server/packages/fe-sdk-demo/package.json @@ -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" }, diff --git a/examples/web/pages/xior/page.tsx b/examples/web/pages/xior/page.tsx index ee70fbc..a83714e 100644 --- a/examples/web/pages/xior/page.tsx +++ b/examples/web/pages/xior/page.tsx @@ -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 @@ -35,6 +36,7 @@ const apiURL = baseURL + `api/${apiType}`; export default function Home() { const [handlerName, setHanlderName] = useState(''); const [result, setResult] = useState(); + const { data } = useQueryTodo(handlerName ? {} : undefined); useEffect(() => { const io = SocketIO(socketURL, { @@ -80,6 +82,7 @@ export default function Home() { }; }, []); + console.log(data); return (
diff --git a/packages/tsdk/fe-sdk-template/src/gen-api.ts b/packages/tsdk/fe-sdk-template/src/gen-api.ts index 1840bb6..87a2b23 100644 --- a/packages/tsdk/fe-sdk-template/src/gen-api.ts +++ b/packages/tsdk/fe-sdk-template/src/gen-api.ts @@ -13,6 +13,8 @@ let handler = ( return Promise.reject(new NoHandlerError(`Call \`setHandler\` first`)); }; +export type Handler = typeof handler; + /** * @example * ```ts diff --git a/packages/tsdk/src/sync-api.ts b/packages/tsdk/src/sync-api.ts index f659f18..22249ac 100644 --- a/packages/tsdk/src/sync-api.ts +++ b/packages/tsdk/src/sync-api.ts @@ -59,6 +59,7 @@ export async function syncAPI() { } ` } + import { Handler } from './gen-api'; `; let dataHookImportStr = ``; let dataHookBodyStr = isReactQuery @@ -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 ); @@ -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 ); @@ -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( { @@ -237,7 +241,7 @@ return useSWR( if (typeof payload === 'undefined') { return undefined; } - return ${name}(payload, requestConfig); + return ${name}(payload, requestConfig, customHandler); }, }, queryClient || _queryClient @@ -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 diff --git a/packages/tsdk/src/sync-files.ts b/packages/tsdk/src/sync-files.ts index 35252c4..14aa508 100644 --- a/packages/tsdk/src/sync-files.ts +++ b/packages/tsdk/src/sync-files.ts @@ -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) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5cc0247..9a8db1c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -140,8 +140,8 @@ importers: specifier: ^2.0.1 version: 2.0.1 kysely: - specifier: ^0.27.2 - version: 0.27.3 + specifier: ^0.27.4 + version: 0.27.4 reflect-metadata: specifier: ^0.2.1 version: 0.2.2 @@ -195,8 +195,8 @@ importers: examples/server/packages/fe-sdk-demo: dependencies: '@tanstack/react-query': - specifier: ^5.22.2 - version: 5.35.1(react@18.2.0) + specifier: ^5.56.2 + version: 5.56.2(react@18.2.0) axios: specifier: ^1.7.7 version: 1.7.7 @@ -210,8 +210,8 @@ importers: specifier: ^2.0.1 version: 2.0.1 kysely: - specifier: ^0.26.3 - version: 0.26.3 + specifier: ^0.27.4 + version: 0.27.4 socket.io-client: specifier: ^4.8.0 version: 4.8.0(bufferutil@4.0.8)(utf-8-validate@6.0.3) @@ -1224,13 +1224,13 @@ packages: '@swc/helpers@0.5.2': resolution: {integrity: sha512-E4KcWTpoLHqwPHLxidpOqQbcrZVgi0rsmmZXUle1jXmJfuIf/UWpczUJ7MZZ5tlxytgJXyp0w4PGkkeLiuIdZw==} - '@tanstack/query-core@5.35.1': - resolution: {integrity: sha512-0Dnpybqb8+ps6WgqBnqFEC+1F/xLvUosRAq+wiGisTgolOZzqZfkE2995dEXmhuzINiTM7/a6xSGznU0NIvBkw==} + '@tanstack/query-core@5.56.2': + resolution: {integrity: sha512-gor0RI3/R5rVV3gXfddh1MM+hgl0Z4G7tj6Xxpq6p2I03NGPaJ8dITY9Gz05zYYb/EJq9vPas/T4wn9EaDPd4Q==} - '@tanstack/react-query@5.35.1': - resolution: {integrity: sha512-i2T7m2ffQdNqlX3pO+uMsnQ0H4a59Ens2GxtlMsRiOvdSB4SfYmHb27MnvFV8rGmtWRaa4gPli0/rpDoSS5LbQ==} + '@tanstack/react-query@5.56.2': + resolution: {integrity: sha512-SR0GzHVo6yzhN72pnRhkEFRAHMsUo5ZPzAxfTMvUxFIDVS6W9LYUp6nXW3fcHVdg0ZJl8opSH85jqahvm6DSVg==} peerDependencies: - react: ^18.0.0 + react: ^18 || ^19 '@tanstack/react-virtual@3.5.0': resolution: {integrity: sha512-rtvo7KwuIvqK9zb0VZ5IL7fiJAEnG+0EiFZz8FUOs+2mhGqdGmjKIaT1XU7Zq0eFqL0jonLlhbayJI/J2SA/Bw==} @@ -3642,12 +3642,8 @@ packages: resolution: {integrity: sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ==} engines: {node: '>=6'} - kysely@0.26.3: - resolution: {integrity: sha512-yWSgGi9bY13b/W06DD2OCDDHQmq1kwTGYlQ4wpZkMOJqMGCstVCFIvxCCVG4KfY1/3G0MhDAcZsip/Lw8/vJWw==} - engines: {node: '>=14.0.0'} - - kysely@0.27.3: - resolution: {integrity: sha512-lG03Ru+XyOJFsjH3OMY6R/9U38IjDPfnOfDgO3ynhbDr+Dz8fak+X6L62vqu3iybQnj+lG84OttBuU9KY3L9kA==} + kysely@0.27.4: + resolution: {integrity: sha512-dyNKv2KRvYOQPLCAOCjjQuCk4YFd33BvGdf/o5bC7FiW+BB6snA81Zt+2wT9QDFzKqxKa5rrOmvlK/anehCcgA==} engines: {node: '>=14.0.0'} layout-base@1.0.2: @@ -6651,11 +6647,11 @@ snapshots: dependencies: tslib: 2.6.2 - '@tanstack/query-core@5.35.1': {} + '@tanstack/query-core@5.56.2': {} - '@tanstack/react-query@5.35.1(react@18.2.0)': + '@tanstack/react-query@5.56.2(react@18.2.0)': dependencies: - '@tanstack/query-core': 5.35.1 + '@tanstack/query-core': 5.56.2 react: 18.2.0 '@tanstack/react-virtual@3.5.0(react-dom@18.2.0(react@18.2.0))(react@18.2.0)': @@ -9609,9 +9605,7 @@ snapshots: kleur@4.1.5: {} - kysely@0.26.3: {} - - kysely@0.27.3: {} + kysely@0.27.4: {} layout-base@1.0.2: {} diff --git a/website/pages/docs/quick-start/react-query.en-US.mdx b/website/pages/docs/quick-start/react-query.en-US.mdx index 09a4d67..c9ef75f 100644 --- a/website/pages/docs/quick-start/react-query.en-US.mdx +++ b/website/pages/docs/quick-start/react-query.en-US.mdx @@ -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" }, ... } diff --git a/website/pages/docs/quick-start/react-query.zh-CN.mdx b/website/pages/docs/quick-start/react-query.zh-CN.mdx index f587f4b..c89d72a 100644 --- a/website/pages/docs/quick-start/react-query.zh-CN.mdx +++ b/website/pages/docs/quick-start/react-query.zh-CN.mdx @@ -26,7 +26,7 @@ import { Callout } from 'nextra/components'; }, "dependencies": { ... - "@tanstack/react-query": "^5.8.4" + "@tanstack/react-query": "^5.56.2" }, ... }