soda-tanstack-query 0.0.10 → 0.0.12
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/dist/index.cjs +12 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/utils/createUseMutation.cjs +76 -0
- package/dist/utils/createUseMutation.d.ts +7 -0
- package/dist/utils/createUseMutation.js +39 -0
- package/dist/utils/createUseQuery.d.ts +9 -9
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/utils/createUseMutation.ts +78 -0
- package/src/utils/createUseQuery.ts +9 -9
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __webpack_modules__ = {
|
|
3
|
+
"@/utils/createUseMutation": function(module) {
|
|
4
|
+
module.exports = require("./utils/createUseMutation.cjs");
|
|
5
|
+
},
|
|
3
6
|
"@/utils/createUseQuery": function(module) {
|
|
4
7
|
module.exports = require("./utils/createUseQuery.cjs");
|
|
5
8
|
}
|
|
@@ -47,10 +50,16 @@ function __webpack_require__(moduleId) {
|
|
|
47
50
|
var __webpack_exports__ = {};
|
|
48
51
|
(()=>{
|
|
49
52
|
__webpack_require__.r(__webpack_exports__);
|
|
50
|
-
var
|
|
53
|
+
var _utils_createUseMutation__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("@/utils/createUseMutation");
|
|
51
54
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
|
52
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
|
53
|
-
return
|
|
55
|
+
for(var __WEBPACK_IMPORT_KEY__ in _utils_createUseMutation__WEBPACK_IMPORTED_MODULE_0__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
|
56
|
+
return _utils_createUseMutation__WEBPACK_IMPORTED_MODULE_0__[key];
|
|
57
|
+
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
|
58
|
+
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
|
59
|
+
var _utils_createUseQuery__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("@/utils/createUseQuery");
|
|
60
|
+
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
|
61
|
+
for(var __WEBPACK_IMPORT_KEY__ in _utils_createUseQuery__WEBPACK_IMPORTED_MODULE_1__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
|
62
|
+
return _utils_createUseQuery__WEBPACK_IMPORTED_MODULE_1__[key];
|
|
54
63
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
|
55
64
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
|
56
65
|
})();
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,76 @@
|
|
|
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
|
+
withUseMutationDefaults: ()=>withUseMutationDefaults,
|
|
28
|
+
createUseMutation: ()=>createUseMutation
|
|
29
|
+
});
|
|
30
|
+
const react_query_namespaceObject = require("@tanstack/react-query");
|
|
31
|
+
function createUseMutation(baseOptions, baseQueryClient) {
|
|
32
|
+
return function({ onMutate: overrideOnMutate, onSuccess: overrideOnSuccess, onError: overrideOnError, onSettled: overrideOnSettled, ...overrideOptions } = {}, overrideQueryClient) {
|
|
33
|
+
const { mutationFn, onMutate: baseOnMutate, onSuccess: baseOnSuccess, onError: baseOnError, onSettled: baseOnSettled, ...baseOptionsRest } = "function" == typeof baseOptions ? baseOptions() : baseOptions;
|
|
34
|
+
return (0, react_query_namespaceObject.useMutation)({
|
|
35
|
+
mutationFn,
|
|
36
|
+
async onMutate (variables, context) {
|
|
37
|
+
await (null == baseOnMutate ? void 0 : baseOnMutate(variables, context));
|
|
38
|
+
return null == overrideOnMutate ? void 0 : overrideOnMutate(variables, context);
|
|
39
|
+
},
|
|
40
|
+
async onSuccess (data, variables, onMutateResult, context) {
|
|
41
|
+
await (null == baseOnSuccess ? void 0 : baseOnSuccess(data, variables, onMutateResult, context));
|
|
42
|
+
return null == overrideOnSuccess ? void 0 : overrideOnSuccess(data, variables, onMutateResult, context);
|
|
43
|
+
},
|
|
44
|
+
async onError (error, variables, onMutateResult, context) {
|
|
45
|
+
await (null == baseOnError ? void 0 : baseOnError(error, variables, onMutateResult, context));
|
|
46
|
+
return null == overrideOnError ? void 0 : overrideOnError(error, variables, onMutateResult, context);
|
|
47
|
+
},
|
|
48
|
+
async onSettled (data, error, variables, onMutateResult, context) {
|
|
49
|
+
await (null == baseOnSettled ? void 0 : baseOnSettled(data, error, variables, onMutateResult, context));
|
|
50
|
+
return null == overrideOnSettled ? void 0 : overrideOnSettled(data, error, variables, onMutateResult, context);
|
|
51
|
+
},
|
|
52
|
+
...baseOptionsRest,
|
|
53
|
+
...overrideOptions
|
|
54
|
+
}, overrideQueryClient ?? baseQueryClient);
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function withUseMutationDefaults(defaultOptions) {
|
|
58
|
+
return function(mutationFn, queryClient) {
|
|
59
|
+
return createUseMutation(()=>{
|
|
60
|
+
const resolvedDefaultOptions = "function" == typeof defaultOptions ? defaultOptions() : defaultOptions;
|
|
61
|
+
return {
|
|
62
|
+
mutationFn,
|
|
63
|
+
...resolvedDefaultOptions
|
|
64
|
+
};
|
|
65
|
+
}, queryClient);
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
exports.createUseMutation = __webpack_exports__.createUseMutation;
|
|
69
|
+
exports.withUseMutationDefaults = __webpack_exports__.withUseMutationDefaults;
|
|
70
|
+
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
71
|
+
"createUseMutation",
|
|
72
|
+
"withUseMutationDefaults"
|
|
73
|
+
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
74
|
+
Object.defineProperty(exports, '__esModule', {
|
|
75
|
+
value: true
|
|
76
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { DefaultError, QueryClient, UseMutationOptions, UseMutationResult } from "@tanstack/react-query";
|
|
2
|
+
type MutationFn = (param: any) => any;
|
|
3
|
+
export type CreateUseMutationOptions<TMutationFn extends MutationFn> = UseMutationOptions<Awaited<ReturnType<TMutationFn>>, unknown, Parameters<TMutationFn>[0], unknown>;
|
|
4
|
+
export type UseConfiguredMutation<TMutationFn extends MutationFn> = <TError = DefaultError, TOnMutateResult = unknown>(options?: Omit<UseMutationOptions<Awaited<ReturnType<TMutationFn>>, TError, Parameters<TMutationFn>[0], TOnMutateResult>, "mutationFn">, queryClient?: QueryClient) => UseMutationResult<Awaited<ReturnType<TMutationFn>>, TError, Parameters<TMutationFn>[0], TOnMutateResult>;
|
|
5
|
+
export declare function createUseMutation<TMutationFn extends MutationFn>(baseOptions: CreateUseMutationOptions<TMutationFn> | (() => CreateUseMutationOptions<TMutationFn>), baseQueryClient?: QueryClient): <TError = DefaultError, TOnMutateResult = unknown>({ onMutate: overrideOnMutate, onSuccess: overrideOnSuccess, onError: overrideOnError, onSettled: overrideOnSettled, ...overrideOptions }?: Omit<UseMutationOptions<Awaited<ReturnType<TMutationFn>>, TError, Parameters<TMutationFn>[0], TOnMutateResult>, "mutationFn">, overrideQueryClient?: QueryClient) => UseMutationResult<Awaited<ReturnType<TMutationFn>>, TError, Parameters<TMutationFn>[0], TOnMutateResult>;
|
|
6
|
+
export declare function withUseMutationDefaults<TMutationFn extends MutationFn>(defaultOptions: Omit<CreateUseMutationOptions<TMutationFn>, "mutationFn"> | (() => Omit<CreateUseMutationOptions<TMutationFn>, "mutationFn">)): (mutationFn: TMutationFn, queryClient?: QueryClient) => <TError = Error, TOnMutateResult = unknown>({ onMutate: overrideOnMutate, onSuccess: overrideOnSuccess, onError: overrideOnError, onSettled: overrideOnSettled, ...overrideOptions }?: Omit<UseMutationOptions<Awaited<ReturnType<TMutationFn>>, TError, Parameters<TMutationFn>[0], TOnMutateResult>, "mutationFn">, overrideQueryClient?: QueryClient) => UseMutationResult<Awaited<ReturnType<TMutationFn>>, TError, Parameters<TMutationFn>[0], TOnMutateResult>;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { useMutation } from "@tanstack/react-query";
|
|
2
|
+
function createUseMutation(baseOptions, baseQueryClient) {
|
|
3
|
+
return function({ onMutate: overrideOnMutate, onSuccess: overrideOnSuccess, onError: overrideOnError, onSettled: overrideOnSettled, ...overrideOptions } = {}, overrideQueryClient) {
|
|
4
|
+
const { mutationFn, onMutate: baseOnMutate, onSuccess: baseOnSuccess, onError: baseOnError, onSettled: baseOnSettled, ...baseOptionsRest } = "function" == typeof baseOptions ? baseOptions() : baseOptions;
|
|
5
|
+
return useMutation({
|
|
6
|
+
mutationFn,
|
|
7
|
+
async onMutate (variables, context) {
|
|
8
|
+
await (null == baseOnMutate ? void 0 : baseOnMutate(variables, context));
|
|
9
|
+
return null == overrideOnMutate ? void 0 : overrideOnMutate(variables, context);
|
|
10
|
+
},
|
|
11
|
+
async onSuccess (data, variables, onMutateResult, context) {
|
|
12
|
+
await (null == baseOnSuccess ? void 0 : baseOnSuccess(data, variables, onMutateResult, context));
|
|
13
|
+
return null == overrideOnSuccess ? void 0 : overrideOnSuccess(data, variables, onMutateResult, context);
|
|
14
|
+
},
|
|
15
|
+
async onError (error, variables, onMutateResult, context) {
|
|
16
|
+
await (null == baseOnError ? void 0 : baseOnError(error, variables, onMutateResult, context));
|
|
17
|
+
return null == overrideOnError ? void 0 : overrideOnError(error, variables, onMutateResult, context);
|
|
18
|
+
},
|
|
19
|
+
async onSettled (data, error, variables, onMutateResult, context) {
|
|
20
|
+
await (null == baseOnSettled ? void 0 : baseOnSettled(data, error, variables, onMutateResult, context));
|
|
21
|
+
return null == overrideOnSettled ? void 0 : overrideOnSettled(data, error, variables, onMutateResult, context);
|
|
22
|
+
},
|
|
23
|
+
...baseOptionsRest,
|
|
24
|
+
...overrideOptions
|
|
25
|
+
}, overrideQueryClient ?? baseQueryClient);
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function withUseMutationDefaults(defaultOptions) {
|
|
29
|
+
return function(mutationFn, queryClient) {
|
|
30
|
+
return createUseMutation(()=>{
|
|
31
|
+
const resolvedDefaultOptions = "function" == typeof defaultOptions ? defaultOptions() : defaultOptions;
|
|
32
|
+
return {
|
|
33
|
+
mutationFn,
|
|
34
|
+
...resolvedDefaultOptions
|
|
35
|
+
};
|
|
36
|
+
}, queryClient);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export { createUseMutation, withUseMutationDefaults };
|
|
@@ -21,9 +21,9 @@ export interface UseQueryWithOptionalParams<TFn extends (params?: any) => any, T
|
|
|
21
21
|
/** 根据参数是否必需,选择不同的 useQuery */
|
|
22
22
|
type _UseQuery<TFn extends (param: any) => any, TInitSelectData = Awaited<ReturnType<TFn>>> = IsParamRequired<TFn> extends true ? UseQueryWithParams<TFn, TInitSelectData> : UseQueryWithOptionalParams<TFn, TInitSelectData>;
|
|
23
23
|
export type UseQuery<TFn extends (param: any) => any, TInitSelectData = Awaited<ReturnType<TFn>>> = _UseQuery<TFn, TInitSelectData> & {
|
|
24
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryDefinedInitialDataParams<TFn,
|
|
25
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryUndefinedInitialDataParams<TFn,
|
|
26
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryParams<TFn,
|
|
24
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryDefinedInitialDataParams<TFn, unknown>, "queryKey" | "queryFn">>(params: TRefineParams | ((params: CreateUseQueryParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams), client?: QueryClient | ((params: CreateUseQueryParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined)): GetHookFromParams<Assign<CreateUseQueryParams<TFn, TInitSelectData>, TRefineParams>>;
|
|
25
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryUndefinedInitialDataParams<TFn, unknown>, "queryKey" | "queryFn">>(params: TRefineParams | ((params: CreateUseQueryParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams), client?: QueryClient | ((params: CreateUseQueryParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined)): GetHookFromParams<Assign<CreateUseQueryParams<TFn, TInitSelectData>, TRefineParams>>;
|
|
26
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryParams<TFn, unknown>, "queryKey" | "queryFn">>(params: TRefineParams | ((params: CreateUseQueryParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams), client?: QueryClient | ((params: CreateUseQueryParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined)): GetHookFromParams<Assign<CreateUseQueryParams<TFn, TInitSelectData>, TRefineParams>>;
|
|
27
27
|
};
|
|
28
28
|
/** 传递了初始化参数的 createUseQuery 参数 */
|
|
29
29
|
interface _CreateUseQueryDefinedInitialDataParams<TFn extends (param: any) => any, TInitSelectData = Awaited<ReturnType<TFn>>, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> extends Omit<DefinedInitialDataOptions<TResponse, Error, TInitSelectData, [string, TParams]>, "queryKey" | "queryFn"> {
|
|
@@ -47,9 +47,9 @@ export interface UseQueryDefinedInitialDataWithOptionalParams<TFn extends (param
|
|
|
47
47
|
/** 根据参数是否必需,选择不同的 useQuery */
|
|
48
48
|
export type _UseQueryDefinedInitialData<TFn extends (params?: any) => any, TInitSelectData = Awaited<ReturnType<TFn>>> = IsParamRequired<TFn> extends true ? UseQueryDefinedInitialDataWithParams<TFn, TInitSelectData> : UseQueryDefinedInitialDataWithOptionalParams<TFn, TInitSelectData>;
|
|
49
49
|
export type UseQueryDefinedInitialData<TFn extends (params?: any) => any, TInitSelectData = Awaited<ReturnType<TFn>>> = _UseQueryDefinedInitialData<TFn, TInitSelectData> & {
|
|
50
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryDefinedInitialDataParams<TFn,
|
|
51
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryUndefinedInitialDataParams<TFn,
|
|
52
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryParams<TFn,
|
|
50
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryDefinedInitialDataParams<TFn, unknown>, "queryKey" | "queryFn">>(params: TRefineParams | ((params: CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams), client?: QueryClient | ((params: CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined)): GetHookFromParams<Assign<CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, TRefineParams>>;
|
|
51
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryUndefinedInitialDataParams<TFn, unknown>, "queryKey" | "queryFn">>(params: TRefineParams | ((params: CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams), client?: QueryClient | ((params: CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined)): GetHookFromParams<Assign<CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, TRefineParams>>;
|
|
52
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryParams<TFn, unknown>, "queryKey" | "queryFn">>(params: TRefineParams | ((params: CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams), client?: QueryClient | ((params: CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined)): GetHookFromParams<Assign<CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, TRefineParams>>;
|
|
53
53
|
};
|
|
54
54
|
/** 未传递初始化参数的 createUseQuery 参数 */
|
|
55
55
|
interface _CreateUseQueryUndefinedInitialDataParams<TFn extends (param: any) => any, TInitSelectData = Awaited<ReturnType<TFn>>, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> extends Omit<UndefinedInitialDataOptions<TResponse, Error, TInitSelectData, [string, TParams]>, "queryKey" | "queryFn"> {
|
|
@@ -73,9 +73,9 @@ export interface UseQueryUndefinedInitialDataWithOptionalParams<TFn extends (par
|
|
|
73
73
|
/** 根据参数是否必需,选择不同的 useQuery */
|
|
74
74
|
type _UseQueryUndefinedInitialData<TFn extends (params?: any) => any, TInitSelectData = Awaited<ReturnType<TFn>>> = IsParamRequired<TFn> extends true ? UseQueryUndefinedInitialDataWithParams<TFn, TInitSelectData> : UseQueryUndefinedInitialDataWithOptionalParams<TFn, TInitSelectData>;
|
|
75
75
|
export type UseQueryUndefinedInitialData<TFn extends (params?: any) => any, TInitSelectData = Awaited<ReturnType<TFn>>> = _UseQueryUndefinedInitialData<TFn, TInitSelectData> & {
|
|
76
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryDefinedInitialDataParams<TFn,
|
|
77
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryUndefinedInitialDataParams<TFn,
|
|
78
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryParams<TFn,
|
|
76
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryDefinedInitialDataParams<TFn, unknown>, "queryKey" | "queryFn">>(params: TRefineParams | ((params: CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams), client?: QueryClient | ((params: CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined)): GetHookFromParams<Assign<CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, TRefineParams>>;
|
|
77
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryUndefinedInitialDataParams<TFn, unknown>, "queryKey" | "queryFn">>(params: TRefineParams | ((params: CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams), client?: QueryClient | ((params: CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined)): GetHookFromParams<Assign<CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, TRefineParams>>;
|
|
78
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryParams<TFn, unknown>, "queryKey" | "queryFn">>(params: TRefineParams | ((params: CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams), client?: QueryClient | ((params: CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined)): GetHookFromParams<Assign<CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, TRefineParams>>;
|
|
79
79
|
};
|
|
80
80
|
interface _CreateUseQueryParams<TFn extends (param: any) => any, TInitSelectData = Awaited<ReturnType<TFn>>, TParams = Parameters<TFn>[0], TResponse = Awaited<ReturnType<TFn>>> extends Omit<UseQueryOptions<TResponse, Error, TInitSelectData, [string, TParams]>, "queryKey" | "queryFn"> {
|
|
81
81
|
queryFn: TFn;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { DefaultError, QueryClient, useMutation, UseMutationOptions, UseMutationResult } from "@tanstack/react-query"
|
|
2
|
+
|
|
3
|
+
type MutationFn = (param: any) => any
|
|
4
|
+
|
|
5
|
+
export type CreateUseMutationOptions<TMutationFn extends MutationFn> = UseMutationOptions<
|
|
6
|
+
Awaited<ReturnType<TMutationFn>>,
|
|
7
|
+
unknown,
|
|
8
|
+
Parameters<TMutationFn>[0],
|
|
9
|
+
unknown
|
|
10
|
+
>
|
|
11
|
+
|
|
12
|
+
export type UseConfiguredMutation<TMutationFn extends MutationFn> = <TError = DefaultError, TOnMutateResult = unknown>(
|
|
13
|
+
options?: Omit<UseMutationOptions<Awaited<ReturnType<TMutationFn>>, TError, Parameters<TMutationFn>[0], TOnMutateResult>, "mutationFn">,
|
|
14
|
+
queryClient?: QueryClient,
|
|
15
|
+
) => UseMutationResult<Awaited<ReturnType<TMutationFn>>, TError, Parameters<TMutationFn>[0], TOnMutateResult>
|
|
16
|
+
|
|
17
|
+
export function createUseMutation<TMutationFn extends MutationFn>(
|
|
18
|
+
baseOptions: CreateUseMutationOptions<TMutationFn> | (() => CreateUseMutationOptions<TMutationFn>),
|
|
19
|
+
baseQueryClient?: QueryClient,
|
|
20
|
+
) {
|
|
21
|
+
return function useConfiguredMutation<TError = DefaultError, TOnMutateResult = unknown>(
|
|
22
|
+
{
|
|
23
|
+
onMutate: overrideOnMutate,
|
|
24
|
+
onSuccess: overrideOnSuccess,
|
|
25
|
+
onError: overrideOnError,
|
|
26
|
+
onSettled: overrideOnSettled,
|
|
27
|
+
...overrideOptions
|
|
28
|
+
}: Omit<UseMutationOptions<Awaited<ReturnType<TMutationFn>>, TError, Parameters<TMutationFn>[0], TOnMutateResult>, "mutationFn"> = {},
|
|
29
|
+
overrideQueryClient?: QueryClient,
|
|
30
|
+
) {
|
|
31
|
+
const {
|
|
32
|
+
mutationFn,
|
|
33
|
+
onMutate: baseOnMutate,
|
|
34
|
+
onSuccess: baseOnSuccess,
|
|
35
|
+
onError: baseOnError,
|
|
36
|
+
onSettled: baseOnSettled,
|
|
37
|
+
...baseOptionsRest
|
|
38
|
+
} = typeof baseOptions === "function" ? baseOptions() : baseOptions
|
|
39
|
+
return useMutation<Awaited<ReturnType<TMutationFn>>, TError, Parameters<TMutationFn>[0], TOnMutateResult>(
|
|
40
|
+
{
|
|
41
|
+
mutationFn,
|
|
42
|
+
async onMutate(variables, context) {
|
|
43
|
+
await baseOnMutate?.(variables, context)
|
|
44
|
+
return overrideOnMutate?.(variables, context) as TOnMutateResult
|
|
45
|
+
},
|
|
46
|
+
async onSuccess(data, variables, onMutateResult, context) {
|
|
47
|
+
await baseOnSuccess?.(data, variables, onMutateResult, context)
|
|
48
|
+
return overrideOnSuccess?.(data, variables, onMutateResult, context)
|
|
49
|
+
},
|
|
50
|
+
async onError(error, variables, onMutateResult, context) {
|
|
51
|
+
await baseOnError?.(error, variables, onMutateResult, context)
|
|
52
|
+
return overrideOnError?.(error, variables, onMutateResult, context)
|
|
53
|
+
},
|
|
54
|
+
async onSettled(data, error, variables, onMutateResult, context) {
|
|
55
|
+
await baseOnSettled?.(data, error, variables, onMutateResult, context)
|
|
56
|
+
return overrideOnSettled?.(data, error, variables, onMutateResult, context)
|
|
57
|
+
},
|
|
58
|
+
...baseOptionsRest,
|
|
59
|
+
...overrideOptions,
|
|
60
|
+
},
|
|
61
|
+
overrideQueryClient ?? baseQueryClient,
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function withUseMutationDefaults<TMutationFn extends MutationFn>(
|
|
67
|
+
defaultOptions: Omit<CreateUseMutationOptions<TMutationFn>, "mutationFn"> | (() => Omit<CreateUseMutationOptions<TMutationFn>, "mutationFn">),
|
|
68
|
+
) {
|
|
69
|
+
return function bindMutation(mutationFn: TMutationFn, queryClient?: QueryClient) {
|
|
70
|
+
return createUseMutation(() => {
|
|
71
|
+
const resolvedDefaultOptions = typeof defaultOptions === "function" ? defaultOptions() : defaultOptions
|
|
72
|
+
return {
|
|
73
|
+
mutationFn,
|
|
74
|
+
...resolvedDefaultOptions,
|
|
75
|
+
}
|
|
76
|
+
}, queryClient)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -76,15 +76,15 @@ type _UseQuery<TFn extends (param: any) => any, TInitSelectData = Awaited<Return
|
|
|
76
76
|
IsParamRequired<TFn> extends true ? UseQueryWithParams<TFn, TInitSelectData> : UseQueryWithOptionalParams<TFn, TInitSelectData>
|
|
77
77
|
|
|
78
78
|
export type UseQuery<TFn extends (param: any) => any, TInitSelectData = Awaited<ReturnType<TFn>>> = _UseQuery<TFn, TInitSelectData> & {
|
|
79
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryDefinedInitialDataParams<TFn,
|
|
79
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryDefinedInitialDataParams<TFn, unknown>, "queryKey" | "queryFn">>(
|
|
80
80
|
params: TRefineParams | ((params: CreateUseQueryParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams),
|
|
81
81
|
client?: QueryClient | ((params: CreateUseQueryParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined),
|
|
82
82
|
): GetHookFromParams<Assign<CreateUseQueryParams<TFn, TInitSelectData>, TRefineParams>>
|
|
83
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryUndefinedInitialDataParams<TFn,
|
|
83
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryUndefinedInitialDataParams<TFn, unknown>, "queryKey" | "queryFn">>(
|
|
84
84
|
params: TRefineParams | ((params: CreateUseQueryParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams),
|
|
85
85
|
client?: QueryClient | ((params: CreateUseQueryParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined),
|
|
86
86
|
): GetHookFromParams<Assign<CreateUseQueryParams<TFn, TInitSelectData>, TRefineParams>>
|
|
87
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryParams<TFn,
|
|
87
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryParams<TFn, unknown>, "queryKey" | "queryFn">>(
|
|
88
88
|
params: TRefineParams | ((params: CreateUseQueryParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams),
|
|
89
89
|
client?: QueryClient | ((params: CreateUseQueryParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined),
|
|
90
90
|
): GetHookFromParams<Assign<CreateUseQueryParams<TFn, TInitSelectData>, TRefineParams>>
|
|
@@ -154,15 +154,15 @@ export type UseQueryDefinedInitialData<TFn extends (params?: any) => any, TInitS
|
|
|
154
154
|
TFn,
|
|
155
155
|
TInitSelectData
|
|
156
156
|
> & {
|
|
157
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryDefinedInitialDataParams<TFn,
|
|
157
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryDefinedInitialDataParams<TFn, unknown>, "queryKey" | "queryFn">>(
|
|
158
158
|
params: TRefineParams | ((params: CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams),
|
|
159
159
|
client?: QueryClient | ((params: CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined),
|
|
160
160
|
): GetHookFromParams<Assign<CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, TRefineParams>>
|
|
161
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryUndefinedInitialDataParams<TFn,
|
|
161
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryUndefinedInitialDataParams<TFn, unknown>, "queryKey" | "queryFn">>(
|
|
162
162
|
params: TRefineParams | ((params: CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams),
|
|
163
163
|
client?: QueryClient | ((params: CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined),
|
|
164
164
|
): GetHookFromParams<Assign<CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, TRefineParams>>
|
|
165
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryParams<TFn,
|
|
165
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryParams<TFn, unknown>, "queryKey" | "queryFn">>(
|
|
166
166
|
params: TRefineParams | ((params: CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams),
|
|
167
167
|
client?: QueryClient | ((params: CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined),
|
|
168
168
|
): GetHookFromParams<Assign<CreateUseQueryDefinedInitialDataParams<TFn, TInitSelectData>, TRefineParams>>
|
|
@@ -232,15 +232,15 @@ export type UseQueryUndefinedInitialData<TFn extends (params?: any) => any, TIni
|
|
|
232
232
|
TFn,
|
|
233
233
|
TInitSelectData
|
|
234
234
|
> & {
|
|
235
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryDefinedInitialDataParams<TFn,
|
|
235
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryDefinedInitialDataParams<TFn, unknown>, "queryKey" | "queryFn">>(
|
|
236
236
|
params: TRefineParams | ((params: CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams),
|
|
237
237
|
client?: QueryClient | ((params: CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined),
|
|
238
238
|
): GetHookFromParams<Assign<CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, TRefineParams>>
|
|
239
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryUndefinedInitialDataParams<TFn,
|
|
239
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryUndefinedInitialDataParams<TFn, unknown>, "queryKey" | "queryFn">>(
|
|
240
240
|
params: TRefineParams | ((params: CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams),
|
|
241
241
|
client?: QueryClient | ((params: CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined),
|
|
242
242
|
): GetHookFromParams<Assign<CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, TRefineParams>>
|
|
243
|
-
refine<TRefineParams extends StrictPartial<CreateUseQueryParams<TFn,
|
|
243
|
+
refine<TRefineParams extends StrictPartial<CreateUseQueryParams<TFn, unknown>, "queryKey" | "queryFn">>(
|
|
244
244
|
params: TRefineParams | ((params: CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => TRefineParams),
|
|
245
245
|
client?: QueryClient | ((params: CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, client?: QueryClient) => QueryClient | undefined),
|
|
246
246
|
): GetHookFromParams<Assign<CreateUseQueryUndefinedInitialDataParams<TFn, TInitSelectData>, TRefineParams>>
|