@ic-reactor/react 2.0.1 → 3.0.0-beta.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.
Files changed (155) hide show
  1. package/README.md +105 -79
  2. package/dist/createActorHooks.d.ts +39 -0
  3. package/dist/createActorHooks.d.ts.map +1 -0
  4. package/dist/createActorHooks.js +30 -0
  5. package/dist/createActorHooks.js.map +1 -0
  6. package/dist/createAuthHooks.d.ts +37 -0
  7. package/dist/createAuthHooks.d.ts.map +1 -0
  8. package/dist/createAuthHooks.js +94 -0
  9. package/dist/createAuthHooks.js.map +1 -0
  10. package/dist/createInfiniteQuery.d.ts +129 -0
  11. package/dist/createInfiniteQuery.d.ts.map +1 -0
  12. package/dist/createInfiniteQuery.js +160 -0
  13. package/dist/createInfiniteQuery.js.map +1 -0
  14. package/dist/createMutation.d.ts +19 -0
  15. package/dist/createMutation.d.ts.map +1 -0
  16. package/dist/createMutation.js +98 -0
  17. package/dist/createMutation.js.map +1 -0
  18. package/dist/createQuery.d.ts +20 -0
  19. package/dist/createQuery.d.ts.map +1 -0
  20. package/dist/createQuery.js +111 -0
  21. package/dist/createQuery.js.map +1 -0
  22. package/dist/createSuspenseInfiniteQuery.d.ts +122 -0
  23. package/dist/createSuspenseInfiniteQuery.d.ts.map +1 -0
  24. package/dist/createSuspenseInfiniteQuery.js +160 -0
  25. package/dist/createSuspenseInfiniteQuery.js.map +1 -0
  26. package/dist/createSuspenseQuery.d.ts +25 -0
  27. package/dist/createSuspenseQuery.d.ts.map +1 -0
  28. package/dist/createSuspenseQuery.js +116 -0
  29. package/dist/createSuspenseQuery.js.map +1 -0
  30. package/dist/hooks/index.d.ts +6 -2
  31. package/dist/hooks/index.d.ts.map +1 -0
  32. package/dist/hooks/index.js +6 -18
  33. package/dist/hooks/index.js.map +1 -0
  34. package/dist/hooks/useActorInfiniteQuery.d.ts +37 -0
  35. package/dist/hooks/useActorInfiniteQuery.d.ts.map +1 -0
  36. package/dist/hooks/useActorInfiniteQuery.js +33 -0
  37. package/dist/hooks/useActorInfiniteQuery.js.map +1 -0
  38. package/dist/hooks/useActorMutation.d.ts +23 -0
  39. package/dist/hooks/useActorMutation.d.ts.map +1 -0
  40. package/dist/hooks/useActorMutation.js +39 -0
  41. package/dist/hooks/useActorMutation.js.map +1 -0
  42. package/dist/hooks/useActorQuery.d.ts +32 -0
  43. package/dist/hooks/useActorQuery.d.ts.map +1 -0
  44. package/dist/hooks/useActorQuery.js +35 -0
  45. package/dist/hooks/useActorQuery.js.map +1 -0
  46. package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts +36 -0
  47. package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts.map +1 -0
  48. package/dist/hooks/useActorSuspenseInfiniteQuery.js +33 -0
  49. package/dist/hooks/useActorSuspenseInfiniteQuery.js.map +1 -0
  50. package/dist/hooks/useActorSuspenseQuery.d.ts +32 -0
  51. package/dist/hooks/useActorSuspenseQuery.d.ts.map +1 -0
  52. package/dist/hooks/useActorSuspenseQuery.js +36 -0
  53. package/dist/hooks/useActorSuspenseQuery.js.map +1 -0
  54. package/dist/index.d.ts +11 -8
  55. package/dist/index.d.ts.map +1 -0
  56. package/dist/index.js +17 -49
  57. package/dist/index.js.map +1 -0
  58. package/dist/types.d.ts +232 -13
  59. package/dist/types.d.ts.map +1 -0
  60. package/dist/types.js +5 -22
  61. package/dist/types.js.map +1 -0
  62. package/dist/validation.d.ts +131 -0
  63. package/dist/validation.d.ts.map +1 -0
  64. package/dist/validation.js +125 -0
  65. package/dist/validation.js.map +1 -0
  66. package/package.json +70 -35
  67. package/LICENSE.md +0 -8
  68. package/dist/context/actor/create.d.ts +0 -63
  69. package/dist/context/actor/create.js +0 -119
  70. package/dist/context/actor/hooks/useActorInterface.d.ts +0 -4
  71. package/dist/context/actor/hooks/useActorInterface.js +0 -10
  72. package/dist/context/actor/hooks/useActorState.d.ts +0 -21
  73. package/dist/context/actor/hooks/useActorState.js +0 -25
  74. package/dist/context/actor/hooks/useActorStore.d.ts +0 -32
  75. package/dist/context/actor/hooks/useActorStore.js +0 -36
  76. package/dist/context/actor/hooks/useInitializeActor.d.ts +0 -6
  77. package/dist/context/actor/hooks/useInitializeActor.js +0 -10
  78. package/dist/context/actor/hooks/useMethod.d.ts +0 -29
  79. package/dist/context/actor/hooks/useMethod.js +0 -34
  80. package/dist/context/actor/hooks/useMethodAttributes.d.ts +0 -7
  81. package/dist/context/actor/hooks/useMethodAttributes.js +0 -11
  82. package/dist/context/actor/hooks/useMethodNames.d.ts +0 -7
  83. package/dist/context/actor/hooks/useMethodNames.js +0 -11
  84. package/dist/context/actor/hooks/useQueryCall.d.ts +0 -28
  85. package/dist/context/actor/hooks/useQueryCall.js +0 -33
  86. package/dist/context/actor/hooks/useUpdateCall.d.ts +0 -29
  87. package/dist/context/actor/hooks/useUpdateCall.js +0 -34
  88. package/dist/context/actor/hooks/useVisitMethod.d.ts +0 -8
  89. package/dist/context/actor/hooks/useVisitMethod.js +0 -13
  90. package/dist/context/actor/hooks/useVisitService.d.ts +0 -8
  91. package/dist/context/actor/hooks/useVisitService.js +0 -13
  92. package/dist/context/actor/index.d.ts +0 -16
  93. package/dist/context/actor/index.js +0 -33
  94. package/dist/context/actor/provider.d.ts +0 -71
  95. package/dist/context/actor/provider.js +0 -75
  96. package/dist/context/actor/types.d.ts +0 -35
  97. package/dist/context/actor/types.js +0 -2
  98. package/dist/context/adapter/create.d.ts +0 -2
  99. package/dist/context/adapter/create.js +0 -102
  100. package/dist/context/adapter/hooks/useCandidAdapter.d.ts +0 -23
  101. package/dist/context/adapter/hooks/useCandidAdapter.js +0 -27
  102. package/dist/context/adapter/hooks/useCandidEvaluation.d.ts +0 -5
  103. package/dist/context/adapter/hooks/useCandidEvaluation.js +0 -9
  104. package/dist/context/adapter/index.d.ts +0 -6
  105. package/dist/context/adapter/index.js +0 -24
  106. package/dist/context/adapter/provider.d.ts +0 -23
  107. package/dist/context/adapter/provider.js +0 -27
  108. package/dist/context/adapter/types.d.ts +0 -52
  109. package/dist/context/adapter/types.js +0 -2
  110. package/dist/context/agent/create.d.ts +0 -74
  111. package/dist/context/agent/create.js +0 -104
  112. package/dist/context/agent/hooks/useAgent.d.ts +0 -14
  113. package/dist/context/agent/hooks/useAgent.js +0 -18
  114. package/dist/context/agent/hooks/useAgentManager.d.ts +0 -14
  115. package/dist/context/agent/hooks/useAgentManager.js +0 -18
  116. package/dist/context/agent/hooks/useAgentState.d.ts +0 -21
  117. package/dist/context/agent/hooks/useAgentState.js +0 -25
  118. package/dist/context/agent/hooks/useAuth.d.ts +0 -57
  119. package/dist/context/agent/hooks/useAuth.js +0 -61
  120. package/dist/context/agent/hooks/useAuthState.d.ts +0 -19
  121. package/dist/context/agent/hooks/useAuthState.js +0 -23
  122. package/dist/context/agent/hooks/useUserPrincipal.d.ts +0 -17
  123. package/dist/context/agent/hooks/useUserPrincipal.js +0 -21
  124. package/dist/context/agent/index.d.ts +0 -12
  125. package/dist/context/agent/index.js +0 -29
  126. package/dist/context/agent/provider.d.ts +0 -28
  127. package/dist/context/agent/provider.js +0 -32
  128. package/dist/context/agent/types.d.ts +0 -17
  129. package/dist/context/agent/types.js +0 -2
  130. package/dist/core.d.ts +0 -1
  131. package/dist/core.js +0 -9
  132. package/dist/createReactor.d.ts +0 -49
  133. package/dist/createReactor.js +0 -69
  134. package/dist/helpers/actorHooks.d.ts +0 -18
  135. package/dist/helpers/actorHooks.js +0 -283
  136. package/dist/helpers/agentHooks.d.ts +0 -3
  137. package/dist/helpers/agentHooks.js +0 -22
  138. package/dist/helpers/authHooks.d.ts +0 -2
  139. package/dist/helpers/authHooks.js +0 -120
  140. package/dist/helpers/extractActorContext.d.ts +0 -4
  141. package/dist/helpers/extractActorContext.js +0 -44
  142. package/dist/helpers/extractAgentContext.d.ts +0 -28
  143. package/dist/helpers/extractAgentContext.js +0 -59
  144. package/dist/helpers/index.d.ts +0 -5
  145. package/dist/helpers/index.js +0 -21
  146. package/dist/helpers/types.d.ts +0 -222
  147. package/dist/helpers/types.js +0 -2
  148. package/dist/hooks/types.d.ts +0 -22
  149. package/dist/hooks/types.js +0 -2
  150. package/dist/hooks/useActor.d.ts +0 -67
  151. package/dist/hooks/useActor.js +0 -197
  152. package/dist/hooks/useActorManager.d.ts +0 -68
  153. package/dist/hooks/useActorManager.js +0 -75
  154. package/dist/utils.d.ts +0 -1
  155. package/dist/utils.js +0 -17
