@ic-reactor/core 1.0.1 → 1.0.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.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- The `@ic-reactor/core` package provides a streamlined way to interact with the Internet Computer (IC) by simplifying agent and actor management. It offers utilities for creating and managing IC agents, enabling seamless communication with canisters through a friendly API.
1
+ The `@ic-reactor/core` package provides a streamlined way to interact with the Internet Computer (IC). It simplifies agent and actor management, ensuring type-safe communication with canisters. This package offers utilities for creating and managing IC agents, enabling seamless interaction through a friendly API.
2
2
 
3
3
  ## Installation
4
4
 
@@ -16,111 +16,92 @@ npm install @ic-reactor/core
16
16
  yarn add @ic-reactor/core
17
17
  ```
18
18
 
19
- ## Usage
19
+ ### Using `createReactorCore`
20
20
 
21
- `@ic-reactor/core` can be utilized in two primary ways: automatic agent creation and manual agent management. Below are examples of both approaches to suit your project's needs.
22
-
23
- ### Automatic Agent Creation
24
-
25
- For ease of use, the `createReActorStore` factory function automatically sets up a new ReActor store instance, managing the agent and its state internally.
21
+ For ease of use, the `createReactorCore` factory function automatically sets up a new Reactor instance, managing the agent and its state internally, and providing a simple API for authenticating, querying, and updating actors.
26
22
 
27
23
  **Example:**
28
24
 
29
25
  ```typescript
30
- import { createReActorStore } from "@ic-reactor/core"
31
- import { candid, canisterId, idlFactory } from "./candid"
26
+ import { createReactorCore } from "@ic-reactor/core"
27
+ import { candid, canisterId, idlFactory } from "./declarations/candid"
32
28
 
33
29
  type Candid = typeof candid
34
30
 
35
- const { callMethod, authenticate } = createReActor<Candid>({
36
- canisterId,
37
- idlFactory,
38
- })
39
-
40
- // Usage example
41
- const identity = await authenticate()
42
- const data = await callMethod("version")
43
- console.log(data)
44
- ```
45
-
46
- ### Manual Agent Creation
47
-
48
- If you require more control over the agent's lifecycle or configuration, `@ic-reactor/core` provides the `createAgentManager` function for manual agent instantiation.
49
-
50
- **IC Agent Example:**
51
-
52
- ```typescript
53
- // agent.ts
54
- import { createAgentManager } from "@ic-reactor/core"
55
-
56
- export const agentManager = createAgentManager() // Connects to IC network by default
57
-
58
- // Usage example
59
- await agentManager.authenticate()
60
- // Then use the store to access the authClient, identity, and more...
61
- const { authClient, identity, authenticating } =
62
- agentManager.authStore.getState()
31
+ const { queryCall, updateCall, getPrincipal, login } =
32
+ createReactorCore<Candid>({
33
+ canisterId,
34
+ idlFactory,
35
+ withProcessEnv: true, // will use process.env.DFX_NETWORK
36
+ })
63
37
  ```
64
38
 
65
- **Local Agent Example:**
66
-
67
- For development purposes, you might want to connect to a local instance of the IC network:
39
+ You can find All available methods are returned from the `createReactorCore` function here: [ReactorCore](https://b3pay.github.io/ic-reactor/interfaces/core.types.ReactorCore.html)
68
40
 
69
41
  ```typescript
