@pixotope/query 0.6.0 → 0.7.0
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/.turbo/turbo-build.log +10 -10
- package/.turbo/turbo-check-types.log +1 -1
- package/.turbo/turbo-test.log +9 -7
- package/CHANGELOG.md +7 -0
- package/dist/index.cjs +61 -66
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +35 -77
- package/dist/index.d.ts +35 -77
- package/dist/index.js +61 -66
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/factories.test.ts +65 -0
- package/src/factories.ts +48 -31
- package/src/proxyBinders.ts +3 -1
- package/src/useMutation.test.ts +106 -0
- package/src/useMutation.ts +87 -116
- package/src/useQuery.test.ts +77 -139
- package/src/useQuery.ts +5 -8
- package/src/mock/useQuery.ts +0 -48
package/dist/index.d.cts
CHANGED
|
@@ -31,80 +31,34 @@ type UseQueryResult<TData = unknown, TError = unknown, TParams = void, TSelectDa
|
|
|
31
31
|
} & (Stateless extends true ? {} : WithData$1<TSelectData>);
|
|
32
32
|
declare const useQuery: <TData = unknown, TError = unknown, TParams = void, TSelected = TData, Stateless extends true | undefined = undefined>({ queryFn, ...options }: QueryOptions<TData, TError, TParams, TSelected, Stateless>) => UseQueryResult<TSelected, TError, TParams, TSelected, Stateless>;
|
|
33
33
|
|
|
34
|
-
type MutationFunction<TData = unknown, TParams = unknown> = (params: TParams,
|
|
35
|
-
interface MutationOptions<TData = unknown, TError = unknown, TParams = void> {
|
|
36
|
-
mutationFn: MutationFunction<TData, TParams>;
|
|
34
|
+
type MutationFunction<TData = unknown, TParams = unknown, TMetadata = unknown> = (params: TParams, meta?: TMetadata) => Promise<TData>;
|
|
35
|
+
interface MutationOptions<TData = unknown, TError = unknown, TParams = void, TMetadata = unknown> {
|
|
36
|
+
mutationFn: MutationFunction<TData, TParams, TMetadata>;
|
|
37
37
|
params?: TParams;
|
|
38
|
-
onSuccess?: (data: TData, variables: TParams) => Promise<unknown> | unknown;
|
|
39
|
-
onError?: (error: TMutationError<TError>, variables: TParams) => Promise<unknown> | unknown;
|
|
40
|
-
onSettled?: (data: TData | undefined, error: TMutationError<TError> | null, variables: TParams) => Promise<unknown> | unknown;
|
|
38
|
+
onSuccess?: (data: TData, variables: TParams, meta?: TMetadata) => Promise<unknown> | unknown;
|
|
39
|
+
onError?: (error: TMutationError<TError>, variables: TParams, meta?: TMetadata) => Promise<unknown> | unknown;
|
|
40
|
+
onSettled?: (data: TData | undefined, error: TMutationError<TError> | null, variables: TParams, meta?: TMetadata) => Promise<unknown> | unknown;
|
|
41
41
|
compareOrThrow?: (result: TData | TError) => boolean;
|
|
42
42
|
timeout?: number;
|
|
43
|
-
prepareParams?: (variables: TParams, defaults?: TParams) => TParams;
|
|
43
|
+
prepareParams?: (variables: TParams, defaults?: TParams, meta?: TMetadata) => TParams;
|
|
44
44
|
}
|
|
45
45
|
type TMutationError<TError> = TError | Error | null;
|
|
46
|
-
interface MutateOptions<TData = unknown, TError = unknown, TParams = void> {
|
|
47
|
-
onSuccess?: (data: TData, variables: TParams) => void;
|
|
48
|
-
onError?: (error: TMutationError<TError>, variables: TParams) => void;
|
|
49
|
-
onSettled?: (data: TData | undefined, error: TMutationError<TError> | null, variables: TParams) => void;
|
|
46
|
+
interface MutateOptions<TData = unknown, TError = unknown, TParams = void, TMetadata = unknown> {
|
|
47
|
+
onSuccess?: (data: TData, variables: TParams, meta?: TMetadata) => void;
|
|
48
|
+
onError?: (error: TMutationError<TError>, variables: TParams, meta?: TMetadata) => void;
|
|
49
|
+
onSettled?: (data: TData | undefined, error: TMutationError<TError> | null, variables: TParams, meta?: TMetadata) => void;
|
|
50
50
|
}
|
|
51
51
|
type MutationStatus = "idle" | "loading" | "success" | "error";
|
|
52
52
|
declare class ComparisonFailError<TPayload = unknown> extends Error {
|
|
53
53
|
readonly payload: TPayload;
|
|
54
54
|
constructor(message: string, payload: TPayload);
|
|
55
55
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
* providing extensive API to tap into the status and results of the mutation.
|
|
59
|
-
* @param mutationFn The mutation function to be called when the `mutate` function is called.
|
|
60
|
-
* `useMutation` does not care about what the function does, all it needs it a function that returns
|
|
61
|
-
* a promise.
|
|
62
|
-
* @param options Options to configure the mutation.
|
|
63
|
-
* @returns An object with the following properties:
|
|
64
|
-
* - `mutateAsync`: A function that takes in the variables and returns a promise that resolves to the
|
|
65
|
-
* result of the mutation. This function can be awaited to get the result of the mutation. Callbacks can
|
|
66
|
-
* be passed to this function to be called when the mutation is successful or when it fails. It's good idea
|
|
67
|
-
* to wrap this function in a try/catch block.
|
|
68
|
-
* - `mutate`: A function that takes in the variables and returns nothing. This function does not
|
|
69
|
-
* return a promise. Callbacks can be passed to this function to be called when the mutation is
|
|
70
|
-
* successful or when it fails.
|
|
71
|
-
* - `status`: The status of the mutation. Can be one of `idle`, `loading`, `success` or `error`.
|
|
72
|
-
* - `result`: The result of the mutation. This is undefined until the mutation is successful.
|
|
73
|
-
* - `reset`: A function that resets the mutation to the initial state.
|
|
74
|
-
* - `error`: The error object if the mutation has failed.
|
|
75
|
-
* @example
|
|
76
|
-
* ```typescript
|
|
77
|
-
* const mutationRes = useMutation({
|
|
78
|
-
* mutationFn: (variables) => {
|
|
79
|
-
* return ZMQHelper.callSomethingAsync(variables)
|
|
80
|
-
* },
|
|
81
|
-
* onSuccess: (data) => {
|
|
82
|
-
* // do something with the result
|
|
83
|
-
* },
|
|
84
|
-
* onError: (error) => {
|
|
85
|
-
* // do something with the error
|
|
86
|
-
* },
|
|
87
|
-
* onSettled: (data, error) => {
|
|
88
|
-
* // do something when the mutation is done
|
|
89
|
-
* }});
|
|
90
|
-
*
|
|
91
|
-
* // call the mutation
|
|
92
|
-
* const res = await mutationRes.mutateAsync({ foo: "bar" });
|
|
93
|
-
* // OR
|
|
94
|
-
* mutationRes.mutate({ foo: "bar" }, {
|
|
95
|
-
* onSuccess: (data) => {
|
|
96
|
-
* // do something with the result
|
|
97
|
-
* // for example show a toast message
|
|
98
|
-
* // stop local loading state
|
|
99
|
-
* });
|
|
100
|
-
* ```
|
|
101
|
-
*/
|
|
102
|
-
declare const useMutation: <TData = unknown, TError = unknown, TParams = void>(options: MutationOptions<TData, TError, TParams>) => {
|
|
103
|
-
mutateAsync: (params: TParams, overrideTargetMachine?: string) => Promise<TData>;
|
|
56
|
+
declare const useMutation: <TData = unknown, TError = unknown, TParams = void, TMetadata = unknown>(options: MutationOptions<TData, TError, TParams, TMetadata>) => {
|
|
57
|
+
mutateAsync: (params: TParams, meta?: TMetadata) => Promise<TData>;
|
|
104
58
|
status: MutationStatus;
|
|
105
59
|
result: TData | undefined;
|
|
106
|
-
mutate: (variables: TParams, options?: MutateOptions<TData, TError, TParams> & {
|
|
107
|
-
|
|
60
|
+
mutate: (variables: TParams, options?: MutateOptions<TData, TError, TParams, TMetadata> & {
|
|
61
|
+
meta?: TMetadata;
|
|
108
62
|
}) => void;
|
|
109
63
|
reset: () => void;
|
|
110
64
|
error: TMutationError<TError>;
|
|
@@ -116,36 +70,38 @@ type MutationInfo<TData, TError, TParams> = {
|
|
|
116
70
|
params: TParams;
|
|
117
71
|
};
|
|
118
72
|
type FnToParamsBase = Record<string, MutationInfo<unknown, unknown, unknown>>;
|
|
119
|
-
type TPromiseFunction<TResponse, TParams> = (args: Omit<ZMQClient.Call<TResponse, {
|
|
73
|
+
type TPromiseFunction<TResponse, TParams, TExtraParams> = (args: Omit<ZMQClient.Call<TResponse, {
|
|
120
74
|
functionName: string;
|
|
121
75
|
parameters: TParams;
|
|
122
76
|
options?: Omit<TSocketServerCallOptions, "toCallIfNoResponse">;
|
|
123
|
-
}>, "id">
|
|
124
|
-
|
|
125
|
-
|
|
77
|
+
}>, "id"> & {
|
|
78
|
+
meta?: TExtraParams;
|
|
79
|
+
}) => Promise<TResponse>;
|
|
80
|
+
declare const useCallMutationBase: <TFnToParams extends FnToParamsBase, TData = unknown, TError = unknown, TParams = unknown, TMetadata = unknown>(promiseFunc: TPromiseFunction<TData, TParams, TMetadata>, functionName: keyof TFnToParams, service: (meta?: TMetadata) => string, options?: Omit<MutationOptions<TData, TError, TParams, TMetadata>, "mutationFn">) => {
|
|
81
|
+
mutateAsync: (params: TParams, meta?: TMetadata | undefined) => Promise<TData>;
|
|
126
82
|
status: "idle" | "loading" | "success" | "error";
|
|
127
83
|
result: TData | undefined;
|
|
128
|
-
mutate: (variables: TParams, options?: (MutateOptions<TData, TError, TParams> & {
|
|
129
|
-
|
|
84
|
+
mutate: (variables: TParams, options?: (MutateOptions<TData, TError, TParams, TMetadata> & {
|
|
85
|
+
meta?: TMetadata | undefined;
|
|
130
86
|
}) | undefined) => void;
|
|
131
87
|
reset: () => void;
|
|
132
88
|
error: Error | TError | null;
|
|
133
89
|
};
|
|
134
|
-
declare function buildMutationHook<TFnToParams extends FnToParamsBase>(promiseFunc: TPromiseFunction<unknown, unknown>, genTargetService: (
|
|
135
|
-
|
|
136
|
-
}) => {
|
|
137
|
-
mutateAsync: (params: TFnToParams[TFn]["params"], overrideTargetMachine?: string) => Promise<TFnToParams[TFn]["data"]>;
|
|
90
|
+
declare function buildMutationHook<TFnToParams extends FnToParamsBase, TMetadata = unknown>(promiseFunc: TPromiseFunction<unknown, unknown, unknown>, genTargetService: (meta?: TMetadata) => string): <TFn extends keyof TFnToParams>(func: TFn, options?: Parameters<typeof useCallMutationBase<TFnToParams, TFnToParams[TFn]["data"], TFnToParams[TFn]["error"], TFnToParams[TFn]["params"], TMetadata>>[3]) => {
|
|
91
|
+
mutateAsync: (params: TFnToParams[TFn]["params"], meta?: TMetadata | undefined) => Promise<TFnToParams[TFn]["data"]>;
|
|
138
92
|
status: "idle" | "loading" | "success" | "error";
|
|
139
93
|
result: TFnToParams[TFn]["data"] | undefined;
|
|
140
|
-
mutate: (variables: TFnToParams[TFn]["params"], options?: (MutateOptions<TFnToParams[TFn]["data"], TFnToParams[TFn]["error"], TFnToParams[TFn]["params"]> & {
|
|
141
|
-
|
|
94
|
+
mutate: (variables: TFnToParams[TFn]["params"], options?: (MutateOptions<TFnToParams[TFn]["data"], TFnToParams[TFn]["error"], TFnToParams[TFn]["params"], TMetadata> & {
|
|
95
|
+
meta?: TMetadata | undefined;
|
|
142
96
|
}) | undefined) => void;
|
|
143
97
|
reset: () => void;
|
|
144
98
|
error: Error | TFnToParams[TFn]["error"] | null;
|
|
145
99
|
};
|
|
146
|
-
declare const useCallQueryBase: <TFnToParams extends FnToParamsBase, TData = unknown, TError = unknown, TParams = unknown, TSelect = TData>(promiseFunction: TPromiseFunction<TData, TParams>, functionName: keyof TFnToParams, params: TParams, service: string, options?: Omit<QueryOptions<TData, TError, TParams, TSelect>, "queryFn">
|
|
147
|
-
|
|
148
|
-
|
|
100
|
+
declare const useCallQueryBase: <TFnToParams extends FnToParamsBase, TData = unknown, TError = unknown, TParams = unknown, TSelect = TData, TMetadata = unknown>(promiseFunction: TPromiseFunction<TData, TParams, TMetadata>, functionName: keyof TFnToParams, params: TParams, service: (meta?: TMetadata) => string, options?: Omit<QueryOptions<TData, TError, TParams, TSelect>, "queryFn"> & {
|
|
101
|
+
meta?: TMetadata;
|
|
102
|
+
}) => UseQueryResult<TSelect, TError, TParams, TSelect, undefined>;
|
|
103
|
+
declare function buildQueryHook<TFnToParams extends FnToParamsBase, TMetadata = unknown>(promiseFunction: TPromiseFunction<unknown, unknown, unknown>, getTargetService: (meta?: TMetadata) => string): <TFn extends keyof TFnToParams, TSelect = TFnToParams[TFn]["data"]>(func: TFn, params: TFnToParams[TFn]["params"], options?: Parameters<typeof useCallQueryBase<TFnToParams, TFnToParams[TFn]["data"], TFnToParams[TFn]["error"], TFnToParams[TFn]["params"], TSelect, TMetadata>>[4] & {
|
|
104
|
+
meta?: TMetadata;
|
|
149
105
|
}) => UseQueryResult<TSelect, TFnToParams[TFn]["error"], TFnToParams[TFn]["params"], TSelect, undefined>;
|
|
150
106
|
|
|
151
107
|
/**
|
|
@@ -256,6 +212,8 @@ declare function bindPromiseFunction(clientSocketProxy: ZMQProxyWSClient): (args
|
|
|
256
212
|
functionName: string;
|
|
257
213
|
parameters: unknown;
|
|
258
214
|
options?: Omit<_pixotope_zmq_client.TSocketServerCallOptions, "toCallIfNoResponse">;
|
|
259
|
-
}>, "id">
|
|
215
|
+
}>, "id"> & {
|
|
216
|
+
meta?: unknown;
|
|
217
|
+
}) => Promise<unknown>;
|
|
260
218
|
|
|
261
219
|
export { ComparisonFailError, type Connection, type MessageHandler, type MutateOptions, type MutationFunction, type MutationInfo, type MutationOptions, type QueryOptions, type SubscriptionCoreOptions, type SubscriptionCoreReturnType, type SubscriptionOptions, type TPromiseFunction, type TSubscriptionHandlerFunction, type UseQueryResult, bindClientSocketProxy, bindPromiseFunction, buildMutationHook, buildQueryHook, buildServiceSubscriptionHook, useMutation, useQuery, useSubscription, useSubscriptionCore };
|
package/dist/index.d.ts
CHANGED
|
@@ -31,80 +31,34 @@ type UseQueryResult<TData = unknown, TError = unknown, TParams = void, TSelectDa
|
|
|
31
31
|
} & (Stateless extends true ? {} : WithData$1<TSelectData>);
|
|
32
32
|
declare const useQuery: <TData = unknown, TError = unknown, TParams = void, TSelected = TData, Stateless extends true | undefined = undefined>({ queryFn, ...options }: QueryOptions<TData, TError, TParams, TSelected, Stateless>) => UseQueryResult<TSelected, TError, TParams, TSelected, Stateless>;
|
|
33
33
|
|
|
34
|
-
type MutationFunction<TData = unknown, TParams = unknown> = (params: TParams,
|
|
35
|
-
interface MutationOptions<TData = unknown, TError = unknown, TParams = void> {
|
|
36
|
-
mutationFn: MutationFunction<TData, TParams>;
|
|
34
|
+
type MutationFunction<TData = unknown, TParams = unknown, TMetadata = unknown> = (params: TParams, meta?: TMetadata) => Promise<TData>;
|
|
35
|
+
interface MutationOptions<TData = unknown, TError = unknown, TParams = void, TMetadata = unknown> {
|
|
36
|
+
mutationFn: MutationFunction<TData, TParams, TMetadata>;
|
|
37
37
|
params?: TParams;
|
|
38
|
-
onSuccess?: (data: TData, variables: TParams) => Promise<unknown> | unknown;
|
|
39
|
-
onError?: (error: TMutationError<TError>, variables: TParams) => Promise<unknown> | unknown;
|
|
40
|
-
onSettled?: (data: TData | undefined, error: TMutationError<TError> | null, variables: TParams) => Promise<unknown> | unknown;
|
|
38
|
+
onSuccess?: (data: TData, variables: TParams, meta?: TMetadata) => Promise<unknown> | unknown;
|
|
39
|
+
onError?: (error: TMutationError<TError>, variables: TParams, meta?: TMetadata) => Promise<unknown> | unknown;
|
|
40
|
+
onSettled?: (data: TData | undefined, error: TMutationError<TError> | null, variables: TParams, meta?: TMetadata) => Promise<unknown> | unknown;
|
|
41
41
|
compareOrThrow?: (result: TData | TError) => boolean;
|
|
42
42
|
timeout?: number;
|
|
43
|
-
prepareParams?: (variables: TParams, defaults?: TParams) => TParams;
|
|
43
|
+
prepareParams?: (variables: TParams, defaults?: TParams, meta?: TMetadata) => TParams;
|
|
44
44
|
}
|
|
45
45
|
type TMutationError<TError> = TError | Error | null;
|
|
46
|
-
interface MutateOptions<TData = unknown, TError = unknown, TParams = void> {
|
|
47
|
-
onSuccess?: (data: TData, variables: TParams) => void;
|
|
48
|
-
onError?: (error: TMutationError<TError>, variables: TParams) => void;
|
|
49
|
-
onSettled?: (data: TData | undefined, error: TMutationError<TError> | null, variables: TParams) => void;
|
|
46
|
+
interface MutateOptions<TData = unknown, TError = unknown, TParams = void, TMetadata = unknown> {
|
|
47
|
+
onSuccess?: (data: TData, variables: TParams, meta?: TMetadata) => void;
|
|
48
|
+
onError?: (error: TMutationError<TError>, variables: TParams, meta?: TMetadata) => void;
|
|
49
|
+
onSettled?: (data: TData | undefined, error: TMutationError<TError> | null, variables: TParams, meta?: TMetadata) => void;
|
|
50
50
|
}
|
|
51
51
|
type MutationStatus = "idle" | "loading" | "success" | "error";
|
|
52
52
|
declare class ComparisonFailError<TPayload = unknown> extends Error {
|
|
53
53
|
readonly payload: TPayload;
|
|
54
54
|
constructor(message: string, payload: TPayload);
|
|
55
55
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
* providing extensive API to tap into the status and results of the mutation.
|
|
59
|
-
* @param mutationFn The mutation function to be called when the `mutate` function is called.
|
|
60
|
-
* `useMutation` does not care about what the function does, all it needs it a function that returns
|
|
61
|
-
* a promise.
|
|
62
|
-
* @param options Options to configure the mutation.
|
|
63
|
-
* @returns An object with the following properties:
|
|
64
|
-
* - `mutateAsync`: A function that takes in the variables and returns a promise that resolves to the
|
|
65
|
-
* result of the mutation. This function can be awaited to get the result of the mutation. Callbacks can
|
|
66
|
-
* be passed to this function to be called when the mutation is successful or when it fails. It's good idea
|
|
67
|
-
* to wrap this function in a try/catch block.
|
|
68
|
-
* - `mutate`: A function that takes in the variables and returns nothing. This function does not
|
|
69
|
-
* return a promise. Callbacks can be passed to this function to be called when the mutation is
|
|
70
|
-
* successful or when it fails.
|
|
71
|
-
* - `status`: The status of the mutation. Can be one of `idle`, `loading`, `success` or `error`.
|
|
72
|
-
* - `result`: The result of the mutation. This is undefined until the mutation is successful.
|
|
73
|
-
* - `reset`: A function that resets the mutation to the initial state.
|
|
74
|
-
* - `error`: The error object if the mutation has failed.
|
|
75
|
-
* @example
|
|
76
|
-
* ```typescript
|
|
77
|
-
* const mutationRes = useMutation({
|
|
78
|
-
* mutationFn: (variables) => {
|
|
79
|
-
* return ZMQHelper.callSomethingAsync(variables)
|
|
80
|
-
* },
|
|
81
|
-
* onSuccess: (data) => {
|
|
82
|
-
* // do something with the result
|
|
83
|
-
* },
|
|
84
|
-
* onError: (error) => {
|
|
85
|
-
* // do something with the error
|
|
86
|
-
* },
|
|
87
|
-
* onSettled: (data, error) => {
|
|
88
|
-
* // do something when the mutation is done
|
|
89
|
-
* }});
|
|
90
|
-
*
|
|
91
|
-
* // call the mutation
|
|
92
|
-
* const res = await mutationRes.mutateAsync({ foo: "bar" });
|
|
93
|
-
* // OR
|
|
94
|
-
* mutationRes.mutate({ foo: "bar" }, {
|
|
95
|
-
* onSuccess: (data) => {
|
|
96
|
-
* // do something with the result
|
|
97
|
-
* // for example show a toast message
|
|
98
|
-
* // stop local loading state
|
|
99
|
-
* });
|
|
100
|
-
* ```
|
|
101
|
-
*/
|
|
102
|
-
declare const useMutation: <TData = unknown, TError = unknown, TParams = void>(options: MutationOptions<TData, TError, TParams>) => {
|
|
103
|
-
mutateAsync: (params: TParams, overrideTargetMachine?: string) => Promise<TData>;
|
|
56
|
+
declare const useMutation: <TData = unknown, TError = unknown, TParams = void, TMetadata = unknown>(options: MutationOptions<TData, TError, TParams, TMetadata>) => {
|
|
57
|
+
mutateAsync: (params: TParams, meta?: TMetadata) => Promise<TData>;
|
|
104
58
|
status: MutationStatus;
|
|
105
59
|
result: TData | undefined;
|
|
106
|
-
mutate: (variables: TParams, options?: MutateOptions<TData, TError, TParams> & {
|
|
107
|
-
|
|
60
|
+
mutate: (variables: TParams, options?: MutateOptions<TData, TError, TParams, TMetadata> & {
|
|
61
|
+
meta?: TMetadata;
|
|
108
62
|
}) => void;
|
|
109
63
|
reset: () => void;
|
|
110
64
|
error: TMutationError<TError>;
|
|
@@ -116,36 +70,38 @@ type MutationInfo<TData, TError, TParams> = {
|
|
|
116
70
|
params: TParams;
|
|
117
71
|
};
|
|
118
72
|
type FnToParamsBase = Record<string, MutationInfo<unknown, unknown, unknown>>;
|
|
119
|
-
type TPromiseFunction<TResponse, TParams> = (args: Omit<ZMQClient.Call<TResponse, {
|
|
73
|
+
type TPromiseFunction<TResponse, TParams, TExtraParams> = (args: Omit<ZMQClient.Call<TResponse, {
|
|
120
74
|
functionName: string;
|
|
121
75
|
parameters: TParams;
|
|
122
76
|
options?: Omit<TSocketServerCallOptions, "toCallIfNoResponse">;
|
|
123
|
-
}>, "id">
|
|
124
|
-
|
|
125
|
-
|
|
77
|
+
}>, "id"> & {
|
|
78
|
+
meta?: TExtraParams;
|
|
79
|
+
}) => Promise<TResponse>;
|
|
80
|
+
declare const useCallMutationBase: <TFnToParams extends FnToParamsBase, TData = unknown, TError = unknown, TParams = unknown, TMetadata = unknown>(promiseFunc: TPromiseFunction<TData, TParams, TMetadata>, functionName: keyof TFnToParams, service: (meta?: TMetadata) => string, options?: Omit<MutationOptions<TData, TError, TParams, TMetadata>, "mutationFn">) => {
|
|
81
|
+
mutateAsync: (params: TParams, meta?: TMetadata | undefined) => Promise<TData>;
|
|
126
82
|
status: "idle" | "loading" | "success" | "error";
|
|
127
83
|
result: TData | undefined;
|
|
128
|
-
mutate: (variables: TParams, options?: (MutateOptions<TData, TError, TParams> & {
|
|
129
|
-
|
|
84
|
+
mutate: (variables: TParams, options?: (MutateOptions<TData, TError, TParams, TMetadata> & {
|
|
85
|
+
meta?: TMetadata | undefined;
|
|
130
86
|
}) | undefined) => void;
|
|
131
87
|
reset: () => void;
|
|
132
88
|
error: Error | TError | null;
|
|
133
89
|
};
|
|
134
|
-
declare function buildMutationHook<TFnToParams extends FnToParamsBase>(promiseFunc: TPromiseFunction<unknown, unknown>, genTargetService: (
|
|
135
|
-
|
|
136
|
-
}) => {
|
|
137
|
-
mutateAsync: (params: TFnToParams[TFn]["params"], overrideTargetMachine?: string) => Promise<TFnToParams[TFn]["data"]>;
|
|
90
|
+
declare function buildMutationHook<TFnToParams extends FnToParamsBase, TMetadata = unknown>(promiseFunc: TPromiseFunction<unknown, unknown, unknown>, genTargetService: (meta?: TMetadata) => string): <TFn extends keyof TFnToParams>(func: TFn, options?: Parameters<typeof useCallMutationBase<TFnToParams, TFnToParams[TFn]["data"], TFnToParams[TFn]["error"], TFnToParams[TFn]["params"], TMetadata>>[3]) => {
|
|
91
|
+
mutateAsync: (params: TFnToParams[TFn]["params"], meta?: TMetadata | undefined) => Promise<TFnToParams[TFn]["data"]>;
|
|
138
92
|
status: "idle" | "loading" | "success" | "error";
|
|
139
93
|
result: TFnToParams[TFn]["data"] | undefined;
|
|
140
|
-
mutate: (variables: TFnToParams[TFn]["params"], options?: (MutateOptions<TFnToParams[TFn]["data"], TFnToParams[TFn]["error"], TFnToParams[TFn]["params"]> & {
|
|
141
|
-
|
|
94
|
+
mutate: (variables: TFnToParams[TFn]["params"], options?: (MutateOptions<TFnToParams[TFn]["data"], TFnToParams[TFn]["error"], TFnToParams[TFn]["params"], TMetadata> & {
|
|
95
|
+
meta?: TMetadata | undefined;
|
|
142
96
|
}) | undefined) => void;
|
|
143
97
|
reset: () => void;
|
|
144
98
|
error: Error | TFnToParams[TFn]["error"] | null;
|
|
145
99
|
};
|
|
146
|
-
declare const useCallQueryBase: <TFnToParams extends FnToParamsBase, TData = unknown, TError = unknown, TParams = unknown, TSelect = TData>(promiseFunction: TPromiseFunction<TData, TParams>, functionName: keyof TFnToParams, params: TParams, service: string, options?: Omit<QueryOptions<TData, TError, TParams, TSelect>, "queryFn">
|
|
147
|
-
|
|
148
|
-
|
|
100
|
+
declare const useCallQueryBase: <TFnToParams extends FnToParamsBase, TData = unknown, TError = unknown, TParams = unknown, TSelect = TData, TMetadata = unknown>(promiseFunction: TPromiseFunction<TData, TParams, TMetadata>, functionName: keyof TFnToParams, params: TParams, service: (meta?: TMetadata) => string, options?: Omit<QueryOptions<TData, TError, TParams, TSelect>, "queryFn"> & {
|
|
101
|
+
meta?: TMetadata;
|
|
102
|
+
}) => UseQueryResult<TSelect, TError, TParams, TSelect, undefined>;
|
|
103
|
+
declare function buildQueryHook<TFnToParams extends FnToParamsBase, TMetadata = unknown>(promiseFunction: TPromiseFunction<unknown, unknown, unknown>, getTargetService: (meta?: TMetadata) => string): <TFn extends keyof TFnToParams, TSelect = TFnToParams[TFn]["data"]>(func: TFn, params: TFnToParams[TFn]["params"], options?: Parameters<typeof useCallQueryBase<TFnToParams, TFnToParams[TFn]["data"], TFnToParams[TFn]["error"], TFnToParams[TFn]["params"], TSelect, TMetadata>>[4] & {
|
|
104
|
+
meta?: TMetadata;
|
|
149
105
|
}) => UseQueryResult<TSelect, TFnToParams[TFn]["error"], TFnToParams[TFn]["params"], TSelect, undefined>;
|
|
150
106
|
|
|
151
107
|
/**
|
|
@@ -256,6 +212,8 @@ declare function bindPromiseFunction(clientSocketProxy: ZMQProxyWSClient): (args
|
|
|
256
212
|
functionName: string;
|
|
257
213
|
parameters: unknown;
|
|
258
214
|
options?: Omit<_pixotope_zmq_client.TSocketServerCallOptions, "toCallIfNoResponse">;
|
|
259
|
-
}>, "id">
|
|
215
|
+
}>, "id"> & {
|
|
216
|
+
meta?: unknown;
|
|
217
|
+
}) => Promise<unknown>;
|
|
260
218
|
|
|
261
219
|
export { ComparisonFailError, type Connection, type MessageHandler, type MutateOptions, type MutationFunction, type MutationInfo, type MutationOptions, type QueryOptions, type SubscriptionCoreOptions, type SubscriptionCoreReturnType, type SubscriptionOptions, type TPromiseFunction, type TSubscriptionHandlerFunction, type UseQueryResult, bindClientSocketProxy, bindPromiseFunction, buildMutationHook, buildQueryHook, buildServiceSubscriptionHook, useMutation, useQuery, useSubscription, useSubscriptionCore };
|
package/dist/index.js
CHANGED
|
@@ -183,63 +183,61 @@ var useMutation = (options) => {
|
|
|
183
183
|
};
|
|
184
184
|
const internalCallbackRef = useRef(internalCallbackRefistry);
|
|
185
185
|
internalCallbackRef.current = internalCallbackRefistry;
|
|
186
|
-
const mutateAsync = useCallback(
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
)
|
|
186
|
+
const mutateAsync = useCallback((params2, meta) => __async(null, null, function* () {
|
|
187
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
188
|
+
setStatus("loading");
|
|
189
|
+
setError(null);
|
|
190
|
+
try {
|
|
191
|
+
const promises = [
|
|
192
|
+
mutationFnRef.current(
|
|
193
|
+
internalCallbackRef.current.prepareParams ? internalCallbackRef.current.prepareParams(
|
|
194
|
+
params2,
|
|
195
|
+
internalOptionsRef.current.params,
|
|
196
|
+
meta
|
|
197
|
+
) : params2,
|
|
198
|
+
meta
|
|
199
|
+
),
|
|
200
|
+
...internalOptionsRef.current.timeout ? [
|
|
201
|
+
new Promise(
|
|
202
|
+
(resolve) => setTimeout(
|
|
203
|
+
() => resolve("timeout"),
|
|
204
|
+
internalOptionsRef.current.timeout
|
|
206
205
|
)
|
|
207
|
-
|
|
208
|
-
]
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
if ((_b = (_a = internalCallbackRef.current).compareOrThrow) == null ? void 0 : _b.call(_a, res)) {
|
|
214
|
-
throw new ComparisonFailError(JSON.stringify(res), res);
|
|
215
|
-
}
|
|
216
|
-
setResult(res);
|
|
217
|
-
setStatus("success");
|
|
218
|
-
(_d = (_c = internalCallbackRef.current).onSuccess) == null ? void 0 : _d.call(_c, res, params2);
|
|
219
|
-
(_f = (_e = internalCallbackRef.current).onSettled) == null ? void 0 : _f.call(_e, res, null, params2);
|
|
220
|
-
return res;
|
|
221
|
-
} catch (error2) {
|
|
222
|
-
setError(error2);
|
|
223
|
-
setStatus("error");
|
|
224
|
-
(_h = (_g = internalCallbackRef.current).onError) == null ? void 0 : _h.call(_g, error2, params2);
|
|
225
|
-
(_j = (_i = internalCallbackRef.current).onSettled) == null ? void 0 : _j.call(_i, void 0, error2, params2);
|
|
226
|
-
throw error2;
|
|
206
|
+
)
|
|
207
|
+
] : []
|
|
208
|
+
];
|
|
209
|
+
const res = yield Promise.race(promises);
|
|
210
|
+
if (res === "timeout") {
|
|
211
|
+
throw new Error("timeout");
|
|
227
212
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
213
|
+
if ((_b = (_a = internalCallbackRef.current).compareOrThrow) == null ? void 0 : _b.call(_a, res)) {
|
|
214
|
+
throw new ComparisonFailError(JSON.stringify(res), res);
|
|
215
|
+
}
|
|
216
|
+
setResult(res);
|
|
217
|
+
setStatus("success");
|
|
218
|
+
(_d = (_c = internalCallbackRef.current).onSuccess) == null ? void 0 : _d.call(_c, res, params2, meta);
|
|
219
|
+
(_f = (_e = internalCallbackRef.current).onSettled) == null ? void 0 : _f.call(_e, res, null, params2, meta);
|
|
220
|
+
return res;
|
|
221
|
+
} catch (error2) {
|
|
222
|
+
setError(error2);
|
|
223
|
+
setStatus("error");
|
|
224
|
+
(_h = (_g = internalCallbackRef.current).onError) == null ? void 0 : _h.call(_g, error2, params2, meta);
|
|
225
|
+
(_j = (_i = internalCallbackRef.current).onSettled) == null ? void 0 : _j.call(_i, void 0, error2, params2, meta);
|
|
226
|
+
throw error2;
|
|
227
|
+
}
|
|
228
|
+
}), []);
|
|
231
229
|
const mutate = useCallback(
|
|
232
230
|
(variables, options2) => {
|
|
233
|
-
mutateAsync(variables, options2 == null ? void 0 : options2.
|
|
231
|
+
mutateAsync(variables, options2 == null ? void 0 : options2.meta).then(
|
|
234
232
|
(data) => {
|
|
235
233
|
var _a, _b;
|
|
236
|
-
(_a = options2 == null ? void 0 : options2.onSuccess) == null ? void 0 : _a.call(options2, data, variables);
|
|
237
|
-
(_b = options2 == null ? void 0 : options2.onSettled) == null ? void 0 : _b.call(options2, data, null, variables);
|
|
234
|
+
(_a = options2 == null ? void 0 : options2.onSuccess) == null ? void 0 : _a.call(options2, data, variables, options2 == null ? void 0 : options2.meta);
|
|
235
|
+
(_b = options2 == null ? void 0 : options2.onSettled) == null ? void 0 : _b.call(options2, data, null, variables, options2 == null ? void 0 : options2.meta);
|
|
238
236
|
},
|
|
239
237
|
(error2) => {
|
|
240
238
|
var _a, _b;
|
|
241
|
-
(_a = options2 == null ? void 0 : options2.onError) == null ? void 0 : _a.call(options2, error2, variables);
|
|
242
|
-
(_b = options2 == null ? void 0 : options2.onSettled) == null ? void 0 : _b.call(options2, void 0, error2, variables);
|
|
239
|
+
(_a = options2 == null ? void 0 : options2.onError) == null ? void 0 : _a.call(options2, error2, variables, options2 == null ? void 0 : options2.meta);
|
|
240
|
+
(_b = options2 == null ? void 0 : options2.onSettled) == null ? void 0 : _b.call(options2, void 0, error2, variables, options2 == null ? void 0 : options2.meta);
|
|
243
241
|
}
|
|
244
242
|
);
|
|
245
243
|
},
|
|
@@ -408,14 +406,11 @@ var useQuery = (_a) => {
|
|
|
408
406
|
});
|
|
409
407
|
}
|
|
410
408
|
if (refetchInterval > 0) {
|
|
411
|
-
const interval = setInterval(
|
|
412
|
-
()
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
},
|
|
417
|
-
Math.max(refetchInterval, 1e3)
|
|
418
|
-
);
|
|
409
|
+
const interval = setInterval(() => {
|
|
410
|
+
if (shouldRefetch()) {
|
|
411
|
+
refetch();
|
|
412
|
+
}
|
|
413
|
+
}, refetchInterval);
|
|
419
414
|
cleanups.push(() => {
|
|
420
415
|
clearInterval(interval);
|
|
421
416
|
});
|
|
@@ -449,16 +444,17 @@ var useCallMutationBase = (promiseFunc, functionName, service, options) => {
|
|
|
449
444
|
const timeout = options == null ? void 0 : options.timeout;
|
|
450
445
|
const rest = __objRest(useMemo(() => options != null ? options : {}, [options]), []);
|
|
451
446
|
const mutationFn = useCallback3(
|
|
452
|
-
(parameters) => __async(null, null, function* () {
|
|
447
|
+
(parameters, meta) => __async(null, null, function* () {
|
|
453
448
|
try {
|
|
454
449
|
return yield promiseFunc({
|
|
455
|
-
service,
|
|
450
|
+
service: service(meta),
|
|
456
451
|
functionName: functionName.toString(),
|
|
457
452
|
parameters,
|
|
458
453
|
options: {
|
|
459
454
|
avoidTimeout: !timeout,
|
|
460
455
|
customTimeout: timeout
|
|
461
|
-
}
|
|
456
|
+
},
|
|
457
|
+
meta
|
|
462
458
|
});
|
|
463
459
|
} catch (e) {
|
|
464
460
|
if (e instanceof Error) {
|
|
@@ -475,24 +471,24 @@ var useCallMutationBase = (promiseFunc, functionName, service, options) => {
|
|
|
475
471
|
};
|
|
476
472
|
function buildMutationHook(promiseFunc, genTargetService) {
|
|
477
473
|
return function useCallMutationBuilder(func, options) {
|
|
478
|
-
|
|
479
|
-
return useCallMutationBase(promiseFunc, func, targetService, options);
|
|
474
|
+
return useCallMutationBase(promiseFunc, func, genTargetService, options);
|
|
480
475
|
};
|
|
481
476
|
}
|
|
482
477
|
var useCallQueryBase = (promiseFunction, functionName, params, service, options) => {
|
|
483
478
|
const timeout = options == null ? void 0 : options.timeout;
|
|
484
479
|
const rest = __objRest(useMemo(() => options != null ? options : {}, [options]), []);
|
|
485
480
|
const queryFn = useCallback3(
|
|
486
|
-
(refetchParams
|
|
481
|
+
(refetchParams) => __async(null, null, function* () {
|
|
487
482
|
try {
|
|
488
483
|
return yield promiseFunction({
|
|
489
|
-
service:
|
|
484
|
+
service: service(options == null ? void 0 : options.meta),
|
|
490
485
|
functionName: functionName.toString(),
|
|
491
486
|
parameters: refetchParams != null ? refetchParams : params,
|
|
492
487
|
options: {
|
|
493
488
|
avoidTimeout: !timeout,
|
|
494
489
|
customTimeout: timeout
|
|
495
|
-
}
|
|
490
|
+
},
|
|
491
|
+
meta: options == null ? void 0 : options.meta
|
|
496
492
|
});
|
|
497
493
|
} catch (e) {
|
|
498
494
|
if (e instanceof Error) {
|
|
@@ -509,8 +505,7 @@ var useCallQueryBase = (promiseFunction, functionName, params, service, options)
|
|
|
509
505
|
};
|
|
510
506
|
function buildQueryHook(promiseFunction, getTargetService) {
|
|
511
507
|
return function useCallQueryBuilder(func, params, options) {
|
|
512
|
-
|
|
513
|
-
return useCallQueryBase(promiseFunction, func, params, targetService, options);
|
|
508
|
+
return useCallQueryBase(promiseFunction, func, params, getTargetService, options);
|
|
514
509
|
};
|
|
515
510
|
}
|
|
516
511
|
|