@ic-reactor/react 1.0.2 → 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 (33) hide show
  1. package/dist/helpers/actor.d.ts +4 -3
  2. package/dist/helpers/actor.js +28 -48
  3. package/dist/helpers/agent.js +4 -6
  4. package/dist/helpers/auth.d.ts +4 -18
  5. package/dist/helpers/auth.js +65 -87
  6. package/dist/helpers/types.d.ts +79 -2
  7. package/dist/hooks/index.d.ts +1 -1
  8. package/dist/hooks/index.js +1 -1
  9. package/dist/hooks/useCandid.js +1 -1
  10. package/dist/hooks/{useActor.d.ts → useReactor.d.ts} +1 -3
  11. package/dist/hooks/{useActor.js → useReactor.js} +4 -6
  12. package/dist/index.d.ts +3 -29
  13. package/dist/index.js +4 -39
  14. package/dist/main.d.ts +2 -0
  15. package/dist/main.js +35 -0
  16. package/dist/provider/actor/index.d.ts +15 -0
  17. package/dist/{context → provider}/actor/index.js +2 -3
  18. package/dist/{context → provider}/actor/types.d.ts +4 -11
  19. package/dist/{context → provider}/agent/hooks.d.ts +4 -16
  20. package/dist/{context → provider}/agent/hooks.js +9 -9
  21. package/dist/types.d.ts +6 -72
  22. package/dist/types.js +2 -2
  23. package/package.json +3 -3
  24. package/dist/context/actor/index.d.ts +0 -16
  25. /package/dist/{context → provider}/actor/types.js +0 -0
  26. /package/dist/{context → provider}/agent/context.d.ts +0 -0
  27. /package/dist/{context → provider}/agent/context.js +0 -0
  28. /package/dist/{context → provider}/agent/index.d.ts +0 -0
  29. /package/dist/{context → provider}/agent/index.js +0 -0
  30. /package/dist/{context → provider}/agent/types.d.ts +0 -0
  31. /package/dist/{context → provider}/agent/types.js +0 -0
  32. /package/dist/{context → provider}/index.d.ts +0 -0
  33. /package/dist/{context → provider}/index.js +0 -0
@@ -1,3 +1,4 @@
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
+ 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
67
  const _b = useReactorCall(rest), { call } = _b, state = __rest(_b, ["call"]);
79
- const intervalId = (0, react_1.useRef)(undefined);
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];
82
+ const visit = useVisitMethod(args.functionName);
103
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 {
@@ -1,21 +1,7 @@
1
- import type { AuthClientLoginOptions } from "@dfinity/auth-client";
2
- import type { Identity, Principal } from "@ic-reactor/core/dist/types";
3
1
  import type { AgentManager } from "@ic-reactor/core/dist/agent";
4
- import { AuthArgs } from "../types";
2
+ import type { UseAuthClientArgs, UseAuthClientReturn } from "../types";
5
3
  export declare const getAuthHooks: (agentManager: AgentManager) => {
6
- useUserPrincipal: () => Principal | undefined;
7
- useAuthState: () => 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
- };
4
+ useUserPrincipal: () => import("@dfinity/principal").Principal | undefined;
5
+ useAuthState: () => import("@ic-reactor/core/dist/agent/types").AuthState;
6
+ useAuthClient: (events?: UseAuthClientArgs) => UseAuthClientReturn;
21
7
  };
@@ -10,106 +10,84 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.getAuthHooks = void 0;
13
- const react_1 = require("react");
14
13
  const zustand_1 = require("zustand");
14
+ const react_1 = require("react");
15
+ const tools_1 = require("@ic-reactor/core/dist/tools");
15
16
  const getAuthHooks = (agentManager) => {
16
17
  const { authenticate: authenticator, authStore, isLocalEnv } = agentManager;
17
- const useAuthState = () => {
18
- return (0, zustand_1.useStore)(authStore, (state) => state);
19
- };
20
- const useUserPrincipal = () => {
21
- const { identity } = useAuthState();
22
- return identity === null || identity === void 0 ? void 0 : identity.getPrincipal();
23
- };
24
- const useAuthClient = ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, } = {}) => {
25
- const [loginLoading, setLoginLoading] = (0, react_1.useState)(false);
26
- const [loginError, setLoginError] = (0, react_1.useState)(null);
27
- const { authClient, authenticated, authenticating, identity } = useAuthState();
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();
28
26
  const authenticate = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
29
- const authenticatePromise = new Promise((resolve, reject) => {
30
- authenticator()
31
- .then((identity) => {
32
- onAuthenticationSuccess === null || onAuthenticationSuccess === void 0 ? void 0 : onAuthenticationSuccess(identity);
33
- resolve(identity);
34
- })
35
- .catch((e) => {
36
- onAuthenticationFailure === null || onAuthenticationFailure === void 0 ? void 0 : onAuthenticationFailure(e);
37
- reject(e);
38
- });
39
- });
40
- onAuthentication === null || onAuthentication === void 0 ? void 0 : onAuthentication(() => authenticatePromise);
41
- return authenticatePromise;
42
- }), [
43
- authenticator,
44
- onAuthentication,
45
- onAuthenticationSuccess,
46
- onAuthenticationFailure,
47
- ]);
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]);
48
38
  const login = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
