@ic-reactor/react 0.4.6 → 0.5.2

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.
@@ -2,8 +2,8 @@ import React, { PropsWithChildren } from "react";
2
2
  import { IDL } from "@dfinity/candid";
3
3
  import { ActorSubclass, ActorManagerOptions } from "@ic-reactor/store";
4
4
  import { AgentContextType } from "./agent";
5
- import type { ActorHooks } from "../hooks/actor";
6
- export type ActorContextType<A = ActorSubclass<any>> = ActorHooks<A>;
5
+ import { ActorHooksWithField } from "../types";
6
+ export type ActorContextType<A = ActorSubclass<any>> = ActorHooksWithField<A>;
7
7
  export declare const ActorContext: React.Context<ActorContextType<any> | null>;
8
8
  type UseActorType = <A = ActorSubclass<any>>() => ActorContextType<A>;
9
9
  export declare const useActor: UseActorType;
@@ -58,7 +58,7 @@ const useActor = () => {
58
58
  };
59
59
  exports.useActor = useActor;
60
60
  const ActorProvider = (_a) => {
61
- var { children, canisterId, agentContext, loadingComponent = react_1.default.createElement("div", null, "Loading...") } = _a, config = __rest(_a, ["children", "canisterId", "agentContext", "loadingComponent"]);
61
+ var { children, canisterId, agentContext, loadingComponent = react_1.default.createElement("div", null, "Loading..."), withServiceField = false } = _a, config = __rest(_a, ["children", "canisterId", "agentContext", "loadingComponent", "withServiceField"]);
62
62
  const agentManager = (0, agent_1.useAgentManager)(agentContext);
63
63
  const [didJs, setDidJS] = (0, react_1.useState)();
64
64
  const [fetching, setFetching] = (0, react_1.useState)(false);
@@ -112,6 +112,7 @@ const ActorProvider = (_a) => {
112
112
  agentManager,
113
113
  canisterId,
114
114
  withDevtools: config.withDevtools,
115
+ withServiceField: withServiceField,
115
116
  });
116
117
  }
