@ic-reactor/react 1.0.1 → 1.0.3

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 (45) hide show
  1. package/README.md +2 -2
  2. package/dist/core.d.ts +1 -0
  3. package/dist/core.js +8 -0
  4. package/dist/helpers/actor.d.ts +4 -0
  5. package/dist/{hooks → helpers}/actor.js +30 -50
  6. package/dist/{hooks → helpers}/agent.js +4 -6
  7. package/dist/helpers/auth.d.ts +7 -0
  8. package/dist/helpers/auth.js +108 -0
  9. package/dist/helpers/index.d.ts +3 -0
  10. package/dist/helpers/index.js +19 -0
  11. package/dist/helpers/types.d.ts +81 -0
  12. package/dist/hooks/index.d.ts +1 -4
  13. package/dist/hooks/index.js +1 -4
  14. package/dist/hooks/useCandid.js +1 -1
  15. package/dist/hooks/{useActor.d.ts → useReactor.d.ts} +2 -4
  16. package/dist/hooks/{useActor.js → useReactor.js} +5 -7
  17. package/dist/index.d.ts +5 -29
  18. package/dist/index.js +9 -44
  19. package/dist/main.d.ts +2 -0
  20. package/dist/main.js +35 -0
  21. package/dist/provider/actor/index.d.ts +15 -0
  22. package/dist/{context → provider}/actor/index.js +7 -8
  23. package/dist/{context → provider}/actor/types.d.ts +4 -11
  24. package/dist/{context → provider}/agent/context.d.ts +2 -0
  25. package/dist/{context → provider}/agent/context.js +5 -4
  26. package/dist/provider/agent/hooks.d.ts +12 -0
  27. package/dist/{context → provider}/agent/hooks.js +10 -10
  28. package/dist/{context → provider}/agent/types.d.ts +2 -2
  29. package/dist/types.d.ts +7 -73
  30. package/dist/types.js +3 -3
  31. package/package.json +3 -3
  32. package/dist/context/actor/index.d.ts +0 -16
  33. package/dist/context/agent/hooks.d.ts +0 -24
  34. package/dist/hooks/actor.d.ts +0 -3
  35. package/dist/hooks/auth.d.ts +0 -21
  36. package/dist/hooks/auth.js +0 -131
  37. package/dist/hooks/types.d.ts +0 -4
  38. /package/dist/{hooks → helpers}/agent.d.ts +0 -0
  39. /package/dist/{context/actor → helpers}/types.js +0 -0
  40. /package/dist/{context/agent → provider/actor}/types.js +0 -0
  41. /package/dist/{context → provider}/agent/index.d.ts +0 -0
  42. /package/dist/{context → provider}/agent/index.js +0 -0
  43. /package/dist/{hooks → provider/agent}/types.js +0 -0
  44. /package/dist/{context → provider}/index.d.ts +0 -0
  45. /package/dist/{context → provider}/index.js +0 -0