49
- setLoginLoading(true);
50
- setLoginError(null);
51
- const loginPromise = new Promise((resolve, reject) => {
52
- try {
53
- if (!authClient) {
54
- throw new Error("Auth client not initialized");
55
- }
56
- authClient.login(Object.assign(Object.assign({ identityProvider: isLocalEnv
57
- ? "https://identity.ic0.app/#authorize"
58
- : "http://rdmx6-jaaaa-aaaaa-aaadq-cai.localhost:4943/#authorize" }, options), { onSuccess: () => {
59
- authenticate()
60
- .then((identity) => {
61
- var _a;
62
- const principal = identity.getPrincipal();
63
- (_a = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _a === void 0 ? void 0 : _a.call(options);
64
- onLoginSuccess === null || onLoginSuccess === void 0 ? void 0 : onLoginSuccess(principal);
65
- resolve(principal);
66
- })
67
- .catch((e) => {
68
- setLoginError(e);
69
- onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(e);
70
- reject(e);
71
- });
72
- }, onError: (e) => {
73
- var _a;
74
- (_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, e);
75
- const error = new Error("Login failed: " + e);
76
- setLoginError(error);
77
- onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(error);
78
- reject(error);
79
- } }));
80
- }
81
- catch (e) {
82
- setLoginError(e);
83
- onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(e);
84
- reject(e);
85
- }
86
- finally {
87
- setLoginLoading(false);
88
- }
89
- });
90
- onLogin === null || onLogin === void 0 ? void 0 : onLogin(() => loginPromise);
91
- }), [
92
- authClient,
93
- onLogin,
94
- onLoginSuccess,
95
- onLoginError,
96
- isLocalEnv,
97
- authenticate,
98
- ]);
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]);
99
74
  const logout = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
75
+ var _g;
100
76
  if (!authClient) {
101
77
  throw new Error("Auth client not initialized");
102
78
  }
103
79
  yield authClient.logout(options);
104
80
  yield authenticate();
105
- onLoggedOut === null || onLoggedOut === void 0 ? void 0 : onLoggedOut();
106
- }), [authClient, onLoggedOut]);
81
+ (_g = events === null || events === void 0 ? void 0 : events.onLoggedOut) === null || _g === void 0 ? void 0 : _g.call(events);
82
+ }), [authClient, authenticate, events]);
107
83
  (0, react_1.useEffect)(() => {
108
84
  if (!authClient && !authenticating) {
109
- authenticate();
85
+ // eslint-disable-next-line no-console
86
+ authenticate().catch(console.error);
110
87
  }
111
- }, [authClient, authenticating]);
88
+ }, [authClient, authenticating, authenticate]);
112
89
  return {
90
+ error,
113
91
  authClient,
114
92
  authenticated,
115
93
  authenticating,
@@ -117,8 +95,8 @@ const getAuthHooks = (agentManager) => {
117
95
  login,
118
96
  logout,
119
97
  authenticate,
120
- loginLoading,
121
- loginError,
98
+ loginLoading: loginState.loading,
99
+ loginError: loginState.error,
122
100
  };
123
101
  };
124
102
  return {
@@ -1,4 +1,81 @@
1
- import { getAgentHooks } from "./agent";
2
- import { getAuthHooks } from "./auth";
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";
3
6
  export type AgentHooks = ReturnType<typeof getAgentHooks>;
4
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,2 +1,2 @@
1
- export * from "./useActor";
1
+ export * from "./useReactor";
2
2
  export * from "./useCandid";
@@ -14,5 +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);
@@ -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,10 +9,8 @@ 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;
@@ -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)({
@@ -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,32 +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
- useAuthState: () => 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 * from "./context/agent";
28
- export * from "./context/actor";
1
+ export * from "./main";
2
+ export * from "./hooks";
3
+ export * from "./provider";
29
4
  export * as helpers from "./helpers";
30
- export * as hooks from "./hooks";
31
5
  export * as types from "./types";
32
6
  export * as core from "./core";
package/dist/index.js CHANGED
@@ -25,46 +25,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  __setModuleDefault(result, mod);
26
26
  return result;
27
27
  };
28
- var __rest = (this && this.__rest) || function (s, e) {
29
- var t = {};
30
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
31
- t[p] = s[p];
32
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
33
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
34
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
35
- t[p[i]] = s[p[i]];
36
- }
37
- return t;
38
- };
39
28
  Object.defineProperty(exports, "__esModule", { value: true });