117
118
  catch (err) {
@@ -1,41 +1,3 @@
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>) => {
8
- data: import("@ic-reactor/store").ExtractActorMethodReturnType<A[M]> | undefined;
9
- error: Error | undefined;
10
- loading: boolean;
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>;
13
- };
14
- useUpdateCall: <M_1 extends keyof A>(args: ActorUseUpdateArgs<A, M_1>) => {
15
- data: import("@ic-reactor/store").ExtractActorMethodReturnType<A[M_1]> | undefined;
16
- error: Error | undefined;
17
- loading: boolean;
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>;
29
- };
30
- useActorStore: () => {
31
- canisterId: import("@ic-reactor/store").CanisterId;
32
- initialized: boolean;
33
- initializing: boolean;
34
- error: Error | undefined;
35
- methodState: import("@ic-reactor/store").ActorMethodStates<A>;
36
- };
37
- useMethodField: (functionName: keyof A & string) => ExtractedFunction<A>;
38
- useMethodFields: () => ExtractedFunction<A>[];
39
- useMethodNames: () => ServiceMethodTypeAndName<A>[];
40
- useServiceFields: () => ExtractedService<A>;
41
- };
1
+ import type { ActorManager } from "@ic-reactor/store";
2
+ import type { ActorHooks } from "../types";
3
+ export declare const getActorHooks: <A extends unknown>({ initialize, serviceFields, withServiceField, canisterId, actorStore, callMethod, }: ActorManager<A>) => ActorHooks<A, boolean>;
@@ -23,27 +23,34 @@ 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 = ({ initialize, serviceFields, canisterId, actorStore, callMethod, }) => {
26
+ const getActorHooks = ({ initialize, serviceFields, withServiceField, 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
31
  const useServiceFields = () => {
32
+ if (!withServiceField || !serviceFields) {
33
+ throw new Error("Service fields not initialized. Pass `withServiceField` to initialize service fields.");
34
+ }
32
35
  return serviceFields;
33
36
  };
34
- const useMethodNames = () => {
35
- const serviceField = useServiceFields();
36
- return serviceField.methodNames;
37
+ const useMethods = () => {
38
+ const serviceFields = useServiceFields();
39
+ return (0, react_1.useMemo)(() => {
40
+ return Object.values(serviceFields.methods);
41
+ }, [serviceFields]);
37
42
  };
38
43
  const useMethodFields = () => {
39
- const methodFields = useServiceFields();
44
+ const serviceFields = useServiceFields();
40
45
  return (0, react_1.useMemo)(() => {
41
- return Object.values(methodFields.methods);
42
- }, [methodFields]);
46
+ return Object.values(serviceFields.methodFields);
47
+ }, [serviceFields]);
43
48
  };
44
49
  const useMethodField = (functionName) => {
45
50
  const serviceMethod = useServiceFields();
46
- return (0, react_1.useMemo)(() => serviceMethod.methods[functionName], [functionName, serviceMethod]);
51
+ return (0, react_1.useMemo)(() => {
52
+ return serviceMethod.methodFields[functionName];
53
+ }, [functionName, serviceMethod]);
47
54
  };
48
55
  const useReActorCall = ({ onError, onSuccess, onLoading, args = [], functionName, throwOnError = false, }) => {
49
56
  const [state, setState] = (0, react_1.useState)({
@@ -76,7 +83,9 @@ const getActorHooks = ({ initialize, serviceFields, canisterId, actorStore, call
76
83
  throw error;
77
84
  }
78
85
  }), [args, functionName, onError, onSuccess, onLoading]);
79
- const field = useMethodField(functionName);
86
+ const field = (0, react_1.useMemo)(() => {
87
+ return serviceFields === null || serviceFields === void 0 ? void 0 : serviceFields.methodFields[functionName];
88
+ }, [functionName]);
80
89
  return Object.assign({ call, field }, state);
81
90
  };
82
91
  const useQueryCall = (_a) => {
@@ -120,9 +129,9 @@ const getActorHooks = ({ initialize, serviceFields, canisterId, actorStore, call
120
129
  useUpdateCall,
121
130
  useMethodCall,
122
131
  useActorStore,
132
+ useMethods,
123
133
  useMethodField,
124
134
  useMethodFields,
125
- useMethodNames,
126
135
  useServiceFields,
127
136
  };
128
137
  };
package/dist/index.d.ts CHANGED
@@ -1,24 +1,5 @@
1
- import type { CreateReActorOptions } from "@ic-reactor/store";
2
- import { ActorHooks } from "./hooks/actor";
3
- import { AuthHooks } from "./hooks/auth";
4
- export { createReActorStore, createAgentManager, createActorManager, } from "@ic-reactor/store";
1
+ import { CreateReActor } from "./types";
2
+ export * from "@ic-reactor/store";
5
3
  export * from "./context/agent";
6
4
  export * from "./context/actor";
7
- export declare const createReActor: <A extends unknown>({ isLocalEnv, ...options }: CreateReActorOptions) => ActorHooks<A> & {
8
- useUserPrincipal: () => import("@dfinity/principal").Principal | undefined;
9
- useAgentManager: () => import("@ic-reactor/store").AgentManager;
10
- useAuthStore: () => import("@ic-reactor/store").AgentAuthState;
11
- useAuthClient: ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, }?: import("./types").AuthArgs) => {
12
- authClient: import("@dfinity/auth-client").AuthClient | null;
13
- authenticated: boolean;
14
- authenticating: boolean;
15
- identity: import("@ic-reactor/store").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("@ic-reactor/store").Identity>;
21
- loginLoading: boolean;
22
- loginError: Error | null;
23
- };
24
- };
5
+ export declare const createReActor: CreateReActor;
package/dist/index.js CHANGED
@@ -25,40 +25,22 @@ var __rest = (this && this.__rest) || function (s, e) {
25
25
  return t;
26
26
  };
27
27
  Object.defineProperty(exports, "__esModule", { value: true });
28
- exports.createReActor = exports.createActorManager = exports.createAgentManager = exports.createReActorStore = void 0;
28
+ exports.createReActor = void 0;
29
29
  const store_1 = require("@ic-reactor/store");
30
30
  const actor_1 = require("./hooks/actor");
31
31
  const auth_1 = require("./hooks/auth");
32
- var store_2 = require("@ic-reactor/store");
33
- Object.defineProperty(exports, "createReActorStore", { enumerable: true, get: function () { return store_2.createReActorStore; } });
34
- Object.defineProperty(exports, "createAgentManager", { enumerable: true, get: function () { return store_2.createAgentManager; } });
35
- Object.defineProperty(exports, "createActorManager", { enumerable: true, get: function () { return store_2.createActorManager; } });
32
+ __exportStar(require("@ic-reactor/store"), exports);
36
33
  __exportStar(require("./context/agent"), exports);
37
34
  __exportStar(require("./context/actor"), exports);
38
35
  const createReActor = (_a) => {
39
- var { isLocalEnv } = _a, options = __rest(_a, ["isLocalEnv"]);
36
+ var { isLocalEnv, withServiceField } = _a, options = __rest(_a, ["isLocalEnv", "withServiceField"]);
40
37
  isLocalEnv =
41
38
  isLocalEnv ||
42
39
  (typeof process !== "undefined" &&
43
- (process.env.NODE_ENV === "development" ||
44
- process.env.DFX_NETWORK === "local"));
45
- const actorManager = (0, store_1.createReActorStore)(Object.assign({ isLocalEnv }, options));
46
- const { useAuthClient, useUserPrincipal, useAgentManager, useAuthStore } = (0, auth_1.getAuthHooks)(actorManager.agentManager);
47
- const { initialize, useActorStore, useQueryCall, useUpdateCall, useMethodCall, useMethodField, useMethodFields, useMethodNames, useServiceFields, } = (0, actor_1.getActorHooks)(actorManager);
48
- return {
49
- initialize,
50
- useUserPrincipal,
51
- useAgentManager,
52
- useMethodFields,
53
- useMethodField,
54
- useActorStore,
55
- useAuthStore,
56
- useQueryCall,
57
- useUpdateCall,
58
- useMethodCall,
59
- useAuthClient,
60
- useMethodNames,
61
- useServiceFields,
62
- };
40
+ (process.env.DFX_NETWORK === "local" ||
41
+ process.env.NODE_ENV === "development"));
42
+ const actorManager = (0, store_1.createReActorStore)(Object.assign({ isLocalEnv,
43
+ withServiceField }, options));
44
+ return Object.assign(Object.assign({}, (0, actor_1.getActorHooks)(actorManager)), (0, auth_1.getAuthHooks)(actorManager.agentManager));
63
45
  };