package/README.md CHANGED
@@ -31,12 +31,12 @@ First, create an actor declaration file:
31
31
  ```ts
32
32
  // store.ts
33
33
  import { canisterId, idlFactory, actor } from "declaration/actor"
34
- import { createReActor } from "@ic-reactor/react"
34
+ import { createReactor } from "@ic-reactor/react"
35
35
 
36
36
  type Actor = typeof actor
37
37
 
38
38
  export const { useActorStore, useAuthClient, useQueryCall } =
39
- createReActor<Actor>({
39
+ createReactor<Actor>({
40
40
  canisterId: "rrkah-fqaaa-aaaaa-aaaaq-cai",
41
41
  idlFactory,
42
42
  host: "https://localhost:4943",
package/dist/core.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { createReactorStore, createActorManager, createAgentManager, createCandidAdapter, } from "@ic-reactor/core";
package/dist/core.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createCandidAdapter = exports.createAgentManager = exports.createActorManager = exports.createReactorStore = void 0;
4
+ var core_1 = require("@ic-reactor/core");
5
+ Object.defineProperty(exports, "createReactorStore", { enumerable: true, get: function () { return core_1.createReactorStore; } });
6
+ Object.defineProperty(exports, "createActorManager", { enumerable: true, get: function () { return core_1.createActorManager; } });
7
+ Object.defineProperty(exports, "createAgentManager", { enumerable: true, get: function () { return core_1.createAgentManager; } });
8
+ Object.defineProperty(exports, "createCandidAdapter", { enumerable: true, get: function () { return core_1.createCandidAdapter; } });
@@ -0,0 +1,4 @@
1
+ import type { ActorHooks } from "../types";
2
+ import type { BaseActor } from "@ic-reactor/core/dist/types";
3
+ import { ActorManager } from "@ic-reactor/core";
4
+ export declare const getActorHooks: <A = BaseActor>(actorManager: ActorManager<A>) => ActorHooks<A>;
@@ -21,7 +21,6 @@ var __rest = (this && this.__rest) || function (s, e) {
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
23
  exports.getActorHooks = void 0;
24
- /* eslint-disable no-console */
25
24
  const react_1 = require("react");
26
25
  const zustand_1 = require("zustand");
27
26
  const DEFAULT_STATE = {
@@ -29,86 +28,67 @@ const DEFAULT_STATE = {
29
28
  error: undefined,
30
29
  loading: false,
31
30
  };
32
- const getActorHooks = ({ initialize, canisterId, actorStore, callMethod, visitFunction, }) => {
33
- const useActorState = () => {
34
- const actorState = (0, zustand_1.useStore)(actorStore, (state) => state);
35
- return Object.assign(Object.assign({}, actorState), { canisterId });
36
- };
37
- const useVisitFunction = () => {
38
- return visitFunction;
39
- };
31
+ const getActorHooks = (actorManager) => {
32
+ const { actorStore, canisterId, visitFunction, callMethod, initialize } = actorManager;
33
+ const useActorState = () => (Object.assign(Object.assign({}, (0, zustand_1.useStore)(actorStore)), { canisterId }));
40
34
  const useVisitMethod = (functionName) => {
41
- const serviceFields = useVisitFunction();
42
- return (0, react_1.useMemo)(() => {
43
- return serviceFields[functionName];
44
- }, [functionName, serviceFields]);
35
+ return (0, react_1.useMemo)(() => visitFunction[functionName], [functionName]);
45
36
  };
46
- const useReActorCall = ({ onError, onSuccess, onLoading, args = [], functionName, throwOnError = false, }) => {
37
+ const useReactorCall = (_a) => {
38
+ var { args = [], functionName, throwOnError = false } = _a, events = __rest(_a, ["args", "functionName", "throwOnError"]);
47
39
  const [state, setState] = (0, react_1.useState)(DEFAULT_STATE);
48
40
  const reset = (0, react_1.useCallback)(() => setState(DEFAULT_STATE), []);
49
41
  const call = (0, react_1.useCallback)((eventOrReplaceArgs) => __awaiter(void 0, void 0, void 0, function* () {
50
- onLoading === null || onLoading === void 0 ? void 0 : onLoading(true);
51
- onError === null || onError === void 0 ? void 0 : onError(undefined);
52
- setState((prevState) => (Object.assign(Object.assign({}, prevState), { loading: true, error: undefined })));
42
+ var _b, _c, _d, _e, _f;
43
+ setState({ data: undefined, error: undefined, loading: true });
44
+ (_b = events === null || events === void 0 ? void 0 : events.onLoading) === null || _b === void 0 ? void 0 : _b.call(events, true);
53
45
  try {
54
- const replaceArgs = eventOrReplaceArgs !== undefined
55
- ? eventOrReplaceArgs.length > 0
56
- ? eventOrReplaceArgs
57
- : undefined
58
- : undefined;
59
- const data = yield callMethod(functionName, ...(replaceArgs !== null && replaceArgs !== void 0 ? replaceArgs : args));
60
- onLoading === null || onLoading === void 0 ? void 0 : onLoading(false);
61
- onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
62
- setState((prevState) => (Object.assign(Object.assign({}, prevState), { data, loading: false })));
46
+ const replaceArgs = eventOrReplaceArgs instanceof Array ? eventOrReplaceArgs : args;
47
+ const data = yield callMethod(functionName, ...replaceArgs);
48
+ setState({ data, error: undefined, loading: false });
49
+ (_c = events === null || events === void 0 ? void 0 : events.onSuccess) === null || _c === void 0 ? void 0 : _c.call(events, data);
50
+ (_d = events === null || events === void 0 ? void 0 : events.onLoading) === null || _d === void 0 ? void 0 : _d.call(events, false);
63
51
  return data;
64
52
  }
65
53
  catch (error) {
54
+ // eslint-disable-next-line no-console
66
55
  console.error("Error in call:", error);
67
- onError === null || onError === void 0 ? void 0 : onError(error);
68
- onLoading === null || onLoading === void 0 ? void 0 : onLoading(false);
69
56
  setState((prevState) => (Object.assign(Object.assign({}, prevState), { error: error, loading: false })));
57
+ (_e = events === null || events === void 0 ? void 0 : events.onError) === null || _e === void 0 ? void 0 : _e.call(events, error);
58
+ (_f = events === null || events === void 0 ? void 0 : events.onLoading) === null || _f === void 0 ? void 0 : _f.call(events, false);
70
59
  if (throwOnError)
71
60
  throw error;
72
61
  }
73
- }), [args, functionName, onError, onSuccess, onLoading]);
62
+ }), [args, functionName, events, callMethod]);
74
63
  return Object.assign({ call, reset }, state);
75
64
  };
76
65
  const useQueryCall = (_a) => {
77
66
  var { refetchOnMount = true, refetchInterval = false } = _a, rest = __rest(_a, ["refetchOnMount", "refetchInterval"]);
78
- const _b = useReActorCall(rest), { call } = _b, state = __rest(_b, ["call"]);
79
- const intervalId = (0, react_1.useRef)(undefined);
67
+ const _b = useReactorCall(rest), { call } = _b, state = __rest(_b, ["call"]);
68
+ const intervalId = (0, react_1.useRef)();
80
69
  (0, react_1.useEffect)(() => {
81
- // Auto-refresh logic
82
- if (refetchInterval) {
83
- intervalId.current = setInterval(() => {
84
- call();
85
- }, refetchInterval);
86
- }
87
- return () => {
88
- clearInterval(intervalId.current);
89
- };
90
- }, [refetchInterval]);
70
+ if (refetchInterval)
71
+ intervalId.current = setInterval(call, refetchInterval);
72
+ return () => clearInterval(intervalId.current);
73
+ }, [refetchInterval, call]);
91
74
  (0, react_1.useLayoutEffect)(() => {
92
- if (refetchOnMount) {
75
+ if (refetchOnMount)
93
76
  call();
94
- }
95
- }, []);
77
+ }, [call, refetchOnMount]);
96
78
  return Object.assign({ call }, state);
97
79
  };
98
- const useUpdateCall = (args) => {
99
- return useReActorCall(args);
100
- };
80
+ const useUpdateCall = useReactorCall;
101
81
  const useMethodCall = (args) => {
102
- const visit = visitFunction[args.functionName];
103
- return Object.assign({ visit }, useReActorCall(args));
82
+ const visit = useVisitMethod(args.functionName);
83
+ return Object.assign({ visit }, useReactorCall(args));
104
84
  };
105
85
  return {
106
86
  initialize,
107
87
  useQueryCall,
108
88
  useUpdateCall,
109
- useVisitMethod,
110
89
  useActorState,
111
90
  useMethodCall,
91
+ useVisitMethod,
112
92
  };
113
93
  };
114
94
  exports.getActorHooks = getActorHooks;
@@ -4,13 +4,11 @@ exports.getAgentHooks = void 0;
4
4
  const react_1 = require("react");
5
5
  const zustand_1 = require("zustand");
6
6
  const getAgentHooks = (agentManager) => {
7
- const { agentStore, getAgent } = agentManager;
8
- const useAgentState = () => {
9
- return (0, zustand_1.useStore)(agentStore, (state) => state);
10
- };
7
+ const { agentStore, getAgent, subscribeAgent } = agentManager;
8
+ const useAgentState = () => (0, zustand_1.useStore)(agentStore);
11
9
  const useAgent = () => {
12
- const [agent, setAgent] = (0, react_1.useState)(getAgent());
13
- (0, react_1.useEffect)(() => agentManager.subscribeAgent(setAgent), []);
10
+ const [agent, setAgent] = (0, react_1.useState)(getAgent);
11
+ (0, react_1.useEffect)(() => subscribeAgent(setAgent), [subscribeAgent]);
14
12
  return agent;
15
13
  };
16
14
  return {
@@ -0,0 +1,7 @@
1
+ import type { AgentManager } from "@ic-reactor/core/dist/agent";
2
+ import type { UseAuthClientArgs, UseAuthClientReturn } from "../types";
3
+ export declare const getAuthHooks: (agentManager: AgentManager) => {
4
+ useUserPrincipal: () => import("@dfinity/principal").Principal | undefined;
5
+ useAuthState: () => import("@ic-reactor/core/dist/agent/types").AuthState;
6
+ useAuthClient: (events?: UseAuthClientArgs) => UseAuthClientReturn;
7
+ };
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.getAuthHooks = void 0;
13
+ const zustand_1 = require("zustand");
14
+ const react_1 = require("react");
15
+ const tools_1 = require("@ic-reactor/core/dist/tools");
16
+ const getAuthHooks = (agentManager) => {
17
+ const { authenticate: authenticator, authStore, isLocalEnv } = agentManager;
18
+ const useAuthState = () => (0, zustand_1.useStore)(authStore);
19
+ const useUserPrincipal = () => { var _a, _b; return (_b = (_a = useAuthState()) === null || _a === void 0 ? void 0 : _a.identity) === null || _b === void 0 ? void 0 : _b.getPrincipal(); };
20
+ const useAuthClient = (events) => {
21
+ const [loginState, setLoginState] = (0, react_1.useState)({
22
+ loading: false,
23
+ error: null,
24
+ });
25
+ const { authClient, authenticated, authenticating, identity, error } = useAuthState();
26
+ const authenticate = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
27
+ var _a, _b;
28
+ try {
29
+ const identity = yield authenticator();
30
+ (_a = events === null || events === void 0 ? void 0 : events.onAuthenticationSuccess) === null || _a === void 0 ? void 0 : _a.call(events, identity);
31
+ return identity;
32
+ }
33
+ catch (e) {
34
+ (_b = events === null || events === void 0 ? void 0 : events.onAuthenticationFailure) === null || _b === void 0 ? void 0 : _b.call(events, e);
35
+ throw e;
36
+ }
37
+ }), [authenticator, events]);
38
+ const login = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
39
+ var _c;
40
+ if (!authClient) {
41
+ throw new Error("Auth client not initialized");
42
+ }
43
+ try {
44
+ setLoginState({ loading: true, error: null });
45
+ yield authClient.login(Object.assign(Object.assign({ identityProvider: isLocalEnv
46
+ ? tools_1.LOCAL_INTERNET_IDENTITY_PROVIDER
47
+ : tools_1.IC_INTERNET_IDENTITY_PROVIDER }, options), { onSuccess: () => __awaiter(void 0, void 0, void 0, function* () {
48
+ var _d, _e, _f;
49
+ try {
50
+ const identity = yield authenticate();
51
+ const principal = identity.getPrincipal();
52
+ (_d = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _d === void 0 ? void 0 : _d.call(options);
53
+ (_e = events === null || events === void 0 ? void 0 : events.onLoginSuccess) === null || _e === void 0 ? void 0 : _e.call(events, principal);
54
+ setLoginState({ loading: false, error: null });
55
+ }
56
+ catch (e) {
57
+ const error = e;
58
+ setLoginState({ loading: false, error });
59
+ (_f = events === null || events === void 0 ? void 0 : events.onLoginError) === null || _f === void 0 ? void 0 : _f.call(events, error);
60
+ }
61
+ }), onError: (e) => {
62
+ var _a;
63
+ const error = new Error(`Login failed: ${e}`);
64
+ setLoginState({ loading: false, error });
65
+ (_a = events === null || events === void 0 ? void 0 : events.onLoginError) === null || _a === void 0 ? void 0 : _a.call(events, error);
66
+ } }));
67
+ }
68
+ catch (e) {
69
+ const error = e;
70
+ setLoginState({ loading: false, error });
71
+ (_c = events === null || events === void 0 ? void 0 : events.onLoginError) === null || _c === void 0 ? void 0 : _c.call(events, error);
72
+ }
73
+ }), [authClient, authenticate, isLocalEnv, events]);
74
+ const logout = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
75
+ var _g;
76
+ if (!authClient) {
77
+ throw new Error("Auth client not initialized");
78
+ }
79
+ yield authClient.logout(options);
80
+ yield authenticate();
81
+ (_g = events === null || events === void 0 ? void 0 : events.onLoggedOut) === null || _g === void 0 ? void 0 : _g.call(events);
82
+ }), [authClient, authenticate, events]);
83
+ (0, react_1.useEffect)(() => {
84
+ if (!authClient && !authenticating) {
85
+ // eslint-disable-next-line no-console
86
+ authenticate().catch(console.error);
87
+ }
88
+ }, [authClient, authenticating, authenticate]);
89
+ return {
90
+ error,
91
+ authClient,
92
+ authenticated,
93
+ authenticating,
94
+ identity,
95
+ login,
96
+ logout,
97
+ authenticate,
98
+ loginLoading: loginState.loading,
99
+ loginError: loginState.error,
100
+ };
101
+ };
102
+ return {
103
+ useUserPrincipal,
104
+ useAuthState,
105
+ useAuthClient,
106
+ };
107
+ };
108
+ exports.getAuthHooks = getAuthHooks;
@@ -0,0 +1,3 @@
1
+ export * from "./agent";
2
+ export * from "./actor";
3
+ export * from "./auth";
@@ -0,0 +1,19 @@
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("./agent"), exports);
18
+ __exportStar(require("./actor"), exports);
19
+ __exportStar(require("./auth"), exports);
@@ -0,0 +1,81 @@
1
+ /// <reference types="react" />
2
+ import type { AuthClientLoginOptions } from "@dfinity/auth-client";
3
+ import type { getAgentHooks } from "./agent";
4
+ import type { getAuthHooks } from "./auth";
5
+ import type { ActorState, CanisterId, ActorMethodArgs, ActorMethodReturnType, Identity, Principal, FunctionName, VisitService, AuthClient } from "@ic-reactor/core/dist/types";
6
+ export type AgentHooks = ReturnType<typeof getAgentHooks>;
7
+ export type AuthHooks = ReturnType<typeof getAuthHooks>;
8
+ export interface UseAuthClientArgs {
9
+ onAuthentication?: (promise: () => Promise<Identity>) => void;
10
+ onAuthenticationSuccess?: (identity: Identity) => void;
11
+ onAuthenticationFailure?: (error: Error) => void;
12
+ onLoginSuccess?: (principal: Principal) => void;
13
+ onLoginError?: (error: Error) => void;
14
+ onLogin?: (promise: () => Promise<Principal>) => void;
15
+ onLoggedOut?: () => void;
16
+ }
17
+ export interface UseAuthClientReturn {
18
+ error: Error | undefined;
19
+ authClient: AuthClient | null;
20
+ authenticated: boolean;
21
+ authenticating: boolean;
22
+ identity: Identity | null;
23
+ login: (options?: LoginOptions) => Promise<void>;
24
+ logout: (options?: LogoutOptions) => Promise<void>;
25
+ authenticate: () => Promise<Identity>;
26
+ loginLoading: boolean;
27
+ loginError: Error | null;
28
+ }
29
+ export type LoginState = {
30
+ loading: boolean;
31
+ error: Error | null;
32
+ };
33
+ export type LoginOptions = AuthClientLoginOptions;
34
+ export type LogoutOptions = {
35
+ returnTo?: string;
36
+ };
37
+ export type ReactorCallArgs<A, M extends FunctionName<A>> = {
38
+ functionName: M;
39
+ args?: ActorMethodArgs<A[M]>;
40
+ onLoading?: (loading: boolean) => void;
41
+ onError?: (error: Error | undefined) => void;
42
+ onSuccess?: (data: ActorMethodReturnType<A[M]> | undefined) => void;
43
+ throwOnError?: boolean;
44
+ };
45
+ export type ActorCallState<A, M extends FunctionName<A>> = {
46
+ data: ActorMethodReturnType<A[M]> | undefined;
47
+ error: Error | undefined;
48
+ loading: boolean;
49
+ };
50
+ export interface UseQueryCallArgs<A, M extends FunctionName<A>> extends ReactorCallArgs<A, M> {
51
+ refetchOnMount?: boolean;
52
+ refetchInterval?: number | false;
53
+ }
54
+ export interface UseUpdateCallArgs<A, M extends FunctionName<A>> extends ReactorCallArgs<A, M> {
55
+ }
56
+ export interface ReactorCallReturn<A, M extends FunctionName<A> = FunctionName<A>> extends ActorCallState<A, M> {
57
+ reset: () => void;
58
+ call: (eventOrReplaceArgs?: React.MouseEvent | ActorMethodArgs<A[M]>) => Promise<ActorMethodReturnType<A[M]> | undefined>;
59
+ }
60
+ export type ReactorCall<A> = <M extends FunctionName<A>>(args: ReactorCallArgs<A, M>) => ReactorCallReturn<A, M>;
61
+ export type UseQueryCall<A> = <M extends FunctionName<A>>(args: UseQueryCallArgs<A, M>) => ReactorCallReturn<A, M>;
62
+ export type UseUpdateCall<A> = <M extends FunctionName<A>>(args: UseUpdateCallArgs<A, M>) => ReactorCallReturn<A, M>;
63
+ export type UseMethodCall<A> = <M extends FunctionName<A>>(args: UseMethodCallArg<A, M>) => UseMethodCallReturn<A, M>;
64
+ export interface UseMethodCallReturn<A, M extends FunctionName<A> = FunctionName<A>> extends ReactorCallReturn<A, M> {
65
+ visit: VisitService<A>[M];
66
+ reset: () => void;
67
+ error: Error | undefined;
68
+ loading: boolean;
69
+ }
70
+ export type UseMethodCallArg<A, M extends FunctionName<A>> = ReactorCallArgs<A, M>;
71
+ export interface UseActorState extends Omit<ActorState, "methodState"> {
72
+ canisterId: CanisterId;
73
+ }
74
+ export interface ActorHooks<A> {
75
+ initialize: () => Promise<void>;
76
+ useActorState: () => UseActorState;
77
+ useQueryCall: UseQueryCall<A>;
78
+ useUpdateCall: UseUpdateCall<A>;
79
+ useMethodCall: UseMethodCall<A>;
80
+ useVisitMethod: <M extends FunctionName<A>>(functionName: M) => VisitService<A>[M];
81
+ }
@@ -1,5 +1,2 @@
1
- export * from "./useActor";
1
+ export * from "./useReactor";
2
2
  export * from "./useCandid";
3
- export * from "./agent";
4
- export * from "./actor";
5
- export * from "./auth";
@@ -14,8 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./useActor"), exports);
17
+ __exportStar(require("./useReactor"), exports);
18
18
  __exportStar(require("./useCandid"), exports);
19
- __exportStar(require("./agent"), exports);
20
- __exportStar(require("./actor"), exports);
21
- __exportStar(require("./auth"), exports);
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.useCandid = void 0;
13
13
  const react_1 = require("react");
14
14
  const core_1 = require("@ic-reactor/core");
15
- const agent_1 = require("../context/agent");
15
+ const agent_1 = require("../provider/agent");
16
16
  const DEFAULT_STATE = {
17
17
  candid: { idlFactory: undefined, init: () => [] },
18
18
  fetching: false,
@@ -9,13 +9,11 @@ interface DynamicActorArgs extends Omit<ActorManagerOptions, "idlFactory" | "age
9
9
  }
10
10
  /**
11
11
  * A hook to create an actor manager and fetch the actor's candid interface.
12
- *
13
- * @category Hooks
14
12
  */
15
- export declare const useActor: <A = BaseActor>({ canisterId, agentContext, idlFactory: maybeIdlFactory, didjsCanisterId, ...config }: DynamicActorArgs) => {
13
+ export declare const useReactor: <A = BaseActor>({ canisterId, agentContext, idlFactory: maybeIdlFactory, didjsCanisterId, ...config }: DynamicActorArgs) => {
16
14
  fetchCandid: () => Promise<import("@ic-reactor/core/dist/types").CandidDefenition | undefined>;
17
15
  fetching: boolean;
18
16
  fetchError: string | null;
19
- actorManager: import("@ic-reactor/core/dist/actor").ActorManager<A> | null;
17
+ actorManager: import("@ic-reactor/core").ActorManager<A> | null;
20
18
  };
21
19
  export {};
@@ -11,17 +11,15 @@ var __rest = (this && this.__rest) || function (s, e) {
11
11
  return t;
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.useActor = void 0;
14
+ exports.useReactor = void 0;
15
15
  const core_1 = require("@ic-reactor/core");
16
16
  const react_1 = require("react");
17
- const agent_1 = require("../context/agent");
17
+ const agent_1 = require("../provider/agent");
18
18
  const useCandid_1 = require("./useCandid");
19
19
  /**
20
20
  * A hook to create an actor manager and fetch the actor's candid interface.
21
- *
22
- * @category Hooks
23
21
  */
24
- const useActor = (_a) => {
22
+ const useReactor = (_a) => {
25
23
  var { canisterId, agentContext, idlFactory: maybeIdlFactory, didjsCanisterId } = _a, config = __rest(_a, ["canisterId", "agentContext", "idlFactory", "didjsCanisterId"]);
26
24
  const agentManager = (0, agent_1.useAgentManager)(agentContext);
27
25
  const _b = (0, useCandid_1.useCandid)({
@@ -34,7 +32,7 @@ const useActor = (_a) => {
34
32
  return null;
35
33
  }
36
34
  else {
37
- const manager = (0, core_1.createReActorStore)({
35
+ const manager = (0, core_1.createReactorStore)({
38
36
  agentManager,
39
37
  idlFactory,
40
38
  canisterId,
@@ -45,4 +43,4 @@ const useActor = (_a) => {
45
43
  }, [idlFactory]);
46
44
  return Object.assign({ actorManager }, rest);
47
45
  };
48
- exports.useActor = useActor;
46
+ exports.useReactor = useReactor;
package/dist/index.d.ts CHANGED
@@ -1,30 +1,6 @@
1
- import { BaseActor, CreateReActorOptions } from "@ic-reactor/core/dist/types";
2
- export declare const createReActor: <A = BaseActor>({ isLocalEnv, withVisitor, withProcessEnv, ...options }: CreateReActorOptions) => {
3
- useUserPrincipal: () => import("@dfinity/principal").Principal | undefined;
4
- useAuthStore: () => import("@ic-reactor/core/dist/types").AuthState;
5
- useAuthClient: ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, }?: import("./types").AuthArgs) => {
6
- authClient: import("@dfinity/auth-client").AuthClient | null;
7
- authenticated: boolean;
8
- authenticating: boolean;
9
- identity: import("@ic-reactor/core/dist/types").Identity | null;
10
- login: (options?: import("@dfinity/auth-client").AuthClientLoginOptions | undefined) => Promise<void>;
11
- logout: (options?: {
12
- returnTo?: string | undefined;
13
- } | undefined) => Promise<void>;
14
- authenticate: () => Promise<import("@ic-reactor/core/dist/types").Identity>;
15
- loginLoading: boolean;
16
- loginError: Error | null;
17
- };
18
- initialize: () => Promise<void>;
19
- useActorState: () => import("./types").UseActorStoreReturn<A>;
20
- useQueryCall: import("./types").ActorQueryCall<A>;
21
- useUpdateCall: import("./types").ActorUpdateCall<A>;
22
- useMethodCall: <M extends import("@ic-reactor/core/dist/types").FunctionName<A>>(args: import("./types").ActorUseMethodCallArg<A, M>) => import("./types").ActorUseMethodCallReturn<A, M, true>;
23
- useVisitMethod: <M_1 extends import("@ic-reactor/core/dist/types").FunctionName<A>>(functionName: M_1) => import("@ic-reactor/core/dist/types").VisitService<A>[M_1];
24
- getAgent: () => import("@ic-reactor/core/dist/types").HttpAgent;
25
- getVisitFunction: () => import("@ic-reactor/core/dist/types").VisitService<A>;
26
- };
27
- export * as agent from "./context/agent";
28
- export * as actor from "./context/actor";
29
- export * as hooks from "./hooks";
1
+ export * from "./main";
2
+ export * from "./hooks";
3
+ export * from "./provider";
4
+ export * as helpers from "./helpers";
30
5
  export * as types from "./types";
6
+ export * as core from "./core";
package/dist/index.js CHANGED
@@ -15,6 +15,9 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
+ };
18
21
  var __importStar = (this && this.__importStar) || function (mod) {
19
22
  if (mod && mod.__esModule) return mod;
20
23
  var result = {};
@@ -22,49 +25,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
25
  __setModuleDefault(result, mod);
23
26
  return result;
24
27
  };
25
- var __rest = (this && this.__rest) || function (s, e) {
26
- var t = {};
27
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
28
- t[p] = s[p];
29
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
30
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
31
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
32
- t[p[i]] = s[p[i]];
33
- }
34
- return t;
35
- };
36
28
  Object.defineProperty(exports, "__esModule", { value: true });
37
- exports.types = exports.hooks = exports.actor = exports.agent = exports.createReActor = void 0;
38
- const core_1 = require("@ic-reactor/core");
39
- const actor_1 = require("./hooks/actor");
40
- const auth_1 = require("./hooks/auth");
41
- const createReActor = (_a) => {
42
- var { isLocalEnv, withVisitor, withProcessEnv } = _a, options = __rest(_a, ["isLocalEnv", "withVisitor", "withProcessEnv"]);
43
- isLocalEnv =
44
- isLocalEnv ||
45
- (withProcessEnv
46
- ? typeof process !== "undefined" &&
47
- (process.env.DFX_NETWORK === "local" ||
48
- process.env.NODE_ENV === "development")
49
- : false);
50
- const actorManager = (0, core_1.createReActorStore)(Object.assign({ isLocalEnv,
51
- withVisitor }, options));
52
- const getVisitFunction = () => {
53
- if (!withVisitor) {
54
- throw new Error("Service fields not initialized. Pass `withVisitor` to initialize service fields.");
55
- }
56
- return actorManager.visitFunction;
57
- };
58
- const getAgent = () => {
59
- return actorManager.agentManager.getAgent();
60
- };
61
- const actorHooks = (0, actor_1.getActorHooks)(actorManager);
62
- const authHooks = (0, auth_1.getAuthHooks)(actorManager.agentManager);
63
- return Object.assign(Object.assign({ getAgent,
64
- getVisitFunction }, actorHooks), authHooks);
65
- };
66
- exports.createReActor = createReActor;
67
- exports.agent = __importStar(require("./context/agent"));
68
- exports.actor = __importStar(require("./context/actor"));
69
- exports.hooks = __importStar(require("./hooks"));
29
+ exports.core = exports.types = exports.helpers = void 0;
30
+ __exportStar(require("./main"), exports);
31
+ __exportStar(require("./hooks"), exports);
32
+ __exportStar(require("./provider"), exports);
33
+ exports.helpers = __importStar(require("./helpers"));
70
34
  exports.types = __importStar(require("./types"));
35
+ exports.core = __importStar(require("./core"));
package/dist/main.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { BaseActor, CreateReactorReturn, CreateReactorOptions } from "./types";
2
+ export declare const createReactor: <A = BaseActor>(options: CreateReactorOptions) => CreateReactorReturn<A>;
package/dist/main.js ADDED
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.createReactor = void 0;
15
+ const core_1 = require("@ic-reactor/core");
16
+ const actor_1 = require("./helpers/actor");
17
+ const auth_1 = require("./helpers/auth");
18
+ const tools_1 = require("@ic-reactor/core/dist/tools");
19
+ const helpers_1 = require("./helpers");
20
+ const createReactor = (options) => {
21
+ const { isLocalEnv, withVisitor, withProcessEnv } = options, args = __rest(options, ["isLocalEnv", "withVisitor", "withProcessEnv"]);
22
+ const actorManager = (0, core_1.createReactorStore)(Object.assign({ isLocalEnv: isLocalEnv || (withProcessEnv ? (0, tools_1.isInLocalOrDevelopment)() : false), withVisitor }, args));
23
+ const getVisitFunction = () => {
24
+ return actorManager.visitFunction;
25
+ };
26
+ const getAgent = () => {
27
+ return actorManager.agentManager.getAgent();
28
+ };
29
+ const actorHooks = (0, actor_1.getActorHooks)(actorManager);
30
+ const authHooks = (0, auth_1.getAuthHooks)(actorManager.agentManager);
31
+ const agentHooks = (0, helpers_1.getAgentHooks)(actorManager.agentManager);
32
+ return Object.assign(Object.assign(Object.assign({ getAgent,
33
+ getVisitFunction }, agentHooks), actorHooks), authHooks);
34
+ };
35
+ exports.createReactor = createReactor;
@@ -0,0 +1,15 @@
1
+ import React from "react";
2
+ import { BaseActor, FunctionName, UseMethodCallArg, UseQueryCallArgs, UseUpdateCallArgs } from "../../types";
3
+ import { CreateActorOptions, ActorContextType, ActorProviderProps } from "./types";
4
+ export declare const ActorContext: React.Context<ActorContextType<BaseActor> | null>, ActorProvider: React.FC<ActorProviderProps>, useActorContext: () => ActorContextType<BaseActor>, useActorState: () => import("../../types").UseActorState, useQueryCall: <M extends string>(args: UseQueryCallArgs<BaseActor, M>) => import("../../types").ReactorCallReturn<BaseActor, M>, useUpdateCall: <M extends string>(args: UseUpdateCallArgs<BaseActor, M>) => import("../../types").ReactorCallReturn<BaseActor, M>, useMethodCall: <M extends string>(args: UseMethodCallArg<BaseActor, M>) => import("../../types").UseMethodCallReturn<BaseActor, M>, useVisitMethod: (functionName: string) => <V extends import("@dfinity/candid/lib/cjs/idl").Visitor<unknown, unknown>>(extractorClass: V, data?: import("@ic-reactor/core/dist/actor/types").VisitorType<V>["data"] | undefined) => ReturnType<V["visitFunc"]>;
5
+ export declare function createReactorContext<Actor = BaseActor>({ canisterId: defaultCanisterId, ...defaultConfig }?: Partial<CreateActorOptions>): {
6
+ ActorContext: React.Context<ActorContextType<BaseActor> | null>;
7
+ ActorProvider: React.FC<ActorProviderProps>;
8
+ useActorContext: () => ActorContextType<Actor>;
9
+ useActorState: () => import("../../types").UseActorState;
10
+ useQueryCall: <M extends FunctionName<Actor>>(args: UseQueryCallArgs<Actor, M>) => import("../../types").ReactorCallReturn<Actor, M>;
11
+ useUpdateCall: <M_1 extends FunctionName<Actor>>(args: UseUpdateCallArgs<Actor, M_1>) => import("../../types").ReactorCallReturn<Actor, M_1>;
12
+ useMethodCall: <M_2 extends FunctionName<Actor>>(args: UseMethodCallArg<Actor, M_2>) => import("../../types").UseMethodCallReturn<Actor, M_2>;
13
+ useVisitMethod: (functionName: FunctionName<Actor>) => import("@ic-reactor/core/dist/actor/types").VisitService<Actor>[FunctionName<Actor>];
14
+ initialize: () => Promise<void>;
15
+ };
@@ -35,13 +35,12 @@ var __rest = (this && this.__rest) || function (s, e) {
35
35
  };
36
36
  var _a;
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.createReActorContext = exports.useVisitMethod = exports.useMethodCall = exports.useUpdateCall = exports.useQueryCall = exports.useActorState = exports.useActorContext = exports.ActorProvider = exports.ActorContext = void 0;
39
- /* eslint-disable @typescript-eslint/no-explicit-any */
38
+ exports.createReactorContext = exports.useVisitMethod = exports.useMethodCall = exports.useUpdateCall = exports.useQueryCall = exports.useActorState = exports.useActorContext = exports.ActorProvider = exports.ActorContext = void 0;
40
39
  const react_1 = __importStar(require("react"));
41
- const actor_1 = require("../../hooks/actor");
42
- const useActor_1 = require("../../hooks/useActor");
43
- _a = createReActorContext(), exports.ActorContext = _a.ActorContext, exports.ActorProvider = _a.ActorProvider, exports.useActorContext = _a.useActorContext, exports.useActorState = _a.useActorState, exports.useQueryCall = _a.useQueryCall, exports.useUpdateCall = _a.useUpdateCall, exports.useMethodCall = _a.useMethodCall, exports.useVisitMethod = _a.useVisitMethod;
44
- function createReActorContext(_a = {}) {
40
+ const actor_1 = require("../../helpers/actor");
41
+ const useReactor_1 = require("../../hooks/useReactor");
42
+ _a = createReactorContext(), exports.ActorContext = _a.ActorContext, exports.ActorProvider = _a.ActorProvider, exports.useActorContext = _a.useActorContext, exports.useActorState = _a.useActorState, exports.useQueryCall = _a.useQueryCall, exports.useUpdateCall = _a.useUpdateCall, exports.useMethodCall = _a.useMethodCall, exports.useVisitMethod = _a.useVisitMethod;
43
+ function createReactorContext(_a = {}) {
45
44
  var { canisterId: defaultCanisterId } = _a, defaultConfig = __rest(_a, ["canisterId"]);
46
45
  const ActorContext = (0, react_1.createContext)(null);
47
46
  const ActorProvider = (_a) => {
@@ -50,7 +49,7 @@ function createReActorContext(_a = {}) {
50
49
  throw new Error("canisterId is required");
51
50
  }
52
51
  const config = (0, react_1.useMemo)(() => (Object.assign(Object.assign({}, defaultConfig), restConfig)), [defaultConfig, restConfig]);
53
- const { actorManager, fetchError, fetching } = (0, useActor_1.useActor)(Object.assign({ canisterId }, config));
52
+ const { actorManager, fetchError, fetching } = (0, useReactor_1.useReactor)(Object.assign({ canisterId }, config));
54
53
  const hooks = (0, react_1.useMemo)(() => {
55
54
  if (actorManager) {
56
55
  return (0, actor_1.getActorHooks)(actorManager);
@@ -85,4 +84,4 @@ function createReActorContext(_a = {}) {
85
84
  initialize,
86
85
  };
87
86
  }
88
- exports.createReActorContext = createReActorContext;
87
+ exports.createReactorContext = createReactorContext;
@@ -2,19 +2,12 @@
2
2
  import { IDL } from "@dfinity/candid";
3
3
  import { ActorManagerOptions, BaseActor } from "@ic-reactor/core/dist/types";
4
4
  import { ActorHooks, AgentContextType } from "../../types";
5
- export type ActorContextType<Actor = BaseActor, F extends boolean = true> = ActorHooks<Actor, F> & {
6
- ActorContext: React.Context<ActorContextType<Actor, F> | null>;
5
+ export interface ActorContextType<Actor = BaseActor> extends ActorHooks<Actor> {
6
+ ActorContext: React.Context<ActorContextType<Actor> | null>;
7
7
  useActorContext: <A = Actor>() => ActorContextType<A>;
8
8
  ActorProvider: React.FC<ActorProviderProps>;
9
- };
10
- export type CreateReActorContext = {
11
- <A = BaseActor>(options?: Partial<CreateActorOptions> & {
12
- withVisitor: true;
13
- }): ActorContextType<A, true>;
14
- <A = BaseActor>(options?: Partial<CreateActorOptions> & {
15
- withVisitor?: false | undefined;
16
- }): ActorContextType<A, false>;
17
- };
9
+ }
10
+ export type CreateReactorContext = <A = BaseActor>(options?: Partial<CreateActorOptions>) => ActorContextType<A>;
18
11
  export interface CreateActorOptions extends Omit<ActorManagerOptions, "idlFactory" | "agentManager" | "canisterId"> {
19
12
  didjsId?: string;
20
13
  canisterId?: string;
@@ -1,5 +1,7 @@
1
1
  import React from "react";
2
+ import type { AgentManagerOptions } from "@ic-reactor/core/dist/types";
2
3
  import type { AgentContextValue, AgentProviderProps } from "./types";
3
4
  export declare const AgentContext: React.Context<AgentContextValue | null>;
4
5
  declare const AgentProvider: React.FC<AgentProviderProps>;
5
6
  export { AgentProvider };
7
+ export declare const createAgentContext: (config: AgentManagerOptions) => AgentContextValue;
@@ -34,15 +34,15 @@ var __rest = (this && this.__rest) || function (s, e) {
34
34
  return t;
35
35
  };
36
36
  Object.defineProperty(exports, "__esModule", { value: true });
37
- exports.AgentProvider = exports.AgentContext = void 0;
37
+ exports.createAgentContext = exports.AgentProvider = exports.AgentContext = void 0;
38
38
  const react_1 = __importStar(require("react"));
39
39
  const core_1 = require("@ic-reactor/core");
40
- const agent_1 = require("../../hooks/agent");
41
- const auth_1 = require("../../hooks/auth");
40
+ const agent_1 = require("../../helpers/agent");
41
+ const auth_1 = require("../../helpers/auth");
42
42
  exports.AgentContext = (0, react_1.createContext)(null);
43
43
  const AgentProvider = (_a) => {
44
44
  var { children } = _a, config = __rest(_a, ["children"]);
45
- const value = (0, react_1.useMemo)(() => createAgentContext(config), [config]);
45
+ const value = (0, react_1.useMemo)(() => (0, exports.createAgentContext)(config), [config]);
46
46
  return react_1.default.createElement(exports.AgentContext.Provider, { value: value }, children);
47
47
  };
48
48
  exports.AgentProvider = AgentProvider;
@@ -53,3 +53,4 @@ const createAgentContext = (config) => {
53
53
  const authHooks = (0, auth_1.getAuthHooks)(agentManager);
54
54
  return Object.assign(Object.assign(Object.assign({}, agenthooks), authHooks), { agentManager });
55
55
  };
56
+ exports.createAgentContext = createAgentContext;
@@ -0,0 +1,12 @@
1
+ import { AgentManager } from "@ic-reactor/core/dist/agent";
2
+ import type { AgentContextType } from "./types";
3
+ import { UseAuthClientArgs } from "../../types";
4
+ export declare const useAgentContext: (agentContext?: AgentContextType) => import("./types").AgentContextValue;
5
+ export declare const useAgentManager: (agentContext?: AgentContextType) => AgentManager;
6
+ export declare const useAgent: (agentContext?: AgentContextType) => import("@dfinity/agent/lib/cjs/agent/http").HttpAgent | undefined;
7
+ export declare const useAuthState: (agentContext?: AgentContextType) => import("@ic-reactor/core/dist/agent/types").AuthState;
8
+ export declare const useAgentState: (agentContext?: AgentContextType) => import("@ic-reactor/core/dist/agent/types").AgentState;
9
+ export declare const useAuthClient: ({ agentContext, ...args }: UseAuthClientArgs & {
10
+ agentContext?: AgentContextType | undefined;
11
+ }) => import("../../types").UseAuthClientReturn;
12
+ export declare const useUserPrincipal: (agentContext?: AgentContextType) => import("@dfinity/principal").Principal | undefined;
@@ -11,33 +11,33 @@ var __rest = (this && this.__rest) || function (s, e) {
11
11
  return t;
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.useUserPrincipal = exports.useAuthClient = exports.useAgentState = exports.useAuthStore = exports.useAgent = exports.useAgentManager = exports.useAgentManagerContext = void 0;
14
+ exports.useUserPrincipal = exports.useAuthClient = exports.useAgentState = exports.useAuthState = exports.useAgent = exports.useAgentManager = exports.useAgentContext = void 0;
15
15
  const react_1 = require("react");
16
16
  const context_1 = require("./context");
17
- const useAgentManagerContext = (agentContext) => {
17
+ const useAgentContext = (agentContext) => {
18
18
  const context = (0, react_1.useContext)(agentContext || context_1.AgentContext);
19
19
  if (!context) {
20
20
  throw new Error("Agent context must be used within a AgentProvider");
21
21
  }
22
22
  return context;
23
23
  };
24
- exports.useAgentManagerContext = useAgentManagerContext;
24
+ exports.useAgentContext = useAgentContext;
25
25
  const useAgentManager = (agentContext) => {
26
- const context = (0, exports.useAgentManagerContext)(agentContext);
26
+ const context = (0, exports.useAgentContext)(agentContext);
27
27
  return context.agentManager;
28
28
  };
29
29
  exports.useAgentManager = useAgentManager;
30
- const useAgent = (agentContext) => (0, exports.useAgentManagerContext)(agentContext).useAgent();
30
+ const useAgent = (agentContext) => (0, exports.useAgentContext)(agentContext).useAgent();
31
31
  exports.useAgent = useAgent;
32
- const useAuthStore = (agentContext) => (0, exports.useAgentManagerContext)(agentContext).useAuthStore();
33
- exports.useAuthStore = useAuthStore;
34
- const useAgentState = (agentContext) => (0, exports.useAgentManagerContext)(agentContext).useAgentState();
32
+ const useAuthState = (agentContext) => (0, exports.useAgentContext)(agentContext).useAuthState();
33
+ exports.useAuthState = useAuthState;
34
+ const useAgentState = (agentContext) => (0, exports.useAgentContext)(agentContext).useAgentState();
35
35
  exports.useAgentState = useAgentState;
36
36
  const useAuthClient = (_a) => {
37
37
  var { agentContext } = _a, args = __rest(_a, ["agentContext"]);
38
- const context = (0, exports.useAgentManagerContext)(agentContext);
38
+ const context = (0, exports.useAgentContext)(agentContext);
39
39
  return context.useAuthClient(args);
40
40
  };
41
41
  exports.useAuthClient = useAuthClient;
42
- const useUserPrincipal = (agentContext) => (0, exports.useAgentManagerContext)(agentContext).useUserPrincipal();
42
+ const useUserPrincipal = (agentContext) => (0, exports.useAgentContext)(agentContext).useUserPrincipal();
43
43
  exports.useUserPrincipal = useUserPrincipal;
@@ -1,8 +1,8 @@
1
1
  import React, { PropsWithChildren } from "react";
2
2
  import type { AgentManagerOptions } from "@ic-reactor/core/dist/types";
3
3
  import type { AgentManager } from "@ic-reactor/core/dist/agent";
4
- import type { getAuthHooks } from "../../hooks/auth";
5
- import type { getAgentHooks } from "../../hooks/agent";
4
+ import type { getAuthHooks } from "../../helpers/auth";
5
+ import type { getAgentHooks } from "../../helpers/agent";
6
6
  export type AgentContextValue = ReturnType<typeof getAuthHooks> & ReturnType<typeof getAgentHooks> & {
7
7
  agentManager: AgentManager;
8
8
  };
package/dist/types.d.ts CHANGED
@@ -1,76 +1,10 @@
1
- /// <reference types="react" />
2
- import type { ActorState, CanisterId, CreateReActorOptions, ActorMethodArgs, ActorMethodReturnType, HttpAgent, Identity, Principal, FunctionName, VisitService } from "@ic-reactor/core/dist/types";
3
- import type { AgentHooks, AuthHooks } from "./hooks/types";
1
+ import type { HttpAgent, VisitService } from "@ic-reactor/core/dist/types";
2
+ import type { ActorHooks, AgentHooks, AuthHooks } from "./helpers/types";
4
3
  export * from "@ic-reactor/core/dist/types";
5
- export * from "./context/agent/types";
6
- export * from "./context/actor/types";
7
- export * from "./hooks/types";
8
- export type AuthArgs = {
9
- onAuthentication?: (promise: () => Promise<Identity>) => void;
10
- onAuthenticationSuccess?: (identity: Identity) => void;
11
- onAuthenticationFailure?: (error: Error) => void;
12
- onLoginSuccess?: (principal: Principal) => void;
13
- onLoginError?: (error: Error) => void;
14
- onLogin?: (promise: () => Promise<Principal>) => void;
15
- onLoggedOut?: () => void;
16
- };
17
- export type ActorCallArgs<A, M extends FunctionName<A>> = {
18
- functionName: M;
19
- args?: ActorMethodArgs<A[M]>;
20
- onLoading?: (loading: boolean) => void;
21
- onError?: (error: Error | undefined) => void;
22
- onSuccess?: (data: ActorMethodReturnType<A[M]> | undefined) => void;
23
- throwOnError?: boolean;
24
- };
25
- export type ActorHookState<A, M extends FunctionName<A>> = {
26
- data: ActorMethodReturnType<A[M]> | undefined;
27
- error: Error | undefined;
28
- loading: boolean;
29
- };
30
- export interface ActorUseQueryArgs<A, M extends FunctionName<A>> extends ActorCallArgs<A, M> {
31
- refetchOnMount?: boolean;
32
- refetchInterval?: number | false;
33
- }
34
- export interface ActorUseUpdateArgs<A, M extends FunctionName<A>> extends ActorCallArgs<A, M> {
35
- }
36
- export type ActorCallReturn<A, M extends FunctionName<A>> = ActorHookState<A, M> & {
37
- reset: () => void;
38
- call: (eventOrReplaceArgs?: React.MouseEvent | ActorMethodArgs<A[M]>) => Promise<ActorMethodReturnType<A[M]> | undefined>;
39
- };
40
- export type ActorCall<A> = <M extends FunctionName<A>>(args: ActorCallArgs<A, M>) => ActorCallReturn<A, M>;
41
- export type ActorQueryCall<A> = <M extends FunctionName<A>>(args: ActorUseQueryArgs<A, M>) => ActorCallReturn<A, M>;
42
- export type ActorUpdateCall<A> = <M extends FunctionName<A>>(args: ActorUseUpdateArgs<A, M>) => ActorCallReturn<A, M>;
43
- export type ActorMethodCall<A, M extends FunctionName<A>> = (args: ActorUseMethodCallArg<A, M>) => ActorUseMethodCallReturn<A, M>;
44
- export interface ActorUseMethodCallReturn<A, M extends FunctionName<A>, F extends boolean = false> extends ActorCallReturn<A, M> {
45
- visit: F extends true ? VisitService<A>[M] : undefined;
46
- reset: () => void;
47
- error: Error | undefined;
48
- loading: boolean;
49
- }
50
- export type ActorUseMethodCallArg<A, M extends FunctionName<A>> = ActorCallArgs<A, M>;
51
- export type ActorHooks<A, F extends boolean = false> = ActorDefaultHooks<A, F> & (F extends true ? ActorFieldHooks<A> : object);
52
- export interface ActorFieldHooks<A> {
53
- useVisitMethod: <M extends FunctionName<A>>(functionName: M) => VisitService<A>[M];
54
- }
55
- export type UseActorStoreReturn<A> = ActorState<A> & {
56
- canisterId: CanisterId;
57
- };
58
- export interface ActorDefaultHooks<A, F extends boolean> {
59
- initialize: () => Promise<void>;
60
- useActorState: () => UseActorStoreReturn<A>;
61
- useQueryCall: ActorQueryCall<A>;
62
- useUpdateCall: ActorUpdateCall<A>;
63
- useMethodCall: <M extends FunctionName<A>>(args: ActorUseMethodCallArg<A, M>) => ActorUseMethodCallReturn<A, M, F>;
64
- }
65
- export type GetFunctions<A> = {
4
+ export * from "./provider/agent/types";
5
+ export * from "./provider/actor/types";
6
+ export * from "./helpers/types";
7
+ export interface CreateReactorReturn<A> extends ActorHooks<A>, AuthHooks, AgentHooks {
66
8
  getAgent: () => HttpAgent;
67
9
  getVisitFunction: () => VisitService<A>;
68
- };
69
- export type CreateReActor = {
70
- <A>(options: CreateReActorOptions & {
71
- withVisitor: true;
72
- }): GetFunctions<A> & ActorHooks<A, true> & AuthHooks & AgentHooks;
73
- <A>(options: CreateReActorOptions & {
74
- withVisitor?: false | undefined;
75
- }): GetFunctions<A> & ActorHooks<A, false> & AuthHooks & AgentHooks;
76
- };
10
+ }
package/dist/types.js CHANGED
@@ -15,6 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("@ic-reactor/core/dist/types"), exports);
18
- __exportStar(require("./context/agent/types"), exports);
19
- __exportStar(require("./context/actor/types"), exports);
20
- __exportStar(require("./hooks/types"), exports);
18
+ __exportStar(require("./provider/agent/types"), exports);
19
+ __exportStar(require("./provider/actor/types"), exports);
20
+ __exportStar(require("./helpers/types"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/react",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
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",
@@ -35,7 +35,7 @@
35
35
  "node": ">=10"
36
36
  },
37
37
  "dependencies": {
38
- "@ic-reactor/core": "^1.0.1",
38
+ "@ic-reactor/core": "^1.0.3",
39
39
  "zustand-utils": "^1.3"
40
40
  },
41
41
  "peerDependencies": {
@@ -48,5 +48,5 @@
48
48
  "react": ">=16.8",
49
49
  "zustand": "4.5"
50
50
  },
51
- "gitHead": "54c9942a98bcad64e63c8d52788103f8b168720e"
51
+ "gitHead": "acf8b5da61713482264c9eb5c227dc94a7e43cbc"
52
52
  }
@@ -1,16 +0,0 @@
1
- import React from "react";
2
- import { ActorUseMethodCallArg, ActorUseQueryArgs, ActorUseUpdateArgs } from "../../types";
3
- import { CreateActorOptions, ActorContextType, ActorProviderProps } from "./types";
4
- import type { ActorSubclass } from "@dfinity/agent";
5
- export declare const ActorContext: React.Context<ActorContextType | null>, ActorProvider: React.FC<ActorProviderProps>, useActorContext: <A extends unknown = unknown>() => ActorContextType<A>, useActorState: () => import("../../types").UseActorStoreReturn<unknown>, useQueryCall: <M extends never>(args: ActorUseQueryArgs<unknown, M>) => import("../../types").ActorCallReturn<unknown, M>, useUpdateCall: <M extends never>(args: ActorUseUpdateArgs<unknown, M>) => import("../../types").ActorCallReturn<unknown, M>, useMethodCall: <M extends never>(args: ActorUseMethodCallArg<unknown, M>) => import("../../types").ActorUseMethodCallReturn<unknown, M, true>, useVisitMethod: (functionName: never) => never;
6
- export declare function createReActorContext<Actor extends ActorSubclass<any>>({ canisterId: defaultCanisterId, ...defaultConfig }?: Partial<CreateActorOptions>): {
7
- ActorContext: React.Context<ActorContextType | null>;
8
- ActorProvider: React.FC<ActorProviderProps>;
9
- useActorContext: <A extends unknown = Actor>() => ActorContextType<A>;
10
- useActorState: () => import("../../types").UseActorStoreReturn<Actor>;
11
- useQueryCall: <M extends keyof Actor & string>(args: ActorUseQueryArgs<Actor, M>) => import("../../types").ActorCallReturn<Actor, M>;
12
- useUpdateCall: <M_1 extends keyof Actor & string>(args: ActorUseUpdateArgs<Actor, M_1>) => import("../../types").ActorCallReturn<Actor, M_1>;
13
- useMethodCall: <M_2 extends keyof Actor & string>(args: ActorUseMethodCallArg<Actor, M_2>) => import("../../types").ActorUseMethodCallReturn<Actor, M_2, true>;
14
- useVisitMethod: (functionName: keyof Actor & string) => import("@ic-reactor/core/dist/actor/types").VisitService<Actor>[keyof Actor & string];
15
- initialize: () => Promise<void>;
16
- };
@@ -1,24 +0,0 @@
1
- import { AgentManager } from "@ic-reactor/core/dist/agent";
2
- import type { AgentContextType } from "./types";
3
- import { AuthArgs } from "../../types";
4
- export declare const useAgentManagerContext: (agentContext?: AgentContextType) => import("./types").AgentContextValue;
5
- export declare const useAgentManager: (agentContext?: AgentContextType) => AgentManager;
6
- export declare const useAgent: (agentContext?: AgentContextType) => import("@dfinity/agent/lib/cjs/agent/http").HttpAgent | undefined;
7
- export declare const useAuthStore: (agentContext?: AgentContextType) => import("@ic-reactor/core/dist/agent/types").AuthState;
8
- export declare const useAgentState: (agentContext?: AgentContextType) => import("@ic-reactor/core/dist/agent/types").AgentState;
9
- export declare const useAuthClient: ({ agentContext, ...args }: AuthArgs & {
10
- agentContext?: AgentContextType | undefined;
11
- }) => {
12
- authClient: import("@dfinity/auth-client").AuthClient | null;
13
- authenticated: boolean;
14
- authenticating: boolean;
15
- identity: import("@dfinity/agent/lib/cjs/auth").Identity | null;
16
- login: (options?: import("@dfinity/auth-client").AuthClientLoginOptions | undefined) => Promise<void>;
17
- logout: (options?: {
18
- returnTo?: string | undefined;
19
- } | undefined) => Promise<void>;
20
- authenticate: () => Promise<import("@dfinity/agent/lib/cjs/auth").Identity>;
21
- loginLoading: boolean;
22
- loginError: Error | null;
23
- };
24
- export declare const useUserPrincipal: (agentContext?: AgentContextType) => import("@dfinity/principal").Principal | undefined;
@@ -1,3 +0,0 @@
1
- import { ActorHooks } from "../types";
2
- import type { ActorManager } from "@ic-reactor/core/dist/actor";
3
- export declare const getActorHooks: <A>({ initialize, canisterId, actorStore, callMethod, visitFunction, }: ActorManager<A>) => ActorHooks<A, true>;
@@ -1,21 +0,0 @@
1
- import type { AuthClientLoginOptions } from "@dfinity/auth-client";
2
- import type { Identity, Principal } from "@ic-reactor/core/dist/types";
3
- import type { AgentManager } from "@ic-reactor/core/dist/agent";
4
- import { AuthArgs } from "../types";
5
- export declare const getAuthHooks: (agentManager: AgentManager) => {
6
- useUserPrincipal: () => Principal | undefined;
7
- useAuthStore: () => import("@ic-reactor/core/dist/types").AuthState;
8
- useAuthClient: ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, }?: AuthArgs) => {
9
- authClient: import("@dfinity/auth-client").AuthClient | null;
10
- authenticated: boolean;
11
- authenticating: boolean;
12
- identity: Identity | null;
13
- login: (options?: AuthClientLoginOptions) => Promise<void>;
14
- logout: (options?: {
15
- returnTo?: string;
16
- }) => Promise<void>;
17
- authenticate: () => Promise<Identity>;
18
- loginLoading: boolean;
19
- loginError: Error | null;
20
- };
21
- };
@@ -1,131 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.getAuthHooks = void 0;
13
- const react_1 = require("react");
14
- const zustand_1 = require("zustand");
15
- const getAuthHooks = (agentManager) => {
16
- const { authenticate: authenticator, authStore, isLocalEnv } = agentManager;
17
- const useAuthStore = () => {
18
- const authState = (0, zustand_1.useStore)(authStore, (state) => state);
19
- return authState;
20
- };
21
- const useUserPrincipal = () => {
22
- const { identity } = useAuthStore();
23
- return identity === null || identity === void 0 ? void 0 : identity.getPrincipal();
24
- };
25
- const useAuthClient = ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, } = {}) => {
26
- const [loginLoading, setLoginLoading] = (0, react_1.useState)(false);
27
- const [loginError, setLoginError] = (0, react_1.useState)(null);
28
- const { authClient, authenticated, authenticating, identity } = useAuthStore();
29
- const authenticate = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
30
- const authenticatePromise = new Promise((resolve, reject) => {
31
- authenticator()
32
- .then((identity) => {
33
- onAuthenticationSuccess === null || onAuthenticationSuccess === void 0 ? void 0 : onAuthenticationSuccess(identity);
34
- resolve(identity);
35
- })
36
- .catch((e) => {
37
- onAuthenticationFailure === null || onAuthenticationFailure === void 0 ? void 0 : onAuthenticationFailure(e);
38
- reject(e);
39
- });
40
- });
41
- onAuthentication === null || onAuthentication === void 0 ? void 0 : onAuthentication(() => authenticatePromise);
42
- return authenticatePromise;
43
- }), [
44
- authenticator,
45
- onAuthentication,
46
- onAuthenticationSuccess,
47
- onAuthenticationFailure,
48
- ]);
49
- const login = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
50
- setLoginLoading(true);
51
- setLoginError(null);
52
- const loginPromise = new Promise((resolve, reject) => {
53
- try {
54
- if (!authClient) {
55
- throw new Error("Auth client not initialized");
56
- }
57
- authClient.login(Object.assign(Object.assign({ identityProvider: isLocalEnv
58
- ? "https://identity.ic0.app/#authorize"
59
- : "http://rdmx6-jaaaa-aaaaa-aaadq-cai.localhost:4943/#authorize" }, options), { onSuccess: () => {
60
- authenticate()
61
- .then((identity) => {
62
- var _a;
63
- const principal = identity.getPrincipal();
64
- (_a = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _a === void 0 ? void 0 : _a.call(options);
65
- onLoginSuccess === null || onLoginSuccess === void 0 ? void 0 : onLoginSuccess(principal);
66
- resolve(principal);
67
- })
68
- .catch((e) => {
69
- setLoginError(e);
70
- onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(e);
71
- reject(e);
72
- });
73
- }, onError: (e) => {
74
- var _a;
75
- (_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, e);
76
- const error = new Error("Login failed: " + e);
77
- setLoginError(error);
78
- onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(error);
79
- reject(error);
80
- } }));
81
- }
82
- catch (e) {
83
- setLoginError(e);
84
- onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(e);
85
- reject(e);
86
- }
87
- finally {
88
- setLoginLoading(false);
89
- }
90
- });
91
- onLogin === null || onLogin === void 0 ? void 0 : onLogin(() => loginPromise);
92
- }), [
93
- authClient,
94
- onLogin,
95
- onLoginSuccess,
96
- onLoginError,
97
- isLocalEnv,
98
- authenticate,
99
- ]);
100
- const logout = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
101
- if (!authClient) {
102
- throw new Error("Auth client not initialized");
103
- }
104
- yield authClient.logout(options);
105
- yield authenticate();
106
- onLoggedOut === null || onLoggedOut === void 0 ? void 0 : onLoggedOut();
107
- }), [authClient, onLoggedOut]);
108
- (0, react_1.useEffect)(() => {
109
- if (!authClient && !authenticating) {
110
- authenticate();
111
- }
112
- }, [authClient, authenticating]);
113
- return {
114
- authClient,
115
- authenticated,
116
- authenticating,
117
- identity,
118
- login,
119
- logout,
120
- authenticate,
121
- loginLoading,
122
- loginError,
123
- };
124
- };
125
- return {
126
- useUserPrincipal,
127
- useAuthStore,
128
- useAuthClient,
129
- };
130
- };
131
- exports.getAuthHooks = getAuthHooks;
@@ -1,4 +0,0 @@
1
- import { getAgentHooks } from "./agent";
2
- import { getAuthHooks } from "./auth";
3
- export type AgentHooks = ReturnType<typeof getAgentHooks>;
4
- export type AuthHooks = ReturnType<typeof getAuthHooks>;
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes