@ic-reactor/react 0.3.0 → 0.3.1

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.
@@ -0,0 +1,41 @@
1
+ import type { AuthClientLoginOptions } from "@dfinity/auth-client";
2
+ import type { CreateReActorOptions } from "@ic-reactor/store";
3
+ export * from "./context";
4
+ export declare const createReActor: <A extends unknown>(options: Omit<CreateReActorOptions, "isLocal">) => {
5
+ useMethodFields: () => import("@ic-reactor/store").ActorMethodField<A>[];
6
+ useMethodField: (functionName: keyof A & string) => import("@ic-reactor/store").ActorMethodField<A> | undefined;
7
+ useActorStore: () => {
8
+ initialized: boolean;
9
+ initializing: boolean;
10
+ error: Error | undefined;
11
+ methodState: import("@ic-reactor/store").ActorMethodStates<A>;
12
+ };
13
+ useAuthStore: () => import("@ic-reactor/store").ActorAuthState;
14
+ useQueryCall: <M extends keyof A>({ autoRefresh, refreshInterval, disableInitialCall, ...rest }: import("./types").ReActorUseQueryArgs<A, M>) => {
15
+ data: import("@ic-reactor/store").ExtractActorMethodReturnType<A[M]> | undefined;
16
+ error: Error | undefined;
17
+ loading: boolean;
18
+ field: import("@ic-reactor/store").ActorMethodField<A> | undefined;
19
+ call: (replaceArgs?: import("@ic-reactor/store").ExtractActorMethodArgs<A[M]> | undefined) => Promise<import("@ic-reactor/store").ExtractActorMethodReturnType<A[M]> | undefined>;
20
+ };
21
+ useUpdateCall: <M_1 extends keyof A>(args: import("./types").ReActorUseUpdateArgs<A, M_1>) => {
22
+ data: import("@ic-reactor/store").ExtractActorMethodReturnType<A[M_1]> | undefined;
23
+ error: Error | undefined;
24
+ loading: boolean;
25
+ call: (replaceArgs?: import("@ic-reactor/store").ExtractActorMethodArgs<A[M_1]> | undefined) => Promise<import("@ic-reactor/store").ExtractActorMethodReturnType<A[M_1]> | undefined>;
26
+ field: import("@ic-reactor/store").ActorMethodField<A> | undefined;
27
+ };
28
+ useAuthClient: () => {
29
+ authClient: import("@dfinity/auth-client").AuthClient | null;
30
+ authenticated: boolean;
31
+ authenticating: boolean;
32
+ identity: import("@ic-reactor/store").Identity | null;
33
+ login: (options?: AuthClientLoginOptions) => Promise<void>;
34
+ logout: (options?: {
35
+ returnTo?: string;
36
+ }) => Promise<void>;
37
+ authenticate: () => Promise<void>;
38
+ loginLoading: boolean;
39
+ loginError: unknown;
40
+ };
41
+ };
@@ -0,0 +1,102 @@
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
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
+ return new (P || (P = Promise))(function (resolve, reject) {
19
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
23
+ });
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.createReActor = void 0;
27
+ const store_1 = require("@ic-reactor/store");
28
+ const react_1 = require("react");
29
+ const zustand_1 = require("zustand");
30
+ const hooks_1 = require("./hooks");
31
+ __exportStar(require("./context"), exports);
32
+ const createReActor = (options) => {
33
+ const isLocal = typeof process !== "undefined" &&
34
+ (process.env.NODE_ENV === "development" ||
35
+ process.env.DFX_NETWORK === "local");
36
+ const { authenticate, authStore, callMethod, actorStore, methodFields } = (0, store_1.createReActorStore)(Object.assign({ isLocal }, options));
37
+ const useActorStore = () => {
38
+ const actorState = (0, zustand_1.useStore)(actorStore, (state) => state);
39
+ return Object.assign({}, actorState);
40
+ };
41
+ const useAuthStore = () => {
42
+ const authState = (0, zustand_1.useStore)(authStore, (state) => state);
43
+ return authState;
44
+ };
45
+ const useAuthClient = () => {
46
+ const [loginLoading, setLoginLoading] = (0, react_1.useState)(false);
47
+ const [loginError, setLoginError] = (0, react_1.useState)(null);
48
+ const { authClient, authenticated, authenticating, identity } = useAuthStore();
49
+ const login = (options) => __awaiter(void 0, void 0, void 0, function* () {
50
+ setLoginLoading(true);
51
+ setLoginError(null);
52
+ try {
53
+ yield (authClient === null || authClient === void 0 ? void 0 : authClient.login(Object.assign(Object.assign({}, options), { onSuccess: () => __awaiter(void 0, void 0, void 0, function* () {
54
+ var _a;
55
+ setLoginLoading(false);
56
+ yield authenticate();
57
+ (_a = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _a === void 0 ? void 0 : _a.call(options);
58
+ }), onError: (e) => {
59
+ var _a;
60
+ setLoginError(e);
61
+ setLoginLoading(false);
62
+ (_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, e);
63
+ } })));
64
+ }
65
+ catch (e) {
66
+ setLoginLoading(false);
67
+ setLoginError(e);
68
+ }
69
+ });
70
+ const logout = (options) => __awaiter(void 0, void 0, void 0, function* () {
71
+ yield (authClient === null || authClient === void 0 ? void 0 : authClient.logout(options));
72
+ yield authenticate();
73
+ });
74
+ (0, react_1.useEffect)(() => {
75
+ if (!authClient && !authenticating) {
76
+ authenticate();
77
+ }
78
+ }, [authClient, authenticating]);
79
+ return {
80
+ authClient,
81
+ authenticated,
82
+ authenticating,
83
+ identity,
84
+ login,
85
+ logout,
86
+ authenticate,
87
+ loginLoading,
88
+ loginError,
89
+ };
90
+ };
91
+ const { useQueryCall, useMethodField, useMethodFields, useUpdateCall } = (0, hooks_1.getActorHooks)(callMethod, methodFields);
92
+ return {
93
+ useMethodFields,
94
+ useMethodField,
95
+ useActorStore,
96
+ useAuthStore,
97
+ useQueryCall,
98
+ useUpdateCall,
99
+ useAuthClient,
100
+ };
101
+ };
102
+ exports.createReActor = createReActor;
package/dist/context.d.ts CHANGED
@@ -1,14 +1,12 @@
1
1
  import React, { PropsWithChildren } from "react";
2
- import { ActorSubclass } from "@dfinity/agent";
3
- import { createReActor } from "./index";
2
+ import { type ReActorHooks } from "./index";
4
3
  import { IDL } from "@dfinity/candid";
5
- import { ReActorOptions } from "@ic-reactor/store";
6
- export type ReActor<A> = ReturnType<typeof createReActor<A>>;
7
- export interface ReActorContextType<A = ActorSubclass<any>> extends ReActor<A> {
8
- }
4
+ import { ActorSubclass, AgentManager, CreateReActorOptions } from "@ic-reactor/store";
5
+ export type ReActorContextType<A = ActorSubclass<any>> = ReActorHooks<A>;
9
6
  export declare const ReActorContext: React.Context<ReActorContextType<any> | null>;
10
7
  export declare const useReActor: <A = any>() => ReActorContextType<A>;
11
- interface ReActorProviderProps extends PropsWithChildren, Omit<ReActorOptions, "idlFactory"> {
8
+ interface ReActorProviderProps extends PropsWithChildren, Omit<CreateReActorOptions, "idlFactory"> {
9
+ agentManager?: AgentManager;
12
10
  idlFactory?: IDL.InterfaceFactory;
13
11
  loadingComponent?: React.ReactNode;
14
12
  }
package/dist/context.js CHANGED
@@ -45,12 +45,11 @@ var __rest = (this && this.__rest) || function (s, e) {
45
45
  Object.defineProperty(exports, "__esModule", { value: true });
46
46
  exports.ReActorProvider = exports.useReActor = exports.ReActorContext = void 0;
47
47
  const react_1 = __importStar(require("react"));
48
- const agent_1 = require("@dfinity/agent");
49
48
  const index_1 = require("./index");
50
49
  const store_1 = require("@ic-reactor/store");
51
50
  exports.ReActorContext = (0, react_1.createContext)(null);
52
51
  const useReActor = () => {
53
- const context = react_1.default.useContext(exports.ReActorContext);
52
+ const context = (0, react_1.useContext)(exports.ReActorContext);
54
53
  if (!context) {
55
54
  throw new Error("useReActor must be used within a ReActorProvider");
56
55
  }
@@ -58,14 +57,14 @@ const useReActor = () => {
58
57
  };
59
58
  exports.useReActor = useReActor;
60
59
  const ReActorProvider = (_a) => {
61
- var { children, loadingComponent = react_1.default.createElement("div", null, "Loading...") } = _a, config = __rest(_a, ["children", "loadingComponent"]);
60
+ var { children, canisterId, loadingComponent = react_1.default.createElement("div", null, "Loading...") } = _a, config = __rest(_a, ["children", "canisterId", "loadingComponent"]);
61
+ const agentManager = (0, react_1.useRef)(config.agentManager || (0, store_1.createAgentManager)(config));
62
62
  const [didJs, setDidJS] = (0, react_1.useState)();
63
63
  const fetchDidJs = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
64
- const { canisterId } = config;
65
64
  if (!canisterId) {
66
65
  throw new Error("canisterId is required");
67
66
  }
68
- const agent = new agent_1.HttpAgent(Object.assign({}, config));
67
+ const agent = agentManager.current.getAgent();
69
68
  (0, store_1.getDidJsFromMetadata)(agent, canisterId).then((idlFactory) => __awaiter(void 0, void 0, void 0, function* () {
70
69
  if (!idlFactory) {
71
70
  try {
@@ -87,7 +86,7 @@ const ReActorProvider = (_a) => {
87
86
  setDidJS(idlFactory);
88
87
  }));
89
88
  return didJs;
90
- }), [config.canisterId]);
89
+ }), [canisterId]);
91
90
  (0, react_1.useEffect)(() => {
92
91
  const { idlFactory } = config;
93
92
  if (idlFactory) {
@@ -96,19 +95,23 @@ const ReActorProvider = (_a) => {
96
95
  }
97
96
  fetchDidJs();
98
97
  }, [fetchDidJs, config.idlFactory]);
99
- const reActorHooks = (0, react_1.useMemo)(() => {
98
+ const hooks = (0, react_1.useMemo)(() => {
100
99
  if (!didJs) {
101
100
  return null;
102
101
  }
103
102
  try {
104
- return (0, index_1.createReActor)(Object.assign(Object.assign({}, config), { idlFactory: didJs.idlFactory }));
103
+ return (0, index_1.createReActor)({
104
+ idlFactory: didJs.idlFactory,
105
+ agentManager: agentManager.current,
106
+ canisterId,
107
+ withDevtools: config.withDevtools,
108
+ });
105
109
  }
106
110
  catch (err) {
107
111
  console.error(err);
108
112
  return null;
109
113
  }
110
- }, [config, didJs]);
111
- (0, react_1.useEffect)(() => reActorHooks === null || reActorHooks === void 0 ? void 0 : reActorHooks.unsubscribeAgent, [reActorHooks === null || reActorHooks === void 0 ? void 0 : reActorHooks.unsubscribeAgent]);
112
- return (react_1.default.createElement(exports.ReActorContext.Provider, { value: reActorHooks }, reActorHooks ? children : loadingComponent));
114
+ }, [canisterId, didJs]);
115
+ return (react_1.default.createElement(exports.ReActorContext.Provider, { value: hooks }, hooks === null ? loadingComponent : children));
113
116
  };
114
117
  exports.ReActorProvider = ReActorProvider;
@@ -0,0 +1,26 @@
1
+ import type { ExtractActorMethodArgs, ActorManager, ActorMethodField } from "@ic-reactor/store";
2
+ import type { ReActorUseQueryArgs, ReActorUseUpdateArgs } from "../types";
3
+ export declare const getActorHooks: <A extends unknown>({ methodFields, actorStore, callMethod, }: ActorManager<A>) => {
4
+ useQueryCall: <M extends keyof A>({ autoRefresh, refreshInterval, disableInitialCall, ...rest }: ReActorUseQueryArgs<A, M>) => {
5
+ data: import("@ic-reactor/store").ExtractActorMethodReturnType<A[M]> | undefined;
6
+ error: Error | undefined;
7
+ loading: boolean;
8
+ field: ActorMethodField<A> | undefined;
9
+ call: (replaceArgs?: ExtractActorMethodArgs<A[M]> | undefined) => Promise<import("@ic-reactor/store").ExtractActorMethodReturnType<A[M]> | undefined>;
10
+ };
11
+ useUpdateCall: <M_1 extends keyof A>(args: ReActorUseUpdateArgs<A, M_1>) => {
12
+ data: import("@ic-reactor/store").ExtractActorMethodReturnType<A[M_1]> | undefined;
13
+ error: Error | undefined;
14
+ 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;
17
+ };
18
+ useActorStore: () => {
19
+ initialized: boolean;
20
+ initializing: boolean;
21
+ error: Error | undefined;
22
+ methodState: import("@ic-reactor/store").ActorMethodStates<A>;
23
+ };
24
+ useMethodField: (functionName: keyof A & string) => ActorMethodField<A> | undefined;
25
+ useMethodFields: () => ActorMethodField<A>[];
26
+ };
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __rest = (this && this.__rest) || function (s, e) {
12
+ var t = {};
13
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
+ t[p] = s[p];
15
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
+ t[p[i]] = s[p[i]];
19
+ }
20
+ return t;
21
+ };
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.getActorHooks = void 0;
24
+ const react_1 = require("react");
25
+ const zustand_1 = require("zustand");
26
+ const getActorHooks = ({ methodFields, actorStore, callMethod, }) => {
27
+ const useActorStore = () => {
28
+ const actorState = (0, zustand_1.useStore)(actorStore, (state) => state);
29
+ return Object.assign({}, actorState);
30
+ };
31
+ const useMethodFields = () => {
32
+ return methodFields;
33
+ };
34
+ 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;
40
+ };
41
+ const useReActorCall = ({ onError, onSuccess, onLoading, args = [], functionName, }) => {
42
+ const [state, setState] = (0, react_1.useState)({
43
+ data: undefined,
44
+ error: undefined,
45
+ loading: false,
46
+ });
47
+ const call = (0, react_1.useCallback)((replaceArgs) => __awaiter(void 0, void 0, void 0, function* () {
48
+ onLoading === null || onLoading === void 0 ? void 0 : onLoading(true);
49
+ onError === null || onError === void 0 ? void 0 : onError(undefined);
50
+ setState((prevState) => (Object.assign(Object.assign({}, prevState), { loading: true, error: undefined })));
51
+ try {
52
+ const data = yield callMethod(functionName, ...(replaceArgs !== null && replaceArgs !== void 0 ? replaceArgs : args));
53
+ onLoading === null || onLoading === void 0 ? void 0 : onLoading(false);
54
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
55
+ setState((prevState) => (Object.assign(Object.assign({}, prevState), { data, loading: false })));
56
+ return data;
57
+ }
58
+ catch (error) {
59
+ console.error("Error in call:", error);
60
+ onError === null || onError === void 0 ? void 0 : onError(error);
61
+ onLoading === null || onLoading === void 0 ? void 0 : onLoading(false);
62
+ setState((prevState) => (Object.assign(Object.assign({}, prevState), { error: error, loading: false })));
63
+ }
64
+ }), [args, functionName, onError, onSuccess, onLoading]);
65
+ const field = useMethodField(functionName);
66
+ return Object.assign({ call, field }, state);
67
+ };
68
+ const useQueryCall = (_a) => {
69
+ var { autoRefresh, refreshInterval = 5000, disableInitialCall } = _a, rest = __rest(_a, ["autoRefresh", "refreshInterval", "disableInitialCall"]);
70
+ const _b = useReActorCall(rest), { call } = _b, state = __rest(_b, ["call"]);
71
+ let intervalId = (0, react_1.useRef)(undefined);
72
+ (0, react_1.useEffect)(() => {
73
+ // Auto-refresh logic
74
+ if (autoRefresh) {
75
+ intervalId.current = setInterval(() => {
76
+ call();
77
+ }, refreshInterval);
78
+ }
79
+ // Initial call logic
80
+ if (!disableInitialCall) {
81
+ call();
82
+ }
83
+ return () => {
84
+ clearInterval(intervalId.current);
85
+ };
86
+ }, [disableInitialCall, autoRefresh, refreshInterval]);
87
+ return Object.assign({ call }, state);
88
+ };
89
+ const useUpdateCall = (args) => {
90
+ return useReActorCall(args);
91
+ };
92
+ return {
93
+ useQueryCall,
94
+ useUpdateCall,
95
+ useActorStore,
96
+ useMethodField,
97
+ useMethodFields,
98
+ };
99
+ };
100
+ exports.getActorHooks = getActorHooks;
@@ -0,0 +1,18 @@
1
+ import type { AuthClientLoginOptions } from "@dfinity/auth-client";
2
+ import type { AgentManager } from "@ic-reactor/store";
3
+ export declare const getAuthHooks: ({ authStore, authenticate }: AgentManager) => {
4
+ useAuthStore: () => import("@ic-reactor/store").ActorAuthState;
5
+ useAuthClient: () => {
6
+ authClient: import("@dfinity/auth-client").AuthClient | null;
7
+ authenticated: boolean;
8
+ authenticating: boolean;
9
+ identity: import("@ic-reactor/store").Identity | null;
10
+ login: (options?: AuthClientLoginOptions) => Promise<void>;
11
+ logout: (options?: {
12
+ returnTo?: string;
13
+ }) => Promise<void>;
14
+ authenticate: () => Promise<void>;
15
+ loginLoading: boolean;
16
+ loginError: unknown;
17
+ };
18
+ };
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.getAuthHooks = void 0;
13
+ const react_1 = require("react");
14
+ const zustand_1 = require("zustand");
15
+ const getAuthHooks = ({ authStore, authenticate }) => {
16
+ const useAuthStore = () => {
17
+ const authState = (0, zustand_1.useStore)(authStore, (state) => state);
18
+ return authState;
19
+ };
20
+ const useAuthClient = () => {
21
+ const [loginLoading, setLoginLoading] = (0, react_1.useState)(false);
22
+ const [loginError, setLoginError] = (0, react_1.useState)(null);
23
+ const { authClient, authenticated, authenticating, identity } = useAuthStore();
24
+ const login = (options) => __awaiter(void 0, void 0, void 0, function* () {
25
+ setLoginLoading(true);
26
+ setLoginError(null);
27
+ try {
28
+ yield (authClient === null || authClient === void 0 ? void 0 : authClient.login(Object.assign(Object.assign({}, options), { onSuccess: () => __awaiter(void 0, void 0, void 0, function* () {
29
+ var _a;
30
+ setLoginLoading(false);
31
+ yield authenticate();
32
+ (_a = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _a === void 0 ? void 0 : _a.call(options);
33
+ }), onError: (e) => {
34
+ var _a;
35
+ setLoginError(e);
36
+ setLoginLoading(false);
37
+ (_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, e);
38
+ } })));
39
+ }
40
+ catch (e) {
41
+ setLoginLoading(false);
42
+ setLoginError(e);
43
+ }
44
+ });
45
+ const logout = (options) => __awaiter(void 0, void 0, void 0, function* () {
46
+ yield (authClient === null || authClient === void 0 ? void 0 : authClient.logout(options));
47
+ yield authenticate();
48
+ });
49
+ (0, react_1.useEffect)(() => {
50
+ if (!authClient && !authenticating) {
51
+ authenticate();
52
+ }
53
+ }, [authClient, authenticating]);
54
+ return {
55
+ authClient,
56
+ authenticated,
57
+ authenticating,
58
+ identity,
59
+ login,
60
+ logout,
61
+ authenticate,
62
+ loginLoading,
63
+ loginError,
64
+ };
65
+ };
66
+ return {
67
+ useAuthStore,
68
+ useAuthClient,
69
+ };
70
+ };
71
+ exports.getAuthHooks = getAuthHooks;
@@ -0,0 +1,6 @@
1
+ import type { ActorSubclass } from "@ic-reactor/store";
2
+ import { getAuthHooks } from "./auth";
3
+ import { getActorHooks } from "./actor";
4
+ export type ReActorHooks<A extends ActorSubclass<any>> = ReturnType<typeof getActorHooks<A>> & ReturnType<typeof getAuthHooks>;
5
+ export { getActorHooks } from "./actor";
6
+ export { getAuthHooks } from "./auth";
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAuthHooks = exports.getActorHooks = void 0;
4
+ var actor_1 = require("./actor");
5
+ Object.defineProperty(exports, "getActorHooks", { enumerable: true, get: function () { return actor_1.getActorHooks; } });
6
+ var auth_1 = require("./auth");
7
+ Object.defineProperty(exports, "getAuthHooks", { enumerable: true, get: function () { return auth_1.getAuthHooks; } });
package/dist/hooks.d.ts CHANGED
@@ -1,20 +1,20 @@
1
- import type { CallMethod, ExtractReActorMethodArgs, ReActorActorStore } from "@ic-reactor/store";
1
+ import type { CallActorMethod, ExtractActorMethodArgs, ActorMethodField } from "@ic-reactor/store";
2
2
  import type { ReActorUseQueryArgs, ReActorUseUpdateArgs } from "./types";
3
- export declare const getCallHooks: <A extends unknown>(callMethod: CallMethod<A>, actorStore: ReActorActorStore<A>) => {
3
+ export declare const getActorHooks: <A extends unknown>(callMethod: CallActorMethod<A>, methodFields: ActorMethodField<A>[]) => {
4
4
  useQueryCall: <M extends keyof A>({ autoRefresh, refreshInterval, disableInitialCall, ...rest }: ReActorUseQueryArgs<A, M>) => {
5
- data: import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined;
5
+ data: import("@ic-reactor/store").ExtractActorMethodReturnType<A[M]> | undefined;
6
6
  error: Error | undefined;
7
7
  loading: boolean;
8
- field: import("@ic-reactor/store").ReActorMethodField<A> | undefined;
9
- call: (replaceArgs?: ExtractReActorMethodArgs<A[M]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined>;
8
+ field: ActorMethodField<A> | undefined;
9
+ call: (replaceArgs?: ExtractActorMethodArgs<A[M]> | undefined) => Promise<import("@ic-reactor/store").ExtractActorMethodReturnType<A[M]> | undefined>;
10
10
  };
11
11
  useUpdateCall: <M_1 extends keyof A>(args: ReActorUseUpdateArgs<A, M_1>) => {
12
- data: import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M_1]> | undefined;
12
+ data: import("@ic-reactor/store").ExtractActorMethodReturnType<A[M_1]> | undefined;
13
13
  error: Error | undefined;
14
14
  loading: boolean;
15
- call: (replaceArgs?: ExtractReActorMethodArgs<A[M_1]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M_1]> | undefined>;
16
- field: import("@ic-reactor/store").ReActorMethodField<A> | undefined;
15
+ call: (replaceArgs?: ExtractActorMethodArgs<A[M_1]> | undefined) => Promise<import("@ic-reactor/store").ExtractActorMethodReturnType<A[M_1]> | undefined>;
16
+ field: ActorMethodField<A> | undefined;
17
17
  };
18
- useMethodField: (functionName: keyof A & string) => import("@ic-reactor/store").ReActorMethodField<A> | undefined;
19
- useMethodFields: () => import("@ic-reactor/store").ReActorMethodField<A>[];
18
+ useMethodField: (functionName: keyof A & string) => ActorMethodField<A> | undefined;
19
+ useMethodFields: () => ActorMethodField<A>[];
20
20
  };
package/dist/hooks.js CHANGED
@@ -20,12 +20,11 @@ var __rest = (this && this.__rest) || function (s, e) {
20
20
  return t;
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.getCallHooks = void 0;
23
+ exports.getActorHooks = void 0;
24
24
  const react_1 = require("react");
25
- const zustand_1 = require("zustand");
26
- const getCallHooks = (callMethod, actorStore) => {
25
+ const getActorHooks = (callMethod, methodFields) => {
27
26
  const useMethodFields = () => {
28
- return (0, zustand_1.useStore)(actorStore, (state) => state.methodFields);
27
+ return methodFields;
29
28
  };
30
29
  const useMethodField = (functionName) => {
31
30
  const methodFields = useMethodFields();
@@ -92,4 +91,4 @@ const getCallHooks = (callMethod, actorStore) => {
92
91
  useMethodFields,
93
92
  };
94
93
  };
95
- exports.getCallHooks = getCallHooks;
94
+ exports.getActorHooks = getActorHooks;
package/dist/index.d.ts CHANGED
@@ -1,49 +1,7 @@
1
- import type { AuthClientLoginOptions } from "@dfinity/auth-client";
2
- import type { ReActorOptions } from "@ic-reactor/store";
1
+ import type { ActorSubclass, CreateReActorOptions } from "@ic-reactor/store";
2
+ import { getActorHooks } from "./hooks/actor";
3
+ import { getAuthHooks } from "./hooks/auth";
3
4
  export * from "./context";
4
- export declare const createReActor: <A extends unknown>(options: ReActorOptions) => {
5
- unsubscribeAgent: () => void;
6
- useMethodFields: () => import("@ic-reactor/store").ReActorMethodField<A>[];
7
- useMethodField: (functionName: keyof A & string) => import("@ic-reactor/store").ReActorMethodField<A> | undefined;
8
- useAgentStore: () => {
9
- initializeAgent: (agentOptions?: import("@dfinity/agent").HttpAgentOptions | undefined, isLocal?: boolean | undefined) => Promise<void>;
10
- agent: import("@ic-reactor/store").HttpAgent | undefined;
11
- canisterId: import("@ic-reactor/store").CanisterId;
12
- };
13
- useActorStore: () => {
14
- actor: A | null;
15
- initialized: boolean;
16
- initializing: boolean;
17
- error: Error | undefined;
18
- methodState: import("@ic-reactor/store").ReActorMethodStates<A>;
19
- methodFields: import("@ic-reactor/store").ReActorMethodField<A>[];
20
- };
21
- useAuthStore: () => import("@ic-reactor/store").ReActorAuthState<A>;
22
- useQueryCall: <M extends keyof A>({ autoRefresh, refreshInterval, disableInitialCall, ...rest }: import("./types").ReActorUseQueryArgs<A, M>) => {
23
- data: import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined;
24
- error: Error | undefined;
25
- loading: boolean;
26
- field: import("@ic-reactor/store").ReActorMethodField<A> | undefined;
27
- call: (replaceArgs?: import("@ic-reactor/store").ExtractReActorMethodArgs<A[M]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined>;
28
- };
29
- useUpdateCall: <M_1 extends keyof A>(args: import("./types").ReActorUseUpdateArgs<A, M_1>) => {
30
- data: import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M_1]> | undefined;
31
- error: Error | undefined;
32
- loading: boolean;
33
- call: (replaceArgs?: import("@ic-reactor/store").ExtractReActorMethodArgs<A[M_1]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M_1]> | undefined>;
34
- field: import("@ic-reactor/store").ReActorMethodField<A> | undefined;
35
- };
36
- useAuthClient: () => {
37
- authClient: import("@dfinity/auth-client").AuthClient | null;
38
- authenticated: boolean;
39
- authenticating: boolean;
40
- identity: import("@ic-reactor/store").Identity | null;
41
- login: (options?: AuthClientLoginOptions) => Promise<void>;
42
- logout: (options?: {
43
- returnTo?: string;
44
- }) => Promise<void>;
45
- authenticate: () => Promise<void>;
46
- loginLoading: boolean;
47
- loginError: unknown;
48
- };
49
- };
5
+ export type ReActorHooks<A extends ActorSubclass<any>> = ReturnType<typeof getActorHooks<A>> & ReturnType<typeof getAuthHooks>;
6
+ export type CreateReactorOptions = Omit<CreateReActorOptions, "isLocal">;
7
+ export declare const createReActor: <A extends unknown>(options: Omit<CreateReActorOptions, "isLocal">) => ReActorHooks<A>;
package/dist/index.js CHANGED
@@ -13,91 +13,22 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
13
13
  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
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
- return new (P || (P = Promise))(function (resolve, reject) {
19
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
- step((generator = generator.apply(thisArg, _arguments || [])).next());
23
- });
24
- };
25
16
  Object.defineProperty(exports, "__esModule", { value: true });
26
17
  exports.createReActor = void 0;
27
18
  const store_1 = require("@ic-reactor/store");
28
- const react_1 = require("react");
29
- const zustand_1 = require("zustand");
30
- const hooks_1 = require("./hooks");
19
+ const actor_1 = require("./hooks/actor");
20
+ const auth_1 = require("./hooks/auth");
31
21
  __exportStar(require("./context"), exports);
32
22
  const createReActor = (options) => {
33
23
  const isLocal = typeof process !== "undefined" &&
34
24
  (process.env.NODE_ENV === "development" ||
35
25
  process.env.DFX_NETWORK === "local");
36
- const { unsubscribeAgent, initializeAgent, authenticate, callMethod, authStore, agentStore, actorStore, } = (0, store_1.createReActorStore)(Object.assign({ isLocal }, options));
37
- const useAgentStore = () => {
38
- const agentState = (0, zustand_1.useStore)(agentStore, (state) => state);
39
- return Object.assign(Object.assign({}, agentState), { initializeAgent });
40
- };
41
- const useActorStore = () => {
42
- const actorState = (0, zustand_1.useStore)(actorStore, (state) => state);
43
- return Object.assign({}, actorState);
44
- };
45
- const useAuthStore = () => {
46
- const authState = (0, zustand_1.useStore)(authStore, (state) => state);
47
- return authState;
48
- };
49
- const useAuthClient = () => {
50
- const [loginLoading, setLoginLoading] = (0, react_1.useState)(false);
51
- const [loginError, setLoginError] = (0, react_1.useState)(null);
52
- const { authClient, authenticated, authenticating, identity } = useAuthStore();
53
- const login = (options) => __awaiter(void 0, void 0, void 0, function* () {
54
- setLoginLoading(true);
55
- setLoginError(null);
56
- try {
57
- yield (authClient === null || authClient === void 0 ? void 0 : authClient.login(Object.assign(Object.assign({}, options), { onSuccess: () => __awaiter(void 0, void 0, void 0, function* () {
58
- var _a;
59
- setLoginLoading(false);
60
- yield authenticate();
61
- (_a = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _a === void 0 ? void 0 : _a.call(options);
62
- }), onError: (e) => {
63
- var _a;
64
- setLoginError(e);
65
- setLoginLoading(false);
66
- (_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, e);
67
- } })));
68
- }
69
- catch (e) {
70
- setLoginLoading(false);
71
- setLoginError(e);
72
- }
73
- });
74
- const logout = (options) => __awaiter(void 0, void 0, void 0, function* () {
75
- yield (authClient === null || authClient === void 0 ? void 0 : authClient.logout(options));
76
- yield authenticate();
77
- });
78
- (0, react_1.useEffect)(() => {
79
- if (!authClient && !authenticating) {
80
- authenticate();
81
- }
82
- }, [authClient, authenticating]);
83
- return {
84
- authClient,
85
- authenticated,
86
- authenticating,
87
- identity,
88
- login,
89
- logout,
90
- authenticate,
91
- loginLoading,
92
- loginError,
93
- };
94
- };
95
- const { useQueryCall, useMethodField, useMethodFields, useUpdateCall } = (0, hooks_1.getCallHooks)(callMethod, actorStore);
26
+ const actorManager = (0, store_1.createReActorStore)(Object.assign({ isLocal }, options));
27
+ const { useAuthClient, useAuthStore } = (0, auth_1.getAuthHooks)(actorManager.agentManager);
28
+ const { useActorStore, useQueryCall, useMethodField, useMethodFields, useUpdateCall, } = (0, actor_1.getActorHooks)(actorManager);
96
29
  return {
97
- unsubscribeAgent,
98
30
  useMethodFields,
99
31
  useMethodField,
100
- useAgentStore,
101
32
  useActorStore,
102
33
  useAuthStore,
103
34
  useQueryCall,
package/dist/types.d.ts CHANGED
@@ -1,14 +1,14 @@
1
- import { ExtractReActorMethodArgs, ExtractReActorMethodReturnType } from "@ic-reactor/store";
2
- export * from "@ic-reactor/store";
1
+ import type { ExtractActorMethodArgs, ExtractActorMethodReturnType } from "@ic-reactor/store";
2
+ export type * from "@ic-reactor/store";
3
3
  export type ReActorCallArgs<A, M extends keyof A> = {
4
4
  functionName: M & string;
5
- args?: ExtractReActorMethodArgs<A[M]>;
5
+ args?: ExtractActorMethodArgs<A[M]>;
6
6
  onLoading?: (loading: boolean) => void;
7
7
  onError?: (error: Error | unknown) => void;
8
- onSuccess?: (data: ExtractReActorMethodReturnType<A[M]> | undefined) => void;
8
+ onSuccess?: (data: ExtractActorMethodReturnType<A[M]> | undefined) => void;
9
9
  };
10
10
  export type ReActorHookState<A, M extends keyof A> = {
11
- data: ExtractReActorMethodReturnType<A[M]> | undefined;
11
+ data: ExtractActorMethodReturnType<A[M]> | undefined;
12
12
  error: Error | undefined;
13
13
  loading: boolean;
14
14
  };
package/dist/types.js CHANGED
@@ -1,17 +1,2 @@
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 __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
2
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("@ic-reactor/store"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/react",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
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.0",
38
+ "@ic-reactor/store": "^0.3.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": "00b0d7f282c144151f27d344c47d92b9b446b14a"
51
+ "gitHead": "c1204b00b5faca2d75911a684fdbc1e341f610a4"
52
52
  }