@ic-reactor/core 1.0.1 → 1.0.3
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 +164 -90
- package/dist/actor/index.d.ts +0 -3
- package/dist/actor/index.js +4 -10
- package/dist/actor/types.d.ts +5 -5
- package/dist/agent/index.d.ts +0 -3
- package/dist/agent/index.js +3 -7
- package/dist/{tools/candid.d.ts → candid/index.d.ts} +0 -2
- package/dist/{tools/candid.js → candid/index.js} +3 -4
- package/dist/index.d.ts +6 -47
- package/dist/index.js +10 -204
- package/dist/main.d.ts +10 -0
- package/dist/main.js +146 -0
- package/dist/other.d.ts +33 -0
- package/dist/other.js +44 -0
- package/dist/store.d.ts +11 -0
- package/dist/store.js +38 -0
- package/dist/tools/constants.d.ts +6 -0
- package/dist/tools/constants.js +9 -0
- package/dist/tools/helper.d.ts +2 -1
- package/dist/tools/helper.js +10 -4
- package/dist/tools/index.d.ts +1 -1
- package/dist/tools/index.js +1 -1
- package/dist/types.d.ts +5 -5
- package/dist/types.js +1 -1
- package/package.json +2 -2
- /package/dist/{tools → candid}/types.d.ts +0 -0
- /package/dist/{tools → candid}/types.js +0 -0
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)
|
|
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
|
-
|
|
19
|
+
### Using `createReactorCore`
|
|
20
20
|
|
|
21
|
-
|
|
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 {
|
|
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 {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
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
|
-
//
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
106
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
//
|
|
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
|
|
123
|
-
import
|
|
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
|
-
|
|
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
|
|
149
|
-
|
|
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
|
|
153
|
-
|
|
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 `
|
|
161
|
+
### Using `createReactorCore` with `CandidAdapter`
|
|
179
162
|
|
|
180
|
-
You can use the `candidAdapter` to fetch the Candid definition and then pass it to the `
|
|
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 {
|
|
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 } =
|
|
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
|
+
```
|
package/dist/actor/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { HttpAgent } from "@dfinity/agent";
|
|
2
1
|
import type { CanisterId, ActorMethodArgs, ActorMethodReturnType, ActorStore, ActorManagerOptions, FunctionName, VisitService, BaseActor, ActorMethodState } from "./types";
|
|
3
2
|
import type { AgentManager } from "../agent";
|
|
4
3
|
import type { UpdateAgentOptions } from "../types";
|
|
@@ -10,7 +9,6 @@ export declare class ActorManager<A = BaseActor> {
|
|
|
10
9
|
actorStore: ActorStore<A>;
|
|
11
10
|
visitFunction: VisitService<A>;
|
|
12
11
|
private initialState;
|
|
13
|
-
unsubscribeActor: () => void;
|
|
14
12
|
private updateState;
|
|
15
13
|
updateMethodState: (method: FunctionName<A>, hash: string, newState: Partial<{
|
|
16
14
|
data: ActorMethodReturnType<A[FunctionName<A>]> | undefined;
|
|
@@ -23,7 +21,6 @@ export declare class ActorManager<A = BaseActor> {
|
|
|
23
21
|
private initializeActor;
|
|
24
22
|
callMethod: <M extends FunctionName<A>>(functionName: M, ...args: ActorMethodArgs<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
|
25
23
|
get agentManager(): AgentManager;
|
|
26
|
-
getAgent: () => HttpAgent;
|
|
27
24
|
getActor: () => A | null;
|
|
28
25
|
getState: ActorStore<A>["getState"];
|
|
29
26
|
subscribeActorState: ActorStore<A>["subscribe"];
|
package/dist/actor/index.js
CHANGED
|
@@ -29,11 +29,7 @@ class ActorManager {
|
|
|
29
29
|
this.updateMethodState = (method, hash, newState) => {
|
|
30
30
|
this.actorStore.setState((state) => {
|
|
31
31
|
const methodState = state.methodState[method] || {};
|
|
32
|
-
const currentMethodState = methodState[hash] ||
|
|
33
|
-
loading: false,
|
|
34
|
-
data: undefined,
|
|
35
|
-
error: undefined,
|
|
36
|
-
};
|
|
32
|
+
const currentMethodState = methodState[hash] || DEFAULT_STATE;
|
|
37
33
|
const updatedMethodState = Object.assign(Object.assign({}, methodState), { [hash]: Object.assign(Object.assign({}, currentMethodState), newState) });
|
|
38
34
|
return Object.assign(Object.assign({}, state), { methodState: Object.assign(Object.assign({}, state.methodState), { [method]: updatedMethodState }) });
|
|
39
35
|
});
|
|
@@ -82,9 +78,6 @@ class ActorManager {
|
|
|
82
78
|
const data = yield method(...args);
|
|
83
79
|
return data;
|
|
84
80
|
});
|
|
85
|
-
this.getAgent = () => {
|
|
86
|
-
return this._agentManager.getAgent();
|
|
87
|
-
};
|
|
88
81
|
// actor store
|
|
89
82
|
this.getActor = () => {
|
|
90
83
|
return this._actor;
|
|
@@ -100,11 +93,11 @@ class ActorManager {
|
|
|
100
93
|
};
|
|
101
94
|
const { agentManager, canisterId, idlFactory, withVisitor = false, withDevtools = false, initializeOnCreate = true, } = actorConfig;
|
|
102
95
|
this._agentManager = agentManager;
|
|
103
|
-
this.
|
|
96
|
+
this._agentManager.subscribeAgent(this.initializeActor);
|
|
104
97
|
this.canisterId = canisterId;
|
|
105
98
|
this._idlFactory = idlFactory;
|
|
106
99
|
if (withVisitor) {
|
|
107
|
-
this.visitFunction = this.extractService();
|
|
100
|
+
this.visitFunction = withVisitor ? this.extractService() : emptyVisitor;
|
|
108
101
|
}
|
|
109
102
|
else {
|
|
110
103
|
this.visitFunction = emptyVisitor;
|
|
@@ -140,3 +133,4 @@ const emptyVisitor = new Proxy({}, {
|
|
|
140
133
|
throw new Error(`Cannot visit function "${String(prop)}" without initializing the actor with the visitor option, please set the withVisitor option to true when creating the actor manager.`);
|
|
141
134
|
},
|
|
142
135
|
});
|
|
136
|
+
const DEFAULT_STATE = { data: undefined, error: undefined, loading: false };
|
package/dist/actor/types.d.ts
CHANGED
|
@@ -26,21 +26,21 @@ export type VisitService<A = BaseActor, M extends FunctionName<A> = FunctionName
|
|
|
26
26
|
};
|
|
27
27
|
export type ActorMethodArgs<T> = T extends ActorMethod<infer Args, any> ? Args : never;
|
|
28
28
|
export type ActorMethodReturnType<T> = T extends ActorMethod<any, infer Ret> ? Ret : never;
|
|
29
|
-
export interface ActorMethodState<A, M extends
|
|
29
|
+
export interface ActorMethodState<A = BaseActor, M extends FunctionName<A> = FunctionName<A>> {
|
|
30
30
|
[key: string]: {
|
|
31
31
|
data: ActorMethodReturnType<A[M]> | undefined;
|
|
32
32
|
loading: boolean;
|
|
33
33
|
error: Error | undefined;
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
|
-
export type ActorMethodStates<A> = {
|
|
37
|
-
[M in
|
|
36
|
+
export type ActorMethodStates<A = BaseActor> = {
|
|
37
|
+
[M in FunctionName<A>]: ActorMethodState<A, M>;
|
|
38
38
|
};
|
|
39
|
-
export type ActorState<A> = {
|
|
39
|
+
export type ActorState<A = BaseActor> = {
|
|
40
40
|
initialized: boolean;
|
|
41
41
|
initializing: boolean;
|
|
42
42
|
error: Error | undefined;
|
|
43
43
|
methodState: ActorMethodStates<A>;
|
|
44
44
|
};
|
|
45
45
|
export type ActorStore<A = BaseActor> = StoreApi<ActorState<A>>;
|
|
46
|
-
export type CallActorMethod<A = BaseActor> = <M extends
|
|
46
|
+
export type CallActorMethod<A = BaseActor> = <M extends FunctionName<A> = FunctionName<A>>(functionName: M, ...args: ActorMethodArgs<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
package/dist/agent/index.d.ts
CHANGED
|
@@ -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"];
|
package/dist/agent/index.js
CHANGED
|
@@ -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 =
|
|
46
|
+
exports.AgentManager = void 0;
|
|
47
47
|
const agent_1 = require("@dfinity/agent");
|
|
48
48
|
const helper_1 = require("../tools/helper");
|
|
49
|
-
|
|
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
|
-
:
|
|
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 =
|
|
12
|
+
exports.CandidAdapter = void 0;
|
|
13
13
|
const agent_1 = require("@dfinity/agent");
|
|
14
14
|
const principal_1 = require("@dfinity/principal");
|
|
15
|
-
|
|
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() ?
|
|
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,49 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
* Its create a new agent manager if not provided.
|
|
8
|
-
*
|
|
9
|
-
* @includeExample ./packages/core/README.md:30-91
|
|
10
|
-
*/
|
|
11
|
-
export declare const createReActor: <A = BaseActor>({ isLocalEnv, withProcessEnv, ...options }: CreateReActorOptions) => ActorCoreActions<A>;
|
|
12
|
-
/**
|
|
13
|
-
* Create a new actor manager with the given options.
|
|
14
|
-
* Its create a new agent manager if not provided.
|
|
15
|
-
* It also creates a new actor manager with the given options.
|
|
16
|
-
*
|
|
17
|
-
* @includeExample ./packages/core/README.md:32-45
|
|
18
|
-
*/
|
|
19
|
-
export declare const createReActorStore: <A = BaseActor>(options: CreateReActorStoreOptions) => ActorManager<A>;
|
|
20
|
-
/**
|
|
21
|
-
* Agent manager handles the lifecycle of the `@dfinity/agent`.
|
|
22
|
-
* It is responsible for creating agent and managing the agent's state.
|
|
23
|
-
* You can use it to subscribe to the agent changes.
|
|
24
|
-
* login and logout to the internet identity.
|
|
25
|
-
*
|
|
26
|
-
* @includeExample ./packages/core/README.md:55-86
|
|
27
|
-
*/
|
|
28
|
-
export declare const createAgentManager: (options?: AgentManagerOptions) => AgentManager;
|
|
29
|
-
/**
|
|
30
|
-
* Actor manager handles the lifecycle of the actors.
|
|
31
|
-
* It is responsible for creating and managing the actors.
|
|
32
|
-
* You can use it to call and visit the actor's methods.
|
|
33
|
-
* It also provides a way to interact with the actor's state.
|
|
34
|
-
*
|
|
35
|
-
* @includeExample ./packages/core/README.md:94-109
|
|
36
|
-
*/
|
|
37
|
-
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;
|
|
1
|
+
export * from "./main";
|
|
2
|
+
export * from "./store";
|
|
3
|
+
export * from "./other";
|
|
4
|
+
export * from "./actor";
|
|
5
|
+
export * from "./agent";
|
|
6
|
+
export * from "./candid";
|
|
46
7
|
export * as types from "./types";
|
|
47
|
-
export * as actor from "./actor";
|
|
48
|
-
export * as agent from "./agent";
|
|
49
8
|
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 = {};
|
|
@@ -22,210 +25,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
25
|
__setModuleDefault(result, mod);
|
|
23
26
|
return result;
|
|
24
27
|
};
|
|
25
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
35
|
-
var t = {};
|
|
36
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
37
|
-
t[p] = s[p];
|
|
38
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
39
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
40
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
41
|
-
t[p[i]] = s[p[i]];
|
|
42
|
-
}
|
|
43
|
-
return t;
|
|
44
|
-
};
|
|
45
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
|
-
exports.tools = exports.
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
*
|
|
54
|
-
* @includeExample ./packages/core/README.md:30-91
|
|
55
|
-
*/
|
|
56
|
-
const createReActor = (_a) => {
|
|
57
|
-
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"]);
|
|
66
|
-
const reActorMethod = (functionName, ...args) => {
|
|
67
|
-
const requestHash = (0, tools_1.generateRequestHash)(args);
|
|
68
|
-
const updateState = (newState = {}) => {
|
|
69
|
-
updateMethodState(functionName, requestHash, newState);
|
|
70
|
-
};
|
|
71
|
-
updateState();
|
|
72
|
-
try {
|
|
73
|
-
const methodState = ((key) => {
|
|
74
|
-
const state = getState().methodState[functionName][requestHash];
|
|
75
|
-
switch (key) {
|
|
76
|
-
case "data":
|
|
77
|
-
return state.data;
|
|
78
|
-
case "loading":
|
|
79
|
-
return state.loading;
|
|
80
|
-
case "error":
|
|
81
|
-
return state.error;
|
|
82
|
-
default:
|
|
83
|
-
return state;
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
const subscribe = (callback) => {
|
|
87
|
-
const unsubscribe = subscribeActorState((state) => {
|
|
88
|
-
const methodState = state.methodState[functionName];
|
|
89
|
-
const methodStateHash = methodState[requestHash];
|
|
90
|
-
if (methodStateHash) {
|
|
91
|
-
callback(methodStateHash);
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
return unsubscribe;
|
|
95
|
-
};
|
|
96
|
-
const call = (replaceArgs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
97
|
-
updateState({
|
|
98
|
-
loading: true,
|
|
99
|
-
error: undefined,
|
|
100
|
-
});
|
|
101
|
-
try {
|
|
102
|
-
const data = yield callMethod(functionName, ...(replaceArgs !== null && replaceArgs !== void 0 ? replaceArgs : args));
|
|
103
|
-
updateState({ data, loading: false });
|
|
104
|
-
return data;
|
|
105
|
-
}
|
|
106
|
-
catch (error) {
|
|
107
|
-
updateState({
|
|
108
|
-
error: error,
|
|
109
|
-
loading: false,
|
|
110
|
-
});
|
|
111
|
-
throw error;
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
return {
|
|
115
|
-
requestHash,
|
|
116
|
-
subscribe,
|
|
117
|
-
getState: methodState,
|
|
118
|
-
call,
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
catch (error) {
|
|
122
|
-
updateState({
|
|
123
|
-
error: error,
|
|
124
|
-
loading: false,
|
|
125
|
-
});
|
|
126
|
-
throw error;
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
const queryCall = ({ functionName, args = [], refetchOnMount = true, refetchInterval = false, }) => {
|
|
130
|
-
let intervalId = null;
|
|
131
|
-
const _a = reActorMethod(functionName, ...args), { call } = _a, rest = __rest(_a, ["call"]);
|
|
132
|
-
if (refetchInterval) {
|
|
133
|
-
intervalId = setInterval(() => {
|
|
134
|
-
call();
|
|
135
|
-
}, refetchInterval);
|
|
136
|
-
}
|
|
137
|
-
let dataPromise = Promise.resolve();
|
|
138
|
-
if (refetchOnMount)
|
|
139
|
-
dataPromise = call();
|
|
140
|
-
return Object.assign(Object.assign({}, rest), { call, dataPromise, intervalId });
|
|
141
|
-
};
|
|
142
|
-
const updateCall = ({ functionName, args = [] }) => {
|
|
143
|
-
return reActorMethod(functionName, ...args);
|
|
144
|
-
};
|
|
145
|
-
const login = (options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
146
|
-
const authClient = agentManager.getAuthClient();
|
|
147
|
-
if (!authClient) {
|
|
148
|
-
yield agentManager.authenticate();
|
|
149
|
-
}
|
|
150
|
-
yield authClient.login(Object.assign({ identityProvider: isLocalEnv
|
|
151
|
-
? "https://identity.ic0.app/#authorize"
|
|
152
|
-
: "http://rdmx6-jaaaa-aaaaa-aaadq-cai.localhost:4943/#authorize" }, options));
|
|
153
|
-
});
|
|
154
|
-
const logout = (options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
155
|
-
const authClient = agentManager.getAuthClient();
|
|
156
|
-
if (!authClient) {
|
|
157
|
-
throw new Error("Auth client not initialized");
|
|
158
|
-
}
|
|
159
|
-
yield authClient.logout(options);
|
|
160
|
-
yield agentManager.authenticate();
|
|
161
|
-
});
|
|
162
|
-
return Object.assign(Object.assign({ queryCall,
|
|
163
|
-
updateCall,
|
|
164
|
-
callMethod,
|
|
165
|
-
getState,
|
|
166
|
-
login,
|
|
167
|
-
logout,
|
|
168
|
-
subscribeActorState }, agentManager), rest);
|
|
169
|
-
};
|
|
170
|
-
exports.createReActor = createReActor;
|
|
171
|
-
/**
|
|
172
|
-
* Create a new actor manager with the given options.
|
|
173
|
-
* Its create a new agent manager if not provided.
|
|
174
|
-
* It also creates a new actor manager with the given options.
|
|
175
|
-
*
|
|
176
|
-
* @includeExample ./packages/core/README.md:32-45
|
|
177
|
-
*/
|
|
178
|
-
const createReActorStore = (options) => {
|
|
179
|
-
const { idlFactory, canisterId, withDevtools = false, initializeOnCreate = true, withVisitor = false, agentManager: maybeAgentManager } = options, agentOptions = __rest(options, ["idlFactory", "canisterId", "withDevtools", "initializeOnCreate", "withVisitor", "agentManager"]);
|
|
180
|
-
const agentManager = maybeAgentManager ||
|
|
181
|
-
(0, exports.createAgentManager)(Object.assign({ withDevtools }, agentOptions));
|
|
182
|
-
const actorManager = (0, exports.createActorManager)({
|
|
183
|
-
idlFactory,
|
|
184
|
-
canisterId,
|
|
185
|
-
agentManager,
|
|
186
|
-
withVisitor,
|
|
187
|
-
withDevtools,
|
|
188
|
-
initializeOnCreate,
|
|
189
|
-
});
|
|
190
|
-
return actorManager;
|
|
191
|
-
};
|
|
192
|
-
exports.createReActorStore = createReActorStore;
|
|
193
|
-
/**
|
|
194
|
-
* Agent manager handles the lifecycle of the `@dfinity/agent`.
|
|
195
|
-
* It is responsible for creating agent and managing the agent's state.
|
|
196
|
-
* You can use it to subscribe to the agent changes.
|
|
197
|
-
* login and logout to the internet identity.
|
|
198
|
-
*
|
|
199
|
-
* @includeExample ./packages/core/README.md:55-86
|
|
200
|
-
*/
|
|
201
|
-
const createAgentManager = (options) => {
|
|
202
|
-
return new agent_1.AgentManager(options);
|
|
203
|
-
};
|
|
204
|
-
exports.createAgentManager = createAgentManager;
|
|
205
|
-
/**
|
|
206
|
-
* Actor manager handles the lifecycle of the actors.
|
|
207
|
-
* It is responsible for creating and managing the actors.
|
|
208
|
-
* You can use it to call and visit the actor's methods.
|
|
209
|
-
* It also provides a way to interact with the actor's state.
|
|
210
|
-
*
|
|
211
|
-
* @includeExample ./packages/core/README.md:94-109
|
|
212
|
-
*/
|
|
213
|
-
const createActorManager = (options) => {
|
|
214
|
-
return new actor_1.ActorManager(options);
|
|
215
|
-
};
|
|
216
|
-
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;
|
|
29
|
+
exports.tools = exports.types = void 0;
|
|
30
|
+
__exportStar(require("./main"), exports);
|
|
31
|
+
__exportStar(require("./store"), exports);
|
|
32
|
+
__exportStar(require("./other"), exports);
|
|
33
|
+
__exportStar(require("./actor"), exports);
|
|
34
|
+
__exportStar(require("./agent"), exports);
|
|
35
|
+
__exportStar(require("./candid"), exports);
|
|
228
36
|
exports.types = __importStar(require("./types"));
|
|
229
|
-
exports.actor = __importStar(require("./actor"));
|
|
230
|
-
exports.agent = __importStar(require("./agent"));
|
|
231
37
|
exports.tools = __importStar(require("./tools"));
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BaseActor, ReactorCore, CreateReactorOptions } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* The Core module is the main entry point for the library.
|
|
4
|
+
* Create a new actor manager with the given options.
|
|
5
|
+
* Its create a new agent manager if not provided.
|
|
6
|
+
*
|
|
7
|
+
* @category Main
|
|
8
|
+
* @includeExample ./packages/core/README.md:26-80
|
|
9
|
+
*/
|
|
10
|
+
export declare const createReactorCore: <A = BaseActor>({ isLocalEnv, withProcessEnv, ...options }: CreateReactorOptions) => ReactorCore<A>;
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
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.createReactorCore = void 0;
|
|
24
|
+
const constants_1 = require("./tools/constants");
|
|
25
|
+
const tools_1 = require("./tools");
|
|
26
|
+
const store_1 = require("./store");
|
|
27
|
+
/**
|
|
28
|
+
* The Core module is the main entry point for the library.
|
|
29
|
+
* Create a new actor manager with the given options.
|
|
30
|
+
* Its create a new agent manager if not provided.
|
|
31
|
+
*
|
|
32
|
+
* @category Main
|
|
33
|
+
* @includeExample ./packages/core/README.md:26-80
|
|
34
|
+
*/
|
|
35
|
+
const createReactorCore = (_a) => {
|
|
36
|
+
var { isLocalEnv, withProcessEnv = false } = _a, options = __rest(_a, ["isLocalEnv", "withProcessEnv"]);
|
|
37
|
+
isLocalEnv = isLocalEnv || (withProcessEnv ? (0, tools_1.isInLocalOrDevelopment)() : false);
|
|
38
|
+
const _b = (0, store_1.createReactorStore)(Object.assign({ isLocalEnv }, options)), { subscribeActorState, updateMethodState, callMethod, getState, agentManager } = _b, rest = __rest(_b, ["subscribeActorState", "updateMethodState", "callMethod", "getState", "agentManager"]);
|
|
39
|
+
const reActorMethod = (functionName, ...args) => {
|
|
40
|
+
const requestHash = (0, tools_1.generateRequestHash)(args);
|
|
41
|
+
const updateState = (newState = {}) => {
|
|
42
|
+
updateMethodState(functionName, requestHash, newState);
|
|
43
|
+
};
|
|
44
|
+
updateState();
|
|
45
|
+
try {
|
|
46
|
+
const methodState = ((key) => {
|
|
47
|
+
const state = getState().methodState[functionName][requestHash];
|
|
48
|
+
switch (key) {
|
|
49
|
+
case "data":
|
|
50
|
+
return state.data;
|
|
51
|
+
case "loading":
|
|
52
|
+
return state.loading;
|
|
53
|
+
case "error":
|
|
54
|
+
return state.error;
|
|
55
|
+
default:
|
|
56
|
+
return state;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
const subscribe = (callback) => {
|
|
60
|
+
const unsubscribe = subscribeActorState((state) => {
|
|
61
|
+
const methodState = state.methodState[functionName];
|
|
62
|
+
const methodStateHash = methodState[requestHash];
|
|
63
|
+
if (methodStateHash) {
|
|
64
|
+
callback(methodStateHash);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
return unsubscribe;
|
|
68
|
+
};
|
|
69
|
+
const call = (replaceArgs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
70
|
+
updateState({
|
|
71
|
+
loading: true,
|
|
72
|
+
error: undefined,
|
|
73
|
+
});
|
|
74
|
+
try {
|
|
75
|
+
const data = yield callMethod(functionName, ...(replaceArgs !== null && replaceArgs !== void 0 ? replaceArgs : args));
|
|
76
|
+
updateState({ data, loading: false });
|
|
77
|
+
return data;
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
updateState({
|
|
81
|
+
error: error,
|
|
82
|
+
loading: false,
|
|
83
|
+
});
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
return {
|
|
88
|
+
requestHash,
|
|
89
|
+
subscribe,
|
|
90
|
+
getState: methodState,
|
|
91
|
+
call,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
updateState({
|
|
96
|
+
error: error,
|
|
97
|
+
loading: false,
|
|
98
|
+
});
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const queryCall = ({ functionName, args = [], refetchOnMount = true, refetchInterval = false, }) => {
|
|
103
|
+
let intervalId = null;
|
|
104
|
+
const _a = reActorMethod(functionName, ...args), { call } = _a, rest = __rest(_a, ["call"]);
|
|
105
|
+
if (refetchInterval) {
|
|
106
|
+
intervalId = setInterval(() => {
|
|
107
|
+
call();
|
|
108
|
+
}, refetchInterval);
|
|
109
|
+
}
|
|
110
|
+
let dataPromise = Promise.resolve();
|
|
111
|
+
if (refetchOnMount)
|
|
112
|
+
dataPromise = call();
|
|
113
|
+
return Object.assign(Object.assign({}, rest), { call, dataPromise, intervalId });
|
|
114
|
+
};
|
|
115
|
+
const updateCall = ({ functionName, args = [] }) => {
|
|
116
|
+
return reActorMethod(functionName, ...args);
|
|
117
|
+
};
|
|
118
|
+
const login = (options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
119
|
+
const authClient = agentManager.getAuthClient();
|
|
120
|
+
if (!authClient) {
|
|
121
|
+
yield agentManager.authenticate();
|
|
122
|
+
}
|
|
123
|
+
if (!authClient) {
|
|
124
|
+
throw new Error("Auth client not initialized");
|
|
125
|
+
}
|
|
126
|
+
yield authClient.login(Object.assign({ identityProvider: isLocalEnv
|
|
127
|
+
? constants_1.IC_INTERNET_IDENTITY_PROVIDER
|
|
128
|
+
: constants_1.LOCAL_INTERNET_IDENTITY_PROVIDER }, options));
|
|
129
|
+
});
|
|
130
|
+
const logout = (options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
131
|
+
const authClient = agentManager.getAuthClient();
|
|
132
|
+
if (!authClient) {
|
|
133
|
+
throw new Error("Auth client not initialized");
|
|
134
|
+
}
|
|
135
|
+
yield authClient.logout(options);
|
|
136
|
+
yield agentManager.authenticate();
|
|
137
|
+
});
|
|
138
|
+
return Object.assign(Object.assign({ queryCall,
|
|
139
|
+
updateCall,
|
|
140
|
+
callMethod,
|
|
141
|
+
getState,
|
|
142
|
+
login,
|
|
143
|
+
logout,
|
|
144
|
+
subscribeActorState }, agentManager), rest);
|
|
145
|
+
};
|
|
146
|
+
exports.createReactorCore = createReactorCore;
|
package/dist/other.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ActorManager } from "./actor";
|
|
2
|
+
import { AgentManager } from "./agent";
|
|
3
|
+
import { CandidAdapter } from "./candid";
|
|
4
|
+
import { ActorManagerOptions, AgentManagerOptions, BaseActor, CandidAdapterOptions } 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: (options: CandidAdapterOptions) => 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: (options?: AgentManagerOptions) => 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>(options: ActorManagerOptions) => ActorManager<A>;
|
package/dist/other.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createActorManager = exports.createAgentManager = exports.createCandidAdapter = void 0;
|
|
4
|
+
const actor_1 = require("./actor");
|
|
5
|
+
const agent_1 = require("./agent");
|
|
6
|
+
const candid_1 = require("./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 = (options) => {
|
|
16
|
+
return new candid_1.CandidAdapter(options);
|
|
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 = (options) => {
|
|
29
|
+
return new agent_1.AgentManager(options);
|
|
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 = (options) => {
|
|
42
|
+
return new actor_1.ActorManager(options);
|
|
43
|
+
};
|
|
44
|
+
exports.createActorManager = createActorManager;
|
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ActorManager } from "./actor";
|
|
2
|
+
import type { BaseActor, CreateReactorStoreOptions } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Create a new actor manager with the given options.
|
|
5
|
+
* Its create a new agent manager if not provided.
|
|
6
|
+
* It also creates a new actor manager with the given options.
|
|
7
|
+
*
|
|
8
|
+
* @category Main
|
|
9
|
+
* @includeExample ./packages/core/README.md:194-220
|
|
10
|
+
*/
|
|
11
|
+
export declare const createReactorStore: <A = BaseActor>(options: CreateReactorStoreOptions) => ActorManager<A>;
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
+
var t = {};
|
|
4
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
+
t[p] = s[p];
|
|
6
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
+
t[p[i]] = s[p[i]];
|
|
10
|
+
}
|
|
11
|
+
return t;
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.createReactorStore = void 0;
|
|
15
|
+
const other_1 = require("./other");
|
|
16
|
+
/**
|
|
17
|
+
* Create a new actor manager with the given options.
|
|
18
|
+
* Its create a new agent manager if not provided.
|
|
19
|
+
* It also creates a new actor manager with the given options.
|
|
20
|
+
*
|
|
21
|
+
* @category Main
|
|
22
|
+
* @includeExample ./packages/core/README.md:194-220
|
|
23
|
+
*/
|
|
24
|
+
const createReactorStore = (options) => {
|
|
25
|
+
const { idlFactory, canisterId, withDevtools = false, initializeOnCreate = true, withVisitor = false, agentManager: maybeAgentManager } = options, agentOptions = __rest(options, ["idlFactory", "canisterId", "withDevtools", "initializeOnCreate", "withVisitor", "agentManager"]);
|
|
26
|
+
const agentManager = maybeAgentManager ||
|
|
27
|
+
(0, other_1.createAgentManager)(Object.assign({ withDevtools }, agentOptions));
|
|
28
|
+
const actorManager = (0, other_1.createActorManager)({
|
|
29
|
+
idlFactory,
|
|
30
|
+
canisterId,
|
|
31
|
+
agentManager,
|
|
32
|
+
withVisitor,
|
|
33
|
+
withDevtools,
|
|
34
|
+
initializeOnCreate,
|
|
35
|
+
});
|
|
36
|
+
return actorManager;
|
|
37
|
+
};
|
|
38
|
+
exports.createReactorStore = createReactorStore;
|
|
@@ -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";
|
package/dist/tools/helper.d.ts
CHANGED
|
@@ -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
|
|
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}`;
|
package/dist/tools/helper.js
CHANGED
|
@@ -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: "
|
|
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
|
-
|
|
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
|
package/dist/tools/index.d.ts
CHANGED
package/dist/tools/index.js
CHANGED
|
@@ -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,13 +8,13 @@ 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 "./
|
|
11
|
+
export * from "./candid/types";
|
|
12
12
|
export type { ActorMethod, HttpAgentOptions, ActorSubclass, Principal, HttpAgent, Identity, IDL, };
|
|
13
|
-
export interface
|
|
13
|
+
export interface CreateReactorOptions extends CreateReactorStoreOptions {
|
|
14
|
+
withProcessEnv?: boolean;
|
|
14
15
|
}
|
|
15
|
-
export interface
|
|
16
|
+
export interface CreateReactorStoreOptions extends HttpAgentOptions, Omit<ActorManagerOptions, "agentManager"> {
|
|
16
17
|
agentManager?: AgentManager;
|
|
17
|
-
withProcessEnv?: boolean;
|
|
18
18
|
isLocalEnv?: boolean;
|
|
19
19
|
port?: number;
|
|
20
20
|
}
|
|
@@ -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
|
|
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("./
|
|
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.
|
|
3
|
+
"version": "1.0.3",
|
|
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": "
|
|
46
|
+
"gitHead": "acf8b5da61713482264c9eb5c227dc94a7e43cbc"
|
|
47
47
|
}
|
|
File without changes
|
|
File without changes
|