@@ -1,222 +0,0 @@
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";
3
- export interface AgentHooksReturnType {
4
- useAgent: () => HttpAgent | undefined;
5
- useAgentState: () => AgentState;
6
- }
7
- export interface AuthHooksReturnType {
8
- useAuth: (options?: UseAuthParameters) => UseAuthReturnType;
9
- useAuthState: () => AuthState;
10
- useUserPrincipal: () => Principal | undefined;
11
- }
12
- export interface UseAuthParameters {
13
- onAuthentication?: (promise: () => Promise<Identity>) => void;
14
- onAuthenticationSuccess?: (identity: Identity) => void;
15
- onAuthenticationFailure?: (error: string | undefined) => void;
16
- onLoginSuccess?: (principal: Principal) => void;
17
- onLoginError?: (error: string | undefined) => void;
18
- onLogin?: (promise: () => Promise<Principal>) => void;
19
- onLoggedOut?: () => void;
20
- }
21
- /**
22
- * The return type for authentication hooks.
23
- */
24
- export interface UseAuthReturnType {
25
- /**
26
- * Any non-login related error that occurred.
27
- */
28
- error: Error | undefined;
29
- /**
30
- * @deprecated Use `isAuthenticated` instead.
31
- * Indicates whether the user is authenticated.
32
- */
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
- */
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
- */
50
- identity: Identity | null;
51
- /**
52
- * Initiates the login flow with optional parameters.
53
- * @param options Login parameters (e.g. redirect URL).
54
- */
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
- */
60
- logout: (options?: LogoutParameters) => Promise<void>;
61
- /**
62
- * Triggers the authentication flow and resolves to an `Identity`.
63
- */
64
- authenticate: () => Promise<Identity>;
65
- /**
66
- * @deprecated Use `isLoginLoading` instead.
67
- * Indicates whether the login operation is in progress.
68
- */
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
- */
77
- loginError: string | undefined;
78
- }
79
- /**
80
- * Represents the state of a login operation.
81
- */
82
- export type LoginState = {
83
- /**
84
- * @deprecated Use `isLoading` instead.
85
- * Indicates whether the login operation is in progress.
86
- */
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
- */
95
- error: string | undefined;
96
- };
97
- export type LoginParameters = AuthClientLoginOptions;
98
- export type LogoutParameters = {
99
- returnTo?: string;
100
- };
101
- export type UseActorStore<A> = <T>(callback?: (state: ActorState<A>) => T) => T;
102
- export interface UseActorStateReturnType extends Omit<ActorState, "methodState"> {
103
- canisterId: string;
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
- };
127
- export interface UseSharedCallParameters<A, M extends FunctionName<A>> extends CallConfig {
128
- functionName: M;
129
- args?: ActorMethodParameters<A[M]>;
130
- onLoading?: (loading: boolean) => void;
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;
135
- throwOnError?: boolean;
136
- }
137
- export interface UseSharedCallReturnType<A, M extends FunctionName<A> = FunctionName<A>> extends UseSharedCallState<A, M> {
138
- requestKey: string;
139
- reset: () => void;
140
- compileResult: () => CompiledResult<ActorMethodReturnType<A[M]>>;
141
- call: (eventOrReplaceArgs?: ActorMethodParameters<A[M]> | React.MouseEvent) => Promise<ActorMethodReturnType<A[M]> | undefined>;
142
- }
143
- export type UseSharedCall<A> = <M extends FunctionName<A>>(params: UseSharedCallParameters<A, M>) => UseSharedCallReturnType<A, M>;
144
- export interface UseQueryCallParameters<A, M extends FunctionName<A>> extends UseSharedCallParameters<A, M> {
145
- refetchOnMount?: boolean;
146
- refetchInterval?: number | false;
147
- }
148
- export interface UseQueryCallReturnType<A, M extends FunctionName<A>> extends UseSharedCallReturnType<A, M> {
149
- refetch: () => void;
150
- }
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>;
155
- export interface DynamicDataArgs<V = unknown> {
156
- label: string;
157
- value: V;
158
- }
159
- export type UseMethodParameters<A, M extends FunctionName<A>> = UseQueryCallParameters<A, M>;
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
- */
165
- loading: boolean;
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
- */
177
- requestKey: string;
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
- */
185
- data: ActorMethodReturnType<A[M]> | undefined;
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
- */
195
- visit: VisitService<A>[M];
196
- /**
197
- * Resets the method state (data, error, loading) to initial values.
198
- */
199
- reset: () => void;
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>;
206
- }
207
- export type UseMethod<A> = <M extends FunctionName<A>>(args: UseMethodParameters<A, M>) => UseMethodReturnType<A, M>;
208
- export type UseVisitMethod<A> = <M extends FunctionName<A>>(functionName: M) => VisitService<A>[M];
209
- export type UseVisitService<A> = () => VisitService<A>;
210
- export interface ActorHooksReturnType<A = BaseActor> {
211
- initialize: () => Promise<void>;
212
- useActorStore: UseActorStore<A>;
213
- useActorState: () => UseActorStateReturnType;
214
- useActorInterface: () => IDL.ServiceClass;
215
- useMethodNames: <Actor = A>() => FunctionName<Actor>[];
216
- useMethodAttributes: <Actor = A>() => MethodAttributes<Actor>;
217
- useMethod: UseMethod<A>;
218
- useQueryCall: UseQueryCall<A>;
219
- useUpdateCall: UseUpdateCall<A>;
220
- useVisitMethod: UseVisitMethod<A>;
221
- useVisitService: UseVisitService<A>;
222
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,22 +0,0 @@
1
- import { IDL, ActorHooksReturnType, ActorManagerParameters, BaseActor, CanisterId, ActorManager } from "../types";
2
- export type ActorReConfigParameters = Omit<ActorManagerParameters, "idlFactory" | "canisterId" | "agentManager">;
3
- export interface UseActorParameters extends ActorReConfigParameters {
4
- candidString?: string;
5
- canisterId: CanisterId;
6
- idlFactory?: IDL.InterfaceFactory;
7
- disableAutoFetch?: boolean;
8
- }
9
- export interface UseActorManagerParameters<A> extends ActorReConfigParameters {
10
- actorManager: ActorManager<A>;
11
- }
12
- export interface UseActorReturn<A = BaseActor> {
13
- hooks: ActorHooksReturnType<A> | null;
14
- isFetching: boolean;
15
- fetchError: string | null;
16
- isAuthenticating: boolean;
17
- initializeActor: InitializeActor;
18
- }
19
- export interface UseActorManagerReturn<A = BaseActor> {
20
- hooks: ActorHooksReturnType<A>;
21
- }
22
- export type InitializeActor = (idlFactory: IDL.InterfaceFactory, actorReConfig?: ActorReConfigParameters) => void;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,67 +0,0 @@
1
- import type { BaseActor } from "../types";
2
- import type { UseActorParameters, UseActorReturn } 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, useActor } 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 LedgerActor = ({ children }) => {
26
- * const { hooks, fetching, fetchError } = useActor<Ledger>({
27
- * canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai", // ICP Ledger canister
28
- * idlFactory // optional: it will be fetched using CandidAdapter
29
- * })
30
- *
31
- * return (
32
- * <ActorContext.Provider value={hooks}>
33
- * <h2>IC Canister Interaction</h2>
34
- * {fetching && <p>Loading Candid interface...</p>}
35
- * {fetchError && <p>Error: {fetchError}</p>}
36
- * {hooks && children}
37
- * </ActorContext.Provider>
38
- * )
39
- * }
40
- * // later in the code
41
- * const CanisterName = () => {
42
- * const { data } = useQueryCall({
43
- * functionName: "name",
44
- * })
45
- *
46
- * return (
47
- * <div>
48
- * <h3>Query Call</h3>
49
- * <p>Result: {JSON.stringify(data)}</p>
50
- * </div>
51
- * )
52
- * }
53
- *
54
- * const App = () => (
55
- * <AgentProvider withDevtools>
56
- * <CandidAdapterProvider>
57
- * <LedgerActor>
58
- * <CanisterName />
59
- * </LedgerActor>
60
- * </CandidAdapterProvider>
61
- * </AgentProvider>
62
- * )
63
- *
64
- * export default App
65
- * ```
66
- */
67
- export declare const useActor: <A = BaseActor>(config: UseActorParameters) => UseActorReturn<A>;
@@ -1,197 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useActor = void 0;
4
- const core_1 = require("@ic-reactor/core");
5
- const react_1 = require("react");
6
- const helpers_1 = require("../helpers");
7
- const adapter_1 = require("../context/adapter");
8
- const agent_1 = require("../context/agent");
9
- /**
10
- * A comprehensive hook that manages both the fetching of Candid interfaces
11
- * and the initialization of actor stores for Internet Computer (IC) canisters.
12
- * It simplifies the process of interacting with canisters by encapsulating
13
- * the logic for Candid retrieval and actor store management.
14
- *
15
- * You can use react context to share the actor hooks across your application.
16
- *
17
- * @example
18
- * ```tsx
19
- * import { AgentProvider, extractActorHooks, useActor } from "@ic-reactor/react"
20
- * import { createContext } from "react"
21
- * import type { ActorHooks } from "@ic-reactor/react/dist/types"
22
- * // With this import, you can have type safety for the actor's interface.
23
- * // You can get it from the `.did.d.ts` file generated by the DFX tool.
24
- * // or from dashboard https://dashboard.internetcomputer.org/canisters/<canister-id>
25
- * import type { Ledger } from "../declarations/ledger"
26
- *
27
- * const ActorContext = createContext<ActorHooks<Ledger> | null>(null)
28
- *
29
- * export const { useQueryCall, useUpdateCall } = extractActorHooks(ActorContext)
30
- *
31
- * const LedgerActor = ({ children }) => {
32
- * const { hooks, fetching, fetchError } = useActor<Ledger>({
33
- * canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai", // ICP Ledger canister
34
- * idlFactory // optional: it will be fetched using CandidAdapter
35
- * })
36
- *
37
- * return (
38
- * <ActorContext.Provider value={hooks}>
39
- * <h2>IC Canister Interaction</h2>
40
- * {fetching && <p>Loading Candid interface...</p>}
41
- * {fetchError && <p>Error: {fetchError}</p>}
42
- * {hooks && children}
43
- * </ActorContext.Provider>
44
- * )
45
- * }
46
- * // later in the code
47
- * const CanisterName = () => {
48
- * const { data } = useQueryCall({
49
- * functionName: "name",
50
- * })
51
- *
52
- * return (
53
- * <div>
54
- * <h3>Query Call</h3>
55
- * <p>Result: {JSON.stringify(data)}</p>
56
- * </div>
57
- * )
58
- * }
59
- *
60
- * const App = () => (
61
- * <AgentProvider withDevtools>
62
- * <CandidAdapterProvider>
63
- * <LedgerActor>
64
- * <CanisterName />
65
- * </LedgerActor>
66
- * </CandidAdapterProvider>
67
- * </AgentProvider>
68
- * )
69
- *
70
- * export default App
71
- * ```
72
- */
73
- const useActor = (config) => {
74
- const { canisterId, candidString, idlFactory: maybeIdlFactory, disableAutoFetch, ...actorConfig } = config;
75
- if (!canisterId) {
76
- throw new Error("canisterId is required");
77
- }
78
- const [actorManager, setActorManager] = (0, react_1.useState)(null);
79
- (0, react_1.useEffect)(() => {
80
- if (actorManager?.canisterId !== canisterId.toString()) {
81
- setActorManager(null);
82
- }
83
- return actorManager?.cleanup();
84
- }, [canisterId, actorManager]);
85
- const [{ fetching, fetchError }, setState] = (0, react_1.useState)({
86
- fetching: false,
87
- fetchError: null,
88
- });
89
- const candidAdapter = (0, react_1.useContext)(adapter_1.CandidAdapterContext);
90
- const authenticating = (0, agent_1.useAuthState)().isAuthenticating;
91
- const fetchCandid = (0, react_1.useCallback)(async () => {
92
- if (fetching)
93
- return;
94
- setState({
95
- fetching: true,
96
- fetchError: null,
97
- });
98
- try {
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);
103
- setState({
104
- fetching: false,
105
- fetchError: null,
106
- });
107
- return idlFactory;
108
- }
109
- catch (err) {
110
- // eslint-disable-next-line no-console
111
- console.error(err);
112
- setState({
113
- fetchError: `Error fetching canister ${canisterId}`,
114
- fetching: false,
115
- });
116
- return undefined;
117
- }
118
- }, [canisterId]);
119
- const evaluateCandid = (0, react_1.useCallback)(async () => {
120
- try {
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") {
126
- throw new Error("Error evaluating Candid definition");
127
- }
128
- return definition.idlFactory;
129
- }
130
- catch (err) {
131
- // eslint-disable-next-line no-console
132
- console.error(err);
133
- setState({
134
- fetchError: `Error evaluating Candid definition, ${err}`,
135
- fetching: false,
136
- });
137
- return undefined;
138
- }
139
- }, [candidString]);
140
- const agentManager = (0, agent_1.useAgentManager)();
141
- const initializeActor = (0, react_1.useCallback)((idlFactory, actorReConfig) => {
142
- if (authenticating || !idlFactory)
143
- return;
144
- const newActorManager = (0, core_1.createActorManager)({
145
- agentManager,
146
- idlFactory,
147
- canisterId,
148
- ...actorConfig,
149
- ...actorReConfig,
150
- });
151
- setActorManager(newActorManager);
152
- }, [canisterId, agentManager, authenticating]);
153
- const handleActorInitialization = (0, react_1.useCallback)(async () => {
154
- if (authenticating)
155
- return;
156
- if (maybeIdlFactory) {
157
- initializeActor(maybeIdlFactory);
158
- return;
159
- }
160
- if (disableAutoFetch) {
161
- setState({
162
- fetchError: "Candid interface not fetched!",
163
- fetching: false,
164
- });
165
- return;
166
- }
167
- if (!candidAdapter) {
168
- 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.");
169
- }
170
- let idlFactory;
171
- if (candidString) {
172
- idlFactory = await evaluateCandid();
173
- }
174
- else {
175
- idlFactory = await fetchCandid();
176
- }
177
- if (!idlFactory)
178
- return;
179
- initializeActor(idlFactory);
180
- }, [fetchCandid, evaluateCandid, maybeIdlFactory, initializeActor]);
181
- (0, react_1.useEffect)(() => {
182
- handleActorInitialization();
183
- }, [handleActorInitialization]);
184
- const hooks = (0, react_1.useMemo)(() => {
185
- if (!actorManager)
186
- return null;
187
- return (0, helpers_1.actorHooks)(actorManager);
188
- }, [actorManager]);
189
- return {
190
- hooks,
191
- isAuthenticating: authenticating,
192
- isFetching: fetching,
193
- fetchError,
194
- initializeActor,
195
- };
196
- };
197
- exports.useActor = useActor;
@@ -1,68 +0,0 @@
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>;
@@ -1,75 +0,0 @@
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/utils.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from "@ic-reactor/core/dist/utils";
package/dist/utils.js DELETED
@@ -1,17 +0,0 @@
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("@ic-reactor/core/dist/utils"), exports);