@ic-reactor/react 1.0.4 → 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 +13 -23
- package/dist/helpers/{actor.d.ts → actorHooks.d.ts} +4 -7
- package/dist/helpers/{actor.js → actorHooks.js} +8 -15
- package/dist/helpers/agentHooks.d.ts +3 -0
- package/dist/helpers/{agent.js → agentHooks.js} +3 -3
- package/dist/helpers/authHooks.d.ts +2 -0
- package/dist/helpers/authHooks.js +123 -0
- package/dist/helpers/extractActorContext.d.ts +3 -0
- package/dist/helpers/extractActorContext.js +51 -0
- package/dist/helpers/extractAgentContext.d.ts +27 -0
- package/dist/helpers/extractAgentContext.js +56 -0
- package/dist/helpers/index.d.ts +5 -3
- package/dist/helpers/index.js +5 -3
- package/dist/helpers/types.d.ts +30 -40
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/main.d.ts +38 -2
- package/dist/main.js +39 -21
- package/dist/provider/ActorProvider.d.ts +26 -0
- package/dist/provider/ActorProvider.js +29 -0
- package/dist/provider/AgentProvider.d.ts +29 -0
- package/dist/provider/AgentProvider.js +32 -0
- 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/context/actor.d.ts +64 -0
- package/dist/provider/context/actor.js +117 -0
- package/dist/provider/context/agent.d.ts +71 -0
- package/dist/provider/context/agent.js +124 -0
- 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 +3 -0
- package/dist/provider/{agent → hooks}/index.js +3 -2
- package/dist/provider/hooks/types.d.ts +15 -0
- package/dist/provider/hooks/useActor.d.ts +65 -0
- package/dist/{hooks/useReactor.js → provider/hooks/useActor.js} +53 -29
- package/dist/provider/index.d.ts +5 -2
- package/dist/provider/index.js +8 -2
- package/dist/provider/types.d.ts +27 -0
- package/dist/{hooks/index.js → provider/types.js} +1 -1
- package/dist/types.d.ts +8 -8
- package/dist/types.js +2 -4
- package/package.json +3 -3
- package/dist/helpers/agent.d.ts +0 -3
- package/dist/helpers/auth.d.ts +0 -3
- package/dist/helpers/auth.js +0 -108
- package/dist/hooks/index.d.ts +0 -1
- package/dist/hooks/types.d.ts +0 -19
- package/dist/hooks/useReactor.d.ts +0 -41
- package/dist/provider/actor/context.d.ts +0 -20
- package/dist/provider/actor/context.js +0 -184
- package/dist/provider/actor/index.d.ts +0 -3
- package/dist/provider/actor/index.js +0 -9
- package/dist/provider/actor/types.d.ts +0 -21
- package/dist/provider/actor/types.js +0 -2
- package/dist/provider/agent/context.d.ts +0 -7
- package/dist/provider/agent/context.js +0 -56
- package/dist/provider/agent/hooks.d.ts +0 -12
- package/dist/provider/agent/hooks.js +0 -43
- package/dist/provider/agent/index.d.ts +0 -2
- package/dist/provider/agent/types.d.ts +0 -12
- package/dist/provider/agent/types.js +0 -2
- /package/dist/{hooks → provider/hooks}/types.js +0 -0
package/README.md
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
## Features
|
|
4
4
|
|
|
5
5
|
- **React Hooks Integration**: Custom hooks for managing blockchain actor states and authentication within React applications.
|
|
6
|
+
- **Type-Safe Actor Interactions**: Type-safe interaction with IC actors using the provided actor declaration file.
|
|
6
7
|
- **Efficient State Management**: Utilize the power of Zustand for global state management in React components.
|
|
7
8
|
- **Asynchronous Data Handling**: Easy handling of asynchronous operations related to IC actors.
|
|
8
9
|
- **Authentication Support**: Integrated hooks for managing authentication with the IC blockchain.
|
|
@@ -29,25 +30,24 @@ Here's a simple example to get you started:
|
|
|
29
30
|
First, create an actor declaration file:
|
|
30
31
|
|
|
31
32
|
```ts
|
|
32
|
-
//
|
|
33
|
+
// actor.ts
|
|
33
34
|
import { canisterId, idlFactory, actor } from "declaration/actor"
|
|
34
35
|
import { createReactor } from "@ic-reactor/react"
|
|
35
36
|
|
|
36
37
|
type Actor = typeof actor
|
|
37
38
|
|
|
38
|
-
export const { useActorStore,
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
})
|
|
39
|
+
export const { useActorStore, useAuth, useQueryCall } = createReactor<Actor>({
|
|
40
|
+
canisterId,
|
|
41
|
+
idlFactory,
|
|
42
|
+
host: "https://localhost:4943",
|
|
43
|
+
})
|
|
44
44
|
```
|
|
45
45
|
|
|
46
46
|
Then, use the `useQueryCall` hook to call your canister method:
|
|
47
47
|
|
|
48
48
|
```jsx
|
|
49
49
|
// Balance.tsx
|
|
50
|
-
import { useQueryCall } from "./
|
|
50
|
+
import { useQueryCall } from "./actor"
|
|
51
51
|
|
|
52
52
|
const Balance = ({ principal }) => {
|
|
53
53
|
const { call, data, loading, error } = useQueryCall({
|
|
@@ -62,7 +62,7 @@ const Balance = ({ principal }) => {
|
|
|
62
62
|
|
|
63
63
|
return (
|
|
64
64
|
<div>
|
|
65
|
-
<button onClick={
|
|
65
|
+
<button onClick={call} disabled={loading}>
|
|
66
66
|
{loading ? "Loading..." : "Refresh"}
|
|
67
67
|
</button>
|
|
68
68
|
{loading && <p>Loading...</p>}
|
|
@@ -81,7 +81,7 @@ export default Balance
|
|
|
81
81
|
|
|
82
82
|
```jsx
|
|
83
83
|
// Login.tsx
|
|
84
|
-
import {
|
|
84
|
+
import { useAuth } from "./actor"
|
|
85
85
|
|
|
86
86
|
const Login = () => {
|
|
87
87
|
const {
|
|
@@ -92,7 +92,7 @@ const Login = () => {
|
|
|
92
92
|
identity,
|
|
93
93
|
authenticating,
|
|
94
94
|
authenticated,
|
|
95
|
-
} =
|
|
95
|
+
} = useAuth()
|
|
96
96
|
|
|
97
97
|
return (
|
|
98
98
|
<div>
|
|
@@ -104,21 +104,11 @@ const Login = () => {
|
|
|
104
104
|
</div>
|
|
105
105
|
{authenticated ? (
|
|
106
106
|
<div>
|
|
107
|
-
<button onClick={
|
|
107
|
+
<button onClick={logout}>Logout</button>
|
|
108
108
|
</div>
|
|
109
109
|
) : (
|
|
110
110
|
<div>
|
|
111
|
-
<button
|
|
112
|
-
onClick={() =>
|
|
113
|
-
login({
|
|
114
|
-
identityProvider:
|
|
115
|
-
process.env.DFX_NETWORK === "ic"
|
|
116
|
-
? "https://identity.ic0.app/#authorize"
|
|
117
|
-
: "http://rdmx6-jaaaa-aaaaa-aaadq-cai.localhost:4943/#authorize",
|
|
118
|
-
})
|
|
119
|
-
}
|
|
120
|
-
disabled={authenticating}
|
|
121
|
-
>
|
|
111
|
+
<button onClick={login} disabled={authenticating}>
|
|
122
112
|
Login
|
|
123
113
|
</button>
|
|
124
114
|
</div>
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { BaseActor } from "@ic-reactor/core/dist/types";
|
|
3
|
-
import { ActorManager } from "@ic-reactor/core";
|
|
1
|
+
import type { ActorHooksReturnType } from "../types";
|
|
2
|
+
import type { ActorManager, BaseActor } from "@ic-reactor/core/dist/types";
|
|
4
3
|
/**
|
|
5
4
|
* 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
5
|
*
|
|
@@ -11,11 +10,9 @@ import { ActorManager } from "@ic-reactor/core";
|
|
|
11
10
|
* - initialize: Function to initialize actor management.
|
|
12
11
|
* - useActorState: Hook for accessing the actor's state including the canister ID.
|
|
13
12
|
* - 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
13
|
* - useQueryCall: Hook specifically designed for query calls to actors with features such as automatic refetching on mount and at specified intervals.
|
|
16
|
-
* - useUpdateCall:
|
|
17
|
-
* - useMethodCall: Combines useVisitMethod and useReactorCall for a streamlined experience when calling actor methods, including visitation and state management.
|
|
14
|
+
* - useUpdateCall: Hook specifically designed for update calls to actors with features such as error handling and loading state management.
|
|
18
15
|
*
|
|
19
16
|
* 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
17
|
*/
|
|
21
|
-
export declare const
|
|
18
|
+
export declare const actorHooks: <A = BaseActor>(actorManager: ActorManager<A>) => ActorHooksReturnType<A>;
|
|
@@ -20,7 +20,7 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
20
20
|
return t;
|
|
21
21
|
};
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
exports.
|
|
23
|
+
exports.actorHooks = void 0;
|
|
24
24
|
const react_1 = require("react");
|
|
25
25
|
const zustand_1 = require("zustand");
|
|
26
26
|
const DEFAULT_STATE = {
|
|
@@ -38,26 +38,24 @@ const DEFAULT_STATE = {
|
|
|
38
38
|
* - initialize: Function to initialize actor management.
|
|
39
39
|
* - useActorState: Hook for accessing the actor's state including the canister ID.
|
|
40
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
41
|
* - useQueryCall: Hook specifically designed for query calls to actors with features such as automatic refetching on mount and at specified intervals.
|
|
43
|
-
* - useUpdateCall:
|
|
44
|
-
* - useMethodCall: Combines useVisitMethod and useReactorCall for a streamlined experience when calling actor methods, including visitation and state management.
|
|
42
|
+
* - useUpdateCall: Hook specifically designed for update calls to actors with features such as error handling and loading state management.
|
|
45
43
|
*
|
|
46
44
|
* 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
45
|
*/
|
|
48
|
-
const
|
|
46
|
+
const actorHooks = (actorManager) => {
|
|
49
47
|
const { actorStore, canisterId, visitFunction, callMethod, initialize } = actorManager;
|
|
50
48
|
const useActorState = () => (Object.assign(Object.assign({}, (0, zustand_1.useStore)(actorStore)), { canisterId }));
|
|
51
49
|
const useVisitMethod = (functionName) => {
|
|
52
50
|
return (0, react_1.useMemo)(() => visitFunction[functionName], [functionName]);
|
|
53
51
|
};
|
|
54
|
-
const
|
|
52
|
+
const useMethodCall = (_a) => {
|
|
55
53
|
var { args = [], functionName, throwOnError = false } = _a, events = __rest(_a, ["args", "functionName", "throwOnError"]);
|
|
56
54
|
const [state, setState] = (0, react_1.useState)(DEFAULT_STATE);
|
|
57
55
|
const reset = (0, react_1.useCallback)(() => setState(DEFAULT_STATE), []);
|
|
58
56
|
const call = (0, react_1.useCallback)((eventOrReplaceArgs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
57
|
var _b, _c, _d, _e, _f;
|
|
60
|
-
setState(
|
|
58
|
+
setState((prev) => (Object.assign(Object.assign({}, prev), { error: undefined, loading: true })));
|
|
61
59
|
(_b = events === null || events === void 0 ? void 0 : events.onLoading) === null || _b === void 0 ? void 0 : _b.call(events, true);
|
|
62
60
|
try {
|
|
63
61
|
const replaceArgs = eventOrReplaceArgs instanceof Array ? eventOrReplaceArgs : args;
|
|
@@ -81,7 +79,7 @@ const getActorHooks = (actorManager) => {
|
|
|
81
79
|
};
|
|
82
80
|
const useQueryCall = (_a) => {
|
|
83
81
|
var { refetchOnMount = true, refetchInterval = false } = _a, rest = __rest(_a, ["refetchOnMount", "refetchInterval"]);
|
|
84
|
-
const _b =
|
|
82
|
+
const _b = useMethodCall(rest), { call } = _b, state = __rest(_b, ["call"]);
|
|
85
83
|
const intervalId = (0, react_1.useRef)();
|
|
86
84
|
(0, react_1.useEffect)(() => {
|
|
87
85
|
if (refetchInterval) {
|
|
@@ -94,18 +92,13 @@ const getActorHooks = (actorManager) => {
|
|
|
94
92
|
}, [refetchInterval, refetchOnMount]);
|
|
95
93
|
return Object.assign({ call }, state);
|
|
96
94
|
};
|
|
97
|
-
const useUpdateCall =
|
|
98
|
-
const useMethodCall = (args) => {
|
|
99
|
-
const visit = useVisitMethod(args.functionName);
|
|
100
|
-
return Object.assign({ visit }, useReactorCall(args));
|
|
101
|
-
};
|
|
95
|
+
const useUpdateCall = useMethodCall;
|
|
102
96
|
return {
|
|
103
97
|
initialize,
|
|
104
98
|
useQueryCall,
|
|
105
99
|
useUpdateCall,
|
|
106
100
|
useActorState,
|
|
107
|
-
useMethodCall,
|
|
108
101
|
useVisitMethod,
|
|
109
102
|
};
|
|
110
103
|
};
|
|
111
|
-
exports.
|
|
104
|
+
exports.actorHooks = actorHooks;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.agentHooks = void 0;
|
|
4
4
|
const react_1 = require("react");
|
|
5
5
|
const zustand_1 = require("zustand");
|
|
6
|
-
const
|
|
6
|
+
const agentHooks = (agentManager) => {
|
|
7
7
|
const { agentStore, getAgent, subscribeAgent } = agentManager;
|
|
8
8
|
const useAgentState = () => (0, zustand_1.useStore)(agentStore);
|
|
9
9
|
const useAgent = () => {
|
|
@@ -16,4 +16,4 @@ const getAgentHooks = (agentManager) => {
|
|
|
16
16
|
useAgentState,
|
|
17
17
|
};
|
|
18
18
|
};
|
|
19
|
-
exports.
|
|
19
|
+
exports.agentHooks = agentHooks;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.authHooks = void 0;
|
|
13
|
+
const zustand_1 = require("zustand");
|
|
14
|
+
const react_1 = require("react");
|
|
15
|
+
const tools_1 = require("@ic-reactor/core/dist/tools");
|
|
16
|
+
const authHooks = (agentManager) => {
|
|
17
|
+
const { authenticate: authenticator, getAuth, authStore, isLocalEnv, } = agentManager;
|
|
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 useAuth = ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, } = {}) => {
|
|
21
|
+
const [loginState, setLoginState] = (0, react_1.useState)({
|
|
22
|
+
loading: false,
|
|
23
|
+
error: null,
|
|
24
|
+
});
|
|
25
|
+
const { authenticated, authenticating, error, identity } = useAuthState();
|
|
26
|
+
const authenticate = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
|
+
const authenticatePromise = new Promise((resolve, reject) => {
|
|
28
|
+
authenticator()
|
|
29
|
+
.then((identity) => {
|
|
30
|
+
onAuthenticationSuccess === null || onAuthenticationSuccess === void 0 ? void 0 : onAuthenticationSuccess(identity);
|
|
31
|
+
resolve(identity);
|
|
32
|
+
})
|
|
33
|
+
.catch((e) => {
|
|
34
|
+
onAuthenticationFailure === null || onAuthenticationFailure === void 0 ? void 0 : onAuthenticationFailure(e);
|
|
35
|
+
reject(e);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
onAuthentication === null || onAuthentication === void 0 ? void 0 : onAuthentication(() => authenticatePromise);
|
|
39
|
+
return authenticatePromise;
|
|
40
|
+
}), [
|
|
41
|
+
authenticator,
|
|
42
|
+
onAuthentication,
|
|
43
|
+
onAuthenticationSuccess,
|
|
44
|
+
onAuthenticationFailure,
|
|
45
|
+
]);
|
|
46
|
+
const login = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
47
|
+
setLoginState({ loading: true, error: null });
|
|
48
|
+
const loginPromise = new Promise((resolve, reject) => {
|
|
49
|
+
try {
|
|
50
|
+
const authClient = getAuth();
|
|
51
|
+
if (!authClient) {
|
|
52
|
+
throw new Error("Auth client not initialized");
|
|
53
|
+
}
|
|
54
|
+
authClient.login(Object.assign(Object.assign({ identityProvider: isLocalEnv
|
|
55
|
+
? tools_1.IC_INTERNET_IDENTITY_PROVIDER
|
|
56
|
+
: tools_1.LOCAL_INTERNET_IDENTITY_PROVIDER }, options), { onSuccess: () => {
|
|
57
|
+
authenticate()
|
|
58
|
+
.then((identity) => {
|
|
59
|
+
var _a;
|
|
60
|
+
const principal = identity.getPrincipal();
|
|
61
|
+
(_a = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _a === void 0 ? void 0 : _a.call(options);
|
|
62
|
+
onLoginSuccess === null || onLoginSuccess === void 0 ? void 0 : onLoginSuccess(principal);
|
|
63
|
+
resolve(principal);
|
|
64
|
+
setLoginState({ loading: false, error: null });
|
|
65
|
+
})
|
|
66
|
+
.catch((e) => {
|
|
67
|
+
const error = e;
|
|
68
|
+
setLoginState({ loading: false, error });
|
|
69
|
+
onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(error);
|
|
70
|
+
reject(error);
|
|
71
|
+
});
|
|
72
|
+
}, onError: (e) => {
|
|
73
|
+
var _a;
|
|
74
|
+
(_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, e);
|
|
75
|
+
const error = new Error("Login failed: " + e);
|
|
76
|
+
setLoginState({ loading: false, error });
|
|
77
|
+
onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(error);
|
|
78
|
+
reject(error);
|
|
79
|
+
} }));
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
const error = e;
|
|
83
|
+
setLoginState({ loading: false, error });
|
|
84
|
+
onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(error);
|
|
85
|
+
reject(error);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
onLogin === null || onLogin === void 0 ? void 0 : onLogin(() => loginPromise);
|
|
89
|
+
}), [onLogin, onLoginSuccess, onLoginError, isLocalEnv, authenticate]);
|
|
90
|
+
const logout = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
91
|
+
const authClient = getAuth();
|
|
92
|
+
if (!authClient) {
|
|
93
|
+
throw new Error("Auth client not initialized");
|
|
94
|
+
}
|
|
95
|
+
yield authClient.logout(options);
|
|
96
|
+
yield authenticate();
|
|
97
|
+
onLoggedOut === null || onLoggedOut === void 0 ? void 0 : onLoggedOut();
|
|
98
|
+
}), [onLoggedOut]);
|
|
99
|
+
(0, react_1.useEffect)(() => {
|
|
100
|
+
const authClient = getAuth();
|
|
101
|
+
if (!authClient && !authenticating) {
|
|
102
|
+
authenticate();
|
|
103
|
+
}
|
|
104
|
+
}, [authenticating]);
|
|
105
|
+
return {
|
|
106
|
+
authenticated,
|
|
107
|
+
authenticating,
|
|
108
|
+
identity,
|
|
109
|
+
error,
|
|
110
|
+
login,
|
|
111
|
+
logout,
|
|
112
|
+
authenticate,
|
|
113
|
+
loginLoading: loginState.loading,
|
|
114
|
+
loginError: loginState.error,
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
return {
|
|
118
|
+
useUserPrincipal,
|
|
119
|
+
useAuthState,
|
|
120
|
+
useAuth,
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
exports.authHooks = authHooks;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import type { ActorHooksReturnType, BaseActor, CreateActorContextReturnType } from "../types";
|
|
3
|
+
export declare function extractActorContext<A = BaseActor>(actorContext: React.Context<ActorHooksReturnType<A> | null>): Omit<CreateActorContextReturnType<A>, "ActorProvider">;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractActorContext = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function extractActorContext(actorContext) {
|
|
6
|
+
/**
|
|
7
|
+
* Hook for accessing the actor context, including the actor manager and state.
|
|
8
|
+
* @returns The actor context, including the actor manager and state.
|
|
9
|
+
* @example
|
|
10
|
+
* ```tsx
|
|
11
|
+
* function ActorComponent() {
|
|
12
|
+
* const { initialize, useActorState, useQueryCall, useUpdateCall, useMethodCall, useVisitMethod } = useActorContext();
|
|
13
|
+
* const { canisterId } = useActorState();
|
|
14
|
+
*
|
|
15
|
+
* return (
|
|
16
|
+
* <div>
|
|
17
|
+
* <p>Canister ID: {canisterId}</p>
|
|
18
|
+
* </div>
|
|
19
|
+
* );
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* function App() {
|
|
23
|
+
* return (
|
|
24
|
+
* <ActorProvider canisterId="rrkah-fqaaa-aaaaa-aaaaq-cai">
|
|
25
|
+
* <ActorComponent />
|
|
26
|
+
* </ActorProvider>
|
|
27
|
+
* );
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
const useActorContext = () => {
|
|
32
|
+
const context = (0, react_1.useContext)(actorContext);
|
|
33
|
+
if (!context) {
|
|
34
|
+
throw new Error("Actor hooks must be used within a ActorProvider");
|
|
35
|
+
}
|
|
36
|
+
return context;
|
|
37
|
+
};
|
|
38
|
+
const initialize = () => useActorContext().initialize();
|
|
39
|
+
const useActorState = () => useActorContext().useActorState();
|
|
40
|
+
const useQueryCall = (args) => useActorContext().useQueryCall(args);
|
|
41
|
+
const useUpdateCall = (args) => useActorContext().useUpdateCall(args);
|
|
42
|
+
const useVisitMethod = (functionName) => useActorContext().useVisitMethod(functionName);
|
|
43
|
+
return {
|
|
44
|
+
useActorState,
|
|
45
|
+
useQueryCall,
|
|
46
|
+
useUpdateCall,
|
|
47
|
+
useVisitMethod,
|
|
48
|
+
initialize,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
exports.extractActorContext = extractActorContext;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { AgentContext, CreateAgentContextReturnType } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* This function facilitates the use of contextually provided agent functionalities,
|
|
4
|
+
* such as managing the agent's state, authentication state, and user principal.
|
|
5
|
+
*
|
|
6
|
+
* @param agentContext A React context object of type AgentContext or null,
|
|
7
|
+
* typically provided by an AgentProvider at a higher level in the component tree.
|
|
8
|
+
* @returns An object containing the following hooks:
|
|
9
|
+
* - useAgent: Hook for accessing the current agent instance.
|
|
10
|
+
* - useAuthState: Hook for accessing the current authentication state.
|
|
11
|
+
* - useAgentState: Hook for accessing the current state of the agent.
|
|
12
|
+
* - useAuth: Hook for accessing the authentication client, optionally accepting arguments for configuration.
|
|
13
|
+
* - useAgentManager: Hook for accessing the AgentManager instance.
|
|
14
|
+
* - useUserPrincipal: Hook for accessing the user's principal.
|
|
15
|
+
*
|
|
16
|
+
* Each hook is designed to be used within components that are descendants of an AgentProvider,
|
|
17
|
+
* ensuring access to the necessary agent-related context.
|
|
18
|
+
*
|
|
19
|
+
* Throws:
|
|
20
|
+
* - Error if used outside of an AgentProvider context.
|
|
21
|
+
*
|
|
22
|
+
* ### Integration
|
|
23
|
+
*
|
|
24
|
+
* To use these hooks, ensure your components are wrapped in an `AgentProvider` that you've set up to supply the `AgentContext`.
|
|
25
|
+
* This context provides the necessary agent functionalities and state management capabilities required by the hooks.
|
|
26
|
+
*/
|
|
27
|
+
export declare const extractAgentContext: (agentContext: React.Context<AgentContext | null>) => Omit<CreateAgentContextReturnType, "AgentProvider">;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractAgentContext = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
/**
|
|
6
|
+
* This function facilitates the use of contextually provided agent functionalities,
|
|
7
|
+
* such as managing the agent's state, authentication state, and user principal.
|
|
8
|
+
*
|
|
9
|
+
* @param agentContext A React context object of type AgentContext or null,
|
|
10
|
+
* typically provided by an AgentProvider at a higher level in the component tree.
|
|
11
|
+
* @returns An object containing the following hooks:
|
|
12
|
+
* - useAgent: Hook for accessing the current agent instance.
|
|
13
|
+
* - useAuthState: Hook for accessing the current authentication state.
|
|
14
|
+
* - useAgentState: Hook for accessing the current state of the agent.
|
|
15
|
+
* - useAuth: Hook for accessing the authentication client, optionally accepting arguments for configuration.
|
|
16
|
+
* - useAgentManager: Hook for accessing the AgentManager instance.
|
|
17
|
+
* - useUserPrincipal: Hook for accessing the user's principal.
|
|
18
|
+
*
|
|
19
|
+
* Each hook is designed to be used within components that are descendants of an AgentProvider,
|
|
20
|
+
* ensuring access to the necessary agent-related context.
|
|
21
|
+
*
|
|
22
|
+
* Throws:
|
|
23
|
+
* - Error if used outside of an AgentProvider context.
|
|
24
|
+
*
|
|
25
|
+
* ### Integration
|
|
26
|
+
*
|
|
27
|
+
* To use these hooks, ensure your components are wrapped in an `AgentProvider` that you've set up to supply the `AgentContext`.
|
|
28
|
+
* This context provides the necessary agent functionalities and state management capabilities required by the hooks.
|
|
29
|
+
*/
|
|
30
|
+
const extractAgentContext = (agentContext) => {
|
|
31
|
+
const useAgentContext = (mybeAgentContext) => {
|
|
32
|
+
const context = (0, react_1.useContext)(mybeAgentContext || agentContext);
|
|
33
|
+
if (!context) {
|
|
34
|
+
throw new Error("Agent hooks must be used within a AgentProvider");
|
|
35
|
+
}
|
|
36
|
+
return context;
|
|
37
|
+
};
|
|
38
|
+
const useAgentManager = (agentContext) => {
|
|
39
|
+
const context = useAgentContext(agentContext);
|
|
40
|
+
return context.agentManager;
|
|
41
|
+
};
|
|
42
|
+
const useAgent = () => useAgentContext().useAgent();
|
|
43
|
+
const useAuthState = () => useAgentContext().useAuthState();
|
|
44
|
+
const useAgentState = () => useAgentContext().useAgentState();
|
|
45
|
+
const useAuth = (args) => useAgentContext().useAuth(args);
|
|
46
|
+
const useUserPrincipal = () => useAgentContext().useUserPrincipal();
|
|
47
|
+
return {
|
|
48
|
+
useAgent,
|
|
49
|
+
useAuthState,
|
|
50
|
+
useAgentState,
|
|
51
|
+
useAuth: useAuth,
|
|
52
|
+
useAgentManager,
|
|
53
|
+
useUserPrincipal,
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
exports.extractAgentContext = extractAgentContext;
|
package/dist/helpers/index.d.ts
CHANGED
package/dist/helpers/index.js
CHANGED
|
@@ -14,6 +14,8 @@ 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("./
|
|
19
|
-
__exportStar(require("./
|
|
17
|
+
__exportStar(require("./agentHooks"), exports);
|
|
18
|
+
__exportStar(require("./actorHooks"), exports);
|
|
19
|
+
__exportStar(require("./authHooks"), exports);
|
|
20
|
+
__exportStar(require("./extractActorContext"), exports);
|
|
21
|
+
__exportStar(require("./extractAgentContext"), exports);
|
package/dist/helpers/types.d.ts
CHANGED
|
@@ -1,24 +1,15 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import type { AuthClientLoginOptions } from "@
|
|
3
|
-
|
|
4
|
-
export interface AgentHooks {
|
|
2
|
+
import type { ActorState, CanisterId, AuthClientLoginOptions, ActorMethodParameters, ActorMethodReturnType, Identity, Principal, FunctionName, VisitService, AuthState, HttpAgent, AgentState, BaseActor } from "@ic-reactor/core/dist/types";
|
|
3
|
+
export interface AgentHooksReturnType {
|
|
5
4
|
useAgent: () => HttpAgent | undefined;
|
|
6
5
|
useAgentState: () => AgentState;
|
|
7
6
|
}
|
|
8
|
-
export interface
|
|
9
|
-
|
|
7
|
+
export interface AuthHooksReturnType {
|
|
8
|
+
useAuth: (options?: UseAuthParameters) => UseAuthReturnType;
|
|
10
9
|
useAuthState: () => AuthState;
|
|
11
|
-
|
|
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];
|
|
10
|
+
useUserPrincipal: () => Principal | undefined;
|
|
20
11
|
}
|
|
21
|
-
export interface
|
|
12
|
+
export interface UseAuthParameters {
|
|
22
13
|
onAuthentication?: (promise: () => Promise<Identity>) => void;
|
|
23
14
|
onAuthenticationSuccess?: (identity: Identity) => void;
|
|
24
15
|
onAuthenticationFailure?: (error: Error) => void;
|
|
@@ -27,14 +18,13 @@ export interface UseAuthClientArgs {
|
|
|
27
18
|
onLogin?: (promise: () => Promise<Principal>) => void;
|
|
28
19
|
onLoggedOut?: () => void;
|
|
29
20
|
}
|
|
30
|
-
export interface
|
|
21
|
+
export interface UseAuthReturnType {
|
|
31
22
|
error: Error | undefined;
|
|
32
|
-
authClient: AuthClient | null;
|
|
33
23
|
authenticated: boolean;
|
|
34
24
|
authenticating: boolean;
|
|
35
25
|
identity: Identity | null;
|
|
36
|
-
login: (options?:
|
|
37
|
-
logout: (options?:
|
|
26
|
+
login: (options?: LoginParameters) => Promise<void>;
|
|
27
|
+
logout: (options?: LogoutParameters) => Promise<void>;
|
|
38
28
|
authenticate: () => Promise<Identity>;
|
|
39
29
|
loginLoading: boolean;
|
|
40
30
|
loginError: Error | null;
|
|
@@ -43,13 +33,16 @@ export type LoginState = {
|
|
|
43
33
|
loading: boolean;
|
|
44
34
|
error: Error | null;
|
|
45
35
|
};
|
|
46
|
-
export type
|
|
47
|
-
export type
|
|
36
|
+
export type LoginParameters = AuthClientLoginOptions;
|
|
37
|
+
export type LogoutParameters = {
|
|
48
38
|
returnTo?: string;
|
|
49
39
|
};
|
|
50
|
-
export
|
|
40
|
+
export interface UseActorState extends Omit<ActorState, "methodState"> {
|
|
41
|
+
canisterId: CanisterId;
|
|
42
|
+
}
|
|
43
|
+
export type UseMethodCallParameters<A, M extends FunctionName<A>> = {
|
|
51
44
|
functionName: M;
|
|
52
|
-
args?:
|
|
45
|
+
args?: ActorMethodParameters<A[M]>;
|
|
53
46
|
onLoading?: (loading: boolean) => void;
|
|
54
47
|
onError?: (error: Error | undefined) => void;
|
|
55
48
|
onSuccess?: (data: ActorMethodReturnType<A[M]> | undefined) => void;
|
|
@@ -60,27 +53,24 @@ export type ActorCallState<A, M extends FunctionName<A>> = {
|
|
|
60
53
|
error: Error | undefined;
|
|
61
54
|
loading: boolean;
|
|
62
55
|
};
|
|
63
|
-
export interface
|
|
56
|
+
export interface UseQueryCallParameters<A, M extends FunctionName<A>> extends UseMethodCallParameters<A, M> {
|
|
64
57
|
refetchOnMount?: boolean;
|
|
65
58
|
refetchInterval?: number | false;
|
|
66
59
|
}
|
|
67
|
-
export interface
|
|
60
|
+
export interface UseUpdateCallParameters<A, M extends FunctionName<A>> extends UseMethodCallParameters<A, M> {
|
|
68
61
|
}
|
|
69
|
-
export interface
|
|
62
|
+
export interface UseMethodCallReturnType<A, M extends FunctionName<A> = FunctionName<A>> extends ActorCallState<A, M> {
|
|
70
63
|
reset: () => void;
|
|
71
|
-
call: (eventOrReplaceArgs?: React.MouseEvent |
|
|
64
|
+
call: (eventOrReplaceArgs?: React.MouseEvent | ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]> | undefined>;
|
|
72
65
|
}
|
|
73
|
-
export type
|
|
74
|
-
export type UseQueryCall<A> = <M extends FunctionName<A>>(args:
|
|
75
|
-
export type UseUpdateCall<A> = <M extends FunctionName<A>>(args:
|
|
76
|
-
export type
|
|
77
|
-
export interface
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
export type UseMethodCallArg<A, M extends FunctionName<A>> = ReactorCallArgs<A, M>;
|
|
84
|
-
export interface UseActorState extends Omit<ActorState, "methodState"> {
|
|
85
|
-
canisterId: CanisterId;
|
|
66
|
+
export type UseMethodCall<A> = <M extends FunctionName<A>>(args: UseMethodCallParameters<A, M>) => UseMethodCallReturnType<A, M>;
|
|
67
|
+
export type UseQueryCall<A> = <M extends FunctionName<A>>(args: UseQueryCallParameters<A, M>) => UseMethodCallReturnType<A, M>;
|
|
68
|
+
export type UseUpdateCall<A> = <M extends FunctionName<A>>(args: UseUpdateCallParameters<A, M>) => UseMethodCallReturnType<A, M>;
|
|
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>;
|
|
86
76
|
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -28,7 +28,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.core = exports.types = exports.helpers = void 0;
|
|
30
30
|
__exportStar(require("./main"), exports);
|
|
31
|
-
__exportStar(require("./hooks"), exports);
|
|
32
31
|
__exportStar(require("./provider"), exports);
|
|
33
32
|
exports.helpers = __importStar(require("./helpers"));
|
|
34
33
|
exports.types = __importStar(require("./types"));
|