64
46
  exports.createReActor = createReActor;
package/dist/types.d.ts CHANGED
@@ -1,4 +1,6 @@
1
- import type { ExtractActorMethodArgs, ExtractActorMethodReturnType, Identity, Principal, ServiceMethodType } from "@ic-reactor/store";
1
+ /// <reference types="react" />
2
+ import type { ActorState, ActorSubclass, CanisterId, CreateReActorOptions, ExtractActorMethodArgs, ExtractActorMethodReturnType, ExtractedFunction, ExtractedService, Identity, Principal, ServiceMethodType, ServiceMethodTypeAndName } from "@ic-reactor/store";
3
+ import { AuthHooks } from "./hooks/auth";
2
4
  export type * from "@ic-reactor/store";
3
5
  export type * from "@ic-reactor/store/dist/actor/types";
4
6
  export type AuthArgs = {
@@ -27,6 +29,49 @@ export interface ActorUseQueryArgs<A, M extends keyof A> extends ActorCallArgs<A
27
29
  refetchOnMount?: boolean;
28
30
  refetchInterval?: number | false;
29
31
  }
32
+ export interface ActorUseQueryReturn<A, M extends keyof A, W extends boolean = false> {
33
+ call: (eventOrReplaceArgs?: React.MouseEvent | ExtractActorMethodArgs<A[M]>) => Promise<unknown>;
34
+ field: W extends true ? ExtractedFunction<A> : undefined;
35
+ data: unknown;
36
+ error: Error | undefined;
37
+ loading: boolean;
38
+ }
30
39
  export interface ActorUseUpdateArgs<A, M extends keyof A> extends ActorCallArgs<A, M> {
31
40
  }
