@ic-reactor/react 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -8
- package/dist/helpers/authHooks.js +11 -15
- package/dist/helpers/extractActorContext.js +0 -82
- package/dist/helpers/extractAgentContext.d.ts +1 -1
- package/dist/helpers/extractAgentContext.js +3 -81
- package/dist/helpers/types.d.ts +15 -15
- package/dist/main.d.ts +1 -1
- package/dist/main.js +1 -1
- package/dist/provider/ActorProvider.d.ts +0 -1
- package/dist/provider/ActorProvider.js +3 -4
- package/dist/provider/AgentProvider.d.ts +0 -1
- package/dist/provider/AgentProvider.js +3 -4
- package/dist/provider/actorHooks.d.ts +1 -0
- package/dist/provider/actorHooks.js +5 -0
- package/dist/provider/agentHooks.d.ts +1 -0
- package/dist/provider/agentHooks.js +5 -0
- package/dist/provider/{actor.d.ts → context/actor.d.ts} +2 -2
- package/dist/provider/{actor.js → context/actor.js} +2 -2
- package/dist/provider/{agent.d.ts → context/agent.d.ts} +5 -5
- package/dist/provider/{agent.js → context/agent.js} +6 -6
- package/dist/provider/hooks/actor/index.d.ts +4 -0
- package/dist/provider/hooks/actor/index.js +20 -0
- package/dist/provider/hooks/actor/useActorState.d.ts +21 -0
- package/dist/provider/hooks/actor/useActorState.js +25 -0
- package/dist/provider/hooks/actor/useQueryCall.d.ts +28 -0
- package/dist/provider/hooks/actor/useQueryCall.js +34 -0
- package/dist/provider/hooks/actor/useUpdateCall.d.ts +29 -0
- package/dist/provider/hooks/actor/useUpdateCall.js +35 -0
- package/dist/provider/hooks/actor/useVisitMethod.d.ts +8 -0
- package/dist/provider/hooks/actor/useVisitMethod.js +14 -0
- package/dist/provider/hooks/agent/index.d.ts +6 -0
- package/dist/provider/hooks/agent/index.js +22 -0
- package/dist/provider/hooks/agent/useAgent.d.ts +14 -0
- package/dist/provider/hooks/agent/useAgent.js +18 -0
- package/dist/provider/hooks/agent/useAgentManager.d.ts +15 -0
- package/dist/provider/hooks/agent/useAgentManager.js +18 -0
- package/dist/provider/hooks/agent/useAgentState.d.ts +21 -0
- package/dist/provider/hooks/agent/useAgentState.js +25 -0
- package/dist/provider/hooks/agent/useAuth.d.ts +57 -0
- package/dist/provider/hooks/agent/useAuth.js +61 -0
- package/dist/provider/hooks/agent/useAuthState.d.ts +19 -0
- package/dist/provider/hooks/agent/useAuthState.js +23 -0
- package/dist/provider/hooks/agent/useUserPrincipal.d.ts +17 -0
- package/dist/provider/hooks/agent/useUserPrincipal.js +21 -0
- package/dist/provider/hooks/index.d.ts +2 -0
- package/dist/provider/hooks/index.js +2 -0
- package/dist/provider/hooks/useActor.d.ts +5 -3
- package/dist/provider/hooks/useActor.js +7 -5
- package/dist/provider/index.d.ts +4 -7
- package/dist/provider/index.js +5 -6
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -36,12 +36,11 @@ import { createReactor } from "@ic-reactor/react"
|
|
|
36
36
|
|
|
37
37
|
type Actor = typeof actor
|
|
38
38
|
|
|
39
|
-
export const { useActorStore,
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
})
|
|
39
|
+
export const { useActorStore, useAuth, useQueryCall } = createReactor<Actor>({
|
|
40
|
+
canisterId,
|
|
41
|
+
idlFactory,
|
|
42
|
+
host: "https://localhost:4943",
|
|
43
|
+
})
|
|
45
44
|
```
|
|
46
45
|
|
|
47
46
|
Then, use the `useQueryCall` hook to call your canister method:
|
|
@@ -82,7 +81,7 @@ export default Balance
|
|
|
82
81
|
|
|
83
82
|
```jsx
|
|
84
83
|
// Login.tsx
|
|
85
|
-
import {
|
|
84
|
+
import { useAuth } from "./actor"
|
|
86
85
|
|
|
87
86
|
const Login = () => {
|
|
88
87
|
const {
|
|
@@ -93,7 +92,7 @@ const Login = () => {
|
|
|
93
92
|
identity,
|
|
94
93
|
authenticating,
|
|
95
94
|
authenticated,
|
|
96
|
-
} =
|
|
95
|
+
} = useAuth()
|
|
97
96
|
|
|
98
97
|
return (
|
|
99
98
|
<div>
|
|
@@ -14,15 +14,15 @@ const zustand_1 = require("zustand");
|
|
|
14
14
|
const react_1 = require("react");
|
|
15
15
|
const tools_1 = require("@ic-reactor/core/dist/tools");
|
|
16
16
|
const authHooks = (agentManager) => {
|
|
17
|
-
const { authenticate: authenticator, authStore, isLocalEnv } = agentManager;
|
|
17
|
+
const { authenticate: authenticator, getAuth, authStore, isLocalEnv, } = agentManager;
|
|
18
18
|
const useAuthState = () => (0, zustand_1.useStore)(authStore);
|
|
19
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
|
|
20
|
+
const useAuth = ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, } = {}) => {
|
|
21
21
|
const [loginState, setLoginState] = (0, react_1.useState)({
|
|
22
22
|
loading: false,
|
|
23
23
|
error: null,
|
|
24
24
|
});
|
|
25
|
-
const {
|
|
25
|
+
const { authenticated, authenticating, error, identity } = useAuthState();
|
|
26
26
|
const authenticate = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
27
|
const authenticatePromise = new Promise((resolve, reject) => {
|
|
28
28
|
authenticator()
|
|
@@ -47,6 +47,7 @@ const authHooks = (agentManager) => {
|
|
|
47
47
|
setLoginState({ loading: true, error: null });
|
|
48
48
|
const loginPromise = new Promise((resolve, reject) => {
|
|
49
49
|
try {
|
|
50
|
+
const authClient = getAuth();
|
|
50
51
|
if (!authClient) {
|
|
51
52
|
throw new Error("Auth client not initialized");
|
|
52
53
|
}
|
|
@@ -60,6 +61,7 @@ const authHooks = (agentManager) => {
|
|
|
60
61
|
(_a = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _a === void 0 ? void 0 : _a.call(options);
|
|
61
62
|
onLoginSuccess === null || onLoginSuccess === void 0 ? void 0 : onLoginSuccess(principal);
|
|
62
63
|
resolve(principal);
|
|
64
|
+
setLoginState({ loading: false, error: null });
|
|
63
65
|
})
|
|
64
66
|
.catch((e) => {
|
|
65
67
|
const error = e;
|
|
@@ -84,29 +86,23 @@ const authHooks = (agentManager) => {
|
|
|
84
86
|
}
|
|
85
87
|
});
|
|
86
88
|
onLogin === null || onLogin === void 0 ? void 0 : onLogin(() => loginPromise);
|
|
87
|
-
}), [
|
|
88
|
-
authClient,
|
|
89
|
-
onLogin,
|
|
90
|
-
onLoginSuccess,
|
|
91
|
-
onLoginError,
|
|
92
|
-
isLocalEnv,
|
|
93
|
-
authenticate,
|
|
94
|
-
]);
|
|
89
|
+
}), [onLogin, onLoginSuccess, onLoginError, isLocalEnv, authenticate]);
|
|
95
90
|
const logout = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
91
|
+
const authClient = getAuth();
|
|
96
92
|
if (!authClient) {
|
|
97
93
|
throw new Error("Auth client not initialized");
|
|
98
94
|
}
|
|
99
95
|
yield authClient.logout(options);
|
|
100
96
|
yield authenticate();
|
|
101
97
|
onLoggedOut === null || onLoggedOut === void 0 ? void 0 : onLoggedOut();
|
|
102
|
-
}), [
|
|
98
|
+
}), [onLoggedOut]);
|
|
103
99
|
(0, react_1.useEffect)(() => {
|
|
100
|
+
const authClient = getAuth();
|
|
104
101
|
if (!authClient && !authenticating) {
|
|
105
102
|
authenticate();
|
|
106
103
|
}
|
|
107
|
-
}, [
|
|
104
|
+
}, [authenticating]);
|
|
108
105
|
return {
|
|
109
|
-
authClient,
|
|
110
106
|
authenticated,
|
|
111
107
|
authenticating,
|
|
112
108
|
identity,
|
|
@@ -121,7 +117,7 @@ const authHooks = (agentManager) => {
|
|
|
121
117
|
return {
|
|
122
118
|
useUserPrincipal,
|
|
123
119
|
useAuthState,
|
|
124
|
-
|
|
120
|
+
useAuth,
|
|
125
121
|
};
|
|
126
122
|
};
|
|
127
123
|
exports.authHooks = authHooks;
|
|
@@ -35,92 +35,10 @@ function extractActorContext(actorContext) {
|
|
|
35
35
|
}
|
|
36
36
|
return context;
|
|
37
37
|
};
|
|
38
|
-
/**
|
|
39
|
-
* Initializes the actor manager, setting up the actor's state.
|
|
40
|
-
*/
|
|
41
38
|
const initialize = () => useActorContext().initialize();
|
|
42
|
-
/**
|
|
43
|
-
* Hook for accessing the current state of the actor, including the canister ID.
|
|
44
|
-
*
|
|
45
|
-
* @returns An object containing the current state of the actor from Zustand's store and the canister ID.
|
|
46
|
-
* @example
|
|
47
|
-
* ```tsx
|
|
48
|
-
* function ActorStateComponent() {
|
|
49
|
-
* const { canisterId, initializing, error, initialized } = useActorState();
|
|
50
|
-
*
|
|
51
|
-
* return (
|
|
52
|
-
* <div>
|
|
53
|
-
* <p>Canister ID: {canisterId}</p>
|
|
54
|
-
* <p>Initializing: {initializing.toString()}</p>
|
|
55
|
-
* <p>Initialized: {initialized.toString()}</p>
|
|
56
|
-
* <p>Error: {error?.message}</p>
|
|
57
|
-
* </div>
|
|
58
|
-
* );
|
|
59
|
-
* }
|
|
60
|
-
*```
|
|
61
|
-
*/
|
|
62
39
|
const useActorState = () => useActorContext().useActorState();
|
|
63
|
-
/**
|
|
64
|
-
* Hook for making query calls to actors. It supports automatic refetching on component mount and at specified intervals.
|
|
65
|
-
*
|
|
66
|
-
* @param options Configuration object for the query call, including refetching options and other configurations passed to useReactorCall.
|
|
67
|
-
* @returns An object containing the query call function and the current call state (data, error, loading, call, reset).
|
|
68
|
-
* @example
|
|
69
|
-
* ```tsx
|
|
70
|
-
* function QueryCallComponent() {
|
|
71
|
-
* const { call, data, loading } = useQueryCall({
|
|
72
|
-
* functionName: 'getUserProfile',
|
|
73
|
-
* args: ['123'],
|
|
74
|
-
* refetchOnMount: true,
|
|
75
|
-
* refetchInterval: 5000, // refetch every 5 seconds
|
|
76
|
-
* });
|
|
77
|
-
*
|
|
78
|
-
* if (loading) return <p>Loading profile...</p>;
|
|
79
|
-
*
|
|
80
|
-
* return (
|
|
81
|
-
* <div>
|
|
82
|
-
* <p>User Profile: {JSON.stringify(data)}</p>
|
|
83
|
-
* <button onClick={call}>Refetch</button>
|
|
84
|
-
* </div>
|
|
85
|
-
* );
|
|
86
|
-
* }
|
|
87
|
-
* ```
|
|
88
|
-
*/
|
|
89
40
|
const useQueryCall = (args) => useActorContext().useQueryCall(args);
|
|
90
|
-
/**
|
|
91
|
-
* Hook for making update calls to actors, handling loading states, and managing errors. It supports custom event handlers for loading, success, and error events.
|
|
92
|
-
*
|
|
93
|
-
* @param options Configuration object for the actor method call, including the method name, arguments, and event handlers.
|
|
94
|
-
* @returns An object containing the method call function, a reset function to reset the call state to its default, and the current call state (data, error, loading, call, reset).
|
|
95
|
-
* @example
|
|
96
|
-
* ```tsx
|
|
97
|
-
* function UpdateCallComponent() {
|
|
98
|
-
* const { call, data, loading } = useUpdateCall({
|
|
99
|
-
* functionName: 'updateUserProfile',
|
|
100
|
-
* args: ['123', { name: 'John Doe' }],
|
|
101
|
-
* onLoading: (loading) => console.log('Loading:', loading),
|
|
102
|
-
* onError: (error) => console.error('Error:', error),
|
|
103
|
-
* onSuccess: (data) => console.log('Success:', data),
|
|
104
|
-
* });
|
|
105
|
-
*
|
|
106
|
-
* if (loading) return <p>Updating profile...</p>;
|
|
107
|
-
*
|
|
108
|
-
* return (
|
|
109
|
-
* <div>
|
|
110
|
-
* <p>Updated Profile: {JSON.stringify(data)}</p>
|
|
111
|
-
* <button onClick={call}>Update</button>
|
|
112
|
-
* </div>
|
|
113
|
-
* );
|
|
114
|
-
* }
|
|
115
|
-
* ```
|
|
116
|
-
*/
|
|
117
41
|
const useUpdateCall = (args) => useActorContext().useUpdateCall(args);
|
|
118
|
-
/**
|
|
119
|
-
* Memoizes and returns a visit service function for a specific actor method.
|
|
120
|
-
*
|
|
121
|
-
* @param functionName The name of the actor method to visit.
|
|
122
|
-
* @returns The visit service function for the specified method.
|
|
123
|
-
*/
|
|
124
42
|
const useVisitMethod = (functionName) => useActorContext().useVisitMethod(functionName);
|
|
125
43
|
return {
|
|
126
44
|
useActorState,
|
|
@@ -9,7 +9,7 @@ import type { AgentContext, CreateAgentContextReturnType } from "../types";
|
|
|
9
9
|
* - useAgent: Hook for accessing the current agent instance.
|
|
10
10
|
* - useAuthState: Hook for accessing the current authentication state.
|
|
11
11
|
* - useAgentState: Hook for accessing the current state of the agent.
|
|
12
|
-
* -
|
|
12
|
+
* - useAuth: Hook for accessing the authentication client, optionally accepting arguments for configuration.
|
|
13
13
|
* - useAgentManager: Hook for accessing the AgentManager instance.
|
|
14
14
|
* - useUserPrincipal: Hook for accessing the user's principal.
|
|
15
15
|
*
|
|
@@ -12,7 +12,7 @@ const react_1 = require("react");
|
|
|
12
12
|
* - useAgent: Hook for accessing the current agent instance.
|
|
13
13
|
* - useAuthState: Hook for accessing the current authentication state.
|
|
14
14
|
* - useAgentState: Hook for accessing the current state of the agent.
|
|
15
|
-
* -
|
|
15
|
+
* - useAuth: Hook for accessing the authentication client, optionally accepting arguments for configuration.
|
|
16
16
|
* - useAgentManager: Hook for accessing the AgentManager instance.
|
|
17
17
|
* - useUserPrincipal: Hook for accessing the user's principal.
|
|
18
18
|
*
|
|
@@ -35,98 +35,20 @@ const extractAgentContext = (agentContext) => {
|
|
|
35
35
|
}
|
|
36
36
|
return context;
|
|
37
37
|
};
|
|
38
|
-
/**
|
|
39
|
-
* Accesses the `AgentManager` instance for managing agent configurations and state.
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
*```jsx
|
|
43
|
-
* function AgentManagerComponent() {
|
|
44
|
-
* const agentManager = useAgentManager();
|
|
45
|
-
*
|
|
46
|
-
* // Use agentManager for managing agent configurations, etc.
|
|
47
|
-
* return <div>Agent Manager ready.</div>;
|
|
48
|
-
* }
|
|
49
|
-
*```
|
|
50
|
-
*/
|
|
51
38
|
const useAgentManager = (agentContext) => {
|
|
52
39
|
const context = useAgentContext(agentContext);
|
|
53
40
|
return context.agentManager;
|
|
54
41
|
};
|
|
55
|
-
/**
|
|
56
|
-
* Accesses the current agent instance.
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
*```jsx
|
|
60
|
-
* function AgentComponent() {
|
|
61
|
-
* const agent = useAgent();
|
|
62
|
-
*
|
|
63
|
-
* // Use agent for interacting with the Internet Computer.
|
|
64
|
-
* return <div>Agent ready.</div>;
|
|
65
|
-
* }
|
|
66
|
-
*```
|
|
67
|
-
*/
|
|
68
42
|
const useAgent = () => useAgentContext().useAgent();
|
|
69
|
-
/**
|
|
70
|
-
* Accesses the current authentication state.
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* ```jsx
|
|
74
|
-
* function AuthStateComponent() {
|
|
75
|
-
* const { isAuthenticated, user } = useAuthState();
|
|
76
|
-
*
|
|
77
|
-
* return (
|
|
78
|
-
* <div>
|
|
79
|
-
* {isAuthenticated ? `User ${user} is authenticated.` : 'User is not authenticated.'}
|
|
80
|
-
* </div>
|
|
81
|
-
* );
|
|
82
|
-
* }
|
|
83
|
-
* ```
|
|
84
|
-
*/
|
|
85
43
|
const useAuthState = () => useAgentContext().useAuthState();
|
|
86
|
-
/**
|
|
87
|
-
* Accesses the current state of the agent.
|
|
88
|
-
*
|
|
89
|
-
* @example
|
|
90
|
-
* ```jsx
|
|
91
|
-
* function AgentStateComponent() {
|
|
92
|
-
* const { initialized, initializing } = useAgentState();
|
|
93
|
-
*
|
|
94
|
-
* return (
|
|
95
|
-
* <div>
|
|
96
|
-
* {initialized
|
|
97
|
-
* ? 'Agent is initialized.'
|
|
98
|
-
* : initializing
|
|
99
|
-
* ? 'Agent is initializing...'
|
|
100
|
-
* : 'Agent is not initialized.'}
|
|
101
|
-
* </div>
|
|
102
|
-
* );
|
|
103
|
-
* }
|
|
104
|
-
* ```
|
|
105
|
-
*/
|
|
106
44
|
const useAgentState = () => useAgentContext().useAgentState();
|
|
107
|
-
const
|
|
108
|
-
/**
|
|
109
|
-
* Accesses the user's principal.
|
|
110
|
-
*
|
|
111
|
-
* @example
|
|
112
|
-
* ```jsx
|
|
113
|
-
* function UserPrincipalComponent() {
|
|
114
|
-
* const userPrincipal = useUserPrincipal();
|
|
115
|
-
*
|
|
116
|
-
* return (
|
|
117
|
-
* <div>
|
|
118
|
-
* {userPrincipal ? `User principal: ${userPrincipal}` : 'User principal not found.'}
|
|
119
|
-
* </div>
|
|
120
|
-
* );
|
|
121
|
-
* }
|
|
122
|
-
* ```
|
|
123
|
-
*/
|
|
45
|
+
const useAuth = (args) => useAgentContext().useAuth(args);
|
|
124
46
|
const useUserPrincipal = () => useAgentContext().useUserPrincipal();
|
|
125
47
|
return {
|
|
126
48
|
useAgent,
|
|
127
49
|
useAuthState,
|
|
128
50
|
useAgentState,
|
|
129
|
-
|
|
51
|
+
useAuth: useAuth,
|
|
130
52
|
useAgentManager,
|
|
131
53
|
useUserPrincipal,
|
|
132
54
|
};
|
package/dist/helpers/types.d.ts
CHANGED
|
@@ -1,22 +1,15 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import type { ActorState, CanisterId, AuthClientLoginOptions, ActorMethodParameters, ActorMethodReturnType, Identity, Principal, FunctionName, VisitService,
|
|
2
|
+
import type { ActorState, CanisterId, AuthClientLoginOptions, ActorMethodParameters, ActorMethodReturnType, Identity, Principal, FunctionName, VisitService, AuthState, HttpAgent, AgentState, BaseActor } from "@ic-reactor/core/dist/types";
|
|
3
3
|
export interface AgentHooksReturnType {
|
|
4
4
|
useAgent: () => HttpAgent | undefined;
|
|
5
5
|
useAgentState: () => AgentState;
|
|
6
6
|
}
|
|
7
7
|
export interface AuthHooksReturnType {
|
|
8
|
-
|
|
8
|
+
useAuth: (options?: UseAuthParameters) => UseAuthReturnType;
|
|
9
9
|
useAuthState: () => AuthState;
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
export interface ActorHooksReturnType<A = BaseActor> {
|
|
13
|
-
initialize: () => Promise<void>;
|
|
14
|
-
useActorState: () => UseActorState;
|
|
15
|
-
useQueryCall: UseQueryCall<A>;
|
|
16
|
-
useUpdateCall: UseUpdateCall<A>;
|
|
17
|
-
useVisitMethod: <M extends FunctionName<A>>(functionName: M) => VisitService<A>[M];
|
|
10
|
+
useUserPrincipal: () => Principal | undefined;
|
|
18
11
|
}
|
|
19
|
-
export interface
|
|
12
|
+
export interface UseAuthParameters {
|
|
20
13
|
onAuthentication?: (promise: () => Promise<Identity>) => void;
|
|
21
14
|
onAuthenticationSuccess?: (identity: Identity) => void;
|
|
22
15
|
onAuthenticationFailure?: (error: Error) => void;
|
|
@@ -25,9 +18,8 @@ export interface UseAuthClientParameters {
|
|
|
25
18
|
onLogin?: (promise: () => Promise<Principal>) => void;
|
|
26
19
|
onLoggedOut?: () => void;
|
|
27
20
|
}
|
|
28
|
-
export interface
|
|
21
|
+
export interface UseAuthReturnType {
|
|
29
22
|
error: Error | undefined;
|
|
30
|
-
authClient: AuthClient | null;
|
|
31
23
|
authenticated: boolean;
|
|
32
24
|
authenticating: boolean;
|
|
33
25
|
identity: Identity | null;
|
|
@@ -45,6 +37,9 @@ export type LoginParameters = AuthClientLoginOptions;
|
|
|
45
37
|
export type LogoutParameters = {
|
|
46
38
|
returnTo?: string;
|
|
47
39
|
};
|
|
40
|
+
export interface UseActorState extends Omit<ActorState, "methodState"> {
|
|
41
|
+
canisterId: CanisterId;
|
|
42
|
+
}
|
|
48
43
|
export type UseMethodCallParameters<A, M extends FunctionName<A>> = {
|
|
49
44
|
functionName: M;
|
|
50
45
|
args?: ActorMethodParameters<A[M]>;
|
|
@@ -71,6 +66,11 @@ export interface UseMethodCallReturnType<A, M extends FunctionName<A> = Function
|
|
|
71
66
|
export type UseMethodCall<A> = <M extends FunctionName<A>>(args: UseMethodCallParameters<A, M>) => UseMethodCallReturnType<A, M>;
|
|
72
67
|
export type UseQueryCall<A> = <M extends FunctionName<A>>(args: UseQueryCallParameters<A, M>) => UseMethodCallReturnType<A, M>;
|
|
73
68
|
export type UseUpdateCall<A> = <M extends FunctionName<A>>(args: UseUpdateCallParameters<A, M>) => UseMethodCallReturnType<A, M>;
|
|
74
|
-
export
|
|
75
|
-
|
|
69
|
+
export type UseVisitMethod<A> = <M extends FunctionName<A>>(functionName: M) => VisitService<A>[M];
|
|
70
|
+
export interface ActorHooksReturnType<A = BaseActor> {
|
|
71
|
+
initialize: () => Promise<void>;
|
|
72
|
+
useActorState: () => UseActorState;
|
|
73
|
+
useQueryCall: UseQueryCall<A>;
|
|
74
|
+
useUpdateCall: UseUpdateCall<A>;
|
|
75
|
+
useVisitMethod: UseVisitMethod<A>;
|
|
76
76
|
}
|
package/dist/main.d.ts
CHANGED
|
@@ -31,7 +31,7 @@ import type { BaseActor, CreateReactorParameters, CreateReactorReturnType } from
|
|
|
31
31
|
*
|
|
32
32
|
* export type YourActor = typeof yourActor;
|
|
33
33
|
*
|
|
34
|
-
* export const {
|
|
34
|
+
* export const { useAuth, useQueryCall, useUpdateCall } = createReactor<YourActor>(config);
|
|
35
35
|
* // Now you can use the returned hooks in your React components
|
|
36
36
|
* ```
|
|
37
37
|
*/
|
package/dist/main.js
CHANGED
|
@@ -35,7 +35,7 @@ const helpers_1 = require("./helpers");
|
|
|
35
35
|
*
|
|
36
36
|
* export type YourActor = typeof yourActor;
|
|
37
37
|
*
|
|
38
|
-
* export const {
|
|
38
|
+
* export const { useAuth, useQueryCall, useUpdateCall } = createReactor<YourActor>(config);
|
|
39
39
|
* // Now you can use the returned hooks in your React components
|
|
40
40
|
* ```
|
|
41
41
|
*/
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
export declare const ActorHooks: import("./types").CreateActorContextReturnType<import("@ic-reactor/core/dist/types").BaseActor>;
|
|
3
2
|
/**
|
|
4
3
|
* `ActorProvider` is a React functional component that serves as a context provider for IC actor interactions within a React application.
|
|
5
4
|
* It wraps child components, providing them access to actor-specific hooks and functionalities based on the provided canister ID and configuration.
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ActorProvider =
|
|
4
|
-
const
|
|
5
|
-
exports.ActorHooks = (0, actor_1.createActorContext)();
|
|
3
|
+
exports.ActorProvider = void 0;
|
|
4
|
+
const actorHooks_1 = require("./actorHooks");
|
|
6
5
|
/**
|
|
7
6
|
* `ActorProvider` is a React functional component that serves as a context provider for IC actor interactions within a React application.
|
|
8
7
|
* It wraps child components, providing them access to actor-specific hooks and functionalities based on the provided canister ID and configuration.
|
|
@@ -27,4 +26,4 @@ exports.ActorHooks = (0, actor_1.createActorContext)();
|
|
|
27
26
|
* ```
|
|
28
27
|
* This setup ensures that `YourComponent` and any of its children can interact with the specified IC actor through the context provided by `ActorProvider`.
|
|
29
28
|
*/
|
|
30
|
-
exports.ActorProvider =
|
|
29
|
+
exports.ActorProvider = actorHooks_1.ActorHooks.ActorProvider;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
export declare const AgentHooks: import("./types").CreateAgentContextReturnType;
|
|
3
2
|
/**
|
|
4
3
|
* `AgentProvider` is a React functional component that serves as a context provider for IC agent and authentication hooks.
|
|
5
4
|
* It enables any child components to access and use the agent and authentication functionalities seamlessly.
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AgentProvider =
|
|
4
|
-
const
|
|
5
|
-
exports.AgentHooks = (0, agent_1.createAgentContext)();
|
|
3
|
+
exports.AgentProvider = void 0;
|
|
4
|
+
const agentHooks_1 = require("./agentHooks");
|
|
6
5
|
/**
|
|
7
6
|
* `AgentProvider` is a React functional component that serves as a context provider for IC agent and authentication hooks.
|
|
8
7
|
* It enables any child components to access and use the agent and authentication functionalities seamlessly.
|
|
@@ -30,4 +29,4 @@ exports.AgentHooks = (0, agent_1.createAgentContext)();
|
|
|
30
29
|
* Inside `YourComponent` or any of its children, you can use the hooks provided through the context to interact with the IC,
|
|
31
30
|
* manage authentication, and perform other agent-related tasks.
|
|
32
31
|
*/
|
|
33
|
-
exports.AgentProvider =
|
|
32
|
+
exports.AgentProvider = agentHooks_1.AgentHooks.AgentProvider;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const ActorHooks: import("./types").CreateActorContextReturnType<import("@ic-reactor/core/dist/types").BaseActor>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const AgentHooks: import("./types").CreateAgentContextReturnType;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BaseActor } from "
|
|
2
|
-
import { CreateActorContextParameters, CreateActorContextReturnType } from "
|
|
1
|
+
import { BaseActor } from "../../types";
|
|
2
|
+
import { CreateActorContextParameters, CreateActorContextReturnType } from "../types";
|
|
3
3
|
/**
|
|
4
4
|
* Creates a React context specifically designed for managing the state and interactions with an actor on the Internet Computer (IC) blockchain.
|
|
5
5
|
* This context facilitates the dynamic creation and management of IC actors within React applications, leveraging the provided configuration options.
|
|
@@ -36,8 +36,8 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
36
36
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
37
|
exports.createActorContext = void 0;
|
|
38
38
|
const react_1 = __importStar(require("react"));
|
|
39
|
-
const useActor_1 = require("
|
|
40
|
-
const extractActorContext_1 = require("
|
|
39
|
+
const useActor_1 = require("../hooks/useActor");
|
|
40
|
+
const extractActorContext_1 = require("../../helpers/extractActorContext");
|
|
41
41
|
/**
|
|
42
42
|
* Creates a React context specifically designed for managing the state and interactions with an actor on the Internet Computer (IC) blockchain.
|
|
43
43
|
* This context facilitates the dynamic creation and management of IC actors within React applications, leveraging the provided configuration options.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { AgentManagerParameters } from "
|
|
2
|
-
import type { CreateAgentContextReturnType } from "
|
|
1
|
+
import type { AgentManagerParameters } from "../../types";
|
|
2
|
+
import type { CreateAgentContextReturnType } from "../types";
|
|
3
3
|
/**
|
|
4
4
|
* Creates a React context for managing IC agent and authentication states, providing hooks for interacting with the IC blockchain.
|
|
5
5
|
* This function initializes an `AgentContext` with a set of utilities and hooks based on the provided agent configuration.
|
|
@@ -11,7 +11,7 @@ import type { CreateAgentContextReturnType } from "./types";
|
|
|
11
11
|
*
|
|
12
12
|
* Usage:
|
|
13
13
|
* - `AgentProvider`: React component to provide agent context to your application.
|
|
14
|
-
* - `useAgent`, `
|
|
14
|
+
* - `useAgent`, `useAuth`, `useAuthState`, `useAgentState`, `useAgentManager`, `useUserPrincipal`: Hooks extracted from the created context for managing agent and authentication state within components.
|
|
15
15
|
*
|
|
16
16
|
* @example
|
|
17
17
|
* ```tsx
|
|
@@ -30,7 +30,7 @@ import type { CreateAgentContextReturnType } from "./types";
|
|
|
30
30
|
* export const {
|
|
31
31
|
* AgentProvider,
|
|
32
32
|
* useAgent,
|
|
33
|
-
*
|
|
33
|
+
* useAuth,
|
|
34
34
|
* useAuthState,
|
|
35
35
|
* useAgentState,
|
|
36
36
|
* useAgentManager,
|
|
@@ -51,7 +51,7 @@ import type { CreateAgentContextReturnType } from "./types";
|
|
|
51
51
|
* );
|
|
52
52
|
*
|
|
53
53
|
* const Login = () => {
|
|
54
|
-
* const { login } =
|
|
54
|
+
* const { login } = useAuth()
|
|
55
55
|
* const principal = useUserPrincipal()
|
|
56
56
|
*
|
|
57
57
|
* return (
|
|
@@ -37,9 +37,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
37
37
|
exports.createAgentContext = void 0;
|
|
38
38
|
const react_1 = __importStar(require("react"));
|
|
39
39
|
const core_1 = require("@ic-reactor/core");
|
|
40
|
-
const agentHooks_1 = require("
|
|
41
|
-
const authHooks_1 = require("
|
|
42
|
-
const extractAgentContext_1 = require("
|
|
40
|
+
const agentHooks_1 = require("../../helpers/agentHooks");
|
|
41
|
+
const authHooks_1 = require("../../helpers/authHooks");
|
|
42
|
+
const extractAgentContext_1 = require("../../helpers/extractAgentContext");
|
|
43
43
|
/**
|
|
44
44
|
* Creates a React context for managing IC agent and authentication states, providing hooks for interacting with the IC blockchain.
|
|
45
45
|
* This function initializes an `AgentContext` with a set of utilities and hooks based on the provided agent configuration.
|
|
@@ -51,7 +51,7 @@ const extractAgentContext_1 = require("../helpers/extractAgentContext");
|
|
|
51
51
|
*
|
|
52
52
|
* Usage:
|
|
53
53
|
* - `AgentProvider`: React component to provide agent context to your application.
|
|
54
|
-
* - `useAgent`, `
|
|
54
|
+
* - `useAgent`, `useAuth`, `useAuthState`, `useAgentState`, `useAgentManager`, `useUserPrincipal`: Hooks extracted from the created context for managing agent and authentication state within components.
|
|
55
55
|
*
|
|
56
56
|
* @example
|
|
57
57
|
* ```tsx
|
|
@@ -70,7 +70,7 @@ const extractAgentContext_1 = require("../helpers/extractAgentContext");
|
|
|
70
70
|
* export const {
|
|
71
71
|
* AgentProvider,
|
|
72
72
|
* useAgent,
|
|
73
|
-
*
|
|
73
|
+
* useAuth,
|
|
74
74
|
* useAuthState,
|
|
75
75
|
* useAgentState,
|
|
76
76
|
* useAgentManager,
|
|
@@ -91,7 +91,7 @@ const extractAgentContext_1 = require("../helpers/extractAgentContext");
|
|
|
91
91
|
* );
|
|
92
92
|
*
|
|
93
93
|
* const Login = () => {
|
|
94
|
-
* const { login } =
|
|
94
|
+
* const { login } = useAuth()
|
|
95
95
|
* const principal = useUserPrincipal()
|
|
96
96
|
*
|
|
97
97
|
* return (
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./useActorState"), exports);
|
|
18
|
+
__exportStar(require("./useQueryCall"), exports);
|
|
19
|
+
__exportStar(require("./useUpdateCall"), exports);
|
|
20
|
+
__exportStar(require("./useVisitMethod"), exports);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook for accessing the current state of the actor, including the canister ID.
|
|
3
|
+
*
|
|
4
|
+
* @returns An object containing the current state of the actor from Zustand's store and the canister ID.
|
|
5
|
+
* @example
|
|
6
|
+
* ```tsx
|
|
7
|
+
* function ActorStateComponent() {
|
|
8
|
+
* const { canisterId, initializing, error, initialized } = useActorState();
|
|
9
|
+
*
|
|
10
|
+
* return (
|
|
11
|
+
* <div>
|
|
12
|
+
* <p>Canister ID: {canisterId}</p>
|
|
13
|
+
* <p>Initializing: {initializing.toString()}</p>
|
|
14
|
+
* <p>Initialized: {initialized.toString()}</p>
|
|
15
|
+
* <p>Error: {error?.message}</p>
|
|
16
|
+
* </div>
|
|
17
|
+
* );
|
|
18
|
+
* }
|
|
19
|
+
*```
|
|
20
|
+
*/
|
|
21
|
+
export declare const useActorState: () => import("../../../types").UseActorState;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useActorState = void 0;
|
|
4
|
+
const actorHooks_1 = require("../../actorHooks");
|
|
5
|
+
/**
|
|
6
|
+
* Hook for accessing the current state of the actor, including the canister ID.
|
|
7
|
+
*
|
|
8
|
+
* @returns An object containing the current state of the actor from Zustand's store and the canister ID.
|
|
9
|
+
* @example
|
|
10
|
+
* ```tsx
|
|
11
|
+
* function ActorStateComponent() {
|
|
12
|
+
* const { canisterId, initializing, error, initialized } = useActorState();
|
|
13
|
+
*
|
|
14
|
+
* return (
|
|
15
|
+
* <div>
|
|
16
|
+
* <p>Canister ID: {canisterId}</p>
|
|
17
|
+
* <p>Initializing: {initializing.toString()}</p>
|
|
18
|
+
* <p>Initialized: {initialized.toString()}</p>
|
|
19
|
+
* <p>Error: {error?.message}</p>
|
|
20
|
+
* </div>
|
|
21
|
+
* );
|
|
22
|
+
* }
|
|
23
|
+
*```
|
|
24
|
+
*/
|
|
25
|
+
exports.useActorState = actorHooks_1.ActorHooks.useActorState;
|