40
- exports.core = exports.types = exports.hooks = exports.helpers = exports.createReactor = void 0;
41
- const core_1 = require("@ic-reactor/core");
42
- const actor_1 = require("./helpers/actor");
43
- const auth_1 = require("./helpers/auth");
44
- const tools_1 = require("@ic-reactor/core/dist/tools");
45
- const createReactor = (_a) => {
46
- var { isLocalEnv, withVisitor, withProcessEnv } = _a, options = __rest(_a, ["isLocalEnv", "withVisitor", "withProcessEnv"]);
47
- isLocalEnv = isLocalEnv || (withProcessEnv ? (0, tools_1.isInLocalOrDevelopment)() : false);
48
- const actorManager = (0, core_1.createReactorStore)(Object.assign({ isLocalEnv,
49
- withVisitor }, options));
50
- const getVisitFunction = () => {
51
- if (!withVisitor) {
52
- throw new Error("Service fields not initialized. Pass `withVisitor` to initialize service fields.");
53
- }
54
- return actorManager.visitFunction;
55
- };
56
- const getAgent = () => {
57
- return actorManager.agentManager.getAgent();
58
- };
59
- const actorHooks = (0, actor_1.getActorHooks)(actorManager);
60
- const authHooks = (0, auth_1.getAuthHooks)(actorManager.agentManager);
61
- return Object.assign(Object.assign({ getAgent,
62
- getVisitFunction }, actorHooks), authHooks);
63
- };
64
- exports.createReactor = createReactor;
65
- __exportStar(require("./context/agent"), exports);
66
- __exportStar(require("./context/actor"), exports);
29
+ exports.core = exports.types = exports.helpers = void 0;
30
+ __exportStar(require("./main"), exports);
31
+ __exportStar(require("./hooks"), exports);
32
+ __exportStar(require("./provider"), exports);
67
33
  exports.helpers = __importStar(require("./helpers"));
68
- exports.hooks = __importStar(require("./hooks"));
69
34
  exports.types = __importStar(require("./types"));
70
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
+ };
@@ -36,10 +36,9 @@ var __rest = (this && this.__rest) || function (s, e) {
36
36
  var _a;
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
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 */
40
39
  const react_1 = __importStar(require("react"));
41
40
  const actor_1 = require("../../helpers/actor");
42
- const useActor_1 = require("../../hooks/useActor");
41
+ const useReactor_1 = require("../../hooks/useReactor");
43
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;
44
43
  function createReactorContext(_a = {}) {
45
44
  var { canisterId: defaultCanisterId } = _a, defaultConfig = __rest(_a, ["canisterId"]);
@@ -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);
@@ -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,24 +1,12 @@
1
1
  import { AgentManager } from "@ic-reactor/core/dist/agent";
2
2
  import type { AgentContextType } from "./types";
3
- import { AuthArgs } from "../../types";
4
- export declare const useAgentManagerContext: (agentContext?: AgentContextType) => import("./types").AgentContextValue;
3
+ import { UseAuthClientArgs } from "../../types";
4
+ export declare const useAgentContext: (agentContext?: AgentContextType) => import("./types").AgentContextValue;
5
5
  export declare const useAgentManager: (agentContext?: AgentContextType) => AgentManager;
6
6
  export declare const useAgent: (agentContext?: AgentContextType) => import("@dfinity/agent/lib/cjs/agent/http").HttpAgent | undefined;
7
7
  export declare const useAuthState: (agentContext?: AgentContextType) => import("@ic-reactor/core/dist/agent/types").AuthState;
8
8
  export declare const useAgentState: (agentContext?: AgentContextType) => import("@ic-reactor/core/dist/agent/types").AgentState;
9
- export declare const useAuthClient: ({ agentContext, ...args }: AuthArgs & {
9
+ export declare const useAuthClient: ({ agentContext, ...args }: UseAuthClientArgs & {
10
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
- };
11
+ }) => import("../../types").UseAuthClientReturn;
24
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.useAuthState = 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 useAuthState = (agentContext) => (0, exports.useAgentManagerContext)(agentContext).useAuthState();
32
+ const useAuthState = (agentContext) => (0, exports.useAgentContext)(agentContext).useAuthState();
33
33
  exports.useAuthState = useAuthState;
34
- const useAgentState = (agentContext) => (0, exports.useAgentManagerContext)(agentContext).useAgentState();
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;
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 "./helpers/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";
4
+ export * from "./provider/agent/types";
5
+ export * from "./provider/actor/types";
7
6
  export * from "./helpers/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> = {
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);
18
+ __exportStar(require("./provider/agent/types"), exports);
19
+ __exportStar(require("./provider/actor/types"), exports);
20
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.2",
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.2",
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": "4d19bef671056c8046ec64037f51d85380ff5bae"
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
- };
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes