@ic-reactor/react 2.0.0-alpha.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/context/actor/create.js +28 -27
- package/dist/context/actor/hooks/useActorInterface.d.ts +1 -1
- package/dist/context/actor/hooks/useActorInterface.js +1 -2
- package/dist/context/actor/hooks/useMethod.d.ts +3 -3
- package/dist/context/actor/hooks/useMethod.js +4 -5
- package/dist/context/actor/hooks/useQueryCall.d.ts +5 -5
- package/dist/context/actor/hooks/useQueryCall.js +4 -5
- package/dist/context/actor/hooks/useUpdateCall.d.ts +6 -6
- package/dist/context/actor/hooks/useUpdateCall.js +5 -6
- package/dist/context/actor/hooks/useVisitMethod.d.ts +1 -1
- package/dist/context/actor/hooks/useVisitMethod.js +1 -2
- package/dist/context/actor/hooks/useVisitService.js +1 -2
- package/dist/context/actor/provider.d.ts +22 -1
- package/dist/context/actor/provider.js +23 -1
- package/dist/context/actor/types.d.ts +6 -2
- package/dist/context/adapter/create.d.ts +2 -2
- package/dist/context/adapter/create.js +24 -32
- package/dist/context/adapter/index.d.ts +1 -2
- package/dist/context/adapter/provider.d.ts +0 -1
- package/dist/context/adapter/types.d.ts +33 -6
- package/dist/context/agent/create.js +11 -19
- package/dist/context/agent/hooks/useAuth.d.ts +1 -1
- package/dist/context/agent/index.d.ts +2 -1
- package/dist/context/agent/index.js +2 -1
- package/dist/context/agent/provider.d.ts +0 -1
- package/dist/createReactor.js +7 -2
- package/dist/helpers/actorHooks.js +111 -59
- package/dist/helpers/authHooks.js +46 -51
- package/dist/helpers/extractActorContext.d.ts +1 -1
- package/dist/helpers/extractActorContext.js +2 -3
- package/dist/helpers/types.d.ts +138 -20
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/index.js +1 -0
- package/dist/hooks/types.d.ts +9 -4
- package/dist/hooks/useActor.js +37 -38
- package/dist/hooks/useActorManager.d.ts +68 -0
- package/dist/hooks/useActorManager.js +75 -0
- package/dist/index.js +17 -7
- package/dist/types.d.ts +1 -2
- package/package.json +17 -16
package/dist/helpers/types.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import type { IDL, ActorState, AuthClientLoginOptions, ActorMethodParameters, ActorMethodReturnType, Identity, Principal, FunctionName, VisitService, AuthState, HttpAgent, AgentState, BaseActor, MethodAttributes } from "../types";
|
|
1
|
+
import { AgentError, CallConfig } from "@dfinity/agent";
|
|
2
|
+
import type { IDL, ActorState, AuthClientLoginOptions, ActorMethodParameters, ActorMethodReturnType, Identity, Principal, FunctionName, VisitService, AuthState, HttpAgent, AgentState, BaseActor, MethodAttributes, CompiledResult, ExtractOk, ExtractErr } from "../types";
|
|
4
3
|
export interface AgentHooksReturnType {
|
|
5
4
|
useAgent: () => HttpAgent | undefined;
|
|
6
5
|
useAgentState: () => AgentState;
|
|
@@ -19,19 +18,80 @@ export interface UseAuthParameters {
|
|
|
19
18
|
onLogin?: (promise: () => Promise<Principal>) => void;
|
|
20
19
|
onLoggedOut?: () => void;
|
|
21
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* The return type for authentication hooks.
|
|
23
|
+
*/
|
|
22
24
|
export interface UseAuthReturnType {
|
|
25
|
+
/**
|
|
26
|
+
* Any non-login related error that occurred.
|
|
27
|
+
*/
|
|
23
28
|
error: Error | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* @deprecated Use `isAuthenticated` instead.
|
|
31
|
+
* Indicates whether the user is authenticated.
|
|
32
|
+
*/
|
|
24
33
|
authenticated: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Indicates whether the user is authenticated.
|
|
36
|
+
*/
|
|
37
|
+
isAuthenticated: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* @deprecated Use `isAuthenticating` instead.
|
|
40
|
+
* Indicates whether an authentication request is in progress.
|
|
41
|
+
*/
|
|
25
42
|
authenticating: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Indicates whether an authentication request is in progress.
|
|
45
|
+
*/
|
|
46
|
+
isAuthenticating: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* The current identity object, or `null` if not authenticated.
|
|
49
|
+
*/
|
|
26
50
|
identity: Identity | null;
|
|
51
|
+
/**
|
|
52
|
+
* Initiates the login flow with optional parameters.
|
|
53
|
+
* @param options Login parameters (e.g. redirect URL).
|
|
54
|
+
*/
|
|
27
55
|
login: (options?: LoginParameters) => Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Logs the user out with optional parameters.
|
|
58
|
+
* @param options Logout parameters (e.g. return URL).
|
|
59
|
+
*/
|
|
28
60
|
logout: (options?: LogoutParameters) => Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Triggers the authentication flow and resolves to an `Identity`.
|
|
63
|
+
*/
|
|
29
64
|
authenticate: () => Promise<Identity>;
|
|
65
|
+
/**
|
|
66
|
+
* @deprecated Use `isLoginLoading` instead.
|
|
67
|
+
* Indicates whether the login operation is in progress.
|
|
68
|
+
*/
|
|
30
69
|
loginLoading: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Indicates whether the login operation is in progress.
|
|
72
|
+
*/
|
|
73
|
+
isLoginLoading: boolean;
|
|
74
|
+
/**
|
|
75
|
+
* The error message, if any, occurred during login.
|
|
76
|
+
*/
|
|
31
77
|
loginError: string | undefined;
|
|
32
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Represents the state of a login operation.
|
|
81
|
+
*/
|
|
33
82
|
export type LoginState = {
|
|
83
|
+
/**
|
|
84
|
+
* @deprecated Use `isLoading` instead.
|
|
85
|
+
* Indicates whether the login operation is in progress.
|
|
86
|
+
*/
|
|
34
87
|
loading: boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Indicates whether the login operation is in progress.
|
|
90
|
+
*/
|
|
91
|
+
isLoading: boolean;
|
|
92
|
+
/**
|
|
93
|
+
* The error message, if any, occurred during login.
|
|
94
|
+
*/
|
|
35
95
|
error: string | undefined;
|
|
36
96
|
};
|
|
37
97
|
export type LoginParameters = AuthClientLoginOptions;
|
|
@@ -42,49 +102,107 @@ export type UseActorStore<A> = <T>(callback?: (state: ActorState<A>) => T) => T;
|
|
|
42
102
|
export interface UseActorStateReturnType extends Omit<ActorState, "methodState"> {
|
|
43
103
|
canisterId: string;
|
|
44
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* State for shared calls, including the result, error, and loading status.
|
|
107
|
+
*/
|
|
108
|
+
export type UseSharedCallState<A, M extends FunctionName<A>> = {
|
|
109
|
+
/**
|
|
110
|
+
* The data returned from the call, or `undefined` if not yet available.
|
|
111
|
+
*/
|
|
112
|
+
data: ActorMethodReturnType<A[M]> | undefined;
|
|
113
|
+
/**
|
|
114
|
+
* The error that occurred during the call, or `undefined` if none.
|
|
115
|
+
*/
|
|
116
|
+
error: AgentError | undefined;
|
|
117
|
+
/**
|
|
118
|
+
* @deprecated Use `isLoading` instead.
|
|
119
|
+
* Indicates whether the call is in progress.
|
|
120
|
+
*/
|
|
121
|
+
loading: boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Indicates whether the call is in progress.
|
|
124
|
+
*/
|
|
125
|
+
isLoading: boolean;
|
|
126
|
+
};
|
|
45
127
|
export interface UseSharedCallParameters<A, M extends FunctionName<A>> extends CallConfig {
|
|
46
128
|
functionName: M;
|
|
47
129
|
args?: ActorMethodParameters<A[M]>;
|
|
48
130
|
onLoading?: (loading: boolean) => void;
|
|
49
|
-
onError?: (error:
|
|
50
|
-
onSuccess?: (data: ActorMethodReturnType<A[M]>
|
|
131
|
+
onError?: (error: AgentError | undefined) => void;
|
|
132
|
+
onSuccess?: (data: ActorMethodReturnType<A[M]>) => void;
|
|
133
|
+
onSuccessResult?: (value: ExtractOk<ActorMethodReturnType<A[M]>>) => void;
|
|
134
|
+
onErrorResult?: (error: ExtractErr<ActorMethodReturnType<A[M]>>) => void;
|
|
51
135
|
throwOnError?: boolean;
|
|
52
136
|
}
|
|
53
|
-
export type UseSharedCallState<A, M extends FunctionName<A>> = {
|
|
54
|
-
data: ActorMethodReturnType<A[M]> | undefined;
|
|
55
|
-
error: Error | undefined;
|
|
56
|
-
loading: boolean;
|
|
57
|
-
};
|
|
58
137
|
export interface UseSharedCallReturnType<A, M extends FunctionName<A> = FunctionName<A>> extends UseSharedCallState<A, M> {
|
|
59
138
|
requestKey: string;
|
|
60
139
|
reset: () => void;
|
|
61
|
-
|
|
140
|
+
compileResult: () => CompiledResult<ActorMethodReturnType<A[M]>>;
|
|
141
|
+
call: (eventOrReplaceArgs?: ActorMethodParameters<A[M]> | React.MouseEvent) => Promise<ActorMethodReturnType<A[M]> | undefined>;
|
|
62
142
|
}
|
|
63
143
|
export type UseSharedCall<A> = <M extends FunctionName<A>>(params: UseSharedCallParameters<A, M>) => UseSharedCallReturnType<A, M>;
|
|
64
144
|
export interface UseQueryCallParameters<A, M extends FunctionName<A>> extends UseSharedCallParameters<A, M> {
|
|
65
145
|
refetchOnMount?: boolean;
|
|
66
146
|
refetchInterval?: number | false;
|
|
67
147
|
}
|
|
68
|
-
export
|
|
69
|
-
|
|
148
|
+
export interface UseQueryCallReturnType<A, M extends FunctionName<A>> extends UseSharedCallReturnType<A, M> {
|
|
149
|
+
refetch: () => void;
|
|
70
150
|
}
|
|
71
|
-
export type
|
|
151
|
+
export type UseQueryCall<A> = <M extends FunctionName<A>>(params: UseQueryCallParameters<A, M>) => UseQueryCallReturnType<A, M>;
|
|
152
|
+
export type UseUpdateCallParameters<A, M extends FunctionName<A>> = UseSharedCallParameters<A, M>;
|
|
153
|
+
export type UseUpdateCallReturnType<A, M extends FunctionName<A>> = UseSharedCallReturnType<A, M>;
|
|
154
|
+
export type UseUpdateCall<A> = <M extends FunctionName<A>>(params: UseUpdateCallParameters<A, M>) => UseUpdateCallReturnType<A, M>;
|
|
72
155
|
export interface DynamicDataArgs<V = unknown> {
|
|
73
156
|
label: string;
|
|
74
157
|
value: V;
|
|
75
158
|
}
|
|
76
|
-
export
|
|
77
|
-
}
|
|
159
|
+
export type UseMethodParameters<A, M extends FunctionName<A>> = UseQueryCallParameters<A, M>;
|
|
78
160
|
export interface UseMethodReturnType<A, M extends FunctionName<A> = FunctionName<A>> {
|
|
161
|
+
/**
|
|
162
|
+
* @deprecated Use `isLoading` instead.
|
|
163
|
+
* Indicates whether the method call is in progress.
|
|
164
|
+
*/
|
|
79
165
|
loading: boolean;
|
|
80
|
-
|
|
166
|
+
/**
|
|
167
|
+
* Indicates whether the method call is in progress.
|
|
168
|
+
*/
|
|
169
|
+
isLoading: boolean;
|
|
170
|
+
/**
|
|
171
|
+
* Indicates whether the argument form is required for the method.
|
|
172
|
+
*/
|
|
173
|
+
isFormRequired: boolean;
|
|
174
|
+
/**
|
|
175
|
+
* A unique key representing the current request instance.
|
|
176
|
+
*/
|
|
81
177
|
requestKey: string;
|
|
82
|
-
|
|
178
|
+
/**
|
|
179
|
+
* The error that occurred during the method call, if any.
|
|
180
|
+
*/
|
|
181
|
+
error: AgentError | undefined;
|
|
182
|
+
/**
|
|
183
|
+
* The data returned from the method call, or `undefined` if not yet available.
|
|
184
|
+
*/
|
|
83
185
|
data: ActorMethodReturnType<A[M]> | undefined;
|
|
84
|
-
|
|
186
|
+
/**
|
|
187
|
+
* Validates the provided arguments against the method signature.
|
|
188
|
+
* @param args Optional arguments for the method.
|
|
189
|
+
* @returns `true` if the arguments match the expected signature, otherwise `false`.
|
|
190
|
+
*/
|
|
191
|
+
validateArgs: (args?: ActorMethodParameters<A[M]>) => boolean;
|
|
192
|
+
/**
|
|
193
|
+
* The visit service function corresponding to this method.
|
|
194
|
+
*/
|
|
85
195
|
visit: VisitService<A>[M];
|
|
196
|
+
/**
|
|
197
|
+
* Resets the method state (data, error, loading) to initial values.
|
|
198
|
+
*/
|
|
86
199
|
reset: () => void;
|
|
87
|
-
|
|
200
|
+
/**
|
|
201
|
+
* Invokes the method.
|
|
202
|
+
* @param eventOrReplaceArgs Either the arguments for the call or a React mouse event.
|
|
203
|
+
* @returns A promise resolving to the method's return data or `undefined`.
|
|
204
|
+
*/
|
|
205
|
+
call: (eventOrReplaceArgs?: ActorMethodParameters<A[M]> | React.MouseEvent) => Promise<ActorMethodReturnType<A[M]> | undefined>;
|
|
88
206
|
}
|
|
89
207
|
export type UseMethod<A> = <M extends FunctionName<A>>(args: UseMethodParameters<A, M>) => UseMethodReturnType<A, M>;
|
|
90
208
|
export type UseVisitMethod<A> = <M extends FunctionName<A>>(functionName: M) => VisitService<A>[M];
|
package/dist/hooks/index.d.ts
CHANGED
package/dist/hooks/index.js
CHANGED
package/dist/hooks/types.d.ts
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
1
|
-
import { IDL, ActorHooksReturnType, ActorManagerParameters, BaseActor, CanisterId,
|
|
1
|
+
import { IDL, ActorHooksReturnType, ActorManagerParameters, BaseActor, CanisterId, ActorManager } from "../types";
|
|
2
2
|
export type ActorReConfigParameters = Omit<ActorManagerParameters, "idlFactory" | "canisterId" | "agentManager">;
|
|
3
3
|
export interface UseActorParameters extends ActorReConfigParameters {
|
|
4
4
|
candidString?: string;
|
|
5
|
-
agentManager: AgentManager;
|
|
6
5
|
canisterId: CanisterId;
|
|
7
6
|
idlFactory?: IDL.InterfaceFactory;
|
|
8
7
|
disableAutoFetch?: boolean;
|
|
9
8
|
}
|
|
9
|
+
export interface UseActorManagerParameters<A> extends ActorReConfigParameters {
|
|
10
|
+
actorManager: ActorManager<A>;
|
|
11
|
+
}
|
|
10
12
|
export interface UseActorReturn<A = BaseActor> {
|
|
11
13
|
hooks: ActorHooksReturnType<A> | null;
|
|
12
|
-
|
|
14
|
+
isFetching: boolean;
|
|
13
15
|
fetchError: string | null;
|
|
14
|
-
|
|
16
|
+
isAuthenticating: boolean;
|
|
15
17
|
initializeActor: InitializeActor;
|
|
16
18
|
}
|
|
19
|
+
export interface UseActorManagerReturn<A = BaseActor> {
|
|
20
|
+
hooks: ActorHooksReturnType<A>;
|
|
21
|
+
}
|
|
17
22
|
export type InitializeActor = (idlFactory: IDL.InterfaceFactory, actorReConfig?: ActorReConfigParameters) => void;
|
package/dist/hooks/useActor.js
CHANGED
|
@@ -1,24 +1,4 @@
|
|
|
1
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
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
-
var t = {};
|
|
13
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
-
t[p] = s[p];
|
|
15
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
-
t[p[i]] = s[p[i]];
|
|
19
|
-
}
|
|
20
|
-
return t;
|
|
21
|
-
};
|
|
22
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
3
|
exports.useActor = void 0;
|
|
24
4
|
const core_1 = require("@ic-reactor/core");
|
|
@@ -91,24 +71,24 @@ const agent_1 = require("../context/agent");
|
|
|
91
71
|
* ```
|
|
92
72
|
*/
|
|
93
73
|
const useActor = (config) => {
|
|
94
|
-
const { canisterId, candidString, idlFactory: maybeIdlFactory, disableAutoFetch,
|
|
74
|
+
const { canisterId, candidString, idlFactory: maybeIdlFactory, disableAutoFetch, ...actorConfig } = config;
|
|
95
75
|
if (!canisterId) {
|
|
96
76
|
throw new Error("canisterId is required");
|
|
97
77
|
}
|
|
98
78
|
const [actorManager, setActorManager] = (0, react_1.useState)(null);
|
|
99
79
|
(0, react_1.useEffect)(() => {
|
|
100
|
-
if (
|
|
80
|
+
if (actorManager?.canisterId !== canisterId.toString()) {
|
|
101
81
|
setActorManager(null);
|
|
102
82
|
}
|
|
103
|
-
return actorManager
|
|
83
|
+
return actorManager?.cleanup();
|
|
104
84
|
}, [canisterId, actorManager]);
|
|
105
85
|
const [{ fetching, fetchError }, setState] = (0, react_1.useState)({
|
|
106
86
|
fetching: false,
|
|
107
87
|
fetchError: null,
|
|
108
88
|
});
|
|
109
89
|
const candidAdapter = (0, react_1.useContext)(adapter_1.CandidAdapterContext);
|
|
110
|
-
const authenticating = (0, agent_1.useAuthState)().
|
|
111
|
-
const fetchCandid = (0, react_1.useCallback)(() =>
|
|
90
|
+
const authenticating = (0, agent_1.useAuthState)().isAuthenticating;
|
|
91
|
+
const fetchCandid = (0, react_1.useCallback)(async () => {
|
|
112
92
|
if (fetching)
|
|
113
93
|
return;
|
|
114
94
|
setState({
|
|
@@ -116,7 +96,10 @@ const useActor = (config) => {
|
|
|
116
96
|
fetchError: null,
|
|
117
97
|
});
|
|
118
98
|
try {
|
|
119
|
-
|
|
99
|
+
if (!candidAdapter) {
|
|
100
|
+
throw new Error("CandidAdapter is necessary to fetch the Candid interface. Please ensure your application is wrapped with the CandidAdapterProvider, or provide the idlFactory directly.");
|
|
101
|
+
}
|
|
102
|
+
const { idlFactory } = await candidAdapter.getCandidDefinition(canisterId);
|
|
120
103
|
setState({
|
|
121
104
|
fetching: false,
|
|
122
105
|
fetchError: null,
|
|
@@ -130,12 +113,16 @@ const useActor = (config) => {
|
|
|
130
113
|
fetchError: `Error fetching canister ${canisterId}`,
|
|
131
114
|
fetching: false,
|
|
132
115
|
});
|
|
116
|
+
return undefined;
|
|
133
117
|
}
|
|
134
|
-
}
|
|
135
|
-
const evaluateCandid = (0, react_1.useCallback)(() =>
|
|
118
|
+
}, [canisterId]);
|
|
119
|
+
const evaluateCandid = (0, react_1.useCallback)(async () => {
|
|
136
120
|
try {
|
|
137
|
-
|
|
138
|
-
|
|
121
|
+
if (!candidString) {
|
|
122
|
+
throw new Error("Candid string is required to evaluate the Candid definition");
|
|
123
|
+
}
|
|
124
|
+
const definition = await candidAdapter?.evaluateCandidDefinition(candidString);
|
|
125
|
+
if (typeof definition?.idlFactory !== "function") {
|
|
139
126
|
throw new Error("Error evaluating Candid definition");
|
|
140
127
|
}
|
|
141
128
|
return definition.idlFactory;
|
|
@@ -147,17 +134,23 @@ const useActor = (config) => {
|
|
|
147
134
|
fetchError: `Error evaluating Candid definition, ${err}`,
|
|
148
135
|
fetching: false,
|
|
149
136
|
});
|
|
137
|
+
return undefined;
|
|
150
138
|
}
|
|
151
|
-
}
|
|
139
|
+
}, [candidString]);
|
|
140
|
+
const agentManager = (0, agent_1.useAgentManager)();
|
|
152
141
|
const initializeActor = (0, react_1.useCallback)((idlFactory, actorReConfig) => {
|
|
153
142
|
if (authenticating || !idlFactory)
|
|
154
143
|
return;
|
|
155
|
-
const newActorManager = (0, core_1.createActorManager)(
|
|
144
|
+
const newActorManager = (0, core_1.createActorManager)({
|
|
145
|
+
agentManager,
|
|
156
146
|
idlFactory,
|
|
157
|
-
canisterId
|
|
147
|
+
canisterId,
|
|
148
|
+
...actorConfig,
|
|
149
|
+
...actorReConfig,
|
|
150
|
+
});
|
|
158
151
|
setActorManager(newActorManager);
|
|
159
152
|
}, [canisterId, agentManager, authenticating]);
|
|
160
|
-
const handleActorInitialization = (0, react_1.useCallback)(() =>
|
|
153
|
+
const handleActorInitialization = (0, react_1.useCallback)(async () => {
|
|
161
154
|
if (authenticating)
|
|
162
155
|
return;
|
|
163
156
|
if (maybeIdlFactory) {
|
|
@@ -176,15 +169,15 @@ const useActor = (config) => {
|
|
|
176
169
|
}
|
|
177
170
|
let idlFactory;
|
|
178
171
|
if (candidString) {
|
|
179
|
-
idlFactory =
|
|
172
|
+
idlFactory = await evaluateCandid();
|
|
180
173
|
}
|
|
181
174
|
else {
|
|
182
|
-
idlFactory =
|
|
175
|
+
idlFactory = await fetchCandid();
|
|
183
176
|
}
|
|
184
177
|
if (!idlFactory)
|
|
185
178
|
return;
|
|
186
179
|
initializeActor(idlFactory);
|
|
187
|
-
}
|
|
180
|
+
}, [fetchCandid, evaluateCandid, maybeIdlFactory, initializeActor]);
|
|
188
181
|
(0, react_1.useEffect)(() => {
|
|
189
182
|
handleActorInitialization();
|
|
190
183
|
}, [handleActorInitialization]);
|
|
@@ -193,6 +186,12 @@ const useActor = (config) => {
|
|
|
193
186
|
return null;
|
|
194
187
|
return (0, helpers_1.actorHooks)(actorManager);
|
|
195
188
|
}, [actorManager]);
|
|
196
|
-
return {
|
|
189
|
+
return {
|
|
190
|
+
hooks,
|
|
191
|
+
isAuthenticating: authenticating,
|
|
192
|
+
isFetching: fetching,
|
|
193
|
+
fetchError,
|
|
194
|
+
initializeActor,
|
|
195
|
+
};
|
|
197
196
|
};
|
|
198
197
|
exports.useActor = useActor;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { BaseActor } from "../types";
|
|
2
|
+
import type { UseActorManagerParameters, UseActorManagerReturn } 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
|
+
* You can use react context to share the actor hooks across your application.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* import { AgentProvider, extractActorHooks, useActorManager } from "@ic-reactor/react"
|
|
14
|
+
* import { createContext } from "react"
|
|
15
|
+
* import type { ActorHooks } from "@ic-reactor/react/dist/types"
|
|
16
|
+
* // With this import, you can have type safety for the actor's interface.
|
|
17
|
+
* // You can get it from the `.did.d.ts` file generated by the DFX tool.
|
|
18
|
+
* // or from dashboard https://dashboard.internetcomputer.org/canisters/<canister-id>
|
|
19
|
+
* import type { Ledger } from "../declarations/ledger"
|
|
20
|
+
*
|
|
21
|
+
* const ActorContext = createContext<ActorHooks<Ledger> | null>(null)
|
|
22
|
+
*
|
|
23
|
+
* export const { useQueryCall, useUpdateCall } = extractActorHooks(ActorContext)
|
|
24
|
+
*
|
|
25
|
+
* const actorManager = createActorManager<Ledger>({
|
|
26
|
+
* canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai",
|
|
27
|
+
* idlFactory: idlFactory
|
|
28
|
+
* })
|
|
29
|
+
*
|
|
30
|
+
* const LedgerActor = ({ children }) => {
|
|
31
|
+
* const { hooks } = useActorManager<Ledger>({
|
|
32
|
+
* actorManager
|
|
33
|
+
* })
|
|
34
|
+
*
|
|
35
|
+
* return (
|
|
36
|
+
* <ActorContext.Provider value={hooks}>
|
|
37
|
+
* <h2>IC Canister Interaction</h2>
|
|
38
|
+
* </ActorContext.Provider>
|
|
39
|
+
* )
|
|
40
|
+
* }
|
|
41
|
+
* // later in the code
|
|
42
|
+
* const CanisterName = () => {
|
|
43
|
+
* const { data } = useQueryCall({
|
|
44
|
+
* functionName: "name",
|
|
45
|
+
* })
|
|
46
|
+
*
|
|
47
|
+
* return (
|
|
48
|
+
* <div>
|
|
49
|
+
* <h3>Query Call</h3>
|
|
50
|
+
* <p>Result: {JSON.stringify(data)}</p>
|
|
51
|
+
* </div>
|
|
52
|
+
* )
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* const App = () => (
|
|
56
|
+
* <AgentProvider withDevtools>
|
|
57
|
+
* <CandidAdapterProvider>
|
|
58
|
+
* <LedgerActor>
|
|
59
|
+
* <CanisterName />
|
|
60
|
+
* </LedgerActor>
|
|
61
|
+
* </CandidAdapterProvider>
|
|
62
|
+
* </AgentProvider>
|
|
63
|
+
* )
|
|
64
|
+
*
|
|
65
|
+
* export default App
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare const useActorManager: <A = BaseActor>(config: UseActorManagerParameters<A>) => UseActorManagerReturn<A>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useActorManager = void 0;
|
|
4
|
+
const helpers_1 = require("../helpers");
|
|
5
|
+
/**
|
|
6
|
+
* A comprehensive hook that manages both the fetching of Candid interfaces
|
|
7
|
+
* and the initialization of actor stores for Internet Computer (IC) canisters.
|
|
8
|
+
* It simplifies the process of interacting with canisters by encapsulating
|
|
9
|
+
* the logic for Candid retrieval and actor store management.
|
|
10
|
+
*
|
|
11
|
+
* You can use react context to share the actor hooks across your application.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* import { AgentProvider, extractActorHooks, useActorManager } from "@ic-reactor/react"
|
|
16
|
+
* import { createContext } from "react"
|
|
17
|
+
* import type { ActorHooks } from "@ic-reactor/react/dist/types"
|
|
18
|
+
* // With this import, you can have type safety for the actor's interface.
|
|
19
|
+
* // You can get it from the `.did.d.ts` file generated by the DFX tool.
|
|
20
|
+
* // or from dashboard https://dashboard.internetcomputer.org/canisters/<canister-id>
|
|
21
|
+
* import type { Ledger } from "../declarations/ledger"
|
|
22
|
+
*
|
|
23
|
+
* const ActorContext = createContext<ActorHooks<Ledger> | null>(null)
|
|
24
|
+
*
|
|
25
|
+
* export const { useQueryCall, useUpdateCall } = extractActorHooks(ActorContext)
|
|
26
|
+
*
|
|
27
|
+
* const actorManager = createActorManager<Ledger>({
|
|
28
|
+
* canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai",
|
|
29
|
+
* idlFactory: idlFactory
|
|
30
|
+
* })
|
|
31
|
+
*
|
|
32
|
+
* const LedgerActor = ({ children }) => {
|
|
33
|
+
* const { hooks } = useActorManager<Ledger>({
|
|
34
|
+
* actorManager
|
|
35
|
+
* })
|
|
36
|
+
*
|
|
37
|
+
* return (
|
|
38
|
+
* <ActorContext.Provider value={hooks}>
|
|
39
|
+
* <h2>IC Canister Interaction</h2>
|
|
40
|
+
* </ActorContext.Provider>
|
|
41
|
+
* )
|
|
42
|
+
* }
|
|
43
|
+
* // later in the code
|
|
44
|
+
* const CanisterName = () => {
|
|
45
|
+
* const { data } = useQueryCall({
|
|
46
|
+
* functionName: "name",
|
|
47
|
+
* })
|
|
48
|
+
*
|
|
49
|
+
* return (
|
|
50
|
+
* <div>
|
|
51
|
+
* <h3>Query Call</h3>
|
|
52
|
+
* <p>Result: {JSON.stringify(data)}</p>
|
|
53
|
+
* </div>
|
|
54
|
+
* )
|
|
55
|
+
* }
|
|
56
|
+
*
|
|
57
|
+
* const App = () => (
|
|
58
|
+
* <AgentProvider withDevtools>
|
|
59
|
+
* <CandidAdapterProvider>
|
|
60
|
+
* <LedgerActor>
|
|
61
|
+
* <CanisterName />
|
|
62
|
+
* </LedgerActor>
|
|
63
|
+
* </CandidAdapterProvider>
|
|
64
|
+
* </AgentProvider>
|
|
65
|
+
* )
|
|
66
|
+
*
|
|
67
|
+
* export default App
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
const useActorManager = (config) => {
|
|
71
|
+
const { actorManager } = config;
|
|
72
|
+
const hooks = (0, helpers_1.actorHooks)(actorManager);
|
|
73
|
+
return { hooks };
|
|
74
|
+
};
|
|
75
|
+
exports.useActorManager = useActorManager;
|
package/dist/index.js
CHANGED
|
@@ -18,13 +18,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
18
18
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
19
19
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
20
20
|
};
|
|
21
|
-
var __importStar = (this && this.__importStar) || function (
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
};
|
|
21
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
22
|
+
var ownKeys = function(o) {
|
|
23
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
24
|
+
var ar = [];
|
|
25
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
26
|
+
return ar;
|
|
27
|
+
};
|
|
28
|
+
return ownKeys(o);
|
|
29
|
+
};
|
|
30
|
+
return function (mod) {
|
|
31
|
+
if (mod && mod.__esModule) return mod;
|
|
32
|
+
var result = {};
|
|
33
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
34
|
+
__setModuleDefault(result, mod);
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
})();
|
|
28
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
39
|
exports.utils = exports.core = exports.types = exports.helpers = void 0;
|
|
30
40
|
// Note: Order of exports is important
|
package/dist/types.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { HttpAgent, CreateReactorCoreParameters, VisitService } from "@ic-reactor/core/dist/types";
|
|
2
2
|
import type { ActorHooksReturnType, AgentHooksReturnType, AuthHooksReturnType } from "./helpers/types";
|
|
3
|
-
export
|
|
4
|
-
}
|
|
3
|
+
export type CreateReactorParameters = CreateReactorCoreParameters;
|
|
5
4
|
export interface CreateReactorReturnType<A> extends ActorHooksReturnType<A>, AuthHooksReturnType, AgentHooksReturnType {
|
|
6
5
|
getAgent: () => HttpAgent;
|
|
7
6
|
getVisitFunction: () => VisitService<A>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/react",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "A React library for interacting with Internet Computer canisters",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -26,26 +26,27 @@
|
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://b3pay.github.io/ic-reactor/modules/react.html",
|
|
28
28
|
"scripts": {
|
|
29
|
-
"test": "
|
|
30
|
-
"start": "tsc watch",
|
|
31
|
-
"build": "tsc",
|
|
32
|
-
"clean": "
|
|
29
|
+
"test": "bun test",
|
|
30
|
+
"start": "bun run tsc watch",
|
|
31
|
+
"build": "bun run tsc",
|
|
32
|
+
"clean": "bun run rimraf dist && bun run rimraf umd"
|
|
33
33
|
},
|
|
34
34
|
"engines": {
|
|
35
|
-
"node": ">=
|
|
35
|
+
"node": ">=22.0.0"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@ic-reactor/core": "
|
|
39
|
-
"zustand
|
|
38
|
+
"@ic-reactor/core": "2.0.1",
|
|
39
|
+
"zustand": "^5.0.6",
|
|
40
|
+
"zustand-utils": "^2.1.0"
|
|
40
41
|
},
|
|
41
42
|
"peerDependencies": {
|
|
42
|
-
"@dfinity/agent": "
|
|
43
|
-
"@dfinity/auth-client": "
|
|
44
|
-
"@dfinity/candid": "
|
|
45
|
-
"@dfinity/identity": "
|
|
46
|
-
"@dfinity/principal": "
|
|
47
|
-
"react": ">=
|
|
48
|
-
"zustand": "
|
|
43
|
+
"@dfinity/agent": "^3.1.0",
|
|
44
|
+
"@dfinity/auth-client": "^3.1.0",
|
|
45
|
+
"@dfinity/candid": "^3.1.0",
|
|
46
|
+
"@dfinity/identity": "^3.1.0",
|
|
47
|
+
"@dfinity/principal": "^3.1.0",
|
|
48
|
+
"react": ">=19.0.0",
|
|
49
|
+
"zustand": "^5.0.6"
|
|
49
50
|
},
|
|
50
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "1c8f5bfc6b0fea9347602beeebd6443f79d7d54e"
|
|
51
52
|
}
|