@ic-reactor/react 1.1.3 → 1.1.5

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.
@@ -1,4 +1,27 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
26
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
27
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -19,12 +42,9 @@ var __rest = (this && this.__rest) || function (s, e) {
19
42
  }
20
43
  return t;
21
44
  };
22
- var __importDefault = (this && this.__importDefault) || function (mod) {
23
- return (mod && mod.__esModule) ? mod : { "default": mod };
24
- };
25
45
  Object.defineProperty(exports, "__esModule", { value: true });
26
46
  exports.actorHooks = void 0;
27
- const react_1 = __importDefault(require("react"));
47
+ const react_1 = __importStar(require("react"));
28
48
  const zustand_1 = require("zustand");
29
49
  const DEFAULT_STATE = {
30
50
  data: undefined,
@@ -47,23 +67,29 @@ const DEFAULT_STATE = {
47
67
  * Each hook is designed to simplify the process of interacting with actors in IC projects by abstracting away the complexity of state management, error handling, and method invocation.
48
68
  */
49
69
  const actorHooks = (actorManager) => {
50
- const { actorStore, canisterId, visitFunction, callMethod, initialize } = actorManager;
70
+ const { actorStore, canisterId, visitFunction, extractInterface, callMethod, initialize, } = actorManager;
51
71
  const useActorState = () => (Object.assign(Object.assign({}, (0, zustand_1.useStore)(actorStore)), { canisterId }));
72
+ const useActorInterface = () => {
73
+ return extractInterface();
74
+ };
75
+ const useVisitService = () => {
76
+ return visitFunction;
77
+ };
52
78
  const useVisitMethod = (functionName) => {
53
79
  return react_1.default.useMemo(() => visitFunction[functionName], [functionName]);
54
80
  };
55
- const useMethodCall = (_a) => {
81
+ const useSharedCall = (_a) => {
56
82
  var { args = [], functionName, throwOnError = false } = _a, events = __rest(_a, ["args", "functionName", "throwOnError"]);
57
- const [state, setState] = react_1.default.useState(DEFAULT_STATE);
58
- const reset = react_1.default.useCallback(() => setState(DEFAULT_STATE), []);
83
+ const [sharedState, setSharedState] = react_1.default.useState(DEFAULT_STATE);
84
+ const reset = react_1.default.useCallback(() => setSharedState(DEFAULT_STATE), []);
59
85
  const call = react_1.default.useCallback((eventOrReplaceArgs) => __awaiter(void 0, void 0, void 0, function* () {
60
86
  var _b, _c, _d, _e, _f;
61
- setState((prev) => (Object.assign(Object.assign({}, prev), { error: undefined, loading: true })));
87
+ setSharedState((prev) => (Object.assign(Object.assign({}, prev), { error: undefined, loading: true })));
62
88
  (_b = events === null || events === void 0 ? void 0 : events.onLoading) === null || _b === void 0 ? void 0 : _b.call(events, true);
63
89
  try {
64
90
  const replaceArgs = eventOrReplaceArgs instanceof Array ? eventOrReplaceArgs : args;
65
91
  const data = yield callMethod(functionName, ...replaceArgs);
66
- setState({ data, error: undefined, loading: false });
92
+ setSharedState({ data, error: undefined, loading: false });
67
93
  (_c = events === null || events === void 0 ? void 0 : events.onSuccess) === null || _c === void 0 ? void 0 : _c.call(events, data);
68
94
  (_d = events === null || events === void 0 ? void 0 : events.onLoading) === null || _d === void 0 ? void 0 : _d.call(events, false);
69
95
  return data;
@@ -71,18 +97,18 @@ const actorHooks = (actorManager) => {
71
97
  catch (error) {
72
98
  // eslint-disable-next-line no-console
73
99
  console.error("Error in call:", error);
74
- setState((prevState) => (Object.assign(Object.assign({}, prevState), { error: error, loading: false })));
100
+ setSharedState((prevState) => (Object.assign(Object.assign({}, prevState), { error: error, loading: false })));
75
101
  (_e = events === null || events === void 0 ? void 0 : events.onError) === null || _e === void 0 ? void 0 : _e.call(events, error);
76
102
  (_f = events === null || events === void 0 ? void 0 : events.onLoading) === null || _f === void 0 ? void 0 : _f.call(events, false);
77
103
  if (throwOnError)
78
104
  throw error;
79
105
  }
80
106
  }), [args, functionName, events]);
81
- return Object.assign({ call, reset }, state);
107
+ return Object.assign({ call, reset }, sharedState);
82
108
  };
83
109
  const useQueryCall = (_a) => {
84
110
  var { refetchOnMount = true, refetchInterval = false } = _a, rest = __rest(_a, ["refetchOnMount", "refetchInterval"]);
85
- const _b = useMethodCall(rest), { call } = _b, state = __rest(_b, ["call"]);
111
+ const _b = useSharedCall(rest), { call } = _b, state = __rest(_b, ["call"]);
86
112
  const intervalId = react_1.default.useRef();
87
113
  react_1.default.useEffect(() => {
88
114
  if (refetchInterval) {
@@ -95,13 +121,33 @@ const actorHooks = (actorManager) => {
95
121
  }, [refetchInterval, refetchOnMount]);
96
122
  return Object.assign({ call }, state);
97
123
  };
98
- const useUpdateCall = useMethodCall;
124
+ const useUpdateCall = useSharedCall;
125
+ const useMethod = (args) => {
126
+ const _a = useSharedCall(args), { call, data } = _a, state = __rest(_a, ["call", "data"]);
127
+ const visit = useVisitMethod(args.functionName);
128
+ const transformedData = (0, react_1.useMemo)(() => {
129
+ if (data === undefined)
130
+ return data;
131
+ if (args.transform) {
132
+ return visit(args.transform, {
133
+ value: data,
134
+ label: args.functionName,
135
+ });
136
+ }
137
+ return data;
138
+ }, [data, args.transform, visit]);
139
+ return Object.assign({ call,
140
+ visit, data: transformedData }, state);
141
+ };
99
142
  return {
100
143
  initialize,
144
+ useMethod,
101
145
  useQueryCall,
102
146
  useUpdateCall,
103
147
  useActorState,
104
148
  useVisitMethod,
149
+ useVisitService,
150
+ useActorInterface,
105
151
  };
106
152
  };
107
153
  exports.actorHooks = actorHooks;
@@ -40,14 +40,20 @@ function extractActorContext(actorContext) {
40
40
  };
41
41
  const initialize = () => useActorContext().initialize();
42
42
  const useActorState = () => useActorContext().useActorState();
43
+ const useMethod = (args) => useActorContext().useMethod(args);
43
44
  const useQueryCall = (args) => useActorContext().useQueryCall(args);
44
45
  const useUpdateCall = (args) => useActorContext().useUpdateCall(args);
45
46
  const useVisitMethod = (functionName) => useActorContext().useVisitMethod(functionName);
47
+ const useVisitService = () => useActorContext().useVisitService();
48
+ const useActorInterface = () => useActorContext().useActorInterface();
46
49
  return {
47
50
  useActorState,
51
+ useMethod,
48
52
  useQueryCall,
49
53
  useUpdateCall,
50
54
  useVisitMethod,
55
+ useVisitService,
56
+ useActorInterface,
51
57
  initialize,
52
58
  };
53
59
  }
@@ -1,4 +1,5 @@
1
1
  /// <reference types="react" />
2
+ import type { ServiceClass, Visitor } from "@dfinity/candid/lib/cjs/idl";
2
3
  import type { ActorState, CanisterId, AuthClientLoginOptions, ActorMethodParameters, ActorMethodReturnType, Identity, Principal, FunctionName, VisitService, AuthState, HttpAgent, AgentState, BaseActor } from "@ic-reactor/core/dist/types";
3
4
  export interface AgentHooksReturnType {
4
5
  useAgent: () => HttpAgent | undefined;
@@ -40,7 +41,7 @@ export type LogoutParameters = {
40
41
  export interface UseActorState extends Omit<ActorState, "methodState"> {
41
42
  canisterId: CanisterId;
42
43
  }
43
- export type UseMethodCallParameters<A, M extends FunctionName<A>> = {
44
+ export type UseSharedCallParameters<A, M extends FunctionName<A>> = {
44
45
  functionName: M;
45
46
  args?: ActorMethodParameters<A[M]>;
46
47
  onLoading?: (loading: boolean) => void;
@@ -48,29 +49,60 @@ export type UseMethodCallParameters<A, M extends FunctionName<A>> = {
48
49
  onSuccess?: (data: ActorMethodReturnType<A[M]> | undefined) => void;
49
50
  throwOnError?: boolean;
50
51
  };
51
- export type ActorCallState<A, M extends FunctionName<A>> = {
52
+ export type UseSharedCallState<A, M extends FunctionName<A>> = {
52
53
  data: ActorMethodReturnType<A[M]> | undefined;
53
54
  error: Error | undefined;
54
55
  loading: boolean;
55
56
  };
56
- export interface UseQueryCallParameters<A, M extends FunctionName<A>> extends UseMethodCallParameters<A, M> {
57
+ export interface UseSharedCallReturnType<A, M extends FunctionName<A> = FunctionName<A>> extends UseSharedCallState<A, M> {
58
+ reset: () => void;
59
+ call: (eventOrReplaceArgs?: React.MouseEvent | ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]> | undefined>;
60
+ }
61
+ export type UseSharedCall<A> = <M extends FunctionName<A>>(args: UseSharedCallParameters<A, M>) => UseSharedCallReturnType<A, M>;
62
+ export interface UseQueryCallParameters<A, M extends FunctionName<A>> extends UseSharedCallParameters<A, M> {
57
63
  refetchOnMount?: boolean;
58
64
  refetchInterval?: number | false;
59
65
  }
60
- export interface UseUpdateCallParameters<A, M extends FunctionName<A>> extends UseMethodCallParameters<A, M> {
66
+ export type UseQueryCall<A> = <M extends FunctionName<A>>(args: UseQueryCallParameters<A, M>) => UseSharedCallReturnType<A, M>;
67
+ export interface UseUpdateCallParameters<A, M extends FunctionName<A>> extends UseSharedCallParameters<A, M> {
68
+ }
69
+ export type UseUpdateCall<A> = <M extends FunctionName<A>>(args: UseUpdateCallParameters<A, M>) => UseSharedCallReturnType<A, M>;
70
+ export interface DynamicDataArgs<V = unknown> {
71
+ label: string;
72
+ value: V;
61
73
  }
62
- export interface UseMethodCallReturnType<A, M extends FunctionName<A> = FunctionName<A>> extends ActorCallState<A, M> {
74
+ export interface UseMethodParameters<A, M extends FunctionName<A>, T = unknown> extends UseSharedCallParameters<A, M> {
75
+ transform?: Visitor<DynamicDataArgs, T>;
76
+ }
77
+ export interface UseMethodReturnType<A, M extends FunctionName<A> = FunctionName<A>, T = unknown> {
78
+ loading: boolean;
79
+ error: Error | undefined;
80
+ data: T | ActorMethodReturnType<A[M]> | undefined;
81
+ visit: VisitService<A>[M];
63
82
  reset: () => void;
64
83
  call: (eventOrReplaceArgs?: React.MouseEvent | ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]> | undefined>;
65
84
  }
66
- export type UseMethodCall<A> = <M extends FunctionName<A>>(args: UseMethodCallParameters<A, M>) => UseMethodCallReturnType<A, M>;
67
- export type UseQueryCall<A> = <M extends FunctionName<A>>(args: UseQueryCallParameters<A, M>) => UseMethodCallReturnType<A, M>;
68
- export type UseUpdateCall<A> = <M extends FunctionName<A>>(args: UseUpdateCallParameters<A, M>) => UseMethodCallReturnType<A, M>;
85
+ export type UseMethod<A> = {
86
+ <M extends FunctionName<A>, T = unknown>({ transform, }: {
87
+ transform: Visitor<DynamicDataArgs, T>;
88
+ } & UseMethodParameters<A, M, T>): {
89
+ data: T | undefined;
90
+ } & UseMethodReturnType<A, M, T>;
91
+ <M extends FunctionName<A>, T = unknown>({ transform, }: {
92
+ transform?: undefined;
93
+ } & UseMethodParameters<A, M, T>): {
94
+ data: ActorMethodReturnType<A[M]> | undefined;
95
+ } & UseMethodReturnType<A, M, T>;
96
+ };
69
97
  export type UseVisitMethod<A> = <M extends FunctionName<A>>(functionName: M) => VisitService<A>[M];
98
+ export type UseVisitService<A> = () => VisitService<A>;
70
99
  export interface ActorHooksReturnType<A = BaseActor> {
71
100
  initialize: () => Promise<void>;
72
101
  useActorState: () => UseActorState;
102
+ useActorInterface: () => ServiceClass;
103
+ useMethod: UseMethod<A>;
73
104
  useQueryCall: UseQueryCall<A>;
74
105
  useUpdateCall: UseUpdateCall<A>;
75
106
  useVisitMethod: UseVisitMethod<A>;
107
+ useVisitService: UseVisitService<A>;
76
108
  }
@@ -1,4 +1,7 @@
1
+ export * from "./useMethod";
1
2
  export * from "./useActorState";
2
3
  export * from "./useQueryCall";
3
4
  export * from "./useUpdateCall";
4
5
  export * from "./useVisitMethod";
6
+ export * from "./useVisitService";
7
+ export * from "./useActorInterface";
@@ -14,7 +14,10 @@ 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("./useMethod"), exports);
17
18
  __exportStar(require("./useActorState"), exports);
18
19
  __exportStar(require("./useQueryCall"), exports);
19
20
  __exportStar(require("./useUpdateCall"), exports);
20
21
  __exportStar(require("./useVisitMethod"), exports);
22
+ __exportStar(require("./useVisitService"), exports);
23
+ __exportStar(require("./useActorInterface"), exports);
@@ -0,0 +1,4 @@
1
+ /**
2
+ *
3
+ */
4
+ export declare function useActorInterface(): import("@dfinity/candid/lib/cjs/idl").ServiceClass;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useActorInterface = void 0;
4
+ const hooks_1 = require("./hooks");
5
+ /**
6
+ *
7
+ */
8
+ function useActorInterface() {
9
+ return hooks_1.ActorHooks.useActorInterface();
10
+ }
11
+ exports.useActorInterface = useActorInterface;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Hook for making update calls to actors, handling loading states, and managing errors. It supports custom event handlers for loading, success, and error events.
3
+ *
4
+ * @param options Configuration object for the actor method call, including the method name, arguments, and event handlers.
5
+ * @returns An object containing the method call function, a reset function to reset the call state to its default, and the current call state (data, error, loading, call, reset).
6
+ * @example
7
+ * ```tsx
8
+ * function UpdateCallComponent() {
9
+ * const { call, data, loading } = useUpdateCall({
10
+ * functionName: 'updateUserProfile',
11
+ * args: ['123', { name: 'John Doe' }],
12
+ * onLoading: (loading) => console.log('Loading:', loading),
13
+ * onError: (error) => console.error('Error:', error),
14
+ * onSuccess: (data) => console.log('Success:', data),
15
+ * });
16
+ *
17
+ * if (loading) return <p>Updating profile...</p>;
18
+ *
19
+ * return (
20
+ * <div>
21
+ * <p>Updated Profile: {JSON.stringify(data)}</p>
22
+ * <button onClick={call}>Update</button>
23
+ * </div>
24
+ * );
25
+ * }
26
+ * ```
27
+ */
28
+ export declare const useMethod: import("../../types").UseMethod<import("@ic-reactor/core/dist/types").BaseActor>;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useMethod = void 0;
4
+ const hooks_1 = require("./hooks");
5
+ /**
6
+ * Hook for making update calls to actors, handling loading states, and managing errors. It supports custom event handlers for loading, success, and error events.
7
+ *
8
+ * @param options Configuration object for the actor method call, including the method name, arguments, and event handlers.
9
+ * @returns An object containing the method call function, a reset function to reset the call state to its default, and the current call state (data, error, loading, call, reset).
10
+ * @example
11
+ * ```tsx
12
+ * function UpdateCallComponent() {
13
+ * const { call, data, loading } = useUpdateCall({
14
+ * functionName: 'updateUserProfile',
15
+ * args: ['123', { name: 'John Doe' }],
16
+ * onLoading: (loading) => console.log('Loading:', loading),
17
+ * onError: (error) => console.error('Error:', error),
18
+ * onSuccess: (data) => console.log('Success:', data),
19
+ * });
20
+ *
21
+ * if (loading) return <p>Updating profile...</p>;
22
+ *
23
+ * return (
24
+ * <div>
25
+ * <p>Updated Profile: {JSON.stringify(data)}</p>
26
+ * <button onClick={call}>Update</button>
27
+ * </div>
28
+ * );
29
+ * }
30
+ * ```
31
+ */
32
+ exports.useMethod = hooks_1.ActorHooks.useMethod;
@@ -25,4 +25,4 @@ import { BaseActor, FunctionName, UseQueryCallParameters } from "../../types";
25
25
  * }
26
26
  * ```
27
27
  */
28
- export declare function useQueryCall<A = BaseActor, M extends FunctionName<A> = FunctionName<A>>(args: UseQueryCallParameters<A, M>): import("../../types").UseMethodCallReturnType<A, M>;
28
+ export declare function useQueryCall<A = BaseActor, M extends FunctionName<A> = FunctionName<A>>(args: UseQueryCallParameters<A, M>): import("../../types").UseSharedCallReturnType<A, M>;
@@ -26,4 +26,4 @@ import { BaseActor, FunctionName, UseUpdateCallParameters } from "../../types";
26
26
  * }
27
27
  * ```
28
28
  */
29
- export declare function useUpdateCall<A = BaseActor, M extends FunctionName<A> = FunctionName<A>>(args: UseUpdateCallParameters<A, M>): import("../../types").UseMethodCallReturnType<A, M>;
29
+ export declare function useUpdateCall<A = BaseActor, M extends FunctionName<A> = FunctionName<A>>(args: UseUpdateCallParameters<A, M>): import("../../types").UseSharedCallReturnType<A, M>;
@@ -0,0 +1,8 @@
1
+ import { BaseActor } from "../../types";
2
+ /**
3
+ * Memoizes and returns a visit service function for a specific actor method.
4
+ *
5
+ * @param functionName The name of the actor method to visit.
6
+ * @returns The visit service function for the specified method.
7
+ */
8
+ export declare function useVisitService<A = BaseActor>(): import("@ic-reactor/core/dist/classes/actor/types").VisitService<A>;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useVisitService = void 0;
4
+ const hooks_1 = require("./hooks");
5
+ /**
6
+ * Memoizes and returns a visit service function for a specific actor method.
7
+ *
8
+ * @param functionName The name of the actor method to visit.
9
+ * @returns The visit service function for the specified method.
10
+ */
11
+ function useVisitService() {
12
+ return hooks_1.ActorHooks.useVisitService();
13
+ }
14
+ exports.useVisitService = useVisitService;
package/dist/index.d.ts CHANGED
@@ -7,4 +7,4 @@ export * from "./hooks";
7
7
  export * as helpers from "./helpers";
8
8
  export * as types from "./types";
9
9
  export * as core from "./core";
10
- export * as utils from "@ic-reactor/core/dist/utils";
10
+ export * as utils from "./utils";
package/dist/index.js CHANGED
@@ -36,4 +36,4 @@ __exportStar(require("./hooks"), exports);
36
36
  exports.helpers = __importStar(require("./helpers"));
37
37
  exports.types = __importStar(require("./types"));
38
38
  exports.core = __importStar(require("./core"));
39
- exports.utils = __importStar(require("@ic-reactor/core/dist/utils"));
39
+ exports.utils = __importStar(require("./utils"));
@@ -0,0 +1 @@
1
+ export * from "@ic-reactor/core/dist/utils";
package/dist/utils.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("@ic-reactor/core/dist/utils"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/react",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
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.1.1",
38
+ "@ic-reactor/core": "^1.1.2",
39
39
  "zustand-utils": "^1.3"
40
40
  },
41
41
  "peerDependencies": {
@@ -47,5 +47,5 @@
47
47
  "react": ">=16.8",
48
48
  "zustand": "4.5"
49
49
  },
50
- "gitHead": "5420fba37e2721dd9c4a41ba885151eb420c5332"
50
+ "gitHead": "5203d7142eda0153581cb3b6de00e12fc128eb33"
51
51
  }