70
- // agent.ts
71
- import { createAgentManager } from "@ic-reactor/core"
72
-
73
- export const agentManager = createAgentManager({
74
- isLocalEnv: true,
75
- port: 8000, // Default port is 4943
42
+ // later in your code
43
+ await login({
44
+ onSuccess: () => {
45
+ console.log("Logged in successfully")
46
+ },
47
+ onError: (error) => {
48
+ console.error("Failed to login:", error)
49
+ },
76
50
  })
77
- ```
78
51
 
79
- Alternatively, you can specify a host directly:
80
-
81
- ```typescript
82
- export const agentManager = createAgentManager({
83
- host: "http://localhost:8000",
52
+ // queryCall, will automatically call and return a promise with the result
53
+ const { dataPromise, call } = queryCall({
54
+ functionName: "icrc1_balance_of",
55
+ args: [{ owner: getPrincipal(), subaccount: [] }],
84
56
  })
85
- ```
86
-
87
- ### Creating an Actor Manager
88
-
89
- Once you have an agent manager, use `createActorManager` to instantiate an actor manager for calling methods on your canisters.
90
-
91
- ```typescript
92
- // actor.ts
93
- import { createActorManager } from "@ic-reactor/core"
94
- import { candid, canisterId, idlFactory } from "./candid"
95
- import { agentManager } from "./agent"
96
57
 
97
- type Candid = typeof candid
98
-
99
- const candidActor = createActorManager<Candid>({
100
- agentManager,
101
- canisterId,
102
- idlFactory,
58
+ console.log(await dataPromise)
59
+
60
+ // updateCall
61
+ const { call, subscribe } = updateCall({
62
+ functionName: "icrc1_transfer",
63
+ args: [
64
+ {
65
+ to: { owner: getPrincipal(), subaccount: [] },
66
+ amount: BigInt(10000000000),
67
+ fee: [],
68
+ memo: [],
69
+ created_at_time: [],
70
+ from_subaccount: [],
71
+ },
72
+ ],
73
+ })
74
+ // subscribe to the update call
75
+ subscribe(({ loading, error, data }) => {
76
+ console.log({ loading, error, data })
103
77
  })
104
78
 
105
- // Usage example
106
- const data = await candidActor.callMethod("version")
107
- console.log(data)
79
+ const result = await call()
80
+ console.log(result)
108
81
  ```
109
82
 
110
83
  ### Managing Multiple Actors
111
84
 
112
- When interacting with multiple canisters using `@ic-reactor/core`, you can create separate actor managers for each canister. This enables modular interaction with different services on the Internet Computer. Here's how to adjust the example to handle methods that require multiple arguments:
85
+ When interacting with multiple canisters using `@ic-reactor/core`, you need one agent manager for each canister. This way, you can create separate reactor for each canister. This enables modular interaction with different services on the Internet Computer,
86
+ and allows you to manage the state of each actor independently.
87
+ Here's how to adjust the example to handle methods that require multiple arguments:
113
88
 
114
- **Creating Actor Managers:**
115
-
116
- First, ensure you have your actor managers set up for each canister:
89
+ Fist you need to create a agent manager:
117
90
 
118
91
  ```typescript
119
- // Assuming you've already set up `candidA`, `candidB`, `canisterIdA`, `canisterIdB`, and `agentManager`
92
+ // agent.ts
93
+ import { createAgentManager } from "@ic-reactor/core"
120
94
 
95
+ export const agentManager = createAgentManager() // Connects to IC network by default
96
+ ```
97
+
98
+ Then you can create a reactor for each canister:
99
+
100
+ ```typescript
101
+ // Assuming you've already set up `candidA`, `candidB`, and `agentManager`
121
102
  import { createActorManager } from "@ic-reactor/core"
122
- import { candidA, canisterIdA } from "./candidA"
123
- import { candidB, canisterIdB } from "./candidB"
103
+ import candidA from "./declarations/candidA"
104
+ import candidB from "./declarations/candidB"
124
105
  import { agentManager } from "./agent"
125
106
 
126
107
  type CandidA = typeof candidA
@@ -139,18 +120,20 @@ const actorB = createActorManager<CandidB>({
139
120
  })
140
121
  ```
141
122
 
142
- ### Using `callMethod` with Multiple Arguments
143
-
144
- To call a method on a canister that requires multiple arguments, pass the method name followed by the arguments as separate parameters to `callMethod`:
123
+ You can now use the `actorA` and `actorB` instances to interact with their respective canisters:
145
124
 
146
125
  ```typescript
147
126
  // Example usage with CanisterA calling a method that requires one argument
148
- const responseA = await actorA.callMethod("otherMethod", "arg1")
149
- console.log("Response from CanisterA method:", responseA)
127
+ const { dataPromise: versionActorA } = actorA.queryCall({
128
+ functionName: "version",
129
+ })
130
+ console.log("Response from CanisterA method:", await versionActorA)
150
131
 
151
132
  // Example usage with CanisterB calling a different method also with two arguments
152
- const responseB = await actorB.callMethod("anotherMethod", "arg1", "arg2")
153
- console.log("Response from CanisterB method:", responseB)
133
+ const { dataPromise: versionActorB } = actorB.queryCall({
134
+ functionName: "version",
135
+ })
136
+ console.log("Response from CanisterB method:", await versionActorB)
154
137
  ```
155
138
 
156
139
  ### Using Candid Adapter
@@ -175,12 +158,12 @@ try {
175
158
  }
176
159
  ```
177
160
 
178
- ### Using `createReActorStore` with `CandidAdapter`
161
+ ### Using `createReactorCore` with `CandidAdapter`
179
162
 
180
- You can use the `candidAdapter` to fetch the Candid definition and then pass it to the `createReActorStore` function.
163
+ You can use the `candidAdapter` to fetch the Candid definition and then pass it to the `createReactorCore` function.
181
164
 
182
165
  ```typescript
183
- import { createReActorStore, createCandidAdapter } from "@ic-reactor/core"
166
+ import { createReactorCore, createCandidAdapter } from "@ic-reactor/core"
184
167
  import { agentManager } from "./agent"
185
168
 
186
169
  const candidAdapter = createCandidAdapter({ agentManager })
@@ -190,7 +173,7 @@ const canisterId = "ryjl3-tyaaa-aaaaa-aaaba-cai" // NNS ICP Ledger Canister
190
173
  // Usage example
191
174
  try {
192
175
  const { idlFactory } = await candidAdapter.getCandidDefinition(canisterId)
193
- const { callMethod } = createReActorStore({
176
+ const { callMethod } = createReactorCore({
194
177
  agentManager,
195
178
  canisterId,
196
179
  idlFactory,
@@ -202,3 +185,94 @@ try {
202
185
  console.error(error)
203
186
  }
204
187
  ```
188
+
189
+ ### Using store to lower level control
190
+
191
+ If you require more control over the state management, you can use the `createReactorStore` function to create a store that provides methods for querying and updating actors.
192
+
193
+ ```typescript
194
+ import { createReactorStore } from "@ic-reactor/core"
195
+ import { candid, canisterId, idlFactory } from "./declarations/candid"
196
+
197
+ type Candid = typeof candid
198
+
199
+ const { agentManager, callMethod } = createReactorStore<Candid>({
200
+ canisterId,
201
+ idlFactory,
202
+ })
203
+
204
+ // Usage example
205
+ await agentManager.authenticate()
206
+ const authClient = agentManager.getAuthClient()
207
+
208
+ authClient?.login({
209
+ onSuccess: () => {
210
+ console.log("Logged in successfully")
211
+ },
212
+ onError: (error) => {
213
+ console.error("Failed to login:", error)
214
+ },
215
+ })
216
+
217
+ // Call a method
218
+ const version = callMethod("version")
219
+
220
+ console.log("Response from version method:", await version)
221
+ ```
222
+
223
+ **IC Agent Example:**
224
+
225
+ ```typescript
226
+ // agent.ts
227
+ import { createAgentManager } from "@ic-reactor/core"
228
+
229
+ export const agentManager = createAgentManager() // Connects to IC network by default
230
+ ```
231
+
232
+ **Local Agent Example:**
233
+
234
+ For development purposes, you might want to connect to a local instance of the IC network:
235
+
236
+ ```typescript
237
+ // agent.ts
238
+ import { createAgentManager } from "@ic-reactor/core"
239
+
240
+ export const agentManager = createAgentManager({
241
+ isLocalEnv: true,
242
+ port: 8000, // Default port is 4943
243
+ })
244
+ ```
245
+
246
+ Alternatively, you can specify a host directly:
247
+
248
+ ```typescript
249
+ // agent.ts
250
+ import { createAgentManager } from "@ic-reactor/core"
251
+
252
+ export const agentManager = createAgentManager({
253
+ host: "http://localhost:8000",
254
+ })
255
+ ```
256
+
257
+ ### Creating an Actor Manager
258
+
259
+ You can use Actor Managers to create your implementation of an actor. This allows you to manage the actor's lifecycle and state, as well as interact with the actor's methods.
260
+
261
+ ```typescript
262
+ // actor.ts
263
+ import { createActorManager } from "@ic-reactor/core"
264
+ import { candid, canisterId, idlFactory } from "./declarations/candid"
265
+ import { agentManager } from "./agent"
266
+
267
+ type Candid = typeof candid
268
+
269
+ const candidActor = createActorManager<Candid>({
270
+ agentManager,
271
+ canisterId,
272
+ idlFactory,
273
+ })
274
+
275
+ // Usage example
276
+ const data = await candidActor.callMethod("version")
277
+ console.log(data)
278
+ ```
@@ -1,7 +1,5 @@
1
1
  import { HttpAgent } from "@dfinity/agent";
2
2
  import type { AgentStore, AgentManagerOptions, UpdateAgentOptions, AuthStore } from "./types";
3
- export declare const IC_HOST_NETWORK_URI = "https://ic0.app";
4
- export declare const LOCAL_HOST_NETWORK_URI = "http://127.0.0.1:4943";
5
3
  export declare class AgentManager {
6
4
  private _agent;
7
5
  private _subscribers;
@@ -20,7 +18,6 @@ export declare class AgentManager {
20
18
  updateAgent: (options?: UpdateAgentOptions) => Promise<void>;
21
19
  authenticate: () => Promise<import("@dfinity/agent").Identity>;
22
20
  getAgent: () => HttpAgent;
23
- getAgentStore: () => AgentStore;
24
21
  getAgentState: AgentStore["getState"];
25
22
  subscribeAgentState: AgentStore["subscribe"];
26
23
  getAuthState: AuthStore["getState"];
@@ -43,11 +43,10 @@ var __rest = (this && this.__rest) || function (s, e) {
43
43
  return t;
44
44
  };
45
45
  Object.defineProperty(exports, "__esModule", { value: true });
46
- exports.AgentManager = exports.LOCAL_HOST_NETWORK_URI = exports.IC_HOST_NETWORK_URI = void 0;
46
+ exports.AgentManager = void 0;
47
47
  const agent_1 = require("@dfinity/agent");
48
48
  const helper_1 = require("../tools/helper");
49
- exports.IC_HOST_NETWORK_URI = "https://ic0.app";
50
- exports.LOCAL_HOST_NETWORK_URI = "http://127.0.0.1:4943";
49
+ const constants_1 = require("../tools/constants");
51
50
  class AgentManager {
52
51
  constructor(options) {
53
52
  this._subscribers = [];
@@ -133,9 +132,6 @@ class AgentManager {
133
132
  this.getAgent = () => {
134
133
  return this._agent;
135
134
  };
136
- this.getAgentStore = () => {
137
- return this.agentStore;
138
- };
139
135
  this.getAgentState = () => {
140
136
  return this.agentStore.getState();
141
137
  };
@@ -166,7 +162,7 @@ class AgentManager {
166
162
  ? optionHost.includes("localhost")
167
163
  ? optionHost.replace("localhost", "127.0.0.1")
168
164
  : optionHost
169
- : exports.IC_HOST_NETWORK_URI;
165
+ : constants_1.IC_HOST_NETWORK_URI;
170
166
  this.agentStore = (0, helper_1.createStoreWithOptionalDevtools)(this.initialAgentState, {
171
167
  withDevtools,
172
168
  store: "agent",
@@ -1,7 +1,5 @@
1
1
  import { HttpAgent } from "@dfinity/agent";
2
2
  import type { CanisterId, CandidAdapterOptions, CandidDefenition } from "../types";
3
- export declare const DEFAULT_LOCAL_DIDJS_ID = "bd3sg-teaaa-aaaaa-qaaba-cai";
4
- export declare const DEFAULT_IC_DIDJS_ID = "a4gq6-oaaaa-aaaab-qaa4q-cai";
5
3
  export declare class CandidAdapter {
6
4
  agent: HttpAgent;
7
5
  didjsCanisterId: string;
@@ -9,11 +9,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.CandidAdapter = exports.DEFAULT_IC_DIDJS_ID = exports.DEFAULT_LOCAL_DIDJS_ID = void 0;
12
+ exports.CandidAdapter = void 0;
13
13
  const agent_1 = require("@dfinity/agent");
14
14
  const principal_1 = require("@dfinity/principal");
15
- exports.DEFAULT_LOCAL_DIDJS_ID = "bd3sg-teaaa-aaaaa-qaaba-cai";
16
- exports.DEFAULT_IC_DIDJS_ID = "a4gq6-oaaaa-aaaab-qaa4q-cai";
15
+ const constants_1 = require("../tools/constants");
17
16
  class CandidAdapter {
18
17
  constructor({ agentManager, agent, didjsCanisterId }) {
19
18
  if (agent) {
@@ -32,7 +31,7 @@ class CandidAdapter {
32
31
  this.didjsCanisterId = didjsCanisterId || this.getDefaultDidJsId();
33
32
  }
34
33
  getDefaultDidJsId() {
35
- return this.agent.isLocal() ? exports.DEFAULT_LOCAL_DIDJS_ID : exports.DEFAULT_IC_DIDJS_ID;
34
+ return this.agent.isLocal() ? constants_1.DEFAULT_LOCAL_DIDJS_ID : constants_1.DEFAULT_IC_DIDJS_ID;
36
35
  }
37
36
  getCandidDefinition(canisterId) {
38
37
  return __awaiter(this, void 0, void 0, function* () {
package/dist/index.d.ts CHANGED
@@ -1,29 +1,42 @@
1
1
  import { ActorManager } from "./actor";
2
2
  import { AgentManager } from "./agent";
3
- import { CandidAdapter } from "./tools";
4
- import type { ActorManagerOptions, BaseActor, AgentManagerOptions, ActorCoreActions, CreateReActorOptions, CreateReActorStoreOptions, CandidAdapterOptions } from "./types";
3
+ import type { ActorManagerOptions, BaseActor, AgentManagerOptions, ReactorCore, CreateReactorOptions, CreateReactorStoreOptions, CandidAdapterOptions } from "./types";
4
+ import { CandidAdapter } from "./candid";
5
5
  /**
6
+ * The Core module is the main entry point for the library.
6
7
  * Create a new actor manager with the given options.
7
8
  * Its create a new agent manager if not provided.
8
9
  *
9
- * @includeExample ./packages/core/README.md:30-91
10
+ * @category Main
11
+ * @includeExample ./packages/core/README.md:26-80
10
12
  */
11
- export declare const createReActor: <A = BaseActor>({ isLocalEnv, withProcessEnv, ...options }: CreateReActorOptions) => ActorCoreActions<A>;
13
+ export declare const createReactorCore: <A = BaseActor>({ isLocalEnv, withProcessEnv, ...options }: CreateReactorOptions) => ReactorCore<A>;
14
+ /**
15
+ * The `CandidAdapter` class is used to interact with a canister and retrieve its Candid interface definition.
16
+ * It provides methods to fetch the Candid definition either from the canister's metadata or by using a temporary hack method.
17
+ * If both methods fail, it throws an error.
18
+ *
19
+ * @category Main
20
+ * @includeExample ./packages/core/README.md:145-186
21
+ */
22
+ export declare const createCandidAdapter: (options: CandidAdapterOptions) => CandidAdapter;
12
23
  /**
13
24
  * Create a new actor manager with the given options.
14
25
  * Its create a new agent manager if not provided.
15
26
  * It also creates a new actor manager with the given options.
16
27
  *
17
- * @includeExample ./packages/core/README.md:32-45
28
+ * @category Main
29
+ * @includeExample ./packages/core/README.md:194-220
18
30
  */
19
- export declare const createReActorStore: <A = BaseActor>(options: CreateReActorStoreOptions) => ActorManager<A>;
31
+ export declare const createReactorStore: <A = BaseActor>(options: CreateReactorStoreOptions) => ActorManager<A>;
20
32
  /**
21
33
  * Agent manager handles the lifecycle of the `@dfinity/agent`.
22
34
  * It is responsible for creating agent and managing the agent's state.
23
35
  * You can use it to subscribe to the agent changes.
24
36
  * login and logout to the internet identity.
25
37
  *
26
- * @includeExample ./packages/core/README.md:55-86
38
+ * @category Main
39
+ * @includeExample ./packages/core/README.md:226-254
27
40
  */
28
41
  export declare const createAgentManager: (options?: AgentManagerOptions) => AgentManager;
29
42
  /**
@@ -32,18 +45,12 @@ export declare const createAgentManager: (options?: AgentManagerOptions) => Agen
32
45
  * You can use it to call and visit the actor's methods.
33
46
  * It also provides a way to interact with the actor's state.
34
47
  *
35
- * @includeExample ./packages/core/README.md:94-109
48
+ * @category Main
49
+ * @includeExample ./packages/core/README.md:262-277
36
50
  */
37
51
  export declare const createActorManager: <A = BaseActor>(options: ActorManagerOptions) => ActorManager<A>;
38
- /**
39
- * The `CandidAdapter` class is used to interact with a canister and retrieve its Candid interface definition.
40
- * It provides methods to fetch the Candid definition either from the canister's metadata or by using a temporary hack method.
41
- * If both methods fail, it throws an error.
42
- *
43
- * @includeExample ./packages/core/README.md:164-205
44
- */
45
- export declare const createCandidAdapter: (options: CandidAdapterOptions) => CandidAdapter;
52
+ export * from "./actor";
53
+ export * from "./agent";
54
+ export * from "./candid";
46
55
  export * as types from "./types";
47
- export * as actor from "./actor";
48
- export * as agent from "./agent";
49
56
  export * as tools from "./tools";
package/dist/index.js CHANGED
@@ -15,6 +15,9 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
+ };
18
21
  var __importStar = (this && this.__importStar) || function (mod) {
19
22
  if (mod && mod.__esModule) return mod;
20
23
  var result = {};
@@ -43,26 +46,24 @@ var __rest = (this && this.__rest) || function (s, e) {
43
46
  return t;
44
47
  };
45
48
  Object.defineProperty(exports, "__esModule", { value: true });
46
- exports.tools = exports.agent = exports.actor = exports.types = exports.createCandidAdapter = exports.createActorManager = exports.createAgentManager = exports.createReActorStore = exports.createReActor = void 0;
49
+ exports.tools = exports.types = exports.createActorManager = exports.createAgentManager = exports.createReactorStore = exports.createCandidAdapter = exports.createReactorCore = void 0;
47
50
  const actor_1 = require("./actor");
48
51
  const agent_1 = require("./agent");
52
+ const constants_1 = require("./tools/constants");
49
53
  const tools_1 = require("./tools");
54
+ const candid_1 = require("./candid");
50
55
  /**
56
+ * The Core module is the main entry point for the library.
51
57
  * Create a new actor manager with the given options.
52
58
  * Its create a new agent manager if not provided.
53
59
  *
54
- * @includeExample ./packages/core/README.md:30-91
60
+ * @category Main
61
+ * @includeExample ./packages/core/README.md:26-80
55
62
  */
56
- const createReActor = (_a) => {
63
+ const createReactorCore = (_a) => {
57
64
  var { isLocalEnv, withProcessEnv = false } = _a, options = __rest(_a, ["isLocalEnv", "withProcessEnv"]);
58
- isLocalEnv =
59
- isLocalEnv ||
60
- (withProcessEnv
61
- ? typeof process !== "undefined" &&
62
- (process.env.DFX_NETWORK === "local" ||
63
- process.env.NODE_ENV === "development")
64
- : false);
65
- const _b = (0, exports.createReActorStore)(Object.assign({ isLocalEnv }, options)), { subscribeActorState, updateMethodState, callMethod, getState, agentManager } = _b, rest = __rest(_b, ["subscribeActorState", "updateMethodState", "callMethod", "getState", "agentManager"]);
65
+ isLocalEnv = isLocalEnv || (withProcessEnv ? (0, tools_1.isInLocalOrDevelopment)() : false);
66
+ const _b = (0, exports.createReactorStore)(Object.assign({ isLocalEnv }, options)), { subscribeActorState, updateMethodState, callMethod, getState, agentManager } = _b, rest = __rest(_b, ["subscribeActorState", "updateMethodState", "callMethod", "getState", "agentManager"]);
66
67
  const reActorMethod = (functionName, ...args) => {
67
68
  const requestHash = (0, tools_1.generateRequestHash)(args);
68
69
  const updateState = (newState = {}) => {
@@ -147,9 +148,12 @@ const createReActor = (_a) => {
147
148
  if (!authClient) {
148
149
  yield agentManager.authenticate();
149
150
  }
151
+ if (!authClient) {
152
+ throw new Error("Auth client not initialized");
153
+ }
150
154
  yield authClient.login(Object.assign({ identityProvider: isLocalEnv
151
- ? "https://identity.ic0.app/#authorize"
152
- : "http://rdmx6-jaaaa-aaaaa-aaadq-cai.localhost:4943/#authorize" }, options));
155
+ ? constants_1.IC_INTERNET_IDENTITY_PROVIDER
156
+ : constants_1.LOCAL_INTERNET_IDENTITY_PROVIDER }, options));
153
157
  });
154
158
  const logout = (options) => __awaiter(void 0, void 0, void 0, function* () {
155
159
  const authClient = agentManager.getAuthClient();
@@ -167,15 +171,28 @@ const createReActor = (_a) => {
167
171
  logout,
168
172
  subscribeActorState }, agentManager), rest);
169
173
  };
170
- exports.createReActor = createReActor;
174
+ exports.createReactorCore = createReactorCore;
175
+ /**
176
+ * The `CandidAdapter` class is used to interact with a canister and retrieve its Candid interface definition.
177
+ * It provides methods to fetch the Candid definition either from the canister's metadata or by using a temporary hack method.
178
+ * If both methods fail, it throws an error.
179
+ *
180
+ * @category Main
181
+ * @includeExample ./packages/core/README.md:145-186
182
+ */
183
+ const createCandidAdapter = (options) => {
184
+ return new candid_1.CandidAdapter(options);
185
+ };
186
+ exports.createCandidAdapter = createCandidAdapter;
171
187
  /**
172
188
  * Create a new actor manager with the given options.
173
189
  * Its create a new agent manager if not provided.
174
190
  * It also creates a new actor manager with the given options.
175
191
  *
176
- * @includeExample ./packages/core/README.md:32-45
192
+ * @category Main
193
+ * @includeExample ./packages/core/README.md:194-220
177
194
  */
178
- const createReActorStore = (options) => {
195
+ const createReactorStore = (options) => {
179
196
  const { idlFactory, canisterId, withDevtools = false, initializeOnCreate = true, withVisitor = false, agentManager: maybeAgentManager } = options, agentOptions = __rest(options, ["idlFactory", "canisterId", "withDevtools", "initializeOnCreate", "withVisitor", "agentManager"]);
180
197
  const agentManager = maybeAgentManager ||
181
198
  (0, exports.createAgentManager)(Object.assign({ withDevtools }, agentOptions));
@@ -189,14 +206,15 @@ const createReActorStore = (options) => {
189
206
  });
190
207
  return actorManager;
191
208
  };
192
- exports.createReActorStore = createReActorStore;
209
+ exports.createReactorStore = createReactorStore;
193
210
  /**
194
211
  * Agent manager handles the lifecycle of the `@dfinity/agent`.
195
212
  * It is responsible for creating agent and managing the agent's state.
196
213
  * You can use it to subscribe to the agent changes.
197
214
  * login and logout to the internet identity.
198
215
  *
199
- * @includeExample ./packages/core/README.md:55-86
216
+ * @category Main
217
+ * @includeExample ./packages/core/README.md:226-254
200
218
  */
201
219
  const createAgentManager = (options) => {
202
220
  return new agent_1.AgentManager(options);
@@ -208,24 +226,15 @@ exports.createAgentManager = createAgentManager;
208
226
  * You can use it to call and visit the actor's methods.
209
227
  * It also provides a way to interact with the actor's state.
210
228
  *
211
- * @includeExample ./packages/core/README.md:94-109
229
+ * @category Main
230
+ * @includeExample ./packages/core/README.md:262-277
212
231
  */
213
232
  const createActorManager = (options) => {
214
233
  return new actor_1.ActorManager(options);
215
234
  };
216
235
  exports.createActorManager = createActorManager;
217
- /**
218
- * The `CandidAdapter` class is used to interact with a canister and retrieve its Candid interface definition.
219
- * It provides methods to fetch the Candid definition either from the canister's metadata or by using a temporary hack method.
220
- * If both methods fail, it throws an error.
221
- *
222
- * @includeExample ./packages/core/README.md:164-205
223
- */
224
- const createCandidAdapter = (options) => {
225
- return new tools_1.CandidAdapter(options);
226
- };
227
- exports.createCandidAdapter = createCandidAdapter;
236
+ __exportStar(require("./actor"), exports);
237
+ __exportStar(require("./agent"), exports);
238
+ __exportStar(require("./candid"), exports);
228
239
  exports.types = __importStar(require("./types"));
229
- exports.actor = __importStar(require("./actor"));
230
- exports.agent = __importStar(require("./agent"));
231
240
  exports.tools = __importStar(require("./tools"));
@@ -0,0 +1,6 @@
1
+ export declare const IC_HOST_NETWORK_URI = "https://ic0.app";
2
+ export declare const LOCAL_HOST_NETWORK_URI = "http://127.0.0.1:4943";
3
+ export declare const DEFAULT_LOCAL_DIDJS_ID = "bd3sg-teaaa-aaaaa-qaaba-cai";
4
+ export declare const DEFAULT_IC_DIDJS_ID = "a4gq6-oaaaa-aaaab-qaa4q-cai";
5
+ export declare const IC_INTERNET_IDENTITY_PROVIDER = "https://identity.ic0.app/#authorize";
6
+ export declare const LOCAL_INTERNET_IDENTITY_PROVIDER = "http://rdmx6-jaaaa-aaaaa-aaadq-cai.localhost:4943/#authorize";
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LOCAL_INTERNET_IDENTITY_PROVIDER = exports.IC_INTERNET_IDENTITY_PROVIDER = exports.DEFAULT_IC_DIDJS_ID = exports.DEFAULT_LOCAL_DIDJS_ID = exports.LOCAL_HOST_NETWORK_URI = exports.IC_HOST_NETWORK_URI = void 0;
4
+ exports.IC_HOST_NETWORK_URI = "https://ic0.app";
5
+ exports.LOCAL_HOST_NETWORK_URI = "http://127.0.0.1:4943";
6
+ exports.DEFAULT_LOCAL_DIDJS_ID = "bd3sg-teaaa-aaaaa-qaaba-cai";
7
+ exports.DEFAULT_IC_DIDJS_ID = "a4gq6-oaaaa-aaaab-qaa4q-cai";
8
+ exports.IC_INTERNET_IDENTITY_PROVIDER = "https://identity.ic0.app/#authorize";
9
+ exports.LOCAL_INTERNET_IDENTITY_PROVIDER = "http://rdmx6-jaaaa-aaaaa-aaadq-cai.localhost:4943/#authorize";
@@ -8,7 +8,8 @@ export declare function createStoreWithOptionalDevtools<T>(initialState: T, opti
8
8
  type: string;
9
9
  }>(partial: T | Partial<T> | ((state: T) => T | Partial<T>), replace?: boolean | undefined, action?: A | undefined): void;
10
10
  };
11
- export declare function jsonToString(json: unknown): string;
11
+ export declare const isInLocalOrDevelopment: () => boolean;
12
+ export declare const jsonToString: (json: unknown) => string;
12
13
  export declare const generateRequestHash: (args?: unknown[]) => `0x${string}`;
13
14
  export declare const generateHash: (field?: unknown) => `0x${string}`;
14
15
  export declare const generateActorHash: (actor: BaseActor) => `0x${string}`;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.stringToHash = exports.generateActorHash = exports.generateHash = exports.generateRequestHash = exports.jsonToString = exports.createStoreWithOptionalDevtools = void 0;
3
+ exports.stringToHash = exports.generateActorHash = exports.generateHash = exports.generateRequestHash = exports.jsonToString = exports.isInLocalOrDevelopment = exports.createStoreWithOptionalDevtools = void 0;
4
4
  const agent_1 = require("@dfinity/agent");
5
5
  const candid_1 = require("@dfinity/candid");
6
6
  const middleware_1 = require("zustand/middleware");
@@ -8,7 +8,7 @@ const vanilla_1 = require("zustand/vanilla");
8
8
  function createStoreWithOptionalDevtools(initialState, options) {
9
9
  if (options.withDevtools) {
10
10
  return (0, vanilla_1.createStore)((0, middleware_1.devtools)(() => initialState, {
11
- name: "ReActor",
11
+ name: "Reactor",
12
12
  store: options.store,
13
13
  }));
14
14
  }
@@ -17,9 +17,15 @@ function createStoreWithOptionalDevtools(initialState, options) {
17
17
  }
18
18
  }
19
19
  exports.createStoreWithOptionalDevtools = createStoreWithOptionalDevtools;
20
- function jsonToString(json) {
20
+ const isInLocalOrDevelopment = () => {
21
+ return (typeof process !== "undefined" &&
22
+ (process.env.DFX_NETWORK === "local" ||
23
+ process.env.NODE_ENV === "development"));
24
+ };
25
+ exports.isInLocalOrDevelopment = isInLocalOrDevelopment;
26
+ const jsonToString = (json) => {
21
27
  return JSON.stringify(json, (_, value) => (typeof value === "bigint" ? `BigInt(${value})` : value), 2);
22
- }
28
+ };
23
29
  exports.jsonToString = jsonToString;
24
30
  const generateRequestHash = (args = []) => {
25
31
  const serializedArgs = args
@@ -1,2 +1,2 @@
1
- export * from "./candid";
2
1
  export * from "./helper";
2
+ export * from "./constants";
@@ -14,5 +14,5 @@ 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("./candid"), exports);
18
17
  __exportStar(require("./helper"), exports);
18
+ __exportStar(require("./constants"), exports);
package/dist/types.d.ts CHANGED
@@ -8,11 +8,11 @@ import type { AgentManager } from "./agent";
8
8
  import type { AuthClientLoginOptions } from "@dfinity/auth-client";
9
9
  export * from "./agent/types";
10
10
  export * from "./actor/types";
11
- export * from "./tools/types";
11
+ export * from "./candid/types";
12
12
  export type { ActorMethod, HttpAgentOptions, ActorSubclass, Principal, HttpAgent, Identity, IDL, };
13
- export interface CreateReActorOptions extends CreateReActorStoreOptions {
13
+ export interface CreateReactorOptions extends CreateReactorStoreOptions {
14
14
  }
15
- export interface CreateReActorStoreOptions extends HttpAgentOptions, Omit<ActorManagerOptions, "agentManager"> {
15
+ export interface CreateReactorStoreOptions extends HttpAgentOptions, Omit<ActorManagerOptions, "agentManager"> {
16
16
  agentManager?: AgentManager;
17
17
  withProcessEnv?: boolean;
18
18
  isLocalEnv?: boolean;
@@ -53,7 +53,7 @@ export type ActorUpdateArgs<A, M extends FunctionName<A>> = {
53
53
  export type ActorMethodCall<A = Record<string, ActorMethod>> = <M extends FunctionName<A>>(functionName: M, ...args: ActorMethodArgs<A[M]>) => ActorUpdateReturn<A, M>;
54
54
  export type ActorQuery<A = Record<string, ActorMethod>> = <M extends FunctionName<A>>(options: ActorQueryArgs<A, M>) => ActorQueryReturn<A, M>;
55
55
  export type ActorUpdate<A = Record<string, ActorMethod>> = <M extends FunctionName<A>>(options: ActorUpdateArgs<A, M>) => ActorUpdateReturn<A, M>;
56
- export interface ActorCoreActions<A = BaseActor> extends AgentManager, Omit<ActorManager<A>, "updateMethodState"> {
56
+ export interface ReactorCore<A = BaseActor> extends AgentManager, Omit<ActorManager<A>, "updateMethodState"> {
57
57
  login: (options?: AuthClientLoginOptions) => Promise<void>;
58
58
  logout: (options?: {
59
59
  returnTo?: string;
package/dist/types.js CHANGED
@@ -16,4 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./agent/types"), exports);
18
18
  __exportStar(require("./actor/types"), exports);
19
- __exportStar(require("./tools/types"), exports);
19
+ __exportStar(require("./candid/types"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/core",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
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": "54c9942a98bcad64e63c8d52788103f8b168720e"
46
+ "gitHead": "4d19bef671056c8046ec64037f51d85380ff5bae"
47
47
  }
File without changes
File without changes