41
+ export interface ActorUseUpdateReturn<A, M extends keyof A, W extends boolean = false> {
42
+ call: (eventOrReplaceArgs?: React.MouseEvent | ExtractActorMethodArgs<A[M]>) => Promise<unknown>;
43
+ field: W extends true ? ExtractedFunction<A> : undefined;
44
+ data: unknown;
45
+ error: Error | undefined;
46
+ loading: boolean;
47
+ }
32
48
  export type ActorUseMethodArg<A, T extends ServiceMethodType> = T extends "query" ? ActorUseQueryArgs<A, keyof A> : ActorUseUpdateArgs<A, keyof A>;
49
+ export type ActorHooksWithField<A> = ActorDefaultHooks<A, true> & ActorFieldHooks<A>;
50
+ export type ActorHooksWithoutField<A> = ActorDefaultHooks<A, false>;
51
+ export type ActorHooks<A, W extends boolean | undefined = undefined> = W extends true ? ActorHooksWithField<A> : W extends false ? ActorHooksWithoutField<A> : ActorHooksWithField<A> | ActorHooksWithoutField<A>;
52
+ export interface ActorFieldHooks<A> {
53
+ useServiceFields: () => ExtractedService<A>;
54
+ useMethodFields: () => ExtractedFunction<A>[];
55
+ useMethodField: (functionName: keyof A & string) => ExtractedFunction<A>;
56
+ useMethods: () => ServiceMethodTypeAndName<A>[];
57
+ }
58
+ export type UseActorStoreReturn<A> = ActorState<A> & {
59
+ canisterId: CanisterId;
60
+ };
61
+ export interface ActorDefaultHooks<A, W extends boolean = false> {
62
+ initialize: () => Promise<void>;
63
+ useActorStore: () => UseActorStoreReturn<A>;
64
+ useQueryCall: <M extends keyof A>(args: ActorUseQueryArgs<A, M>) => ActorUseQueryReturn<A, M, W>;
65
+ useUpdateCall: <M extends keyof A>(args: ActorUseUpdateArgs<A, M>) => ActorUseUpdateReturn<A, M, W>;
66
+ useMethodCall: <M extends keyof A, T extends ServiceMethodType>(args: ActorUseMethodArg<A, T> & {
67
+ type: T;
68
+ }) => T extends "query" ? ActorUseQueryReturn<A, M, W> : ActorUseUpdateReturn<A, M, W>;
69
+ }
70
+ export type CreateReActor = {
71
+ <A extends ActorSubclass<any>>(options: CreateReActorOptions & {
72
+ withServiceField: true;
73
+ }): ActorHooksWithField<A> & AuthHooks;
74
+ <A extends ActorSubclass<any>>(options: CreateReActorOptions & {
75
+ withServiceField?: false | undefined;
76
+ }): ActorHooksWithoutField<A> & AuthHooks;
77
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/react",
3
- "version": "0.4.6",
3
+ "version": "0.5.2",
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.4.3",
38
+ "@ic-reactor/store": "^0.5.1",
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": "48251ebc25add07c5a94ff63ebc8bf2dc8e0caba"
51
+ "gitHead": "2d329807181eabc24dd5d9dfc677eac0a0b12a6d"
52
52
  }