@ic-reactor/core 1.0.4 → 1.0.6

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
@@ -203,7 +203,7 @@ const { agentManager, callMethod } = createReactorStore<Candid>({
203
203
 
204
204
  // Usage example
205
205
  await agentManager.authenticate()
206
- const authClient = agentManager.getAuthClient()
206
+ const authClient = agentManager.getAuth()
207
207
 
208
208
  authClient?.login({
209
209
  onSuccess: () => {
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.ActorManager = void 0;
13
13
  /* eslint-disable no-console */
14
14
  const agent_1 = require("@dfinity/agent");
15
- const helper_1 = require("../../tools/helper");
15
+ const helper_1 = require("../../utils/helper");
16
16
  const candid_1 = require("@dfinity/candid");
17
17
  class ActorManager {
18
18
  constructor(actorConfig) {
@@ -92,10 +92,9 @@ class ActorManager {
92
92
  return this.actorStore.setState(updater);
93
93
  };
94
94
  const { agentManager, canisterId, idlFactory, withVisitor = false, withDevtools = false, initializeOnCreate = true, } = actorConfig;
95
- this._agentManager = agentManager;
96
- this._agentManager.subscribeAgent(this.initializeActor);
97
95
  this.canisterId = canisterId;
98
96
  this._idlFactory = idlFactory;
97
+ this._agentManager = agentManager;
99
98
  if (withVisitor) {
100
99
  this.visitFunction = withVisitor ? this.extractService() : emptyVisitor;
101
100
  }
@@ -107,9 +106,7 @@ class ActorManager {
107
106
  withDevtools,
108
107
  store: `actor-${String(canisterId)}`,
109
108
  });
110
- if (initializeOnCreate) {
111
- this.initializeActor(agentManager.getAgent());
112
- }
109
+ this._agentManager.subscribeAgent(this.initializeActor, initializeOnCreate);
113
110
  }
114
111
  extractService() {
115
112
  return this._idlFactory({ IDL: candid_1.IDL })._fields.reduce((acc, service) => {
@@ -6,7 +6,7 @@ export interface DefaultActorType {
6
6
  [key: string]: ActorMethod;
7
7
  }
8
8
  export type BaseActor<T = DefaultActorType> = ActorSubclass<T>;
9
- export type FunctionName<A = BaseActor> = keyof A & string;
9
+ export type FunctionName<A = BaseActor> = Extract<keyof A, string>;
10
10
  export type FunctionType = "query" | "update";
11
11
  export type CanisterId = string | Principal;
12
12
  export interface ActorManagerParameters {
@@ -1,7 +1,8 @@
1
1
  import { HttpAgent } from "@dfinity/agent";
2
- import type { AgentStore, AgentManagerParameters, UpdateAgentParameters, AuthStore } from "./types";
2
+ import type { AgentStore, AgentManagerParameters, UpdateAgentParameters, AuthStore, AuthClient } from "./types";
3
3
  export declare class AgentManager {
4
4
  private _agent;
5
+ private _auth;
5
6
  private _subscribers;
6
7
  agentStore: AgentStore;
7
8
  authStore: AuthStore;
@@ -12,7 +13,7 @@ export declare class AgentManager {
12
13
  private updateAuthState;
13
14
  constructor(options?: AgentManagerParameters);
14
15
  private initializeAgent;
15
- subscribeAgent: (callback: (agent: HttpAgent) => void) => () => void;
16
+ subscribeAgent: (callback: (agent: HttpAgent) => void, initialize?: boolean) => () => void;
16
17
  unsubscribeAgent: (callback: (agent: HttpAgent) => void) => void;
17
18
  private notifySubscribers;
18
19
  updateAgent: (options?: UpdateAgentParameters) => Promise<void>;
@@ -22,7 +23,7 @@ export declare class AgentManager {
22
23
  subscribeAgentState: AgentStore["subscribe"];
23
24
  getAuthState: AuthStore["getState"];
24
25
  subscribeAuthState: AuthStore["subscribe"];
25
- getAuthClient: () => import("@dfinity/auth-client").AuthClient | null;
26
+ getAuth: () => AuthClient | null;
26
27
  getIdentity: () => import("@dfinity/agent").Identity | null;
27
28
  getPrincipal: () => import("@dfinity/principal").Principal | null;
28
29
  }
@@ -45,10 +45,11 @@ var __rest = (this && this.__rest) || function (s, e) {
45
45
  Object.defineProperty(exports, "__esModule", { value: true });
46
46
  exports.AgentManager = void 0;
47
47
  const agent_1 = require("@dfinity/agent");
48
- const helper_1 = require("../../tools/helper");
49
- const constants_1 = require("../../tools/constants");
48
+ const helper_1 = require("../../utils/helper");
49
+ const constants_1 = require("../../utils/constants");
50
50
  class AgentManager {
51
51
  constructor(options) {
52
+ this._auth = null;
52
53
  this._subscribers = [];
53
54
  this.initialAgentState = {
54
55
  initialized: false,
@@ -57,7 +58,6 @@ class AgentManager {
57
58
  };
58
59
  this.initialAuthState = {
59
60
  identity: null,
60
- authClient: null,
61
61
  authenticating: false,
62
62
  authenticated: false,
63
63
  error: undefined,
@@ -80,7 +80,10 @@ class AgentManager {
80
80
  }
81
81
  }
82
82
  });
83
- this.subscribeAgent = (callback) => {
83
+ this.subscribeAgent = (callback, initialize = true) => {
84
+ if (initialize) {
85
+ callback(this._agent);
86
+ }
84
87
  this._subscribers.push(callback);
85
88
  return () => this.unsubscribeAgent(callback);
86
89
  };
@@ -110,13 +113,12 @@ class AgentManager {
110
113
  console.error("Failed to import @dfinity/auth-client:", error);
111
114
  throw new Error("Authentication failed: @dfinity/auth-client package is missing.");
112
115
  });
113
- const authClient = yield AuthClient.create();
114
- const authenticated = yield authClient.isAuthenticated();
115
- const identity = authClient.getIdentity();
116
+ this._auth = yield AuthClient.create();
117
+ const authenticated = yield this._auth.isAuthenticated();
118
+ const identity = this._auth.getIdentity();
116
119
  this._agent.replaceIdentity(identity);
117
120
  this.notifySubscribers();
118
121
  this.updateAuthState({
119
- authClient,
120
122
  authenticated,
121
123
  identity,
122
124
  authenticating: false,
@@ -145,8 +147,8 @@ class AgentManager {
145
147
  this.subscribeAuthState = (listener) => {
146
148
  return this.authStore.subscribe(listener);
147
149
  };
148
- this.getAuthClient = () => {
149
- return this.authStore.getState().authClient;
150
+ this.getAuth = () => {
151
+ return this._auth;
150
152
  };
151
153
  this.getIdentity = () => {
152
154
  return this.authStore.getState().identity;
@@ -14,7 +14,6 @@ export interface AgentState {
14
14
  }
15
15
  export interface AuthState {
16
16
  identity: Identity | null;
17
- authClient: AuthClient | null;
18
17
  authenticating: boolean;
19
18
  authenticated: boolean;
20
19
  error: Error | undefined;
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.CandidAdapter = void 0;
13
13
  const agent_1 = require("@dfinity/agent");
14
14
  const principal_1 = require("@dfinity/principal");
15
- const constants_1 = require("../../tools/constants");
15
+ const constants_1 = require("../../utils/constants");
16
16
  class CandidAdapter {
17
17
  constructor({ agentManager, agent, didjsCanisterId, }) {
18
18
  if (agent) {
@@ -0,0 +1,12 @@
1
+ import { ActorManager } from "./classes/actor";
2
+ import { ActorManagerParameters, BaseActor } from "./types";
3
+ /**
4
+ * Actor manager handles the lifecycle of the actors.
5
+ * It is responsible for creating and managing the actors.
6
+ * You can use it to call and visit the actor's methods.
7
+ * It also provides a way to interact with the actor's state.
8
+ *
9
+ * @category Main
10
+ * @includeExample ./packages/core/README.md:262-277
11
+ */
12
+ export declare const createActorManager: <A = BaseActor>(config: ActorManagerParameters) => ActorManager<A>;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createActorManager = void 0;
4
+ const actor_1 = require("./classes/actor");
5
+ /**
6
+ * Actor manager handles the lifecycle of the actors.
7
+ * It is responsible for creating and managing the actors.
8
+ * You can use it to call and visit the actor's methods.
9
+ * It also provides a way to interact with the actor's state.
10
+ *
11
+ * @category Main
12
+ * @includeExample ./packages/core/README.md:262-277
13
+ */
14
+ const createActorManager = (config) => {
15
+ return new actor_1.ActorManager(config);
16
+ };
17
+ exports.createActorManager = createActorManager;
@@ -0,0 +1,12 @@
1
+ import { AgentManager } from "./classes/agent";
2
+ import { AgentManagerParameters } from "./types";
3
+ /**
4
+ * Agent manager handles the lifecycle of the `@dfinity/agent`.
5
+ * It is responsible for creating agent and managing the agent's state.
6
+ * You can use it to subscribe to the agent changes.
7
+ * login and logout to the internet identity.
8
+ *
9
+ * @category Main
10
+ * @includeExample ./packages/core/README.md:226-254
11
+ */
12
+ export declare const createAgentManager: (config?: AgentManagerParameters) => AgentManager;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createAgentManager = void 0;
4
+ const agent_1 = require("./classes/agent");
5
+ /**
6
+ * Agent manager handles the lifecycle of the `@dfinity/agent`.
7
+ * It is responsible for creating agent and managing the agent's state.
8
+ * You can use it to subscribe to the agent changes.
9
+ * login and logout to the internet identity.
10
+ *
11
+ * @category Main
12
+ * @includeExample ./packages/core/README.md:226-254
13
+ */
14
+ const createAgentManager = (config) => {
15
+ return new agent_1.AgentManager(config);
16
+ };
17
+ exports.createAgentManager = createAgentManager;
@@ -0,0 +1,11 @@
1
+ import { CandidAdapter } from "./classes/candid";
2
+ import { CandidAdapterParameters } from "./types";
3
+ /**
4
+ * The `CandidAdapter` class is used to interact with a canister and retrieve its Candid interface definition.
5
+ * It provides methods to fetch the Candid definition either from the canister's metadata or by using a temporary hack method.
6
+ * If both methods fail, it throws an error.
7
+ *
8
+ * @category Main
9
+ * @includeExample ./packages/core/README.md:145-186
10
+ */
11
+ export declare const createCandidAdapter: (config: CandidAdapterParameters) => CandidAdapter;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createCandidAdapter = void 0;
4
+ const candid_1 = require("./classes/candid");
5
+ /**
6
+ * The `CandidAdapter` class is used to interact with a canister and retrieve its Candid interface definition.
7
+ * It provides methods to fetch the Candid definition either from the canister's metadata or by using a temporary hack method.
8
+ * If both methods fail, it throws an error.
9
+ *
10
+ * @category Main
11
+ * @includeExample ./packages/core/README.md:145-186
12
+ */
13
+ const createCandidAdapter = (config) => {
14
+ return new candid_1.CandidAdapter(config);
15
+ };
16
+ exports.createCandidAdapter = createCandidAdapter;
@@ -21,9 +21,9 @@ var __rest = (this && this.__rest) || function (s, e) {
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
23
  exports.createReactorCore = void 0;
24
- const constants_1 = require("./tools/constants");
25
- const tools_1 = require("./tools");
26
- const store_1 = require("./store");
24
+ const constants_1 = require("./utils/constants");
25
+ const utils_1 = require("./utils");
26
+ const createReactorStore_1 = require("./createReactorStore");
27
27
  /**
28
28
  * The Core module is the main entry point for the library.
29
29
  * Create a new actor manager with the given options.
@@ -33,9 +33,9 @@ const store_1 = require("./store");
33
33
  * @includeExample ./packages/core/README.md:26-80
34
34
  */
35
35
  const createReactorCore = (config) => {
36
- const _a = (0, store_1.createReactorStore)(config), { subscribeActorState, updateMethodState, callMethod, getState, agentManager } = _a, rest = __rest(_a, ["subscribeActorState", "updateMethodState", "callMethod", "getState", "agentManager"]);
36
+ const _a = (0, createReactorStore_1.createReactorStore)(config), { subscribeActorState, updateMethodState, callMethod, getState, agentManager } = _a, rest = __rest(_a, ["subscribeActorState", "updateMethodState", "callMethod", "getState", "agentManager"]);
37
37
  const actorMethod = (functionName, ...args) => {
38
- const requestHash = (0, tools_1.generateRequestHash)(args);
38
+ const requestHash = (0, utils_1.generateRequestHash)(args);
39
39
  const updateState = (newState = {}) => {
40
40
  updateMethodState(functionName, requestHash, newState);
41
41
  };
@@ -55,6 +55,7 @@ const createReactorCore = (config) => {
55
55
  }
56
56
  });
57
57
  const subscribe = (callback) => {
58
+ callback(methodState());
58
59
  const unsubscribe = subscribeActorState((state) => {
59
60
  const methodState = state.methodState[functionName];
60
61
  const methodStateHash = methodState[requestHash];
@@ -114,7 +115,7 @@ const createReactorCore = (config) => {
114
115
  return actorMethod(functionName, ...args);
115
116
  };
116
117
  const login = (options) => __awaiter(void 0, void 0, void 0, function* () {
117
- const authClient = agentManager.getAuthClient();
118
+ const authClient = agentManager.getAuth();
118
119
  if (!authClient) {
119
120
  yield agentManager.authenticate();
120
121
  }
@@ -122,11 +123,11 @@ const createReactorCore = (config) => {
122
123
  throw new Error("Auth client not initialized");
123
124
  }
124
125
  yield authClient.login(Object.assign({ identityProvider: agentManager.isLocalEnv
125
- ? constants_1.IC_INTERNET_IDENTITY_PROVIDER
126
- : constants_1.LOCAL_INTERNET_IDENTITY_PROVIDER }, options));
126
+ ? constants_1.LOCAL_INTERNET_IDENTITY_PROVIDER
127
+ : constants_1.IC_INTERNET_IDENTITY_PROVIDER }, options));
127
128
  });
128
129
  const logout = (options) => __awaiter(void 0, void 0, void 0, function* () {
129
- const authClient = agentManager.getAuthClient();
130
+ const authClient = agentManager.getAuth();
130
131
  if (!authClient) {
131
132
  throw new Error("Auth client not initialized");
132
133
  }
@@ -12,8 +12,9 @@ var __rest = (this && this.__rest) || function (s, e) {
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.createReactorStore = void 0;
15
- const other_1 = require("./other");
16
- const tools_1 = require("./tools");
15
+ const utils_1 = require("./utils");
16
+ const createActorManager_1 = require("./createActorManager");
17
+ const createAgentManager_1 = require("./createAgentManager");
17
18
  /**
18
19
  * Create a new actor manager with the given options.
19
20
  * Its create a new agent manager if not provided.
@@ -24,13 +25,13 @@ const tools_1 = require("./tools");
24
25
  */
25
26
  const createReactorStore = (config) => {
26
27
  const isLocalEnv = config.withProcessEnv
27
- ? (0, tools_1.isInLocalOrDevelopment)()
28
+ ? (0, utils_1.isInLocalOrDevelopment)()
28
29
  : undefined;
29
30
  const { idlFactory, canisterId, withDevtools = false, initializeOnCreate = true, withVisitor = false, agentManager: maybeAgentManager } = config, agentParameters = __rest(config, ["idlFactory", "canisterId", "withDevtools", "initializeOnCreate", "withVisitor", "agentManager"]);
30
31
  const agentManager = maybeAgentManager ||
31
- (0, other_1.createAgentManager)(Object.assign({ withDevtools,
32
+ (0, createAgentManager_1.createAgentManager)(Object.assign({ withDevtools,
32
33
  isLocalEnv }, agentParameters));
33
- const actorManager = (0, other_1.createActorManager)({
34
+ const actorManager = (0, createActorManager_1.createActorManager)({
34
35
  idlFactory,
35
36
  canisterId,
36
37
  agentManager,
package/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
- export * from "./main";
2
- export * from "./store";
3
- export * from "./other";
1
+ export * from "./createReactorCore";
2
+ export * from "./createReactorStore";
3
+ export * from "./createCandidAdapter";
4
+ export * from "./createActorManager";
5
+ export * from "./createAgentManager";
4
6
  export * as classes from "./classes";
5
7
  export * as types from "./types";
6
- export * as tools from "./tools";
8
+ export * as utils from "./utils";
package/dist/index.js CHANGED
@@ -26,10 +26,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
26
26
  return result;
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.tools = exports.types = exports.classes = void 0;
30
- __exportStar(require("./main"), exports);
31
- __exportStar(require("./store"), exports);
32
- __exportStar(require("./other"), exports);
29
+ exports.utils = exports.types = exports.classes = void 0;
30
+ __exportStar(require("./createReactorCore"), exports);
31
+ __exportStar(require("./createReactorStore"), exports);
32
+ __exportStar(require("./createCandidAdapter"), exports);
33
+ __exportStar(require("./createActorManager"), exports);
34
+ __exportStar(require("./createAgentManager"), exports);
33
35
  exports.classes = __importStar(require("./classes"));
34
36
  exports.types = __importStar(require("./types"));
35
- exports.tools = __importStar(require("./tools"));
37
+ exports.utils = __importStar(require("./utils"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/core",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "A library for intracting with the Internet Computer canisters",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -43,5 +43,5 @@
43
43
  "engines": {
44
44
  "node": ">=10"
45
45
  },
46
- "gitHead": "6b8947ab2611bf7af4f0fadaa33a1fa231cfc26b"
46
+ "gitHead": "bf69ae06def96cd40814a72535325f7c7a8a6b5a"
47
47
  }
package/dist/other.d.ts DELETED
@@ -1,33 +0,0 @@
1
- import { ActorManager } from "./classes/actor";
2
- import { AgentManager } from "./classes/agent";
3
- import { CandidAdapter } from "./classes/candid";
4
- import { CandidAdapterParameters, ActorManagerParameters, AgentManagerParameters, BaseActor } from "./types";
5
- /**
6
- * The `CandidAdapter` class is used to interact with a canister and retrieve its Candid interface definition.
7
- * It provides methods to fetch the Candid definition either from the canister's metadata or by using a temporary hack method.
8
- * If both methods fail, it throws an error.
9
- *
10
- * @category Main
11
- * @includeExample ./packages/core/README.md:145-186
12
- */
13
- export declare const createCandidAdapter: (config: CandidAdapterParameters) => CandidAdapter;
14
- /**
15
- * Agent manager handles the lifecycle of the `@dfinity/agent`.
16
- * It is responsible for creating agent and managing the agent's state.
17
- * You can use it to subscribe to the agent changes.
18
- * login and logout to the internet identity.
19
- *
20
- * @category Main
21
- * @includeExample ./packages/core/README.md:226-254
22
- */
23
- export declare const createAgentManager: (config?: AgentManagerParameters) => AgentManager;
24
- /**
25
- * Actor manager handles the lifecycle of the actors.
26
- * It is responsible for creating and managing the actors.
27
- * You can use it to call and visit the actor's methods.
28
- * It also provides a way to interact with the actor's state.
29
- *
30
- * @category Main
31
- * @includeExample ./packages/core/README.md:262-277
32
- */
33
- export declare const createActorManager: <A = BaseActor>(config: ActorManagerParameters) => ActorManager<A>;
package/dist/other.js DELETED
@@ -1,44 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createActorManager = exports.createAgentManager = exports.createCandidAdapter = void 0;
4
- const actor_1 = require("./classes/actor");
5
- const agent_1 = require("./classes/agent");
6
- const candid_1 = require("./classes/candid");
7
- /**
8
- * The `CandidAdapter` class is used to interact with a canister and retrieve its Candid interface definition.
9
- * It provides methods to fetch the Candid definition either from the canister's metadata or by using a temporary hack method.
10
- * If both methods fail, it throws an error.
11
- *
12
- * @category Main
13
- * @includeExample ./packages/core/README.md:145-186
14
- */
15
- const createCandidAdapter = (config) => {
16
- return new candid_1.CandidAdapter(config);
17
- };
18
- exports.createCandidAdapter = createCandidAdapter;
19
- /**
20
- * Agent manager handles the lifecycle of the `@dfinity/agent`.
21
- * It is responsible for creating agent and managing the agent's state.
22
- * You can use it to subscribe to the agent changes.
23
- * login and logout to the internet identity.
24
- *
25
- * @category Main
26
- * @includeExample ./packages/core/README.md:226-254
27
- */
28
- const createAgentManager = (config) => {
29
- return new agent_1.AgentManager(config);
30
- };
31
- exports.createAgentManager = createAgentManager;
32
- /**
33
- * Actor manager handles the lifecycle of the actors.
34
- * It is responsible for creating and managing the actors.
35
- * You can use it to call and visit the actor's methods.
36
- * It also provides a way to interact with the actor's state.
37
- *
38
- * @category Main
39
- * @includeExample ./packages/core/README.md:262-277
40
- */
41
- const createActorManager = (config) => {
42
- return new actor_1.ActorManager(config);
43
- };
44
- exports.createActorManager = createActorManager;
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes