@ic-reactor/core 0.5.3 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +161 -65
- package/dist/actor/index.d.ts +31 -0
- package/dist/actor/index.js +142 -0
- package/dist/actor/types.d.ts +46 -0
- package/dist/actor/types.js +2 -0
- package/dist/agent/index.d.ts +31 -0
- package/dist/agent/index.js +183 -0
- package/dist/agent/types.d.ts +26 -0
- package/dist/agent/types.js +2 -0
- package/dist/index.d.ts +49 -20
- package/dist/index.js +150 -47
- package/dist/tools/candid.d.ts +14 -0
- package/dist/tools/candid.js +97 -0
- package/dist/tools/helper.d.ts +16 -0
- package/dist/tools/helper.js +50 -0
- package/dist/tools/index.d.ts +2 -0
- package/dist/tools/index.js +18 -0
- package/dist/tools/types.d.ts +13 -0
- package/dist/tools/types.js +2 -0
- package/dist/types.d.ts +40 -19
- package/dist/types.js +17 -0
- package/package.json +15 -18
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
|
+
exports.AgentManager = exports.LOCAL_HOST_NETWORK_URI = exports.IC_HOST_NETWORK_URI = void 0;
|
|
47
|
+
const agent_1 = require("@dfinity/agent");
|
|
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";
|
|
51
|
+
class AgentManager {
|
|
52
|
+
constructor(options) {
|
|
53
|
+
this._subscribers = [];
|
|
54
|
+
this.initialAgentState = {
|
|
55
|
+
initialized: false,
|
|
56
|
+
initializing: false,
|
|
57
|
+
error: undefined,
|
|
58
|
+
};
|
|
59
|
+
this.initialAuthState = {
|
|
60
|
+
identity: null,
|
|
61
|
+
authClient: null,
|
|
62
|
+
authenticating: false,
|
|
63
|
+
authenticated: false,
|
|
64
|
+
error: undefined,
|
|
65
|
+
};
|
|
66
|
+
this.updateAgentState = (newState) => {
|
|
67
|
+
this.agentStore.setState((state) => (Object.assign(Object.assign({}, state), newState)));
|
|
68
|
+
};
|
|
69
|
+
this.updateAuthState = (newState) => {
|
|
70
|
+
this.authStore.setState((state) => (Object.assign(Object.assign({}, state), newState)));
|
|
71
|
+
};
|
|
72
|
+
this.initializeAgent = () => __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
this.updateAgentState({ initializing: true });
|
|
74
|
+
if (this.isLocalEnv) {
|
|
75
|
+
try {
|
|
76
|
+
yield this._agent.fetchRootKey();
|
|
77
|
+
this.updateAgentState({ initialized: true, initializing: false });
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
this.updateAgentState({ error: error, initializing: false });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
this.subscribeAgent = (callback) => {
|
|
85
|
+
this._subscribers.push(callback);
|
|
86
|
+
return () => this.unsubscribeAgent(callback);
|
|
87
|
+
};
|
|
88
|
+
this.unsubscribeAgent = (callback) => {
|
|
89
|
+
this._subscribers = this._subscribers.filter((sub) => sub !== callback);
|
|
90
|
+
};
|
|
91
|
+
this.notifySubscribers = () => {
|
|
92
|
+
this._subscribers.forEach((callback) => callback(this._agent));
|
|
93
|
+
};
|
|
94
|
+
this.updateAgent = (options) => __awaiter(this, void 0, void 0, function* () {
|
|
95
|
+
const { agent } = options || {};
|
|
96
|
+
if (agent) {
|
|
97
|
+
this._agent = agent;
|
|
98
|
+
}
|
|
99
|
+
else if (options) {
|
|
100
|
+
this._agent = new agent_1.HttpAgent(options);
|
|
101
|
+
this.isLocalEnv = this._agent.isLocal();
|
|
102
|
+
yield this.initializeAgent();
|
|
103
|
+
}
|
|
104
|
+
this.notifySubscribers();
|
|
105
|
+
});
|
|
106
|
+
this.authenticate = () => __awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
this.updateAuthState({ authenticating: true });
|
|
108
|
+
try {
|
|
109
|
+
const { AuthClient } = yield Promise.resolve().then(() => __importStar(require("@dfinity/auth-client"))).catch((error) => {
|
|
110
|
+
// eslint-disable-next-line no-console
|
|
111
|
+
console.error("Failed to import @dfinity/auth-client:", error);
|
|
112
|
+
throw new Error("Authentication failed: @dfinity/auth-client package is missing.");
|
|
113
|
+
});
|
|
114
|
+
const authClient = yield AuthClient.create();
|
|
115
|
+
const authenticated = yield authClient.isAuthenticated();
|
|
116
|
+
const identity = authClient.getIdentity();
|
|
117
|
+
this._agent.replaceIdentity(identity);
|
|
118
|
+
this.notifySubscribers();
|
|
119
|
+
this.updateAuthState({
|
|
120
|
+
authClient,
|
|
121
|
+
authenticated,
|
|
122
|
+
identity,
|
|
123
|
+
authenticating: false,
|
|
124
|
+
});
|
|
125
|
+
return identity;
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
this.updateAuthState({ error: error, authenticating: false });
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
// agent store
|
|
133
|
+
this.getAgent = () => {
|
|
134
|
+
return this._agent;
|
|
135
|
+
};
|
|
136
|
+
this.getAgentStore = () => {
|
|
137
|
+
return this.agentStore;
|
|
138
|
+
};
|
|
139
|
+
this.getAgentState = () => {
|
|
140
|
+
return this.agentStore.getState();
|
|
141
|
+
};
|
|
142
|
+
this.subscribeAgentState = (listener) => {
|
|
143
|
+
return this.agentStore.subscribe(listener);
|
|
144
|
+
};
|
|
145
|
+
// auth store
|
|
146
|
+
this.getAuthState = () => {
|
|
147
|
+
return this.authStore.getState();
|
|
148
|
+
};
|
|
149
|
+
this.subscribeAuthState = (listener) => {
|
|
150
|
+
return this.authStore.subscribe(listener);
|
|
151
|
+
};
|
|
152
|
+
this.getAuthClient = () => {
|
|
153
|
+
return this.authStore.getState().authClient;
|
|
154
|
+
};
|
|
155
|
+
this.getIdentity = () => {
|
|
156
|
+
return this.authStore.getState().identity;
|
|
157
|
+
};
|
|
158
|
+
this.getPrincipal = () => {
|
|
159
|
+
const identity = this.authStore.getState().identity;
|
|
160
|
+
return identity ? identity.getPrincipal() : null;
|
|
161
|
+
};
|
|
162
|
+
const _a = options || {}, { withDevtools, port = 4943, isLocalEnv, host: optionHost } = _a, agentOptions = __rest(_a, ["withDevtools", "port", "isLocalEnv", "host"]);
|
|
163
|
+
const host = isLocalEnv
|
|
164
|
+
? `http://127.0.0.1:${port}`
|
|
165
|
+
: optionHost
|
|
166
|
+
? optionHost.includes("localhost")
|
|
167
|
+
? optionHost.replace("localhost", "127.0.0.1")
|
|
168
|
+
: optionHost
|
|
169
|
+
: exports.IC_HOST_NETWORK_URI;
|
|
170
|
+
this.agentStore = (0, helper_1.createStoreWithOptionalDevtools)(this.initialAgentState, {
|
|
171
|
+
withDevtools,
|
|
172
|
+
store: "agent",
|
|
173
|
+
});
|
|
174
|
+
this.authStore = (0, helper_1.createStoreWithOptionalDevtools)(this.initialAuthState, {
|
|
175
|
+
withDevtools,
|
|
176
|
+
store: "auth",
|
|
177
|
+
});
|
|
178
|
+
this._agent = new agent_1.HttpAgent(Object.assign(Object.assign({}, agentOptions), { host }));
|
|
179
|
+
this.isLocalEnv = this._agent.isLocal();
|
|
180
|
+
this.initializeAgent();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
exports.AgentManager = AgentManager;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { HttpAgent, HttpAgentOptions, Identity } from "@dfinity/agent";
|
|
2
|
+
import type { AuthClient } from "@dfinity/auth-client";
|
|
3
|
+
import type { StoreApi } from "zustand";
|
|
4
|
+
export { HttpAgentOptions, AuthClient, Identity };
|
|
5
|
+
export interface AgentManagerOptions extends HttpAgentOptions {
|
|
6
|
+
port?: number;
|
|
7
|
+
isLocalEnv?: boolean;
|
|
8
|
+
withDevtools?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface AgentState {
|
|
11
|
+
initialized: boolean;
|
|
12
|
+
initializing: boolean;
|
|
13
|
+
error: Error | undefined;
|
|
14
|
+
}
|
|
15
|
+
export interface AuthState {
|
|
16
|
+
identity: Identity | null;
|
|
17
|
+
authClient: AuthClient | null;
|
|
18
|
+
authenticating: boolean;
|
|
19
|
+
authenticated: boolean;
|
|
20
|
+
error: Error | undefined;
|
|
21
|
+
}
|
|
22
|
+
export interface UpdateAgentOptions extends HttpAgentOptions {
|
|
23
|
+
agent?: HttpAgent;
|
|
24
|
+
}
|
|
25
|
+
export type AgentStore = StoreApi<AgentState>;
|
|
26
|
+
export type AuthStore = StoreApi<AuthState>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,49 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
import { ActorManager } from "./actor";
|
|
2
|
+
import { AgentManager } from "./agent";
|
|
3
|
+
import { CandidAdapter } from "./tools";
|
|
4
|
+
import type { ActorManagerOptions, BaseActor, AgentManagerOptions, ActorCoreActions, CreateReActorOptions, CreateReActorStoreOptions, CandidAdapterOptions } from "./types";
|
|
5
|
+
/**
|
|
6
|
+
* Create a new actor manager with the given options.
|
|
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;
|
|
46
|
+
export * as types from "./types";
|
|
47
|
+
export * as actor from "./actor";
|
|
48
|
+
export * as agent from "./agent";
|
|
49
|
+
export * as tools from "./tools";
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
26
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
27
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -20,36 +43,35 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
20
43
|
return t;
|
|
21
44
|
};
|
|
22
45
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
exports.createReActor = void 0;
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
};
|
|
44
|
-
return Object.assign(Object.assign({}, state), { methodState: Object.assign(Object.assign({}, state.methodState), { [method]: Object.assign(Object.assign({}, state.methodState[method]), { states: Object.assign(Object.assign({}, state.methodState[method].states), { [hash]: Object.assign(Object.assign({}, currentMethodState), newState) }) }) }) });
|
|
45
|
-
});
|
|
46
|
-
return hash;
|
|
47
|
-
};
|
|
46
|
+
exports.tools = exports.agent = exports.actor = exports.types = exports.createCandidAdapter = exports.createActorManager = exports.createAgentManager = exports.createReActorStore = exports.createReActor = void 0;
|
|
47
|
+
const actor_1 = require("./actor");
|
|
48
|
+
const agent_1 = require("./agent");
|
|
49
|
+
const tools_1 = require("./tools");
|
|
50
|
+
/**
|
|
51
|
+
* Create a new actor manager with the given options.
|
|
52
|
+
* Its create a new agent manager if not provided.
|
|
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"]);
|
|
48
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();
|
|
49
72
|
try {
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
const state = actorStore.getState().methodState[functionName][requestHash];
|
|
73
|
+
const methodState = ((key) => {
|
|
74
|
+
const state = getState().methodState[functionName][requestHash];
|
|
53
75
|
switch (key) {
|
|
54
76
|
case "data":
|
|
55
77
|
return state.data;
|
|
@@ -60,9 +82,9 @@ const createReActor = (options) => {
|
|
|
60
82
|
default:
|
|
61
83
|
return state;
|
|
62
84
|
}
|
|
63
|
-
};
|
|
85
|
+
});
|
|
64
86
|
const subscribe = (callback) => {
|
|
65
|
-
const unsubscribe =
|
|
87
|
+
const unsubscribe = subscribeActorState((state) => {
|
|
66
88
|
const methodState = state.methodState[functionName];
|
|
67
89
|
const methodStateHash = methodState[requestHash];
|
|
68
90
|
if (methodStateHash) {
|
|
@@ -72,57 +94,138 @@ const createReActor = (options) => {
|
|
|
72
94
|
return unsubscribe;
|
|
73
95
|
};
|
|
74
96
|
const call = (replaceArgs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
75
|
-
|
|
97
|
+
updateState({
|
|
76
98
|
loading: true,
|
|
77
99
|
error: undefined,
|
|
78
100
|
});
|
|
79
101
|
try {
|
|
80
102
|
const data = yield callMethod(functionName, ...(replaceArgs !== null && replaceArgs !== void 0 ? replaceArgs : args));
|
|
81
|
-
|
|
103
|
+
updateState({ data, loading: false });
|
|
82
104
|
return data;
|
|
83
105
|
}
|
|
84
106
|
catch (error) {
|
|
85
|
-
|
|
107
|
+
updateState({
|
|
86
108
|
error: error,
|
|
87
109
|
loading: false,
|
|
88
110
|
});
|
|
89
|
-
|
|
111
|
+
throw error;
|
|
90
112
|
}
|
|
91
113
|
});
|
|
92
114
|
return {
|
|
93
115
|
requestHash,
|
|
94
116
|
subscribe,
|
|
95
|
-
getState,
|
|
117
|
+
getState: methodState,
|
|
96
118
|
call,
|
|
97
119
|
};
|
|
98
120
|
}
|
|
99
121
|
catch (error) {
|
|
100
|
-
|
|
122
|
+
updateState({
|
|
101
123
|
error: error,
|
|
102
124
|
loading: false,
|
|
103
125
|
});
|
|
104
126
|
throw error;
|
|
105
127
|
}
|
|
106
128
|
};
|
|
107
|
-
const queryCall = ({ functionName, args = [],
|
|
129
|
+
const queryCall = ({ functionName, args = [], refetchOnMount = true, refetchInterval = false, }) => {
|
|
108
130
|
let intervalId = null;
|
|
109
131
|
const _a = reActorMethod(functionName, ...args), { call } = _a, rest = __rest(_a, ["call"]);
|
|
110
|
-
if (
|
|
132
|
+
if (refetchInterval) {
|
|
111
133
|
intervalId = setInterval(() => {
|
|
112
134
|
call();
|
|
113
|
-
},
|
|
135
|
+
}, refetchInterval);
|
|
114
136
|
}
|
|
115
|
-
let
|
|
116
|
-
if (
|
|
117
|
-
|
|
118
|
-
return Object.assign(Object.assign({}, rest), { call,
|
|
137
|
+
let dataPromise = Promise.resolve();
|
|
138
|
+
if (refetchOnMount)
|
|
139
|
+
dataPromise = call();
|
|
140
|
+
return Object.assign(Object.assign({}, rest), { call, dataPromise, intervalId });
|
|
119
141
|
};
|
|
120
142
|
const updateCall = ({ functionName, args = [] }) => {
|
|
121
143
|
return reActorMethod(functionName, ...args);
|
|
122
144
|
};
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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);
|
|
127
169
|
};
|
|
128
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;
|
|
228
|
+
exports.types = __importStar(require("./types"));
|
|
229
|
+
exports.actor = __importStar(require("./actor"));
|
|
230
|
+
exports.agent = __importStar(require("./agent"));
|
|
231
|
+
exports.tools = __importStar(require("./tools"));
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { HttpAgent } from "@dfinity/agent";
|
|
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
|
+
export declare class CandidAdapter {
|
|
6
|
+
agent: HttpAgent;
|
|
7
|
+
didjsCanisterId: string;
|
|
8
|
+
constructor({ agentManager, agent, didjsCanisterId }: CandidAdapterOptions);
|
|
9
|
+
private getDefaultDidJsId;
|
|
10
|
+
getCandidDefinition(canisterId: CanisterId): Promise<CandidDefenition>;
|
|
11
|
+
getFromMetadata(canisterId: CanisterId): Promise<CandidDefenition | undefined>;
|
|
12
|
+
getFromTmpHack(canisterId: CanisterId): Promise<CandidDefenition | undefined>;
|
|
13
|
+
didTojs(candidSource: string): Promise<CandidDefenition>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.CandidAdapter = exports.DEFAULT_IC_DIDJS_ID = exports.DEFAULT_LOCAL_DIDJS_ID = void 0;
|
|
13
|
+
const agent_1 = require("@dfinity/agent");
|
|
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";
|
|
17
|
+
class CandidAdapter {
|
|
18
|
+
constructor({ agentManager, agent, didjsCanisterId }) {
|
|
19
|
+
if (agent) {
|
|
20
|
+
this.agent = agent;
|
|
21
|
+
}
|
|
22
|
+
else if (agentManager) {
|
|
23
|
+
this.agent = agentManager.getAgent();
|
|
24
|
+
agentManager.subscribeAgent((agent) => {
|
|
25
|
+
this.agent = agent;
|
|
26
|
+
this.didjsCanisterId = didjsCanisterId || this.getDefaultDidJsId();
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
throw new Error("No agent or agentManager provided");
|
|
31
|
+
}
|
|
32
|
+
this.didjsCanisterId = didjsCanisterId || this.getDefaultDidJsId();
|
|
33
|
+
}
|
|
34
|
+
getDefaultDidJsId() {
|
|
35
|
+
return this.agent.isLocal() ? exports.DEFAULT_LOCAL_DIDJS_ID : exports.DEFAULT_IC_DIDJS_ID;
|
|
36
|
+
}
|
|
37
|
+
getCandidDefinition(canisterId) {
|
|
38
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
// First attempt: Try getting Candid definition from metadata
|
|
40
|
+
const fromMetadata = yield this.getFromMetadata(canisterId);
|
|
41
|
+
if (fromMetadata)
|
|
42
|
+
return fromMetadata;
|
|
43
|
+
// Second attempt: Try the temporary hack method
|
|
44
|
+
const fromTmpHack = yield this.getFromTmpHack(canisterId);
|
|
45
|
+
if (fromTmpHack)
|
|
46
|
+
return fromTmpHack;
|
|
47
|
+
// If both attempts fail, throw an error
|
|
48
|
+
throw new Error("Failed to retrieve Candid definition by any method.");
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
getFromMetadata(canisterId) {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
if (typeof canisterId === "string") {
|
|
54
|
+
canisterId = principal_1.Principal.fromText(canisterId);
|
|
55
|
+
}
|
|
56
|
+
const status = yield agent_1.CanisterStatus.request({
|
|
57
|
+
agent: this.agent,
|
|
58
|
+
canisterId,
|
|
59
|
+
paths: ["candid"],
|
|
60
|
+
});
|
|
61
|
+
const did = status.get("candid");
|
|
62
|
+
return did ? this.didTojs(did) : undefined;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
getFromTmpHack(canisterId) {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
const commonInterface = ({ IDL }) => IDL.Service({
|
|
68
|
+
__get_candid_interface_tmp_hack: IDL.Func([], [IDL.Text], ["query"]),
|
|
69
|
+
});
|
|
70
|
+
const actor = agent_1.Actor.createActor(commonInterface, {
|
|
71
|
+
agent: this.agent,
|
|
72
|
+
canisterId,
|
|
73
|
+
});
|
|
74
|
+
const data = (yield actor.__get_candid_interface_tmp_hack());
|
|
75
|
+
return data ? this.didTojs(data) : undefined;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
didTojs(candidSource) {
|
|
79
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
const didjsInterface = ({ IDL }) => IDL.Service({
|
|
81
|
+
did_to_js: IDL.Func([IDL.Text], [IDL.Opt(IDL.Text)], ["query"]),
|
|
82
|
+
});
|
|
83
|
+
const didjs = agent_1.Actor.createActor(didjsInterface, {
|
|
84
|
+
agent: this.agent,
|
|
85
|
+
canisterId: this.didjsCanisterId,
|
|
86
|
+
});
|
|
87
|
+
const js = yield didjs.did_to_js(candidSource);
|
|
88
|
+
if (JSON.stringify(js) === JSON.stringify([])) {
|
|
89
|
+
throw new Error("Cannot fetch candid file");
|
|
90
|
+
}
|
|
91
|
+
const dataUri = "data:text/javascript;charset=utf-8," +
|
|
92
|
+
encodeURIComponent(js[0]);
|
|
93
|
+
return eval('import("' + dataUri + '")');
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
exports.CandidAdapter = CandidAdapter;
|