@ic-reactor/react 1.0.2 → 1.0.4
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/helpers/actor.d.ts +21 -3
- package/dist/helpers/actor.js +42 -45
- package/dist/helpers/agent.d.ts +2 -5
- package/dist/helpers/agent.js +4 -6
- package/dist/helpers/auth.d.ts +2 -20
- package/dist/helpers/auth.js +65 -87
- package/dist/helpers/types.d.ts +86 -4
- package/dist/hooks/index.d.ts +1 -2
- package/dist/hooks/index.js +1 -2
- package/dist/hooks/types.d.ts +19 -0
- package/dist/hooks/useReactor.d.ts +41 -0
- package/dist/hooks/useReactor.js +121 -0
- package/dist/index.d.ts +3 -29
- package/dist/index.js +4 -39
- package/dist/main.d.ts +2 -0
- package/dist/main.js +35 -0
- package/dist/provider/actor/context.d.ts +20 -0
- package/dist/provider/actor/context.js +184 -0
- package/dist/provider/actor/index.d.ts +3 -0
- package/dist/provider/actor/index.js +9 -0
- package/dist/{context → provider}/actor/types.d.ts +4 -11
- package/dist/{context → provider}/agent/hooks.d.ts +4 -16
- package/dist/{context → provider}/agent/hooks.js +9 -9
- package/dist/provider/agent/types.js +2 -0
- package/dist/types.d.ts +7 -72
- package/dist/types.js +3 -2
- package/package.json +3 -3
- package/dist/context/actor/index.d.ts +0 -16
- package/dist/context/actor/index.js +0 -88
- package/dist/hooks/useActor.d.ts +0 -21
- package/dist/hooks/useActor.js +0 -48
- package/dist/hooks/useCandid.d.ts +0 -18
- package/dist/hooks/useCandid.js +0 -54
- /package/dist/{context/actor → hooks}/types.js +0 -0
- /package/dist/{context/agent → provider/actor}/types.js +0 -0
- /package/dist/{context → provider}/agent/context.d.ts +0 -0
- /package/dist/{context → provider}/agent/context.js +0 -0
- /package/dist/{context → provider}/agent/index.d.ts +0 -0
- /package/dist/{context → provider}/agent/index.js +0 -0
- /package/dist/{context → provider}/agent/types.d.ts +0 -0
- /package/dist/{context → provider}/index.d.ts +0 -0
- /package/dist/{context → provider}/index.js +0 -0
package/dist/helpers/actor.d.ts
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
-
import { ActorHooks } from "../types";
|
|
2
|
-
import type {
|
|
3
|
-
|
|
1
|
+
import type { ActorHooks } from "../types";
|
|
2
|
+
import type { BaseActor } from "@ic-reactor/core/dist/types";
|
|
3
|
+
import { ActorManager } from "@ic-reactor/core";
|
|
4
|
+
/**
|
|
5
|
+
* Provides a set of React hooks designed for interacting with actors in an Internet Computer (IC) project using the React framework and Zustand for state management.
|
|
6
|
+
*
|
|
7
|
+
* @param actorManager An instance of ActorManager containing methods and properties to manage actors, including the actorStore, canisterId, visitFunction, callMethod, and initialize function.
|
|
8
|
+
* @returns An object containing several hooks and utility functions for interacting with actors, managing state, and invoking actor methods.
|
|
9
|
+
*
|
|
10
|
+
* Hooks included:
|
|
11
|
+
* - initialize: Function to initialize actor management.
|
|
12
|
+
* - useActorState: Hook for accessing the actor's state including the canister ID.
|
|
13
|
+
* - useVisitMethod: Hook for memoizing a method visit service for a given actor method name.
|
|
14
|
+
* - useReactorCall: Hook for making calls to actor methods with support for loading states, errors, and custom event handlers.
|
|
15
|
+
* - useQueryCall: Hook specifically designed for query calls to actors with features such as automatic refetching on mount and at specified intervals.
|
|
16
|
+
* - useUpdateCall: Alias for useReactorCall, tailored for update calls to actors.
|
|
17
|
+
* - useMethodCall: Combines useVisitMethod and useReactorCall for a streamlined experience when calling actor methods, including visitation and state management.
|
|
18
|
+
*
|
|
19
|
+
* 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.
|
|
20
|
+
*/
|
|
21
|
+
export declare const getActorHooks: <A = BaseActor>(actorManager: ActorManager<A>) => ActorHooks<A>;
|
package/dist/helpers/actor.js
CHANGED
|
@@ -21,7 +21,6 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
21
21
|
};
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
23
|
exports.getActorHooks = void 0;
|
|
24
|
-
/* eslint-disable no-console */
|
|
25
24
|
const react_1 = require("react");
|
|
26
25
|
const zustand_1 = require("zustand");
|
|
27
26
|
const DEFAULT_STATE = {
|
|
@@ -29,86 +28,84 @@ const DEFAULT_STATE = {
|
|
|
29
28
|
error: undefined,
|
|
30
29
|
loading: false,
|
|
31
30
|
};
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Provides a set of React hooks designed for interacting with actors in an Internet Computer (IC) project using the React framework and Zustand for state management.
|
|
33
|
+
*
|
|
34
|
+
* @param actorManager An instance of ActorManager containing methods and properties to manage actors, including the actorStore, canisterId, visitFunction, callMethod, and initialize function.
|
|
35
|
+
* @returns An object containing several hooks and utility functions for interacting with actors, managing state, and invoking actor methods.
|
|
36
|
+
*
|
|
37
|
+
* Hooks included:
|
|
38
|
+
* - initialize: Function to initialize actor management.
|
|
39
|
+
* - useActorState: Hook for accessing the actor's state including the canister ID.
|
|
40
|
+
* - useVisitMethod: Hook for memoizing a method visit service for a given actor method name.
|
|
41
|
+
* - useReactorCall: Hook for making calls to actor methods with support for loading states, errors, and custom event handlers.
|
|
42
|
+
* - useQueryCall: Hook specifically designed for query calls to actors with features such as automatic refetching on mount and at specified intervals.
|
|
43
|
+
* - useUpdateCall: Alias for useReactorCall, tailored for update calls to actors.
|
|
44
|
+
* - useMethodCall: Combines useVisitMethod and useReactorCall for a streamlined experience when calling actor methods, including visitation and state management.
|
|
45
|
+
*
|
|
46
|
+
* 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.
|
|
47
|
+
*/
|
|
48
|
+
const getActorHooks = (actorManager) => {
|
|
49
|
+
const { actorStore, canisterId, visitFunction, callMethod, initialize } = actorManager;
|
|
50
|
+
const useActorState = () => (Object.assign(Object.assign({}, (0, zustand_1.useStore)(actorStore)), { canisterId }));
|
|
40
51
|
const useVisitMethod = (functionName) => {
|
|
41
|
-
|
|
42
|
-
return (0, react_1.useMemo)(() => {
|
|
43
|
-
return serviceFields[functionName];
|
|
44
|
-
}, [functionName, serviceFields]);
|
|
52
|
+
return (0, react_1.useMemo)(() => visitFunction[functionName], [functionName]);
|
|
45
53
|
};
|
|
46
|
-
const useReactorCall = (
|
|
54
|
+
const useReactorCall = (_a) => {
|
|
55
|
+
var { args = [], functionName, throwOnError = false } = _a, events = __rest(_a, ["args", "functionName", "throwOnError"]);
|
|
47
56
|
const [state, setState] = (0, react_1.useState)(DEFAULT_STATE);
|
|
48
57
|
const reset = (0, react_1.useCallback)(() => setState(DEFAULT_STATE), []);
|
|
49
58
|
const call = (0, react_1.useCallback)((eventOrReplaceArgs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
59
|
+
var _b, _c, _d, _e, _f;
|
|
60
|
+
setState({ data: undefined, error: undefined, loading: true });
|
|
61
|
+
(_b = events === null || events === void 0 ? void 0 : events.onLoading) === null || _b === void 0 ? void 0 : _b.call(events, true);
|
|
53
62
|
try {
|
|
54
|
-
const replaceArgs = eventOrReplaceArgs
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const data = yield callMethod(functionName, ...(replaceArgs !== null && replaceArgs !== void 0 ? replaceArgs : args));
|
|
60
|
-
onLoading === null || onLoading === void 0 ? void 0 : onLoading(false);
|
|
61
|
-
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
|
|
62
|
-
setState((prevState) => (Object.assign(Object.assign({}, prevState), { data, loading: false })));
|
|
63
|
+
const replaceArgs = eventOrReplaceArgs instanceof Array ? eventOrReplaceArgs : args;
|
|
64
|
+
const data = yield callMethod(functionName, ...replaceArgs);
|
|
65
|
+
setState({ data, error: undefined, loading: false });
|
|
66
|
+
(_c = events === null || events === void 0 ? void 0 : events.onSuccess) === null || _c === void 0 ? void 0 : _c.call(events, data);
|
|
67
|
+
(_d = events === null || events === void 0 ? void 0 : events.onLoading) === null || _d === void 0 ? void 0 : _d.call(events, false);
|
|
63
68
|
return data;
|
|
64
69
|
}
|
|
65
70
|
catch (error) {
|
|
71
|
+
// eslint-disable-next-line no-console
|
|
66
72
|
console.error("Error in call:", error);
|
|
67
|
-
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
68
|
-
onLoading === null || onLoading === void 0 ? void 0 : onLoading(false);
|
|
69
73
|
setState((prevState) => (Object.assign(Object.assign({}, prevState), { error: error, loading: false })));
|
|
74
|
+
(_e = events === null || events === void 0 ? void 0 : events.onError) === null || _e === void 0 ? void 0 : _e.call(events, error);
|
|
75
|
+
(_f = events === null || events === void 0 ? void 0 : events.onLoading) === null || _f === void 0 ? void 0 : _f.call(events, false);
|
|
70
76
|
if (throwOnError)
|
|
71
77
|
throw error;
|
|
72
78
|
}
|
|
73
|
-
}), [args, functionName,
|
|
79
|
+
}), [args, functionName, events]);
|
|
74
80
|
return Object.assign({ call, reset }, state);
|
|
75
81
|
};
|
|
76
82
|
const useQueryCall = (_a) => {
|
|
77
83
|
var { refetchOnMount = true, refetchInterval = false } = _a, rest = __rest(_a, ["refetchOnMount", "refetchInterval"]);
|
|
78
84
|
const _b = useReactorCall(rest), { call } = _b, state = __rest(_b, ["call"]);
|
|
79
|
-
const intervalId = (0, react_1.useRef)(
|
|
85
|
+
const intervalId = (0, react_1.useRef)();
|
|
80
86
|
(0, react_1.useEffect)(() => {
|
|
81
|
-
// Auto-refresh logic
|
|
82
87
|
if (refetchInterval) {
|
|
83
|
-
intervalId.current = setInterval(
|
|
84
|
-
call();
|
|
85
|
-
}, refetchInterval);
|
|
88
|
+
intervalId.current = setInterval(call, refetchInterval);
|
|
86
89
|
}
|
|
87
|
-
return () => {
|
|
88
|
-
clearInterval(intervalId.current);
|
|
89
|
-
};
|
|
90
|
-
}, [refetchInterval]);
|
|
91
|
-
(0, react_1.useLayoutEffect)(() => {
|
|
92
90
|
if (refetchOnMount) {
|
|
93
91
|
call();
|
|
94
92
|
}
|
|
95
|
-
|
|
93
|
+
return () => clearInterval(intervalId.current);
|
|
94
|
+
}, [refetchInterval, refetchOnMount]);
|
|
96
95
|
return Object.assign({ call }, state);
|
|
97
96
|
};
|
|
98
|
-
const useUpdateCall =
|
|
99
|
-
return useReactorCall(args);
|
|
100
|
-
};
|
|
97
|
+
const useUpdateCall = useReactorCall;
|
|
101
98
|
const useMethodCall = (args) => {
|
|
102
|
-
const visit =
|
|
99
|
+
const visit = useVisitMethod(args.functionName);
|
|
103
100
|
return Object.assign({ visit }, useReactorCall(args));
|
|
104
101
|
};
|
|
105
102
|
return {
|
|
106
103
|
initialize,
|
|
107
104
|
useQueryCall,
|
|
108
105
|
useUpdateCall,
|
|
109
|
-
useVisitMethod,
|
|
110
106
|
useActorState,
|
|
111
107
|
useMethodCall,
|
|
108
|
+
useVisitMethod,
|
|
112
109
|
};
|
|
113
110
|
};
|
|
114
111
|
exports.getActorHooks = getActorHooks;
|
package/dist/helpers/agent.d.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
1
|
import type { AgentManager } from "@ic-reactor/core/dist/agent";
|
|
2
|
-
import type {
|
|
3
|
-
export declare const getAgentHooks: (agentManager: AgentManager) =>
|
|
4
|
-
useAgent: () => HttpAgent | undefined;
|
|
5
|
-
useAgentState: () => import("@ic-reactor/core/dist/types").AgentState;
|
|
6
|
-
};
|
|
2
|
+
import type { AgentHooks } from "./types";
|
|
3
|
+
export declare const getAgentHooks: (agentManager: AgentManager) => AgentHooks;
|
package/dist/helpers/agent.js
CHANGED
|
@@ -4,13 +4,11 @@ exports.getAgentHooks = void 0;
|
|
|
4
4
|
const react_1 = require("react");
|
|
5
5
|
const zustand_1 = require("zustand");
|
|
6
6
|
const getAgentHooks = (agentManager) => {
|
|
7
|
-
const { agentStore, getAgent } = agentManager;
|
|
8
|
-
const useAgentState = () =>
|
|
9
|
-
return (0, zustand_1.useStore)(agentStore, (state) => state);
|
|
10
|
-
};
|
|
7
|
+
const { agentStore, getAgent, subscribeAgent } = agentManager;
|
|
8
|
+
const useAgentState = () => (0, zustand_1.useStore)(agentStore);
|
|
11
9
|
const useAgent = () => {
|
|
12
|
-
const [agent, setAgent] = (0, react_1.useState)(getAgent
|
|
13
|
-
(0, react_1.useEffect)(() =>
|
|
10
|
+
const [agent, setAgent] = (0, react_1.useState)(getAgent);
|
|
11
|
+
(0, react_1.useEffect)(() => subscribeAgent(setAgent), [subscribeAgent]);
|
|
14
12
|
return agent;
|
|
15
13
|
};
|
|
16
14
|
return {
|
package/dist/helpers/auth.d.ts
CHANGED
|
@@ -1,21 +1,3 @@
|
|
|
1
|
-
import type { AuthClientLoginOptions } from "@dfinity/auth-client";
|
|
2
|
-
import type { Identity, Principal } from "@ic-reactor/core/dist/types";
|
|
3
1
|
import type { AgentManager } from "@ic-reactor/core/dist/agent";
|
|
4
|
-
import {
|
|
5
|
-
export declare const getAuthHooks: (agentManager: AgentManager) =>
|
|
6
|
-
useUserPrincipal: () => Principal | undefined;
|
|
7
|
-
useAuthState: () => import("@ic-reactor/core/dist/types").AuthState;
|
|
8
|
-
useAuthClient: ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, }?: AuthArgs) => {
|
|
9
|
-
authClient: import("@dfinity/auth-client").AuthClient | null;
|
|
10
|
-
authenticated: boolean;
|
|
11
|
-
authenticating: boolean;
|
|
12
|
-
identity: Identity | null;
|
|
13
|
-
login: (options?: AuthClientLoginOptions) => Promise<void>;
|
|
14
|
-
logout: (options?: {
|
|
15
|
-
returnTo?: string;
|
|
16
|
-
}) => Promise<void>;
|
|
17
|
-
authenticate: () => Promise<Identity>;
|
|
18
|
-
loginLoading: boolean;
|
|
19
|
-
loginError: Error | null;
|
|
20
|
-
};
|
|
21
|
-
};
|
|
2
|
+
import type { AuthHooks } from "../types";
|
|
3
|
+
export declare const getAuthHooks: (agentManager: AgentManager) => AuthHooks;
|
package/dist/helpers/auth.js
CHANGED
|
@@ -10,106 +10,84 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.getAuthHooks = void 0;
|
|
13
|
-
const react_1 = require("react");
|
|
14
13
|
const zustand_1 = require("zustand");
|
|
14
|
+
const react_1 = require("react");
|
|
15
|
+
const tools_1 = require("@ic-reactor/core/dist/tools");
|
|
15
16
|
const getAuthHooks = (agentManager) => {
|
|
16
17
|
const { authenticate: authenticator, authStore, isLocalEnv } = agentManager;
|
|
17
|
-
const useAuthState = () =>
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const [loginLoading, setLoginLoading] = (0, react_1.useState)(false);
|
|
26
|
-
const [loginError, setLoginError] = (0, react_1.useState)(null);
|
|
27
|
-
const { authClient, authenticated, authenticating, identity } = useAuthState();
|
|
18
|
+
const useAuthState = () => (0, zustand_1.useStore)(authStore);
|
|
19
|
+
const useUserPrincipal = () => { var _a, _b; return (_b = (_a = useAuthState()) === null || _a === void 0 ? void 0 : _a.identity) === null || _b === void 0 ? void 0 : _b.getPrincipal(); };
|
|
20
|
+
const useAuthClient = (events) => {
|
|
21
|
+
const [loginState, setLoginState] = (0, react_1.useState)({
|
|
22
|
+
loading: false,
|
|
23
|
+
error: null,
|
|
24
|
+
});
|
|
25
|
+
const { authClient, authenticated, authenticating, identity, error } = useAuthState();
|
|
28
26
|
const authenticate = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
onAuthentication === null || onAuthentication === void 0 ? void 0 : onAuthentication(() => authenticatePromise);
|
|
41
|
-
return authenticatePromise;
|
|
42
|
-
}), [
|
|
43
|
-
authenticator,
|
|
44
|
-
onAuthentication,
|
|
45
|
-
onAuthenticationSuccess,
|
|
46
|
-
onAuthenticationFailure,
|
|
47
|
-
]);
|
|
27
|
+
var _a, _b;
|
|
28
|
+
try {
|
|
29
|
+
const identity = yield authenticator();
|
|
30
|
+
(_a = events === null || events === void 0 ? void 0 : events.onAuthenticationSuccess) === null || _a === void 0 ? void 0 : _a.call(events, identity);
|
|
31
|
+
return identity;
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
(_b = events === null || events === void 0 ? void 0 : events.onAuthenticationFailure) === null || _b === void 0 ? void 0 : _b.call(events, e);
|
|
35
|
+
throw e;
|
|
36
|
+
}
|
|
37
|
+
}), [authenticator, events]);
|
|
48
38
|
const login = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
reject(e);
|
|
85
|
-
}
|
|
86
|
-
finally {
|
|
87
|
-
setLoginLoading(false);
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
onLogin === null || onLogin === void 0 ? void 0 : onLogin(() => loginPromise);
|
|
91
|
-
}), [
|
|
92
|
-
authClient,
|
|
93
|
-
onLogin,
|
|
94
|
-
onLoginSuccess,
|
|
95
|
-
onLoginError,
|
|
96
|
-
isLocalEnv,
|
|
97
|
-
authenticate,
|
|
98
|
-
]);
|
|
39
|
+
var _c;
|
|
40
|
+
if (!authClient) {
|
|
41
|
+
throw new Error("Auth client not initialized");
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
setLoginState({ loading: true, error: null });
|
|
45
|
+
yield authClient.login(Object.assign(Object.assign({ identityProvider: isLocalEnv
|
|
46
|
+
? tools_1.LOCAL_INTERNET_IDENTITY_PROVIDER
|
|
47
|
+
: tools_1.IC_INTERNET_IDENTITY_PROVIDER }, options), { onSuccess: () => __awaiter(void 0, void 0, void 0, function* () {
|
|
48
|
+
var _d, _e, _f;
|
|
49
|
+
try {
|
|
50
|
+
const identity = yield authenticate();
|
|
51
|
+
const principal = identity.getPrincipal();
|
|
52
|
+
(_d = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _d === void 0 ? void 0 : _d.call(options);
|
|
53
|
+
(_e = events === null || events === void 0 ? void 0 : events.onLoginSuccess) === null || _e === void 0 ? void 0 : _e.call(events, principal);
|
|
54
|
+
setLoginState({ loading: false, error: null });
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
const error = e;
|
|
58
|
+
setLoginState({ loading: false, error });
|
|
59
|
+
(_f = events === null || events === void 0 ? void 0 : events.onLoginError) === null || _f === void 0 ? void 0 : _f.call(events, error);
|
|
60
|
+
}
|
|
61
|
+
}), onError: (e) => {
|
|
62
|
+
var _a;
|
|
63
|
+
const error = new Error(`Login failed: ${e}`);
|
|
64
|
+
setLoginState({ loading: false, error });
|
|
65
|
+
(_a = events === null || events === void 0 ? void 0 : events.onLoginError) === null || _a === void 0 ? void 0 : _a.call(events, error);
|
|
66
|
+
} }));
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
const error = e;
|
|
70
|
+
setLoginState({ loading: false, error });
|
|
71
|
+
(_c = events === null || events === void 0 ? void 0 : events.onLoginError) === null || _c === void 0 ? void 0 : _c.call(events, error);
|
|
72
|
+
}
|
|
73
|
+
}), [authClient, authenticate, isLocalEnv, events]);
|
|
99
74
|
const logout = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
75
|
+
var _g;
|
|
100
76
|
if (!authClient) {
|
|
101
77
|
throw new Error("Auth client not initialized");
|
|
102
78
|
}
|
|
103
79
|
yield authClient.logout(options);
|
|
104
80
|
yield authenticate();
|
|
105
|
-
|
|
106
|
-
}), [authClient,
|
|
81
|
+
(_g = events === null || events === void 0 ? void 0 : events.onLoggedOut) === null || _g === void 0 ? void 0 : _g.call(events);
|
|
82
|
+
}), [authClient, authenticate, events]);
|
|
107
83
|
(0, react_1.useEffect)(() => {
|
|
108
84
|
if (!authClient && !authenticating) {
|
|
109
|
-
|
|
85
|
+
// eslint-disable-next-line no-console
|
|
86
|
+
authenticate().catch(console.error);
|
|
110
87
|
}
|
|
111
|
-
}, [authClient, authenticating]);
|
|
88
|
+
}, [authClient, authenticating, authenticate]);
|
|
112
89
|
return {
|
|
90
|
+
error,
|
|
113
91
|
authClient,
|
|
114
92
|
authenticated,
|
|
115
93
|
authenticating,
|
|
@@ -117,8 +95,8 @@ const getAuthHooks = (agentManager) => {
|
|
|
117
95
|
login,
|
|
118
96
|
logout,
|
|
119
97
|
authenticate,
|
|
120
|
-
loginLoading,
|
|
121
|
-
loginError,
|
|
98
|
+
loginLoading: loginState.loading,
|
|
99
|
+
loginError: loginState.error,
|
|
122
100
|
};
|
|
123
101
|
};
|
|
124
102
|
return {
|
package/dist/helpers/types.d.ts
CHANGED
|
@@ -1,4 +1,86 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import type { AuthClientLoginOptions } from "@dfinity/auth-client";
|
|
3
|
+
import type { ActorState, CanisterId, ActorMethodArgs, ActorMethodReturnType, Identity, Principal, FunctionName, VisitService, AuthClient, AuthState, HttpAgent, AgentState } from "@ic-reactor/core/dist/types";
|
|
4
|
+
export interface AgentHooks {
|
|
5
|
+
useAgent: () => HttpAgent | undefined;
|
|
6
|
+
useAgentState: () => AgentState;
|
|
7
|
+
}
|
|
8
|
+
export interface AuthHooks {
|
|
9
|
+
useUserPrincipal: () => Principal | undefined;
|
|
10
|
+
useAuthState: () => AuthState;
|
|
11
|
+
useAuthClient: (events?: UseAuthClientArgs) => UseAuthClientReturn;
|
|
12
|
+
}
|
|
13
|
+
export interface ActorHooks<A> {
|
|
14
|
+
initialize: () => Promise<void>;
|
|
15
|
+
useActorState: () => UseActorState;
|
|
16
|
+
useQueryCall: UseQueryCall<A>;
|
|
17
|
+
useUpdateCall: UseUpdateCall<A>;
|
|
18
|
+
useMethodCall: UseMethodCall<A>;
|
|
19
|
+
useVisitMethod: <M extends FunctionName<A>>(functionName: M) => VisitService<A>[M];
|
|
20
|
+
}
|
|
21
|
+
export interface UseAuthClientArgs {
|
|
22
|
+
onAuthentication?: (promise: () => Promise<Identity>) => void;
|
|
23
|
+
onAuthenticationSuccess?: (identity: Identity) => void;
|
|
24
|
+
onAuthenticationFailure?: (error: Error) => void;
|
|
25
|
+
onLoginSuccess?: (principal: Principal) => void;
|
|
26
|
+
onLoginError?: (error: Error) => void;
|
|
27
|
+
onLogin?: (promise: () => Promise<Principal>) => void;
|
|
28
|
+
onLoggedOut?: () => void;
|
|
29
|
+
}
|
|
30
|
+
export interface UseAuthClientReturn {
|
|
31
|
+
error: Error | undefined;
|
|
32
|
+
authClient: AuthClient | null;
|
|
33
|
+
authenticated: boolean;
|
|
34
|
+
authenticating: boolean;
|
|
35
|
+
identity: Identity | null;
|
|
36
|
+
login: (options?: LoginOptions) => Promise<void>;
|
|
37
|
+
logout: (options?: LogoutOptions) => Promise<void>;
|
|
38
|
+
authenticate: () => Promise<Identity>;
|
|
39
|
+
loginLoading: boolean;
|
|
40
|
+
loginError: Error | null;
|
|
41
|
+
}
|
|
42
|
+
export type LoginState = {
|
|
43
|
+
loading: boolean;
|
|
44
|
+
error: Error | null;
|
|
45
|
+
};
|
|
46
|
+
export type LoginOptions = AuthClientLoginOptions;
|
|
47
|
+
export type LogoutOptions = {
|
|
48
|
+
returnTo?: string;
|
|
49
|
+
};
|
|
50
|
+
export type ReactorCallArgs<A, M extends FunctionName<A>> = {
|
|
51
|
+
functionName: M;
|
|
52
|
+
args?: ActorMethodArgs<A[M]>;
|
|
53
|
+
onLoading?: (loading: boolean) => void;
|
|
54
|
+
onError?: (error: Error | undefined) => void;
|
|
55
|
+
onSuccess?: (data: ActorMethodReturnType<A[M]> | undefined) => void;
|
|
56
|
+
throwOnError?: boolean;
|
|
57
|
+
};
|
|
58
|
+
export type ActorCallState<A, M extends FunctionName<A>> = {
|
|
59
|
+
data: ActorMethodReturnType<A[M]> | undefined;
|
|
60
|
+
error: Error | undefined;
|
|
61
|
+
loading: boolean;
|
|
62
|
+
};
|
|
63
|
+
export interface UseQueryCallArgs<A, M extends FunctionName<A>> extends ReactorCallArgs<A, M> {
|
|
64
|
+
refetchOnMount?: boolean;
|
|
65
|
+
refetchInterval?: number | false;
|
|
66
|
+
}
|
|
67
|
+
export interface UseUpdateCallArgs<A, M extends FunctionName<A>> extends ReactorCallArgs<A, M> {
|
|
68
|
+
}
|
|
69
|
+
export interface ReactorCallReturn<A, M extends FunctionName<A> = FunctionName<A>> extends ActorCallState<A, M> {
|
|
70
|
+
reset: () => void;
|
|
71
|
+
call: (eventOrReplaceArgs?: React.MouseEvent | ActorMethodArgs<A[M]>) => Promise<ActorMethodReturnType<A[M]> | undefined>;
|
|
72
|
+
}
|
|
73
|
+
export type ReactorCall<A> = <M extends FunctionName<A>>(args: ReactorCallArgs<A, M>) => ReactorCallReturn<A, M>;
|
|
74
|
+
export type UseQueryCall<A> = <M extends FunctionName<A>>(args: UseQueryCallArgs<A, M>) => ReactorCallReturn<A, M>;
|
|
75
|
+
export type UseUpdateCall<A> = <M extends FunctionName<A>>(args: UseUpdateCallArgs<A, M>) => ReactorCallReturn<A, M>;
|
|
76
|
+
export type UseMethodCall<A> = <M extends FunctionName<A>>(args: UseMethodCallArg<A, M>) => UseMethodCallReturn<A, M>;
|
|
77
|
+
export interface UseMethodCallReturn<A, M extends FunctionName<A> = FunctionName<A>> extends ReactorCallReturn<A, M> {
|
|
78
|
+
visit: VisitService<A>[M];
|
|
79
|
+
reset: () => void;
|
|
80
|
+
error: Error | undefined;
|
|
81
|
+
loading: boolean;
|
|
82
|
+
}
|
|
83
|
+
export type UseMethodCallArg<A, M extends FunctionName<A>> = ReactorCallArgs<A, M>;
|
|
84
|
+
export interface UseActorState extends Omit<ActorState, "methodState"> {
|
|
85
|
+
canisterId: CanisterId;
|
|
86
|
+
}
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./useCandid";
|
|
1
|
+
export * from "./useReactor";
|
package/dist/hooks/index.js
CHANGED
|
@@ -14,5 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./
|
|
18
|
-
__exportStar(require("./useCandid"), exports);
|
|
17
|
+
__exportStar(require("./useReactor"), exports);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { IDL } from "@dfinity/candid";
|
|
2
|
+
import { ActorManagerOptions, BaseActor } from "@ic-reactor/core/dist/types";
|
|
3
|
+
import { ActorHooks, AgentContextType } from "../types";
|
|
4
|
+
export interface UseReactorOptions extends Omit<ActorManagerOptions, "idlFactory" | "agentManager" | "canisterId"> {
|
|
5
|
+
canisterId: string;
|
|
6
|
+
idlFactory?: IDL.InterfaceFactory;
|
|
7
|
+
agentContext?: AgentContextType;
|
|
8
|
+
didjsCanisterId?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface UseReactorState {
|
|
11
|
+
idlFactory?: IDL.InterfaceFactory;
|
|
12
|
+
fetching: boolean;
|
|
13
|
+
fetchError: string | null;
|
|
14
|
+
}
|
|
15
|
+
export interface UseReactorReturn<A = BaseActor> {
|
|
16
|
+
hooks: ActorHooks<A> | null;
|
|
17
|
+
fetching: boolean;
|
|
18
|
+
fetchError: string | null;
|
|
19
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { BaseActor } from "../types";
|
|
2
|
+
import { UseReactorOptions, UseReactorReturn } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* A comprehensive hook that manages both the fetching of Candid interfaces
|
|
5
|
+
* and the initialization of actor stores for Internet Computer (IC) canisters.
|
|
6
|
+
* It simplifies the process of interacting with canisters by encapsulating
|
|
7
|
+
* the logic for Candid retrieval and actor store management.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```tsx
|
|
11
|
+
* import React from 'react';
|
|
12
|
+
* import { useReactor } from '@ic-reactor/react';
|
|
13
|
+
* import { IDL } from '@dfinity/candid';
|
|
14
|
+
*
|
|
15
|
+
* const App = () => {
|
|
16
|
+
* const { actorManager, fetchCandid, candid, fetching, fetchError } = useReactor({
|
|
17
|
+
* canisterId: 'ryjl3-tyaaa-aaaaa-aaaba-cai',
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* return (
|
|
21
|
+
* <div>
|
|
22
|
+
* <h2>IC Canister Interaction</h2>
|
|
23
|
+
* {fetching && <p>Loading Candid interface...</p>}
|
|
24
|
+
* {fetchError && <p>Error: {fetchError}</p>}
|
|
25
|
+
* {candid.idlFactory && (
|
|
26
|
+
* <div>
|
|
27
|
+
* <p>Candid interface fetched successfully.</p>
|
|
28
|
+
* <pre>{JSON.stringify(candid.idlFactory({ IDL }), null, 2)}</pre>
|
|
29
|
+
* </div>
|
|
30
|
+
* )}
|
|
31
|
+
* <button onClick={fetchCandid} disabled={fetching}>
|
|
32
|
+
* {fetching ? 'Fetching...' : 'Fetch Candid'}
|
|
33
|
+
* </button>
|
|
34
|
+
* </div>
|
|
35
|
+
* );
|
|
36
|
+
* };
|
|
37
|
+
*
|
|
38
|
+
* export default App;
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare const useReactor: <A = BaseActor>({ canisterId, agentContext, idlFactory: maybeIdlFactory, didjsCanisterId, ...config }: UseReactorOptions) => UseReactorReturn<A>;
|