soda-tanstack-query 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -0
- package/dist/index.cjs +60 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/utils/createUseQuery.cjs +49 -0
- package/dist/utils/createUseQuery.d.ts +75 -0
- package/dist/utils/createUseQuery.js +15 -0
- package/package.json +52 -0
- package/src/index.ts +1 -0
- package/src/utils/createUseQuery.ts +171 -0
- package/tsconfig.json +16 -0
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# soda-tanstack-form
|
|
2
|
+
|
|
3
|
+
[](https://npmjs.com/package/soda-tanstack-form)
|
|
4
|
+
[](https://npmjs.com/package/soda-tanstack-form)
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
$ pnpm install
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
$ npm run dev
|
|
14
|
+
$ npm run build
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Options
|
|
18
|
+
|
|
19
|
+
TODO
|
|
20
|
+
|
|
21
|
+
## LICENSE
|
|
22
|
+
|
|
23
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_modules__ = {
|
|
3
|
+
"@/utils/createUseQuery": function(module) {
|
|
4
|
+
module.exports = require("./utils/createUseQuery.cjs");
|
|
5
|
+
}
|
|
6
|
+
};
|
|
7
|
+
var __webpack_module_cache__ = {};
|
|
8
|
+
function __webpack_require__(moduleId) {
|
|
9
|
+
var cachedModule = __webpack_module_cache__[moduleId];
|
|
10
|
+
if (void 0 !== cachedModule) return cachedModule.exports;
|
|
11
|
+
var module = __webpack_module_cache__[moduleId] = {
|
|
12
|
+
exports: {}
|
|
13
|
+
};
|
|
14
|
+
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
15
|
+
return module.exports;
|
|
16
|
+
}
|
|
17
|
+
(()=>{
|
|
18
|
+
__webpack_require__.n = (module)=>{
|
|
19
|
+
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
20
|
+
__webpack_require__.d(getter, {
|
|
21
|
+
a: getter
|
|
22
|
+
});
|
|
23
|
+
return getter;
|
|
24
|
+
};
|
|
25
|
+
})();
|
|
26
|
+
(()=>{
|
|
27
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
28
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
get: definition[key]
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
})();
|
|
34
|
+
(()=>{
|
|
35
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
36
|
+
})();
|
|
37
|
+
(()=>{
|
|
38
|
+
__webpack_require__.r = (exports1)=>{
|
|
39
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
40
|
+
value: 'Module'
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
43
|
+
value: true
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
})();
|
|
47
|
+
var __webpack_exports__ = {};
|
|
48
|
+
(()=>{
|
|
49
|
+
__webpack_require__.r(__webpack_exports__);
|
|
50
|
+
var _utils_createUseQuery__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("@/utils/createUseQuery");
|
|
51
|
+
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
|
52
|
+
for(var __WEBPACK_IMPORT_KEY__ in _utils_createUseQuery__WEBPACK_IMPORTED_MODULE_0__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
|
53
|
+
return _utils_createUseQuery__WEBPACK_IMPORTED_MODULE_0__[key];
|
|
54
|
+
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
|
55
|
+
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
|
56
|
+
})();
|
|
57
|
+
for(var __webpack_i__ in __webpack_exports__)exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
58
|
+
Object.defineProperty(exports, '__esModule', {
|
|
59
|
+
value: true
|
|
60
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./utils/createUseQuery";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./utils/createUseQuery.js";
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
createUseQuery: ()=>createUseQuery
|
|
28
|
+
});
|
|
29
|
+
const react_query_namespaceObject = require("@tanstack/react-query");
|
|
30
|
+
function createUseQuery({ queryFn, queryKey, ...rest }) {
|
|
31
|
+
return function(params, options, client) {
|
|
32
|
+
return (0, react_query_namespaceObject.useQuery)({
|
|
33
|
+
queryKey: [
|
|
34
|
+
queryKey,
|
|
35
|
+
params
|
|
36
|
+
],
|
|
37
|
+
queryFn: ()=>queryFn(params),
|
|
38
|
+
...rest,
|
|
39
|
+
...options
|
|
40
|
+
}, client);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
exports.createUseQuery = __webpack_exports__.createUseQuery;
|
|
44
|
+
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
45
|
+
"createUseQuery"
|
|
46
|
+
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
47
|
+
Object.defineProperty(exports, '__esModule', {
|
|
48
|
+
value: true
|
|
49
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { DefinedInitialDataOptions, DefinedUseQueryResult, QueryClient, UndefinedInitialDataOptions, UseQueryOptions, UseQueryResult } from "@tanstack/react-query";
|
|
2
|
+
/** 判断函数的参数是否为必需的 */
|
|
3
|
+
export type IsParamRequired<TFn extends (params: any) => any> = Parameters<TFn> extends [param: any] ? true : false;
|
|
4
|
+
/** 参数必须的 useQuery */
|
|
5
|
+
interface _UseQueryWithParams<TFn extends (params: any) => any, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> {
|
|
6
|
+
<TData = TResponse>(params: TParams): UseQueryResult<NoInfer<TData>, Error>;
|
|
7
|
+
<TData = TResponse>(params: TParams, options?: Omit<DefinedInitialDataOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">, client?: QueryClient): DefinedUseQueryResult<NoInfer<TData>, Error>;
|
|
8
|
+
<TData = TResponse>(params: TParams, options?: Omit<UndefinedInitialDataOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">, client?: QueryClient): UseQueryResult<NoInfer<TData>, Error>;
|
|
9
|
+
<TData = TResponse>(params: TParams, options?: Omit<UseQueryOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">, client?: QueryClient): UseQueryResult<NoInfer<TData>, Error>;
|
|
10
|
+
}
|
|
11
|
+
export interface UseQueryWithParams<TFn extends (params: any) => any> extends _UseQueryWithParams<TFn> {
|
|
12
|
+
}
|
|
13
|
+
/** 参数可选的 useQuery */
|
|
14
|
+
interface _UseQueryWithOptionalParams<TFn extends (params?: any) => any, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> {
|
|
15
|
+
<TData = TResponse>(params?: TParams): UseQueryResult<NoInfer<TData>, Error>;
|
|
16
|
+
<TData = TResponse>(params?: TParams, options?: Omit<DefinedInitialDataOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">, client?: QueryClient): DefinedUseQueryResult<NoInfer<TData>, Error>;
|
|
17
|
+
<TData = TResponse>(params?: TParams, options?: Omit<UndefinedInitialDataOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">, client?: QueryClient): UseQueryResult<NoInfer<TData>, Error>;
|
|
18
|
+
<TData = TResponse>(params?: TParams, options?: Omit<UseQueryOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">, client?: QueryClient): UseQueryResult<NoInfer<TData>, Error>;
|
|
19
|
+
}
|
|
20
|
+
export interface UseQueryWithOptionalParams<TFn extends (params?: any) => any> extends _UseQueryWithOptionalParams<TFn> {
|
|
21
|
+
}
|
|
22
|
+
/** 根据参数是否必需,选择不同的 useQuery */
|
|
23
|
+
export type UseQuery<TFn extends (param: any) => any> = IsParamRequired<TFn> extends true ? UseQueryWithParams<TFn> : UseQueryWithOptionalParams<TFn>;
|
|
24
|
+
/** 传递了初始化参数的 createUseQuery 参数 */
|
|
25
|
+
export interface _CreateUseQueryDefinedInitialDataParams<TFn extends (param: any) => any, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> extends Omit<DefinedInitialDataOptions<TResponse, Error, TResponse, [string, TParams]>, "queryKey" | "queryFn" | "select"> {
|
|
26
|
+
queryFn: TFn;
|
|
27
|
+
queryKey: string;
|
|
28
|
+
}
|
|
29
|
+
export interface CreateUseQueryDefinedInitialDataParams<TFn extends (param: any) => any> extends _CreateUseQueryDefinedInitialDataParams<TFn> {
|
|
30
|
+
}
|
|
31
|
+
/** 传递了初始化参数的参数必需的 useQuery */
|
|
32
|
+
interface _UseQueryDefinedInitialDataWithParams<TFn extends (params: any) => any, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> {
|
|
33
|
+
<TData = TResponse>(params: TParams, options?: Omit<DefinedInitialDataOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">, client?: QueryClient): DefinedUseQueryResult<NoInfer<TData>, Error>;
|
|
34
|
+
}
|
|
35
|
+
export interface UseQueryDefinedInitialDataWithParams<TFn extends (params: any) => any> extends _UseQueryDefinedInitialDataWithParams<TFn> {
|
|
36
|
+
}
|
|
37
|
+
/** 传递了初始化参数的参数可选的 useQuery */
|
|
38
|
+
interface _UseQueryDefinedInitialDataWithOptionalParams<TFn extends (params?: any) => any, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> {
|
|
39
|
+
<TData = TResponse>(params?: TParams, options?: Omit<DefinedInitialDataOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">, client?: QueryClient): DefinedUseQueryResult<NoInfer<TData>, Error>;
|
|
40
|
+
}
|
|
41
|
+
export interface UseQueryDefinedInitialDataWithOptionalParams<TFn extends (params?: any) => any> extends _UseQueryDefinedInitialDataWithOptionalParams<TFn> {
|
|
42
|
+
}
|
|
43
|
+
/** 根据参数是否必需,选择不同的 useQuery */
|
|
44
|
+
export type UseQueryDefinedInitialData<TFn extends (params?: any) => any> = IsParamRequired<TFn> extends true ? UseQueryDefinedInitialDataWithParams<TFn> : UseQueryDefinedInitialDataWithOptionalParams<TFn>;
|
|
45
|
+
/** 未传递初始化参数的 createUseQuery 参数 */
|
|
46
|
+
export interface _CreateUseQueryUndefinedInitialDataParams<TFn extends (param: any) => any, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> extends Omit<UndefinedInitialDataOptions<TResponse, Error, TResponse, [string, TParams]>, "queryKey" | "queryFn" | "select"> {
|
|
47
|
+
queryFn: TFn;
|
|
48
|
+
queryKey: string;
|
|
49
|
+
}
|
|
50
|
+
export interface CreateUseQueryUndefinedInitialDataParams<TFn extends (param: any) => any> extends _CreateUseQueryUndefinedInitialDataParams<TFn> {
|
|
51
|
+
}
|
|
52
|
+
/** 未传递初始化参数的参数必需的 useQuery */
|
|
53
|
+
interface _UseQueryUndefinedInitialDataWithParams<TFn extends (params: any) => any, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> {
|
|
54
|
+
<TData = TResponse>(params: TParams, options?: Omit<UndefinedInitialDataOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">, client?: QueryClient): DefinedUseQueryResult<NoInfer<TData>, Error>;
|
|
55
|
+
}
|
|
56
|
+
export interface UseQueryUndefinedInitialDataWithParams<TFn extends (params: any) => any> extends _UseQueryUndefinedInitialDataWithParams<TFn> {
|
|
57
|
+
}
|
|
58
|
+
/** 未传递初始化参数的参数可选的 useQuery */
|
|
59
|
+
interface _UseQueryUndefinedInitialDataWithOptionalParams<TFn extends (params: any) => any, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> {
|
|
60
|
+
<TData = TResponse>(params?: TParams, options?: Omit<UndefinedInitialDataOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">, client?: QueryClient): DefinedUseQueryResult<NoInfer<TData>, Error>;
|
|
61
|
+
}
|
|
62
|
+
export interface UseQueryUndefinedInitialDataWithOptionalParams<TFn extends (params?: any) => any> extends _UseQueryUndefinedInitialDataWithOptionalParams<TFn> {
|
|
63
|
+
}
|
|
64
|
+
/** 根据参数是否必需,选择不同的 useQuery */
|
|
65
|
+
export type UseQueryUndefinedInitialData<TFn extends (params?: any) => any> = IsParamRequired<TFn> extends true ? UseQueryUndefinedInitialDataWithParams<TFn> : UseQueryUndefinedInitialDataWithOptionalParams<TFn>;
|
|
66
|
+
export interface _CreateUseQueryParams<TFn extends (param: any) => any, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> extends Omit<UseQueryOptions<TResponse, Error, TResponse, [string, TParams]>, "queryKey" | "queryFn" | "select"> {
|
|
67
|
+
queryFn: TFn;
|
|
68
|
+
queryKey: string;
|
|
69
|
+
}
|
|
70
|
+
export interface CreateUseQueryParams<TFn extends (param: any) => any> extends _CreateUseQueryParams<TFn> {
|
|
71
|
+
}
|
|
72
|
+
export declare function createUseQuery<TFn extends (param: any) => any>(params: CreateUseQueryDefinedInitialDataParams<TFn>): UseQueryDefinedInitialData<TFn>;
|
|
73
|
+
export declare function createUseQuery<TFn extends (param: any) => any>(pararm: CreateUseQueryUndefinedInitialDataParams<TFn>): UseQueryUndefinedInitialData<TFn>;
|
|
74
|
+
export declare function createUseQuery<TFn extends (param: any) => any>(pararm: CreateUseQueryParams<TFn>): UseQuery<TFn>;
|
|
75
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useQuery } from "@tanstack/react-query";
|
|
2
|
+
function createUseQuery({ queryFn, queryKey, ...rest }) {
|
|
3
|
+
return function(params, options, client) {
|
|
4
|
+
return useQuery({
|
|
5
|
+
queryKey: [
|
|
6
|
+
queryKey,
|
|
7
|
+
params
|
|
8
|
+
],
|
|
9
|
+
queryFn: ()=>queryFn(params),
|
|
10
|
+
...rest,
|
|
11
|
+
...options
|
|
12
|
+
}, client);
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export { createUseQuery };
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "soda-tanstack-query",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"require": "./dist/index.cjs"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"main": "./dist/index.cjs",
|
|
14
|
+
"module": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"keywords": [],
|
|
18
|
+
"authors": [
|
|
19
|
+
"lurongv@qq.com"
|
|
20
|
+
],
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"files": [
|
|
23
|
+
"compiled",
|
|
24
|
+
"dist",
|
|
25
|
+
"src",
|
|
26
|
+
"tsconfig.json"
|
|
27
|
+
],
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public",
|
|
30
|
+
"registry": "https://registry.npmjs.com/"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/1adybug/deepsea.git"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/1adybug/deepsea/tree/main/packages/soda-tanstack-query",
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@tanstack/react-query": "^5.90.19"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"typescript": ">=5.8.3"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@types/react": ">=19.2.7",
|
|
45
|
+
"react": ">=19.2.3"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"dev": "rslib build --watch",
|
|
49
|
+
"build": "rslib build",
|
|
50
|
+
"prebuild": "bun scripts/export.ts"
|
|
51
|
+
}
|
|
52
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@/utils/createUseQuery"
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DefinedInitialDataOptions,
|
|
3
|
+
DefinedUseQueryResult,
|
|
4
|
+
QueryClient,
|
|
5
|
+
UndefinedInitialDataOptions,
|
|
6
|
+
useQuery,
|
|
7
|
+
UseQueryOptions,
|
|
8
|
+
UseQueryResult,
|
|
9
|
+
} from "@tanstack/react-query"
|
|
10
|
+
|
|
11
|
+
/** 判断函数的参数是否为必需的 */
|
|
12
|
+
export type IsParamRequired<TFn extends (params: any) => any> = Parameters<TFn> extends [param: any] ? true : false
|
|
13
|
+
|
|
14
|
+
// 要在第一个加一个重载,因为 options 是可选的,如果不加这个重载,类型会任务是 DefinedInitialDataOptions,造成返回类型必定不为 undefined
|
|
15
|
+
|
|
16
|
+
/** 参数必须的 useQuery */
|
|
17
|
+
interface _UseQueryWithParams<TFn extends (params: any) => any, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> {
|
|
18
|
+
<TData = TResponse>(params: TParams): UseQueryResult<NoInfer<TData>, Error>
|
|
19
|
+
<TData = TResponse>(
|
|
20
|
+
params: TParams,
|
|
21
|
+
options?: Omit<DefinedInitialDataOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">,
|
|
22
|
+
client?: QueryClient,
|
|
23
|
+
): DefinedUseQueryResult<NoInfer<TData>, Error>
|
|
24
|
+
<TData = TResponse>(
|
|
25
|
+
params: TParams,
|
|
26
|
+
options?: Omit<UndefinedInitialDataOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">,
|
|
27
|
+
client?: QueryClient,
|
|
28
|
+
): UseQueryResult<NoInfer<TData>, Error>
|
|
29
|
+
<TData = TResponse>(
|
|
30
|
+
params: TParams,
|
|
31
|
+
options?: Omit<UseQueryOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">,
|
|
32
|
+
client?: QueryClient,
|
|
33
|
+
): UseQueryResult<NoInfer<TData>, Error>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface UseQueryWithParams<TFn extends (params: any) => any> extends _UseQueryWithParams<TFn> {}
|
|
37
|
+
|
|
38
|
+
/** 参数可选的 useQuery */
|
|
39
|
+
interface _UseQueryWithOptionalParams<TFn extends (params?: any) => any, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> {
|
|
40
|
+
<TData = TResponse>(params?: TParams): UseQueryResult<NoInfer<TData>, Error>
|
|
41
|
+
<TData = TResponse>(
|
|
42
|
+
params?: TParams,
|
|
43
|
+
options?: Omit<DefinedInitialDataOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">,
|
|
44
|
+
client?: QueryClient,
|
|
45
|
+
): DefinedUseQueryResult<NoInfer<TData>, Error>
|
|
46
|
+
<TData = TResponse>(
|
|
47
|
+
params?: TParams,
|
|
48
|
+
options?: Omit<UndefinedInitialDataOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">,
|
|
49
|
+
client?: QueryClient,
|
|
50
|
+
): UseQueryResult<NoInfer<TData>, Error>
|
|
51
|
+
<TData = TResponse>(
|
|
52
|
+
params?: TParams,
|
|
53
|
+
options?: Omit<UseQueryOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">,
|
|
54
|
+
client?: QueryClient,
|
|
55
|
+
): UseQueryResult<NoInfer<TData>, Error>
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface UseQueryWithOptionalParams<TFn extends (params?: any) => any> extends _UseQueryWithOptionalParams<TFn> {}
|
|
59
|
+
|
|
60
|
+
/** 根据参数是否必需,选择不同的 useQuery */
|
|
61
|
+
export type UseQuery<TFn extends (param: any) => any> = IsParamRequired<TFn> extends true ? UseQueryWithParams<TFn> : UseQueryWithOptionalParams<TFn>
|
|
62
|
+
|
|
63
|
+
/** 传递了初始化参数的 createUseQuery 参数 */
|
|
64
|
+
export interface _CreateUseQueryDefinedInitialDataParams<
|
|
65
|
+
TFn extends (param: any) => any,
|
|
66
|
+
TParams = Parameters<TFn>[0],
|
|
67
|
+
TResponse = Awaited<ReturnType<TFn>>,
|
|
68
|
+
> extends Omit<DefinedInitialDataOptions<TResponse, Error, TResponse, [string, TParams]>, "queryKey" | "queryFn" | "select"> {
|
|
69
|
+
queryFn: TFn
|
|
70
|
+
queryKey: string
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface CreateUseQueryDefinedInitialDataParams<TFn extends (param: any) => any> extends _CreateUseQueryDefinedInitialDataParams<TFn> {}
|
|
74
|
+
|
|
75
|
+
/** 传递了初始化参数的参数必需的 useQuery */
|
|
76
|
+
interface _UseQueryDefinedInitialDataWithParams<TFn extends (params: any) => any, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> {
|
|
77
|
+
<TData = TResponse>(
|
|
78
|
+
params: TParams,
|
|
79
|
+
options?: Omit<DefinedInitialDataOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">,
|
|
80
|
+
client?: QueryClient,
|
|
81
|
+
): DefinedUseQueryResult<NoInfer<TData>, Error>
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface UseQueryDefinedInitialDataWithParams<TFn extends (params: any) => any> extends _UseQueryDefinedInitialDataWithParams<TFn> {}
|
|
85
|
+
|
|
86
|
+
/** 传递了初始化参数的参数可选的 useQuery */
|
|
87
|
+
interface _UseQueryDefinedInitialDataWithOptionalParams<TFn extends (params?: any) => any, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> {
|
|
88
|
+
<TData = TResponse>(
|
|
89
|
+
params?: TParams,
|
|
90
|
+
options?: Omit<DefinedInitialDataOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">,
|
|
91
|
+
client?: QueryClient,
|
|
92
|
+
): DefinedUseQueryResult<NoInfer<TData>, Error>
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface UseQueryDefinedInitialDataWithOptionalParams<TFn extends (params?: any) => any> extends _UseQueryDefinedInitialDataWithOptionalParams<TFn> {}
|
|
96
|
+
|
|
97
|
+
/** 根据参数是否必需,选择不同的 useQuery */
|
|
98
|
+
export type UseQueryDefinedInitialData<TFn extends (params?: any) => any> =
|
|
99
|
+
IsParamRequired<TFn> extends true ? UseQueryDefinedInitialDataWithParams<TFn> : UseQueryDefinedInitialDataWithOptionalParams<TFn>
|
|
100
|
+
|
|
101
|
+
/** 未传递初始化参数的 createUseQuery 参数 */
|
|
102
|
+
export interface _CreateUseQueryUndefinedInitialDataParams<
|
|
103
|
+
TFn extends (param: any) => any,
|
|
104
|
+
TParams = Parameters<TFn>[0],
|
|
105
|
+
TResponse = Awaited<ReturnType<TFn>>,
|
|
106
|
+
> extends Omit<UndefinedInitialDataOptions<TResponse, Error, TResponse, [string, TParams]>, "queryKey" | "queryFn" | "select"> {
|
|
107
|
+
queryFn: TFn
|
|
108
|
+
queryKey: string
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface CreateUseQueryUndefinedInitialDataParams<TFn extends (param: any) => any> extends _CreateUseQueryUndefinedInitialDataParams<TFn> {}
|
|
112
|
+
|
|
113
|
+
/** 未传递初始化参数的参数必需的 useQuery */
|
|
114
|
+
interface _UseQueryUndefinedInitialDataWithParams<TFn extends (params: any) => any, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> {
|
|
115
|
+
<TData = TResponse>(
|
|
116
|
+
params: TParams,
|
|
117
|
+
options?: Omit<UndefinedInitialDataOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">,
|
|
118
|
+
client?: QueryClient,
|
|
119
|
+
): DefinedUseQueryResult<NoInfer<TData>, Error>
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export interface UseQueryUndefinedInitialDataWithParams<TFn extends (params: any) => any> extends _UseQueryUndefinedInitialDataWithParams<TFn> {}
|
|
123
|
+
|
|
124
|
+
/** 未传递初始化参数的参数可选的 useQuery */
|
|
125
|
+
interface _UseQueryUndefinedInitialDataWithOptionalParams<
|
|
126
|
+
TFn extends (params: any) => any,
|
|
127
|
+
TParams = Parameters<TFn>[0],
|
|
128
|
+
TResponse = Awaited<ReturnType<TFn>>,
|
|
129
|
+
> {
|
|
130
|
+
<TData = TResponse>(
|
|
131
|
+
params?: TParams,
|
|
132
|
+
options?: Omit<UndefinedInitialDataOptions<TResponse, Error, TData, [string, TParams]>, "queryKey" | "queryFn">,
|
|
133
|
+
client?: QueryClient,
|
|
134
|
+
): DefinedUseQueryResult<NoInfer<TData>, Error>
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface UseQueryUndefinedInitialDataWithOptionalParams<
|
|
138
|
+
TFn extends (params?: any) => any,
|
|
139
|
+
> extends _UseQueryUndefinedInitialDataWithOptionalParams<TFn> {}
|
|
140
|
+
|
|
141
|
+
/** 根据参数是否必需,选择不同的 useQuery */
|
|
142
|
+
export type UseQueryUndefinedInitialData<TFn extends (params?: any) => any> =
|
|
143
|
+
IsParamRequired<TFn> extends true ? UseQueryUndefinedInitialDataWithParams<TFn> : UseQueryUndefinedInitialDataWithOptionalParams<TFn>
|
|
144
|
+
|
|
145
|
+
export interface _CreateUseQueryParams<TFn extends (param: any) => any, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> extends Omit<
|
|
146
|
+
UseQueryOptions<TResponse, Error, TResponse, [string, TParams]>,
|
|
147
|
+
"queryKey" | "queryFn" | "select"
|
|
148
|
+
> {
|
|
149
|
+
queryFn: TFn
|
|
150
|
+
queryKey: string
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export interface CreateUseQueryParams<TFn extends (param: any) => any> extends _CreateUseQueryParams<TFn> {}
|
|
154
|
+
|
|
155
|
+
// 根据是否传递初始化参数,选择不同的 createUseQuery,目前存在一个问题,UndefinedInitialDataOptions 和 UseQueryOptions 是完全一致的,所以匹配不到第三种可能,但这是 @tanstack/react-query 的问题
|
|
156
|
+
export function createUseQuery<TFn extends (param: any) => any>(params: CreateUseQueryDefinedInitialDataParams<TFn>): UseQueryDefinedInitialData<TFn>
|
|
157
|
+
export function createUseQuery<TFn extends (param: any) => any>(pararm: CreateUseQueryUndefinedInitialDataParams<TFn>): UseQueryUndefinedInitialData<TFn>
|
|
158
|
+
export function createUseQuery<TFn extends (param: any) => any>(pararm: CreateUseQueryParams<TFn>): UseQuery<TFn>
|
|
159
|
+
export function createUseQuery<TFn extends (param: any) => any>({ queryFn, queryKey, ...rest }: CreateUseQueryParams<TFn>): UseQuery<TFn> {
|
|
160
|
+
return function useRequest(params: any, options?: any, client?: QueryClient) {
|
|
161
|
+
return useQuery(
|
|
162
|
+
{
|
|
163
|
+
queryKey: [queryKey, params],
|
|
164
|
+
queryFn: () => queryFn(params),
|
|
165
|
+
...rest,
|
|
166
|
+
...options,
|
|
167
|
+
},
|
|
168
|
+
client,
|
|
169
|
+
)
|
|
170
|
+
}
|
|
171
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"baseUrl": ".",
|
|
4
|
+
"strict": true,
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"skipLibCheck": true,
|
|
7
|
+
"jsx": "react-jsx",
|
|
8
|
+
"target": "ESNext",
|
|
9
|
+
"module": "ESNext",
|
|
10
|
+
"moduleResolution": "Bundler",
|
|
11
|
+
"paths": {
|
|
12
|
+
"@/*": ["src/*"]
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"include": ["src"]
|
|
16
|
+
}
|