@ic-reactor/react 0.3.7 → 0.4.0

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.
package/README.md CHANGED
@@ -54,6 +54,7 @@ const Balance = ({ principal }) => {
54
54
  const { call, data, loading, error } = useQueryCall({
55
55
  functionName: "get_balance",
56
56
  args: [principal],
57
+ callOnMount: true,
57
58
  })
58
59
 
59
60
  return (
@@ -1,8 +1,8 @@
1
1
  import React, { PropsWithChildren } from "react";
2
- import { type ActorHooks } from "../index";
3
2
  import { IDL } from "@dfinity/candid";
4
3
  import { ActorSubclass, ActorManagerOptions } from "@ic-reactor/store";
5
4
  import { AgentContextType } from "./agent";
5
+ import type { ActorHooks } from "../hooks/actor";
6
6
  export type ActorContextType<A = ActorSubclass<any>> = ActorHooks<A>;
7
7
  export declare const ActorContext: React.Context<ActorContextType<any> | null>;
8
8
  type UseActorType = <A = ActorSubclass<any>>() => ActorContextType<A>;
@@ -1,19 +1,31 @@
1
- import type { ExtractActorMethodArgs, ActorManager, ActorMethodField } from "@ic-reactor/store";
2
- import type { ActorUseQueryArgs, ActorUseUpdateArgs } from "../types";
3
- export declare const getActorHooks: <A extends unknown>({ methodFields, canisterId, actorStore, callMethod, }: ActorManager<A>) => {
4
- useQueryCall: <M extends keyof A>({ autoRefresh, refreshInterval, disableInitialCall, ...rest }: ActorUseQueryArgs<A, M>) => {
1
+ /// <reference types="react" />
2
+ import type { ActorSubclass, ExtractActorMethodArgs, ActorManager, ExtractedService, ExtractedFunction, ServiceMethodType, ServiceMethodTypeAndName } from "@ic-reactor/store";
3
+ import type { ActorUseMethodArg, ActorUseQueryArgs, ActorUseUpdateArgs } from "../types";
4
+ export type ActorHooks<A extends ActorSubclass<any>> = ReturnType<typeof getActorHooks<A>>;
5
+ export declare const getActorHooks: <A extends unknown>({ initialize, serviceFields, canisterId, actorStore, callMethod, }: ActorManager<A>) => {
6
+ initialize: (options?: import("@ic-reactor/store").UpdateAgentOptions | undefined) => Promise<void>;
7
+ useQueryCall: <M extends keyof A>({ refetchOnMount, refetchInterval, ...rest }: ActorUseQueryArgs<A, M>) => {
5
8
  data: import("@ic-reactor/store").ExtractActorMethodReturnType<A[M]> | undefined;
6
9
  error: Error | undefined;
7
10
  loading: boolean;
8
- field: ActorMethodField<A> | undefined;
9
- call: (replaceArgs?: ExtractActorMethodArgs<A[M]> | undefined) => Promise<import("@ic-reactor/store").ExtractActorMethodReturnType<A[M]> | undefined>;
11
+ field: ExtractedFunction<A>;
12
+ call: (eventOrReplaceArgs?: import("react").MouseEvent<Element, MouseEvent> | ExtractActorMethodArgs<A[M]> | undefined) => Promise<import("@ic-reactor/store").ExtractActorMethodReturnType<A[M]> | undefined>;
10
13
  };
11
14
  useUpdateCall: <M_1 extends keyof A>(args: ActorUseUpdateArgs<A, M_1>) => {
12
15
  data: import("@ic-reactor/store").ExtractActorMethodReturnType<A[M_1]> | undefined;
13
16
  error: Error | undefined;
14
17
  loading: boolean;
15
- call: (replaceArgs?: ExtractActorMethodArgs<A[M_1]> | undefined) => Promise<import("@ic-reactor/store").ExtractActorMethodReturnType<A[M_1]> | undefined>;
16
- field: ActorMethodField<A> | undefined;
18
+ call: (eventOrReplaceArgs?: import("react").MouseEvent<Element, MouseEvent> | ExtractActorMethodArgs<A[M_1]> | undefined) => Promise<import("@ic-reactor/store").ExtractActorMethodReturnType<A[M_1]> | undefined>;
19
+ field: ExtractedFunction<A>;
20
+ };
21
+ useMethodCall: <M_2 extends keyof A, T extends ServiceMethodType>({ type, ...rest }: ActorUseMethodArg<A, T> & {
22
+ type: T;
23
+ }) => {
24
+ data: import("@ic-reactor/store").ExtractActorMethodReturnType<A[M_2]> | undefined;
25
+ error: Error | undefined;
26
+ loading: boolean;
27
+ field: ExtractedFunction<A>;
28
+ call: (eventOrReplaceArgs?: import("react").MouseEvent<Element, MouseEvent> | ExtractActorMethodArgs<A[M_2]> | undefined) => Promise<import("@ic-reactor/store").ExtractActorMethodReturnType<A[M_2]> | undefined>;
17
29
  };
18
30
  useActorStore: () => {
19
31
  canisterId: import("@ic-reactor/store").CanisterId;
@@ -22,6 +34,8 @@ export declare const getActorHooks: <A extends unknown>({ methodFields, canister
22
34
  error: Error | undefined;
23
35
  methodState: import("@ic-reactor/store").ActorMethodStates<A>;
24
36
  };
25
- useMethodField: (functionName: keyof A & string) => ActorMethodField<A> | undefined;
26
- useMethodFields: () => ActorMethodField<A>[];
37
+ useMethodField: (functionName: keyof A & string) => ExtractedFunction<A>;
38
+ useMethodFields: () => ExtractedFunction<A>[];
39
+ useMethodNames: () => ServiceMethodTypeAndName<A>[];
40
+ useServiceFields: () => ExtractedService<A>;
27
41
  };
@@ -23,32 +23,44 @@ Object.defineProperty(exports, "__esModule", { value: true });
23
23
  exports.getActorHooks = void 0;
24
24
  const react_1 = require("react");
25
25
  const zustand_1 = require("zustand");
26
- const getActorHooks = ({ methodFields, canisterId, actorStore, callMethod, }) => {
26
+ const getActorHooks = ({ initialize, serviceFields, canisterId, actorStore, callMethod, }) => {
27
27
  const useActorStore = () => {
28
28
  const actorState = (0, zustand_1.useStore)(actorStore, (state) => state);
29
29
  return Object.assign(Object.assign({}, actorState), { canisterId });
30
30
  };
31
+ const useServiceFields = () => {
32
+ return serviceFields;
33
+ };
34
+ const useMethodNames = () => {
35
+ const serviceField = useServiceFields();
36
+ return serviceField.methodNames;
37
+ };
31
38
  const useMethodFields = () => {
32
- return methodFields;
39
+ const methodFields = useServiceFields();
40
+ return (0, react_1.useMemo)(() => {
41
+ return Object.values(methodFields.methods);
42
+ }, [methodFields]);
33
43
  };
34
44
  const useMethodField = (functionName) => {
35
- const methodFields = useMethodFields();
36
- const field = (0, react_1.useMemo)(() => {
37
- return methodFields.find((f) => f.functionName === functionName);
38
- }, [methodFields, functionName]);
39
- return field;
45
+ const serviceMethod = useServiceFields();
46
+ return (0, react_1.useMemo)(() => serviceMethod.methods[functionName], [functionName, serviceMethod]);
40
47
  };
41
- const useReActorCall = ({ onError, onSuccess, onLoading, args = [], functionName, }) => {
48
+ const useReActorCall = ({ onError, onSuccess, onLoading, args = [], functionName, throwOnError = false, }) => {
42
49
  const [state, setState] = (0, react_1.useState)({
43
50
  data: undefined,
44
51
  error: undefined,
45
52
  loading: false,
46
53
  });
47
- const call = (0, react_1.useCallback)((replaceArgs) => __awaiter(void 0, void 0, void 0, function* () {
54
+ const call = (0, react_1.useCallback)((eventOrReplaceArgs) => __awaiter(void 0, void 0, void 0, function* () {
48
55
  onLoading === null || onLoading === void 0 ? void 0 : onLoading(true);
49
56
  onError === null || onError === void 0 ? void 0 : onError(undefined);
50
57
  setState((prevState) => (Object.assign(Object.assign({}, prevState), { loading: true, error: undefined })));
51
58
  try {
59
+ const replaceArgs = eventOrReplaceArgs !== undefined
60
+ ? eventOrReplaceArgs.length > 0
61
+ ? eventOrReplaceArgs
62
+ : undefined
63
+ : undefined;
52
64
  const data = yield callMethod(functionName, ...(replaceArgs !== null && replaceArgs !== void 0 ? replaceArgs : args));
53
65
  onLoading === null || onLoading === void 0 ? void 0 : onLoading(false);
54
66
  onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
@@ -60,41 +72,58 @@ const getActorHooks = ({ methodFields, canisterId, actorStore, callMethod, }) =>
60
72
  onError === null || onError === void 0 ? void 0 : onError(error);
61
73
  onLoading === null || onLoading === void 0 ? void 0 : onLoading(false);
62
74
  setState((prevState) => (Object.assign(Object.assign({}, prevState), { error: error, loading: false })));
75
+ if (throwOnError)
76
+ throw error;
63
77
  }
64
78
  }), [args, functionName, onError, onSuccess, onLoading]);
65
79
  const field = useMethodField(functionName);
66
80
  return Object.assign({ call, field }, state);
67
81
  };
68
82
  const useQueryCall = (_a) => {
69
- var { autoRefresh, refreshInterval = 5000, disableInitialCall } = _a, rest = __rest(_a, ["autoRefresh", "refreshInterval", "disableInitialCall"]);
83
+ var { refetchOnMount = false, refetchInterval = false } = _a, rest = __rest(_a, ["refetchOnMount", "refetchInterval"]);
70
84
  const _b = useReActorCall(rest), { call } = _b, state = __rest(_b, ["call"]);
71
85
  let intervalId = (0, react_1.useRef)(undefined);
72
86
  (0, react_1.useEffect)(() => {
73
87
  // Auto-refresh logic
74
- if (autoRefresh) {
88
+ if (refetchInterval) {
75
89
  intervalId.current = setInterval(() => {
76
90
  call();
77
- }, refreshInterval);
91
+ }, refetchInterval);
78
92
  }
79
93
  // Initial call logic
80
- if (!disableInitialCall) {
94
+ if (refetchOnMount) {
81
95
  call();
82
96
  }
83
97
  return () => {
84
98
  clearInterval(intervalId.current);
85
99
  };
86
- }, [disableInitialCall, autoRefresh, refreshInterval]);
100
+ }, [refetchOnMount, refetchInterval]);
87
101
  return Object.assign({ call }, state);
88
102
  };
89
103
  const useUpdateCall = (args) => {
90
104
  return useReActorCall(args);
91
105
  };
106
+ const useMethodCall = (_a) => {
107
+ var { type } = _a, rest = __rest(_a, ["type"]);
108
+ switch (type) {
109
+ case "query":
110
+ return useQueryCall(rest);
111
+ case "update":
112
+ return useUpdateCall(rest);
113
+ default:
114
+ throw new Error(`Invalid type: ${type}`);
115
+ }
116
+ };
92
117
  return {
118
+ initialize,
93
119
  useQueryCall,
94
120
  useUpdateCall,
121
+ useMethodCall,
95
122
  useActorStore,
96
123
  useMethodField,
97
124
  useMethodFields,
125
+ useMethodNames,
126
+ useServiceFields,
98
127
  };
99
128
  };
100
129
  exports.getActorHooks = getActorHooks;
@@ -1,8 +1,9 @@
1
1
  import type { AuthClientLoginOptions } from "@dfinity/auth-client";
2
2
  import type { AgentManager } from "@ic-reactor/store";
3
+ export type AuthHooks = ReturnType<typeof getAuthHooks>;
3
4
  export declare const getAuthHooks: (agentManager: AgentManager) => {
4
5
  useAgentManager: () => AgentManager;
5
- useAuthStore: () => import("@ic-reactor/store").ActorAuthState;
6
+ useAuthStore: () => import("@ic-reactor/store").AgentAuthState;
6
7
  useAuthClient: () => {
7
8
  authClient: import("@dfinity/auth-client").AuthClient | null;
8
9
  authenticated: boolean;
package/dist/index.d.ts CHANGED
@@ -1,8 +1,23 @@
1
- import type { ActorSubclass, CreateReActorOptions } from "@ic-reactor/store";
2
- import { getActorHooks } from "./hooks/actor";
3
- import { getAuthHooks } from "./hooks/auth";
1
+ import type { CreateReActorOptions } from "@ic-reactor/store";
2
+ import { ActorHooks } from "./hooks/actor";
3
+ import { AuthHooks } from "./hooks/auth";
4
4
  export { createReActorStore, createAgentManager, createActorManager, } from "@ic-reactor/store";
5
5
  export * from "./context/agent";
6
6
  export * from "./context/actor";
7
- export type ActorHooks<A extends ActorSubclass<any>> = ReturnType<typeof getActorHooks<A>> & ReturnType<typeof getAuthHooks>;
8
- export declare const createReActor: <A extends unknown>({ isLocal, ...options }: CreateReActorOptions) => ActorHooks<A>;
7
+ export declare const createReActor: <A extends unknown>({ isLocalEnv, ...options }: CreateReActorOptions) => ActorHooks<A> & {
8
+ useAgentManager: () => import("@ic-reactor/store").AgentManager;
9
+ useAuthStore: () => import("@ic-reactor/store").AgentAuthState;
10
+ useAuthClient: () => {
11
+ authClient: import("@dfinity/auth-client").AuthClient | null;
12
+ authenticated: boolean;
13
+ authenticating: boolean;
14
+ identity: import("@ic-reactor/store").Identity | null;
15
+ login: (options?: import("@dfinity/auth-client").AuthClientLoginOptions | undefined) => Promise<void>;
16
+ logout: (options?: {
17
+ returnTo?: string | undefined;
18
+ } | undefined) => Promise<void>;
19
+ authenticate: () => Promise<void>;
20
+ loginLoading: boolean;
21
+ loginError: unknown;
22
+ };
23
+ };
package/dist/index.js CHANGED
@@ -36,16 +36,17 @@ Object.defineProperty(exports, "createActorManager", { enumerable: true, get: fu
36
36
  __exportStar(require("./context/agent"), exports);
37
37
  __exportStar(require("./context/actor"), exports);
38
38
  const createReActor = (_a) => {
39
- var { isLocal } = _a, options = __rest(_a, ["isLocal"]);
40
- isLocal =
41
- isLocal ||
39
+ var { isLocalEnv } = _a, options = __rest(_a, ["isLocalEnv"]);
40
+ isLocalEnv =
41
+ isLocalEnv ||
42
42
  (typeof process !== "undefined" &&
43
43
  (process.env.NODE_ENV === "development" ||
44
44
  process.env.DFX_NETWORK === "local"));
45
- const actorManager = (0, store_1.createReActorStore)(Object.assign({ isLocal }, options));
45
+ const actorManager = (0, store_1.createReActorStore)(Object.assign({ isLocalEnv }, options));
46
46
  const { useAuthClient, useAgentManager, useAuthStore } = (0, auth_1.getAuthHooks)(actorManager.agentManager);
47
- const { useActorStore, useQueryCall, useMethodField, useMethodFields, useUpdateCall, } = (0, actor_1.getActorHooks)(actorManager);
47
+ const { initialize, useActorStore, useQueryCall, useUpdateCall, useMethodCall, useMethodField, useMethodFields, useMethodNames, useServiceFields, } = (0, actor_1.getActorHooks)(actorManager);
48
48
  return {
49
+ initialize,
49
50
  useAgentManager,
50
51
  useMethodFields,
51
52
  useMethodField,
@@ -53,7 +54,10 @@ const createReActor = (_a) => {
53
54
  useAuthStore,
54
55
  useQueryCall,
55
56
  useUpdateCall,
57
+ useMethodCall,
56
58
  useAuthClient,
59
+ useMethodNames,
60
+ useServiceFields,
57
61
  };
58
62
  };
59
63
  exports.createReActor = createReActor;
package/dist/types.d.ts CHANGED
@@ -1,11 +1,13 @@
1
- import type { ExtractActorMethodArgs, ExtractActorMethodReturnType } from "@ic-reactor/store";
1
+ import type { ExtractActorMethodArgs, ExtractActorMethodReturnType, ServiceMethodType } from "@ic-reactor/store";
2
2
  export type * from "@ic-reactor/store";
3
+ export type * from "@ic-reactor/store/dist/actor/types";
3
4
  export type ActorCallArgs<A, M extends keyof A> = {
4
5
  functionName: M & string;
5
6
  args?: ExtractActorMethodArgs<A[M]>;
6
7
  onLoading?: (loading: boolean) => void;
7
- onError?: (error: Error | unknown) => void;
8
+ onError?: (error: Error | undefined) => void;
8
9
  onSuccess?: (data: ExtractActorMethodReturnType<A[M]> | undefined) => void;
10
+ throwOnError?: boolean;
9
11
  };
10
12
  export type ActorHookState<A, M extends keyof A> = {
11
13
  data: ExtractActorMethodReturnType<A[M]> | undefined;
@@ -13,9 +15,9 @@ export type ActorHookState<A, M extends keyof A> = {
13
15
  loading: boolean;
14
16
  };
15
17
  export interface ActorUseQueryArgs<A, M extends keyof A> extends ActorCallArgs<A, M> {
16
- disableInitialCall?: boolean;
17
- autoRefresh?: boolean;
18
- refreshInterval?: number;
18
+ refetchOnMount?: boolean;
19
+ refetchInterval?: number | false;
19
20
  }
20
21
  export interface ActorUseUpdateArgs<A, M extends keyof A> extends ActorCallArgs<A, M> {
21
22
  }
23
+ export type ActorUseMethodArg<A, T extends ServiceMethodType> = T extends "query" ? ActorUseQueryArgs<A, keyof A> : ActorUseUpdateArgs<A, keyof A>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/react",
3
- "version": "0.3.7",
3
+ "version": "0.4.0",
4
4
  "description": "A React library for interacting with Dfinity actors",
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/store": "^0.3.5",
38
+ "@ic-reactor/store": "^0.4.0",
39
39
  "zustand-utils": "^1.3"
40
40
  },
41
41
  "peerDependencies": {
@@ -48,5 +48,5 @@
48
48
  "react": ">=16.8",
49
49
  "zustand": "4.4"
50
50
  },
51
- "gitHead": "2e01a26ea775885343887081ddff152944b6e9b2"
51
+ "gitHead": "b91b8bec6e0329d0a7cde22014d9222814311f23"
52
52
  }