@ic-reactor/react 2.0.0 → 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,283 +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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.actorHooks = void 0;
37
- const React = __importStar(require("react"));
38
- const zustand_1 = require("zustand");
39
- const shallow_1 = require("zustand/react/shallow");
40
- const utils_1 = require("../utils");
41
- const DEFAULT_STATE = {
42
- data: undefined,
43
- error: undefined,
44
- loading: false,
45
- isLoading: false,
46
- };
47
- /**
48
- * 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.
49
- *
50
- * @param actorManager An instance of ActorManager containing methods and properties to manage actors, including the actorStore, canisterId, visitFunction, callMethod, and initialize function.
51
- * @returns An object containing several hooks and utility functions for interacting with actors, managing state, and invoking actor methods.
52
- *
53
- * Hooks included:
54
- * - initialize: Function to initialize actor management.
55
- * - useActorState: Hook for accessing the actor's state including the canister ID.
56
- * - useVisitMethod: Hook for memoizing a method visit service for a given actor method name.
57
- * - useQueryCall: Hook specifically designed for query calls to actors with features such as automatic refetching on mount and at specified intervals.
58
- * - useUpdateCall: Hook specifically designed for update calls to actors with features such as error handling and loading state management.
59
- *
60
- * 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.
61
- */
62
- const actorHooks = (actorManager) => {
63
- const { actorStore, canisterId, visitFunction, methodAttributes, updateMethodState, extractMethodAttributes, extractInterface, callMethodWithOptions, initialize, } = actorManager;
64
- const useActorStore = (selector = (s) => s) => {
65
- return (0, zustand_1.useStore)(actorStore, (0, shallow_1.useShallow)(selector));
66
- };
67
- const useActorState = () => {
68
- return useActorStore((state) => ({
69
- name: state.name,
70
- error: state.error,
71
- version: state.version,
72
- initialized: state.isInitialized,
73
- isInitialized: state.isInitialized,
74
- initializing: state.isInitializing,
75
- isInitializing: state.isInitializing,
76
- canisterId,
77
- }));
78
- };
79
- const useMethodState = (functionName, requestKey) => {
80
- const state = useActorStore((state) => state.methodState[functionName]?.[requestKey]);
81
- const setSharedState = React.useCallback((newState) => {
82
- updateMethodState(functionName, requestKey, newState);
83
- }, [functionName, requestKey]);
84
- return [state, setSharedState];
85
- };
86
- const useMethodAttributes = () => {
87
- return React.useMemo(extractMethodAttributes, [
88
- actorManager,
89
- ]);
90
- };
91
- const useMethodNames = () => {
92
- const methodAttributes = useMethodAttributes();
93
- return Object.keys(methodAttributes);
94
- };
95
- const useActorInterface = () => {
96
- return extractInterface();
97
- };
98
- const useVisitService = () => {
99
- return visitFunction;
100
- };
101
- const useVisitMethod = (functionName) => {
102
- return React.useMemo(() => {
103
- if (!visitFunction[functionName]) {
104
- throw new Error(`Method ${functionName} not found`);
105
- }
106
- return visitFunction[functionName];
107
- }, [functionName]);
108
- };
109
- const useSharedCall = ({ args = [], functionName, throwOnError = false, onError, onLoading, onSuccess, onSuccessResult, onErrorResult, ...options }) => {
110
- const requestKey = React.useMemo(() => (0, utils_1.generateRequestHash)(args), [args]);
111
- const [sharedState, setSharedState] = useMethodState(functionName, requestKey);
112
- const latestDataRef = React.useRef(null);
113
- const reset = React.useCallback(() => {
114
- updateMethodState(functionName, requestKey, DEFAULT_STATE);
115
- latestDataRef.current = null;
116
- }, [functionName, requestKey]);
117
- const call = React.useCallback(async (eventOrReplaceArgs) => {
118
- setSharedState({ error: undefined, loading: true, isLoading: true });
119
- onLoading?.(true);
120
- try {
121
- const replaceArgs = eventOrReplaceArgs instanceof Array ? eventOrReplaceArgs : args;
122
- const data = await callMethodWithOptions(options)(functionName, ...(replaceArgs ?? args));
123
- latestDataRef.current = data;
124
- setSharedState({
125
- data,
126
- error: undefined,
127
- loading: false,
128
- isLoading: false,
129
- });
130
- onLoading?.(false);
131
- onSuccess?.(data);
132
- const { isOk, value, error } = (0, utils_1.createCompiledResult)(data);
133
- if (isOk) {
134
- onSuccessResult?.(value);
135
- }
136
- else {
137
- onErrorResult?.(error);
138
- if (throwOnError) {
139
- throw error;
140
- }
141
- }
142
- return data;
143
- }
144
- catch (error) {
145
- // eslint-disable-next-line no-console
146
- console.error(`Error calling method ${functionName}:`, error);
147
- latestDataRef.current = null;
148
- setSharedState({
149
- error: error,
150
- loading: false,
151
- isLoading: false,
152
- });
153
- onError?.(error);
154
- onLoading?.(false);
155
- if (throwOnError)
156
- throw error;
157
- return undefined;
158
- }
159
- }, [
160
- args,
161
- functionName,
162
- options,
163
- onError,
164
- onLoading,
165
- onSuccess,
166
- onSuccessResult,
167
- throwOnError,
168
- ]);
169
- const compileResult = () => {
170
- return (0, utils_1.createCompiledResult)(latestDataRef.current || sharedState?.data);
171
- };
172
- return {
173
- call,
174
- reset,
175
- compileResult,
176
- requestKey,
177
- ...sharedState,
178
- };
179
- };
180
- const useQueryCall = ({ refetchOnMount = true, refetchInterval = false, ...rest }) => {
181
- const { call, requestKey, ...state } = useSharedCall(rest);
182
- const intervalId = React.useRef(null);
183
- React.useEffect(() => {
184
- if (refetchInterval) {
185
- intervalId.current = setInterval(call, refetchInterval);
186
- }
187
- if (refetchOnMount && state.data === undefined) {
188
- call();
189
- }
190
- else if (refetchOnMount && state.data !== undefined) {
191
- rest.onSuccess?.(state.data);
192
- const { isOk, value, error } = (0, utils_1.createCompiledResult)(state.data);
193
- if (isOk) {
194
- rest.onSuccessResult?.(value);
195
- }
196
- else {
197
- rest.onErrorResult?.(error);
198
- }
199
- }
200
- return () => {
201
- if (intervalId.current) {
202
- clearInterval(intervalId.current);
203
- }
204
- };
205
- }, [refetchInterval, refetchOnMount, requestKey]);
206
- const refetch = () => {
207
- call();
208
- };
209
- return { call, refetch, intervalId, requestKey, ...state };
210
- };
211
- const useUpdateCall = useSharedCall;
212
- const useMethod = (params) => {
213
- const attributes = React.useMemo(() => {
214
- if (!methodAttributes[params.functionName]) {
215
- throw new Error(`Method ${params.functionName} not found`);
216
- }
217
- return methodAttributes[params.functionName];
218
- }, [params.functionName]);
219
- const visit = React.useCallback((extractorClass, data) => visitFunction[params.functionName](extractorClass, data), [params.functionName]);
220
- const validateArgs = React.useCallback((args, throwOnError = false) => {
221
- if (attributes.numberOfArgs > 0) {
222
- if (args === undefined || args.length === 0) {
223
- if (throwOnError) {
224
- throw new Error(`Method ${params.functionName} requires ${attributes.numberOfArgs} arguments, but none were provided.`);
225
- }
226
- return false;
227
- }
228
- try {
229
- attributes.validate(args);
230
- return true;
231
- }
232
- catch (error) {
233
- if (throwOnError) {
234
- throw error;
235
- }
236
- return false;
237
- }
238
- }
239
- return true;
240
- }, [attributes]);
241
- let refetchOnMount = params.refetchOnMount;
242
- let refetchInterval = params.refetchInterval;
243
- let isFormRequired = true;
244
- switch (attributes.type) {
245
- case "query":
246
- if (validateArgs(params.args)) {
247
- isFormRequired = params.refetchOnMount === false ? true : false;
248
- }
249
- else {
250
- refetchOnMount = false;
251
- refetchInterval = false;
252
- }
253
- return {
254
- visit,
255
- validateArgs,
256
- ...useQueryCall({
257
- ...params,
258
- refetchOnMount,
259
- refetchInterval,
260
- }),
261
- isFormRequired,
262
- };
263
- case "update":
264
- return { visit, validateArgs, ...useUpdateCall(params), isFormRequired };
265
- default:
266
- throw new Error(`Method type ${attributes.type} not found`);
267
- }
268
- };
269
- return {
270
- initialize,
271
- useActorStore,
272
- useMethodAttributes,
273
- useMethodNames,
274
- useMethod,
275
- useQueryCall,
276
- useUpdateCall,
277
- useActorState,
278
- useVisitMethod,
279
- useVisitService,
280
- useActorInterface,
281
- };
282
- };
283
- exports.actorHooks = actorHooks;
@@ -1,3 +0,0 @@
1
- import type { AgentManager } from "@ic-reactor/core/dist/types";
2
- import type { AgentHooksReturnType } from "./types";
3
- export declare const agentHooks: (agentManager: AgentManager) => AgentHooksReturnType;
@@ -1,22 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.agentHooks = void 0;
7
- const react_1 = __importDefault(require("react"));
8
- const zustand_1 = require("zustand");
9
- const agentHooks = (agentManager) => {
10
- const { agentStore, getAgent, subscribeAgent } = agentManager;
11
- const useAgentState = () => (0, zustand_1.useStore)(agentStore);
12
- const useAgent = () => {
13
- const [agent, setAgent] = react_1.default.useState(getAgent);
14
- react_1.default.useEffect(() => subscribeAgent(setAgent), [subscribeAgent]);
15
- return agent;
16
- };
17
- return {
18
- useAgent,
19
- useAgentState,
20
- };
21
- };
22
- exports.agentHooks = agentHooks;
@@ -1,2 +0,0 @@
1
- import type { AgentManager, AuthHooksReturnType } from "../types";
2
- export declare const authHooks: (agentManager: AgentManager) => AuthHooksReturnType;
@@ -1,120 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.authHooks = void 0;
7
- const react_1 = __importDefault(require("react"));
8
- const zustand_1 = require("zustand");
9
- const utils_1 = require("@ic-reactor/core/dist/utils");
10
- const authHooks = (agentManager) => {
11
- const { authenticate: authenticator, getIsLocal, getAuth, authStore, } = agentManager;
12
- const useAuthState = () => (0, zustand_1.useStore)(authStore);
13
- const useUserPrincipal = () => useAuthState()?.identity?.getPrincipal();
14
- const useAuth = ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, } = {}) => {
15
- const [loginState, setLoginState] = react_1.default.useState({
16
- loading: false,
17
- isLoading: false,
18
- error: undefined,
19
- });
20
- const { isAuthenticated, isAuthenticating, error, identity } = useAuthState();
21
- const authenticate = react_1.default.useCallback(async () => {
22
- const authenticatePromise = new Promise((resolve, reject) => {
23
- authenticator()
24
- .then((identity) => {
25
- onAuthenticationSuccess?.(identity);
26
- resolve(identity);
27
- })
28
- .catch((e) => {
29
- onAuthenticationFailure?.(e);
30
- reject(e);
31
- });
32
- });
33
- onAuthentication?.(() => authenticatePromise);
34
- return authenticatePromise;
35
- }, [
36
- authenticator,
37
- onAuthentication,
38
- onAuthenticationSuccess,
39
- onAuthenticationFailure,
40
- ]);
41
- const login = react_1.default.useCallback(async (options) => {
42
- setLoginState({ loading: true, isLoading: true, error: undefined });
43
- const loginPromise = new Promise((resolve, reject) => {
44
- try {
45
- const authClient = getAuth();
46
- if (!authClient) {
47
- throw new Error("Auth client not initialized");
48
- }
49
- authClient.login({
50
- identityProvider: getIsLocal()
51
- ? utils_1.LOCAL_INTERNET_IDENTITY_PROVIDER
52
- : utils_1.IC_INTERNET_IDENTITY_PROVIDER,
53
- ...options,
54
- onSuccess: (msg) => {
55
- authenticate()
56
- .then((identity) => {
57
- const principal = identity.getPrincipal();
58
- options?.onSuccess?.(msg);
59
- onLoginSuccess?.(principal);
60
- resolve(principal);
61
- setLoginState({
62
- loading: false,
63
- isLoading: false,
64
- error: undefined,
65
- });
66
- })
67
- .catch((error) => {
68
- setLoginState({ loading: false, isLoading: false, error });
69
- onLoginError?.(error);
70
- reject(error);
71
- });
72
- },
73
- onError: (error) => {
74
- options?.onError?.(error);
75
- setLoginState({ loading: false, isLoading: false, error });
76
- onLoginError?.(error);
77
- reject(error);
78
- },
79
- });
80
- }
81
- catch (e) {
82
- const error = e;
83
- setLoginState({ loading: false, isLoading: false, error });
84
- onLoginError?.(error);
85
- reject(error);
86
- }
87
- });
88
- onLogin?.(() => loginPromise);
89
- }, [onLogin, onLoginSuccess, onLoginError, authenticate]);
90
- const logout = react_1.default.useCallback(async (options) => {
91
- const authClient = getAuth();
92
- if (!authClient) {
93
- throw new Error("Auth client not initialized");
94
- }
95
- await authClient.logout(options);
96
- await authenticate();
97
- onLoggedOut?.();
98
- }, [onLoggedOut]);
99
- return {
100
- isAuthenticated,
101
- isAuthenticating,
102
- authenticated: isAuthenticated,
103
- authenticating: isAuthenticating,
104
- identity,
105
- error,
106
- login,
107
- logout,
108
- authenticate,
109
- loginLoading: loginState.isLoading,
110
- isLoginLoading: loginState.isLoading,
111
- loginError: loginState.error,
112
- };
113
- };
114
- return {
115
- useUserPrincipal,
116
- useAuthState,
117
- useAuth,
118
- };
119
- };
120
- exports.authHooks = authHooks;
@@ -1,4 +0,0 @@
1
- import React from "react";
2
- import type { BaseActor } from "../types";
3
- import type { CreateActorContextReturnType, CreateActorContextType } from "../context/actor/types";
4
- export declare function extractActorContext<A = BaseActor>(actorContext: React.Context<CreateActorContextType<A> | null>): Omit<CreateActorContextReturnType<A>, "ActorProvider" | "ActorHookProvider" | "ActorManagerProvider">;
@@ -1,44 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.extractActorContext = extractActorContext;
7
- const react_1 = __importDefault(require("react"));
8
- function extractActorContext(actorContext) {
9
- const useActorContext = () => {
10
- const context = react_1.default.useContext(actorContext);
11
- if (!context) {
12
- throw new Error("Actor hooks must be used within a ActorProvider");
13
- }
14
- return context;
15
- };
16
- const initialize = () => useActorContext().initialize();
17
- const useMethodNames = () => useActorContext().useMethodNames();
18
- const useMethodAttributes = () => useActorContext().useMethodAttributes();
19
- const useActorStore = (selector = (s) => s) => {
20
- return useActorContext().useActorStore(selector);
21
- };
22
- const useActorState = () => useActorContext().useActorState();
23
- const useMethod = (args) => useActorContext().useMethod(args);
24
- const useQueryCall = (args) => useActorContext().useQueryCall(args);
25
- const useUpdateCall = (args) => useActorContext().useUpdateCall(args);
26
- const useVisitMethod = (functionName) => useActorContext().useVisitMethod(functionName);
27
- const useVisitService = () => useActorContext().useVisitService();
28
- const useActorInterface = () => useActorContext().useActorInterface();
29
- const useInitializeActor = () => useActorContext().useInitializeActor?.();
30
- return {
31
- useActorStore,
32
- useActorState,
33
- useMethod,
34
- useMethodNames,
35
- useMethodAttributes,
36
- useQueryCall,
37
- useUpdateCall,
38
- useVisitMethod,
39
- useVisitService,
40
- useActorInterface,
41
- useInitializeActor,
42
- initialize,
43
- };
44
- }
@@ -1,28 +0,0 @@
1
- import React from "react";
2
- import type { AgentContext, CreateAgentContextReturnType } from "../types";
3
- /**
4
- * This function facilitates the use of contextually provided agent functionalities,
5
- * such as managing the agent's state, authentication state, and user principal.
6
- *
7
- * @param agentContext A React context object of type AgentContext or null,
8
- * typically provided by an AgentProvider at a higher level in the component tree.
9
- * @returns An object containing the following hooks:
10
- * - useAgent: Hook for accessing the current agent instance.
11
- * - useAuthState: Hook for accessing the current authentication state.
12
- * - useAgentState: Hook for accessing the current state of the agent.
13
- * - useAuth: Hook for accessing the authentication client, optionally accepting arguments for configuration.
14
- * - useAgentManager: Hook for accessing the AgentManager instance.
15
- * - useUserPrincipal: Hook for accessing the user's principal.
16
- *
17
- * Each hook is designed to be used within components that are descendants of an AgentProvider,
18
- * ensuring access to the necessary agent-related context.
19
- *
20
- * Throws:
21
- * - Error if used outside of an AgentProvider context.
22
- *
23
- * ### Integration
24
- *
25
- * To use these hooks, ensure your components are wrapped in an `AgentProvider` that you've set up to supply the `AgentContext`.
26
- * This context provides the necessary agent functionalities and state management capabilities required by the hooks.
27
- */
28
- export declare const extractAgentContext: (agentContext: React.Context<AgentContext | null>) => Omit<CreateAgentContextReturnType, "AgentProvider" | "AgentContext">;
@@ -1,59 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.extractAgentContext = void 0;
7
- const react_1 = __importDefault(require("react"));
8
- /**
9
- * This function facilitates the use of contextually provided agent functionalities,
10
- * such as managing the agent's state, authentication state, and user principal.
11
- *
12
- * @param agentContext A React context object of type AgentContext or null,
13
- * typically provided by an AgentProvider at a higher level in the component tree.
14
- * @returns An object containing the following hooks:
15
- * - useAgent: Hook for accessing the current agent instance.
16
- * - useAuthState: Hook for accessing the current authentication state.
17
- * - useAgentState: Hook for accessing the current state of the agent.
18
- * - useAuth: Hook for accessing the authentication client, optionally accepting arguments for configuration.
19
- * - useAgentManager: Hook for accessing the AgentManager instance.
20
- * - useUserPrincipal: Hook for accessing the user's principal.
21
- *
22
- * Each hook is designed to be used within components that are descendants of an AgentProvider,
23
- * ensuring access to the necessary agent-related context.
24
- *
25
- * Throws:
26
- * - Error if used outside of an AgentProvider context.
27
- *
28
- * ### Integration
29
- *
30
- * To use these hooks, ensure your components are wrapped in an `AgentProvider` that you've set up to supply the `AgentContext`.
31
- * This context provides the necessary agent functionalities and state management capabilities required by the hooks.
32
- */
33
- const extractAgentContext = (agentContext) => {
34
- const useAgentContext = () => {
35
- const context = react_1.default.useContext(agentContext);
36
- if (!context) {
37
- throw new Error("Agent hooks must be used within a AgentProvider");
38
- }
39
- return context;
40
- };
41
- const useAgentManager = () => {
42
- const context = useAgentContext();
43
- return context.agentManager;
44
- };
45
- const useAgent = () => useAgentContext().useAgent();
46
- const useAuthState = () => useAgentContext().useAuthState();
47
- const useAgentState = () => useAgentContext().useAgentState();
48
- const useAuth = (args) => useAgentContext().useAuth(args);
49
- const useUserPrincipal = () => useAgentContext().useUserPrincipal();
50
- return {
51
- useAgent,
52
- useAuthState,
53
- useAgentState,
54
- useAuth,
55
- useAgentManager,
56
- useUserPrincipal,
57
- };
58
- };
59
- exports.extractAgentContext = extractAgentContext;
@@ -1,5 +0,0 @@
1
- export * from "./agentHooks";
2
- export * from "./actorHooks";
3
- export * from "./authHooks";
4
- export * from "./extractActorContext";
5
- export * from "./extractAgentContext";
@@ -1,21 +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("./agentHooks"), exports);
18
- __exportStar(require("./actorHooks"), exports);
19
- __exportStar(require("./authHooks"), exports);
20
- __exportStar(require("./extractActorContext"), exports);
21
- __exportStar(require("./extractAgentContext"), exports);