@ic-reactor/react 1.2.3 → 1.3.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/dist/context/actor.js +5 -3
- package/dist/helpers/actorHooks.js +26 -5
- package/dist/helpers/types.d.ts +3 -2
- package/dist/hooks/actor/index.d.ts +1 -0
- package/dist/hooks/actor/index.js +1 -0
- package/dist/hooks/actor/useMethodNames.d.ts +2 -1
- package/dist/hooks/actor/useMethodNames.js +2 -1
- package/dist/hooks/useActor.js +1 -0
- package/package.json +3 -3
package/dist/context/actor.js
CHANGED
|
@@ -90,9 +90,11 @@ function createActorContext(contextConfig = {}) {
|
|
|
90
90
|
const config = react_1.default.useMemo(() => (Object.assign(Object.assign({}, defaultConfig), restConfig)), [defaultConfig, restConfig]);
|
|
91
91
|
const { fetchError, authenticating, hooks } = (0, useActor_1.useActor)(Object.assign({ canisterId }, config));
|
|
92
92
|
return (react_1.default.createElement(ActorContext.Provider, { value: hooks }, hooks === null
|
|
93
|
-
?
|
|
94
|
-
?
|
|
95
|
-
:
|
|
93
|
+
? fetchError
|
|
94
|
+
? fetchError
|
|
95
|
+
: authenticating
|
|
96
|
+
? authenticatingComponent
|
|
97
|
+
: loadingComponent
|
|
96
98
|
: children));
|
|
97
99
|
};
|
|
98
100
|
ActorProvider.displayName = "ActorProvider";
|
|
@@ -47,10 +47,10 @@ const DEFAULT_STATE = {
|
|
|
47
47
|
* Each hook is designed to simplify the process of interacting with actors in IC projects by abstracting away the complexity of state management, error handling, and method invocation.
|
|
48
48
|
*/
|
|
49
49
|
const actorHooks = (actorManager) => {
|
|
50
|
-
const { actorStore, canisterId, visitFunction,
|
|
50
|
+
const { actorStore, canisterId, visitFunction, methodAttributes, extractMethodAttributes, extractInterface, callMethod, initialize, } = actorManager;
|
|
51
51
|
const useActorState = () => (Object.assign(Object.assign({}, (0, zustand_1.useStore)(actorStore)), { canisterId }));
|
|
52
52
|
const useMethodNames = () => {
|
|
53
|
-
return react_1.default.useMemo(() =>
|
|
53
|
+
return react_1.default.useMemo(() => Object.keys(extractMethodAttributes()), []);
|
|
54
54
|
};
|
|
55
55
|
const useActorInterface = () => {
|
|
56
56
|
return react_1.default.useMemo(() => extractInterface(), []);
|
|
@@ -111,10 +111,31 @@ const actorHooks = (actorManager) => {
|
|
|
111
111
|
};
|
|
112
112
|
const useUpdateCall = useSharedCall;
|
|
113
113
|
const useMethod = (args) => {
|
|
114
|
-
const _a = useSharedCall(args), { call } = _a, state = __rest(_a, ["call"]);
|
|
115
114
|
const visit = useVisitMethod(args.functionName);
|
|
116
|
-
|
|
117
|
-
|
|
115
|
+
const attributes = methodAttributes[args.functionName];
|
|
116
|
+
let refetchOnMount = args.refetchOnMount;
|
|
117
|
+
let refetchInterval = args.refetchInterval;
|
|
118
|
+
let formRequired = true;
|
|
119
|
+
switch (attributes.type) {
|
|
120
|
+
case "query":
|
|
121
|
+
try {
|
|
122
|
+
if (attributes.numberOfArgs > 0 && args.args === undefined) {
|
|
123
|
+
throw new Error("Args required");
|
|
124
|
+
}
|
|
125
|
+
attributes.validate((args.args || []));
|
|
126
|
+
formRequired = false;
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
refetchOnMount = false;
|
|
130
|
+
refetchInterval = false;
|
|
131
|
+
}
|
|
132
|
+
return Object.assign(Object.assign({ visit }, useQueryCall(Object.assign(Object.assign({}, args), { refetchOnMount,
|
|
133
|
+
refetchInterval }))), { formRequired });
|
|
134
|
+
case "update":
|
|
135
|
+
return Object.assign(Object.assign({ visit }, useUpdateCall(args)), { formRequired });
|
|
136
|
+
default:
|
|
137
|
+
throw new Error(`Method type ${attributes.type} not found`);
|
|
138
|
+
}
|
|
118
139
|
};
|
|
119
140
|
return {
|
|
120
141
|
initialize,
|
package/dist/helpers/types.d.ts
CHANGED
|
@@ -71,10 +71,11 @@ export interface DynamicDataArgs<V = unknown> {
|
|
|
71
71
|
label: string;
|
|
72
72
|
value: V;
|
|
73
73
|
}
|
|
74
|
-
export interface UseMethodParameters<A, M extends FunctionName<A>> extends
|
|
74
|
+
export interface UseMethodParameters<A, M extends FunctionName<A>> extends UseQueryCallParameters<A, M> {
|
|
75
75
|
}
|
|
76
76
|
export interface UseMethodReturnType<A, M extends FunctionName<A> = FunctionName<A>> {
|
|
77
77
|
loading: boolean;
|
|
78
|
+
formRequired: boolean;
|
|
78
79
|
error: Error | undefined;
|
|
79
80
|
data: ActorMethodReturnType<A[M]> | undefined;
|
|
80
81
|
visit: VisitService<A>[M];
|
|
@@ -88,7 +89,7 @@ export interface ActorHooksReturnType<A = BaseActor> {
|
|
|
88
89
|
initialize: () => Promise<void>;
|
|
89
90
|
useActorState: () => UseActorState;
|
|
90
91
|
useActorInterface: () => ServiceClass;
|
|
91
|
-
useMethodNames: () => FunctionName<
|
|
92
|
+
useMethodNames: <Actor = A>() => FunctionName<Actor>[];
|
|
92
93
|
useMethod: UseMethod<A>;
|
|
93
94
|
useQueryCall: UseQueryCall<A>;
|
|
94
95
|
useUpdateCall: UseUpdateCall<A>;
|
|
@@ -2,6 +2,7 @@ export * from "./useMethod";
|
|
|
2
2
|
export * from "./useActorState";
|
|
3
3
|
export * from "./useQueryCall";
|
|
4
4
|
export * from "./useUpdateCall";
|
|
5
|
+
export * from "./useMethodNames";
|
|
5
6
|
export * from "./useVisitMethod";
|
|
6
7
|
export * from "./useVisitService";
|
|
7
8
|
export * from "./useActorInterface";
|
|
@@ -18,6 +18,7 @@ __exportStar(require("./useMethod"), exports);
|
|
|
18
18
|
__exportStar(require("./useActorState"), exports);
|
|
19
19
|
__exportStar(require("./useQueryCall"), exports);
|
|
20
20
|
__exportStar(require("./useUpdateCall"), exports);
|
|
21
|
+
__exportStar(require("./useMethodNames"), exports);
|
|
21
22
|
__exportStar(require("./useVisitMethod"), exports);
|
|
22
23
|
__exportStar(require("./useVisitService"), exports);
|
|
23
24
|
__exportStar(require("./useActorInterface"), exports);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { BaseActor } from "../../types";
|
|
1
2
|
/**
|
|
2
3
|
* Hook for accessing the method names of an actor.
|
|
3
4
|
*
|
|
4
5
|
* @returns An array of method names for the actor.
|
|
5
6
|
*/
|
|
6
|
-
export declare const useMethodNames: () => string[];
|
|
7
|
+
export declare const useMethodNames: <A = BaseActor>() => Extract<keyof A, string>[];
|
|
@@ -7,4 +7,5 @@ const hooks_1 = require("./hooks");
|
|
|
7
7
|
*
|
|
8
8
|
* @returns An array of method names for the actor.
|
|
9
9
|
*/
|
|
10
|
-
|
|
10
|
+
const useMethodNames = () => hooks_1.ActorHooks.useMethodNames();
|
|
11
|
+
exports.useMethodNames = useMethodNames;
|
package/dist/hooks/useActor.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/react",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "A React library for interacting with Internet Computer canisters",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"node": ">=10"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@ic-reactor/core": "^1.
|
|
38
|
+
"@ic-reactor/core": "^1.3.0",
|
|
39
39
|
"zustand-utils": "^1.3"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"react": ">=16.8",
|
|
48
48
|
"zustand": "4.5"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "25043c2d1a78b97e327995abaf13835ac511e5d6"
|
|
51
51
|
}
|