@ic-reactor/core 2.0.0 → 3.0.0-beta.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/dist/client.d.ts +161 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +499 -0
- package/dist/client.js.map +1 -0
- package/dist/display/helper.d.ts +10 -0
- package/dist/display/helper.d.ts.map +1 -0
- package/dist/display/helper.js +67 -0
- package/dist/display/helper.js.map +1 -0
- package/dist/display/index.d.ts +4 -0
- package/dist/display/index.d.ts.map +1 -0
- package/dist/display/index.js +4 -0
- package/dist/display/index.js.map +1 -0
- package/dist/display/types.d.ts +31 -0
- package/dist/display/types.d.ts.map +1 -0
- package/dist/display/types.js +2 -0
- package/dist/display/types.js.map +1 -0
- package/dist/display/visitor.d.ts +28 -0
- package/dist/display/visitor.d.ts.map +1 -0
- package/dist/display/visitor.js +318 -0
- package/dist/display/visitor.js.map +1 -0
- package/dist/display-reactor.d.ts +245 -0
- package/dist/display-reactor.d.ts.map +1 -0
- package/dist/display-reactor.js +331 -0
- package/dist/display-reactor.js.map +1 -0
- package/dist/errors/index.d.ts +118 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +204 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index.d.ts +9 -8
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -47
- package/dist/index.js.map +1 -0
- package/dist/reactor.d.ts +133 -0
- package/dist/reactor.d.ts.map +1 -0
- package/dist/reactor.js +325 -0
- package/dist/reactor.js.map +1 -0
- package/dist/types/client.d.ts +89 -0
- package/dist/types/client.d.ts.map +1 -0
- package/dist/types/client.js +2 -0
- package/dist/types/client.js.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +6 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/reactor.d.ts +117 -0
- package/dist/types/reactor.d.ts.map +1 -0
- package/dist/types/reactor.js +2 -0
- package/dist/types/reactor.js.map +1 -0
- package/dist/types/result.d.ts +48 -0
- package/dist/types/result.d.ts.map +1 -0
- package/dist/types/result.js +2 -0
- package/dist/types/result.js.map +1 -0
- package/dist/types/transform.d.ts +7 -0
- package/dist/types/transform.d.ts.map +1 -0
- package/dist/types/transform.js +2 -0
- package/dist/types/transform.js.map +1 -0
- package/dist/types/variant.d.ts +18 -0
- package/dist/types/variant.d.ts.map +1 -0
- package/dist/types/variant.js +2 -0
- package/dist/types/variant.js.map +1 -0
- package/dist/utils/agent.d.ts +30 -1
- package/dist/utils/agent.d.ts.map +1 -0
- package/dist/utils/agent.js +118 -16
- package/dist/utils/agent.js.map +1 -0
- package/dist/utils/candid.d.ts +39 -1
- package/dist/utils/candid.d.ts.map +1 -0
- package/dist/utils/candid.js +76 -16
- package/dist/utils/candid.js.map +1 -0
- package/dist/utils/constants.d.ts +3 -4
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +7 -11
- package/dist/utils/constants.js.map +1 -0
- package/dist/utils/helper.d.ts +16 -39
- package/dist/utils/helper.d.ts.map +1 -0
- package/dist/utils/helper.js +53 -155
- package/dist/utils/helper.js.map +1 -0
- package/dist/utils/index.d.ts +4 -5
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +5 -49
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/polling.d.ts +176 -0
- package/dist/utils/polling.d.ts.map +1 -0
- package/dist/utils/polling.js +170 -0
- package/dist/utils/polling.js.map +1 -0
- package/dist/version.d.ts +5 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +5 -0
- package/dist/version.js.map +1 -0
- package/package.json +65 -39
- package/LICENSE.md +0 -8
- package/README.md +0 -283
- package/dist/classes/actor/index.d.ts +0 -34
- package/dist/classes/actor/index.js +0 -245
- package/dist/classes/actor/types.d.ts +0 -113
- package/dist/classes/actor/types.js +0 -2
- package/dist/classes/adapter/index.d.ts +0 -19
- package/dist/classes/adapter/index.js +0 -140
- package/dist/classes/adapter/types.d.ts +0 -14
- package/dist/classes/adapter/types.js +0 -2
- package/dist/classes/agent/index.d.ts +0 -36
- package/dist/classes/agent/index.js +0 -218
- package/dist/classes/agent/types.d.ts +0 -87
- package/dist/classes/agent/types.js +0 -2
- package/dist/classes/index.d.ts +0 -3
- package/dist/classes/index.js +0 -19
- package/dist/classes/types.d.ts +0 -15
- package/dist/classes/types.js +0 -20
- package/dist/createActorManager.d.ts +0 -12
- package/dist/createActorManager.js +0 -17
- package/dist/createAgentManager.d.ts +0 -12
- package/dist/createAgentManager.js +0 -17
- package/dist/createCandidAdapter.d.ts +0 -11
- package/dist/createCandidAdapter.js +0 -16
- package/dist/createReactorCore.d.ts +0 -10
- package/dist/createReactorCore.js +0 -112
- package/dist/createReactorStore.d.ts +0 -11
- package/dist/createReactorStore.js +0 -31
- package/dist/types.d.ts +0 -96
- package/dist/types.js +0 -17
- package/dist/utils/hash.d.ts +0 -12
- package/dist/utils/hash.js +0 -70
- package/dist/utils/principal.d.ts +0 -1
- package/dist/utils/principal.js +0 -17
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ActorManager = void 0;
|
|
4
|
-
/* eslint-disable no-console */
|
|
5
|
-
const agent_1 = require("@dfinity/agent");
|
|
6
|
-
const helper_1 = require("../../utils/helper");
|
|
7
|
-
const candid_1 = require("@dfinity/candid");
|
|
8
|
-
const ACTOR_INITIAL_STATE = {
|
|
9
|
-
name: "",
|
|
10
|
-
version: 0,
|
|
11
|
-
methodState: {},
|
|
12
|
-
initializing: false,
|
|
13
|
-
isInitializing: false,
|
|
14
|
-
initialized: false,
|
|
15
|
-
isInitialized: false,
|
|
16
|
-
error: undefined,
|
|
17
|
-
};
|
|
18
|
-
class ActorManager {
|
|
19
|
-
_actor = null;
|
|
20
|
-
_idlFactory;
|
|
21
|
-
_agentManager;
|
|
22
|
-
_unsubscribeAgent;
|
|
23
|
-
_subscribers = [];
|
|
24
|
-
canisterId;
|
|
25
|
-
actorStore;
|
|
26
|
-
visitFunction;
|
|
27
|
-
methodAttributes;
|
|
28
|
-
updateState = (newState, action) => {
|
|
29
|
-
this.actorStore.setState((state) => ({ ...state, ...newState }), false, action);
|
|
30
|
-
};
|
|
31
|
-
updateMethodState = (method, hash, newState) => {
|
|
32
|
-
const actionName = `${method}:${newState.error ? "error" : newState.isLoading ? "loading..." : "loaded"}`;
|
|
33
|
-
this.actorStore.setState((state) => {
|
|
34
|
-
const methodState = state.methodState[method] || {};
|
|
35
|
-
const currentMethodState = methodState[hash] || DEFAULT_STATE;
|
|
36
|
-
const updatedMethodState = {
|
|
37
|
-
...methodState,
|
|
38
|
-
[hash]: { ...currentMethodState, ...newState },
|
|
39
|
-
};
|
|
40
|
-
return {
|
|
41
|
-
...state,
|
|
42
|
-
methodState: {
|
|
43
|
-
...state.methodState,
|
|
44
|
-
[method]: updatedMethodState,
|
|
45
|
-
},
|
|
46
|
-
};
|
|
47
|
-
}, false, actionName);
|
|
48
|
-
};
|
|
49
|
-
constructor(actorConfig) {
|
|
50
|
-
const { agentManager, idlFactory, canisterId, name = canisterId.toString(), withVisitor = false, withDevtools = false, initializeOnCreate = true, } = actorConfig;
|
|
51
|
-
if (!canisterId) {
|
|
52
|
-
throw new agent_1.AgentError(new agent_1.UnexpectedErrorCode("CanisterId is required!"), agent_1.ErrorKindEnum.Unknown);
|
|
53
|
-
}
|
|
54
|
-
this.canisterId = canisterId.toString();
|
|
55
|
-
if (!idlFactory) {
|
|
56
|
-
throw new agent_1.AgentError(new agent_1.UnexpectedErrorCode("IDLFactory is required!"), agent_1.ErrorKindEnum.Unknown);
|
|
57
|
-
}
|
|
58
|
-
this._idlFactory = idlFactory;
|
|
59
|
-
this.methodAttributes = this.extractMethodAttributes();
|
|
60
|
-
if (!agentManager) {
|
|
61
|
-
throw new agent_1.AgentError(new agent_1.UnexpectedErrorCode("AgentManager is required!"), agent_1.ErrorKindEnum.Unknown);
|
|
62
|
-
}
|
|
63
|
-
this._agentManager = agentManager;
|
|
64
|
-
// Initialize stores
|
|
65
|
-
this.actorStore = (0, helper_1.createStoreWithOptionalDevtools)({ ...ACTOR_INITIAL_STATE, name }, {
|
|
66
|
-
withDevtools,
|
|
67
|
-
name: "reactor-actor",
|
|
68
|
-
store: canisterId.toString(),
|
|
69
|
-
});
|
|
70
|
-
this._unsubscribeAgent = this._agentManager.subscribeAgent(this.initializeActor, initializeOnCreate);
|
|
71
|
-
if (withVisitor) {
|
|
72
|
-
this.visitFunction = this.extractVisitor();
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
75
|
-
this.visitFunction = emptyVisitor;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
initialize = async (options) => {
|
|
79
|
-
await this._agentManager.updateAgent(options);
|
|
80
|
-
};
|
|
81
|
-
extractInterface = () => {
|
|
82
|
-
return this._idlFactory({ IDL: candid_1.IDL });
|
|
83
|
-
};
|
|
84
|
-
extractMethodAttributes = () => {
|
|
85
|
-
const iface = this.extractInterface();
|
|
86
|
-
const methodAttributesArray = iface._fields.map(([name, func]) => ({
|
|
87
|
-
name: name,
|
|
88
|
-
attributes: {
|
|
89
|
-
numberOfArgs: func.argTypes.length,
|
|
90
|
-
type: ((0, helper_1.isQuery)(func) ? "query" : "update"),
|
|
91
|
-
validate: (arg) => func.argTypes.some((t, i) => t.covariant(arg[i])),
|
|
92
|
-
},
|
|
93
|
-
}));
|
|
94
|
-
methodAttributesArray.sort((a, b) => {
|
|
95
|
-
if (a.attributes.type === b.attributes.type) {
|
|
96
|
-
return a.attributes.numberOfArgs - b.attributes.numberOfArgs;
|
|
97
|
-
}
|
|
98
|
-
return a.attributes.type === "query" ? -1 : 1;
|
|
99
|
-
});
|
|
100
|
-
return methodAttributesArray.reduce((acc, { name, attributes }) => {
|
|
101
|
-
acc[name] = attributes;
|
|
102
|
-
return acc;
|
|
103
|
-
}, {});
|
|
104
|
-
};
|
|
105
|
-
extractVisitor = () => {
|
|
106
|
-
const iface = this.extractInterface();
|
|
107
|
-
return iface._fields.reduce((acc, service) => {
|
|
108
|
-
const functionName = service[0];
|
|
109
|
-
const type = service[1];
|
|
110
|
-
const visit = ((extractorClass, data) => {
|
|
111
|
-
return type.accept(extractorClass, data);
|
|
112
|
-
});
|
|
113
|
-
acc[functionName] = visit;
|
|
114
|
-
return acc;
|
|
115
|
-
}, {});
|
|
116
|
-
};
|
|
117
|
-
initializeActor = (agent) => {
|
|
118
|
-
const network = this._agentManager.getNetwork();
|
|
119
|
-
console.info(`Initializing actor ${this.canisterId} on ${network} network`);
|
|
120
|
-
const { _idlFactory, canisterId } = this;
|
|
121
|
-
this.updateState({
|
|
122
|
-
initializing: true,
|
|
123
|
-
isInitializing: true,
|
|
124
|
-
initialized: false,
|
|
125
|
-
isInitialized: false,
|
|
126
|
-
methodState: {},
|
|
127
|
-
}, "initializing");
|
|
128
|
-
try {
|
|
129
|
-
if (!agent) {
|
|
130
|
-
throw new agent_1.AgentError(new agent_1.UnexpectedErrorCode("Agent not initialized"), agent_1.ErrorKindEnum.Unknown);
|
|
131
|
-
}
|
|
132
|
-
this._actor = agent_1.Actor.createActor(_idlFactory, {
|
|
133
|
-
agent,
|
|
134
|
-
canisterId,
|
|
135
|
-
});
|
|
136
|
-
if (!this._actor) {
|
|
137
|
-
throw new agent_1.AgentError(new agent_1.UnexpectedErrorCode("Failed to initialize actor"), agent_1.ErrorKindEnum.Unknown);
|
|
138
|
-
}
|
|
139
|
-
this.updateState({
|
|
140
|
-
initializing: false,
|
|
141
|
-
isInitializing: false,
|
|
142
|
-
initialized: true,
|
|
143
|
-
isInitialized: true,
|
|
144
|
-
}, "initialized");
|
|
145
|
-
}
|
|
146
|
-
catch (error) {
|
|
147
|
-
console.error("Error in initializeActor:", error);
|
|
148
|
-
this.updateState({
|
|
149
|
-
error: error,
|
|
150
|
-
initializing: false,
|
|
151
|
-
isInitializing: false,
|
|
152
|
-
}, "error");
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
_getActorMethod = (functionName) => {
|
|
156
|
-
if (!this._actor) {
|
|
157
|
-
throw new agent_1.AgentError(new agent_1.UnexpectedErrorCode("Actor not initialized"), agent_1.ErrorKindEnum.Unknown);
|
|
158
|
-
}
|
|
159
|
-
if (!this._actor[functionName] ||
|
|
160
|
-
typeof this._actor[functionName] !== "function") {
|
|
161
|
-
throw new agent_1.AgentError(new agent_1.UnexpectedErrorCode(`Method ${String(functionName)} not found`), agent_1.ErrorKindEnum.Unknown);
|
|
162
|
-
}
|
|
163
|
-
return this._actor[functionName];
|
|
164
|
-
};
|
|
165
|
-
callMethod = async (functionName, ...args) => {
|
|
166
|
-
const method = this._getActorMethod(functionName);
|
|
167
|
-
const data = await method(...args);
|
|
168
|
-
return data;
|
|
169
|
-
};
|
|
170
|
-
callMethodWithOptions = (options) => {
|
|
171
|
-
return async (functionName, ...args) => {
|
|
172
|
-
const method = this._getActorMethod(functionName);
|
|
173
|
-
const data = await method.withOptions(options)(...args);
|
|
174
|
-
return data;
|
|
175
|
-
};
|
|
176
|
-
};
|
|
177
|
-
call = async (functionName, ...args) => {
|
|
178
|
-
const requestHash = (0, helper_1.generateRequestHash)(args);
|
|
179
|
-
try {
|
|
180
|
-
this.updateMethodState(functionName, requestHash, {
|
|
181
|
-
loading: true,
|
|
182
|
-
isLoading: true,
|
|
183
|
-
error: undefined,
|
|
184
|
-
});
|
|
185
|
-
const data = await this.callMethod(functionName, ...args);
|
|
186
|
-
this.updateMethodState(functionName, requestHash, {
|
|
187
|
-
loading: false,
|
|
188
|
-
isLoading: false,
|
|
189
|
-
data,
|
|
190
|
-
});
|
|
191
|
-
return data;
|
|
192
|
-
}
|
|
193
|
-
catch (error) {
|
|
194
|
-
this.updateMethodState(functionName, requestHash, {
|
|
195
|
-
loading: false,
|
|
196
|
-
isLoading: false,
|
|
197
|
-
error: error,
|
|
198
|
-
data: undefined,
|
|
199
|
-
});
|
|
200
|
-
throw error;
|
|
201
|
-
}
|
|
202
|
-
};
|
|
203
|
-
// agent store
|
|
204
|
-
get agentManager() {
|
|
205
|
-
return this._agentManager;
|
|
206
|
-
}
|
|
207
|
-
// actor store
|
|
208
|
-
getActor = () => {
|
|
209
|
-
return this._actor;
|
|
210
|
-
};
|
|
211
|
-
getState = () => {
|
|
212
|
-
return this.actorStore.getState();
|
|
213
|
-
};
|
|
214
|
-
// @ts-expect-error: Overrides subscribe method signature
|
|
215
|
-
subscribeActorState = (selectorOrListener, listener, options) => {
|
|
216
|
-
let unsubscribe = helper_1.noop;
|
|
217
|
-
if (listener) {
|
|
218
|
-
unsubscribe = this.actorStore.subscribe(selectorOrListener, listener, options);
|
|
219
|
-
}
|
|
220
|
-
else {
|
|
221
|
-
unsubscribe = this.actorStore.subscribe(selectorOrListener);
|
|
222
|
-
}
|
|
223
|
-
this._subscribers.push(unsubscribe);
|
|
224
|
-
return unsubscribe;
|
|
225
|
-
};
|
|
226
|
-
setState = (updater) => {
|
|
227
|
-
return this.actorStore.setState(updater);
|
|
228
|
-
};
|
|
229
|
-
cleanup = () => {
|
|
230
|
-
this._unsubscribeAgent();
|
|
231
|
-
this._subscribers.forEach((unsubscribe) => unsubscribe());
|
|
232
|
-
};
|
|
233
|
-
}
|
|
234
|
-
exports.ActorManager = ActorManager;
|
|
235
|
-
const emptyVisitor = new Proxy({}, {
|
|
236
|
-
get: function (_, prop) {
|
|
237
|
-
throw new agent_1.AgentError(new agent_1.UnexpectedErrorCode(`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.`), agent_1.ErrorKindEnum.Unknown);
|
|
238
|
-
},
|
|
239
|
-
});
|
|
240
|
-
const DEFAULT_STATE = {
|
|
241
|
-
data: undefined,
|
|
242
|
-
error: undefined,
|
|
243
|
-
loading: false,
|
|
244
|
-
isLoading: false,
|
|
245
|
-
};
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import type { AgentManager } from "../agent";
|
|
2
|
-
import type { IDL, ActorMethod, ActorSubclass, Principal, StoreWithAllMiddleware } from "../../types";
|
|
3
|
-
import type { AgentError, CallConfig } from "@dfinity/agent";
|
|
4
|
-
export interface DefaultActorType {
|
|
5
|
-
[key: string]: ActorMethod;
|
|
6
|
-
}
|
|
7
|
-
export type BaseActor<T = DefaultActorType> = ActorSubclass<T>;
|
|
8
|
-
export type FunctionName<A = BaseActor> = Extract<keyof A, string>;
|
|
9
|
-
export type FunctionType = "query" | "update";
|
|
10
|
-
export type CanisterId = string | Principal;
|
|
11
|
-
export interface ActorManagerParameters {
|
|
12
|
-
idlFactory: IDL.InterfaceFactory;
|
|
13
|
-
agentManager: AgentManager;
|
|
14
|
-
name?: string;
|
|
15
|
-
canisterId: CanisterId;
|
|
16
|
-
withVisitor?: boolean;
|
|
17
|
-
withDevtools?: boolean;
|
|
18
|
-
initializeOnCreate?: boolean;
|
|
19
|
-
}
|
|
20
|
-
export type VisitorType<V> = V extends IDL.Visitor<infer D, infer R> ? {
|
|
21
|
-
data: D;
|
|
22
|
-
returnType: R;
|
|
23
|
-
} : never;
|
|
24
|
-
export type VisitService<A = BaseActor, M extends FunctionName<A> = FunctionName<A>> = {
|
|
25
|
-
[K in M]: <V extends IDL.Visitor<unknown, unknown>>(extractorClass: V, data: VisitorType<V>["data"]) => ReturnType<V["visitFunc"]>;
|
|
26
|
-
};
|
|
27
|
-
export type ActorMethodParameters<T> = T extends ActorMethod<infer Args, any> ? Args : never;
|
|
28
|
-
export type ActorMethodReturnType<T> = T extends ActorMethod<any, infer Ret> ? Ret : never;
|
|
29
|
-
/**
|
|
30
|
-
* Interface representing the state of each actor method.
|
|
31
|
-
*
|
|
32
|
-
* @template A - The actor type, defaulting to `BaseActor`.
|
|
33
|
-
* @template M - A specific method name of the actor.
|
|
34
|
-
*/
|
|
35
|
-
export interface ActorMethodState<A = BaseActor, M extends FunctionName<A> = FunctionName<A>> {
|
|
36
|
-
/**
|
|
37
|
-
* The per-method state object, keyed by the method name.
|
|
38
|
-
*/
|
|
39
|
-
[key: string]: {
|
|
40
|
-
/**
|
|
41
|
-
* The data returned from the actor method call, if available.
|
|
42
|
-
*/
|
|
43
|
-
data: ActorMethodReturnType<A[M]> | undefined;
|
|
44
|
-
/**
|
|
45
|
-
* @deprecated Use `isLoading` instead.
|
|
46
|
-
* Flag indicating whether the actor method is in progress.
|
|
47
|
-
*/
|
|
48
|
-
loading: boolean;
|
|
49
|
-
/**
|
|
50
|
-
* Flag indicating whether the actor method is in progress.
|
|
51
|
-
*/
|
|
52
|
-
isLoading: boolean;
|
|
53
|
-
/**
|
|
54
|
-
* Error thrown during the actor method invocation, if any.
|
|
55
|
-
*/
|
|
56
|
-
error: AgentError | undefined;
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
export type ActorMethodStates<A = BaseActor> = {
|
|
60
|
-
[M in FunctionName<A>]: ActorMethodState<A, M>;
|
|
61
|
-
};
|
|
62
|
-
export type ActorMethodType<A, M extends keyof A> = {
|
|
63
|
-
(...args: ActorMethodParameters<A[M]>): Promise<ActorMethodReturnType<A[M]>>;
|
|
64
|
-
withOptions: (options: CallConfig) => (...args: ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
|
65
|
-
};
|
|
66
|
-
/**
|
|
67
|
-
* Represents the state of an actor.
|
|
68
|
-
*
|
|
69
|
-
* @template A - The type of the actor, defaults to `BaseActor`.
|
|
70
|
-
*/
|
|
71
|
-
export type ActorState<A = BaseActor> = {
|
|
72
|
-
/**
|
|
73
|
-
* The name of the actor.
|
|
74
|
-
*/
|
|
75
|
-
name: string;
|
|
76
|
-
/**
|
|
77
|
-
* The version of the actor.
|
|
78
|
-
*/
|
|
79
|
-
version: number;
|
|
80
|
-
/**
|
|
81
|
-
* @deprecated Use `isInitialized` instead.
|
|
82
|
-
* Indicates whether the actor is initialized.
|
|
83
|
-
*/
|
|
84
|
-
initialized: boolean;
|
|
85
|
-
/**
|
|
86
|
-
* Indicates whether the actor is initialized.
|
|
87
|
-
*/
|
|
88
|
-
isInitialized: boolean;
|
|
89
|
-
/**
|
|
90
|
-
* @deprecated Use `isInitializing` instead.
|
|
91
|
-
* Indicates whether the actor is in the process of initializing.
|
|
92
|
-
*/
|
|
93
|
-
initializing: boolean;
|
|
94
|
-
/**
|
|
95
|
-
* Indicates whether the actor is in the process of initializing.
|
|
96
|
-
*/
|
|
97
|
-
isInitializing: boolean;
|
|
98
|
-
/**
|
|
99
|
-
* The error associated with the actor, if any.
|
|
100
|
-
*/
|
|
101
|
-
error: AgentError | undefined;
|
|
102
|
-
/**
|
|
103
|
-
* The state of the actor's methods.
|
|
104
|
-
*/
|
|
105
|
-
methodState: ActorMethodStates<A>;
|
|
106
|
-
};
|
|
107
|
-
export type ActorStore<A = BaseActor> = StoreWithAllMiddleware<ActorState<A>>;
|
|
108
|
-
export type CallActorMethod<A = BaseActor> = <M extends FunctionName<A> = FunctionName<A>>(functionName: M, ...args: ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
|
109
|
-
export type MethodAttributes<A = BaseActor> = Record<FunctionName<A>, {
|
|
110
|
-
type: FunctionType;
|
|
111
|
-
numberOfArgs: number;
|
|
112
|
-
validate: (arg: never) => boolean;
|
|
113
|
-
}>;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { HttpAgent } from "@dfinity/agent";
|
|
2
|
-
import type { CanisterId, CandidAdapterParameters, CandidDefenition, ReactorParser } from "../../types";
|
|
3
|
-
export declare class CandidAdapter {
|
|
4
|
-
agent: HttpAgent;
|
|
5
|
-
didjsCanisterId: string;
|
|
6
|
-
private parserModule?;
|
|
7
|
-
unsubscribeAgent: () => void;
|
|
8
|
-
constructor({ agentManager, agent, didjsCanisterId, }: CandidAdapterParameters);
|
|
9
|
-
initializeParser(module?: ReactorParser): Promise<void>;
|
|
10
|
-
private getDefaultDidJsId;
|
|
11
|
-
fetchCandidDefinition(canisterId: CanisterId): Promise<string>;
|
|
12
|
-
getCandidDefinition(canisterId: CanisterId): Promise<CandidDefenition>;
|
|
13
|
-
getFromMetadata(canisterId: CanisterId): Promise<string | undefined>;
|
|
14
|
-
getFromTmpHack(canisterId: CanisterId): Promise<string>;
|
|
15
|
-
evaluateCandidDefinition(data: string): Promise<CandidDefenition>;
|
|
16
|
-
fetchDidTojs(candidSource: string, didjsCanisterId?: string): Promise<[string]>;
|
|
17
|
-
parseDidToJs(candidSource: string): string;
|
|
18
|
-
validateIDL(candidSource: string): boolean;
|
|
19
|
-
}
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CandidAdapter = void 0;
|
|
4
|
-
const agent_1 = require("@dfinity/agent");
|
|
5
|
-
const constants_1 = require("../../utils/constants");
|
|
6
|
-
const utils_1 = require("../../utils");
|
|
7
|
-
class CandidAdapter {
|
|
8
|
-
agent;
|
|
9
|
-
didjsCanisterId;
|
|
10
|
-
parserModule;
|
|
11
|
-
unsubscribeAgent = utils_1.noop;
|
|
12
|
-
constructor({ agentManager, agent, didjsCanisterId, }) {
|
|
13
|
-
if (agent) {
|
|
14
|
-
this.agent = agent;
|
|
15
|
-
}
|
|
16
|
-
else if (agentManager) {
|
|
17
|
-
this.agent = agentManager.getAgent();
|
|
18
|
-
this.unsubscribeAgent = agentManager.subscribeAgent((agent) => {
|
|
19
|
-
this.agent = agent;
|
|
20
|
-
this.didjsCanisterId = didjsCanisterId || this.getDefaultDidJsId();
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
throw new Error("No agent or agentManager provided");
|
|
25
|
-
}
|
|
26
|
-
this.didjsCanisterId = didjsCanisterId || this.getDefaultDidJsId();
|
|
27
|
-
}
|
|
28
|
-
async initializeParser(module) {
|
|
29
|
-
if (module !== undefined) {
|
|
30
|
-
this.parserModule = module;
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
try {
|
|
34
|
-
this.parserModule = require("@ic-reactor/parser");
|
|
35
|
-
if (typeof this.parserModule !== "undefined" &&
|
|
36
|
-
"default" in this.parserModule) {
|
|
37
|
-
await this.parserModule.default();
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
catch (error) {
|
|
41
|
-
throw new Error(`Error initializing parser: ${error}`);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
getDefaultDidJsId() {
|
|
45
|
-
return this.agent.isLocal?.() === true
|
|
46
|
-
? constants_1.DEFAULT_LOCAL_DIDJS_ID
|
|
47
|
-
: constants_1.DEFAULT_IC_DIDJS_ID;
|
|
48
|
-
}
|
|
49
|
-
async fetchCandidDefinition(canisterId) {
|
|
50
|
-
let candidDef = "";
|
|
51
|
-
// First attempt: Try getting Candid definition from metadata
|
|
52
|
-
try {
|
|
53
|
-
candidDef = await this.getFromMetadata(canisterId);
|
|
54
|
-
if (!candidDef) {
|
|
55
|
-
throw new Error("Cannot retrieve Candid definition from metadata");
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
catch (error) {
|
|
59
|
-
// Second attempt: Try the temporary hack method
|
|
60
|
-
candidDef = await this.getFromTmpHack(canisterId).catch(() => {
|
|
61
|
-
return undefined;
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
if (!candidDef) {
|
|
65
|
-
throw new Error("Failed to retrieve Candid definition by any method.");
|
|
66
|
-
}
|
|
67
|
-
return candidDef;
|
|
68
|
-
}
|
|
69
|
-
async getCandidDefinition(canisterId) {
|
|
70
|
-
try {
|
|
71
|
-
const candidDef = await this.fetchCandidDefinition(canisterId);
|
|
72
|
-
return this.evaluateCandidDefinition(candidDef);
|
|
73
|
-
}
|
|
74
|
-
catch (error) {
|
|
75
|
-
throw new Error(`Error fetching canister ${canisterId}: ${error}`);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
async getFromMetadata(canisterId) {
|
|
79
|
-
const status = await agent_1.CanisterStatus.request({
|
|
80
|
-
agent: this.agent,
|
|
81
|
-
canisterId: canisterId,
|
|
82
|
-
paths: ["candid"],
|
|
83
|
-
});
|
|
84
|
-
return status.get("candid");
|
|
85
|
-
}
|
|
86
|
-
async getFromTmpHack(canisterId) {
|
|
87
|
-
const commonInterface = ({ IDL }) => IDL.Service({
|
|
88
|
-
__get_candid_interface_tmp_hack: IDL.Func([], [IDL.Text], ["query"]),
|
|
89
|
-
});
|
|
90
|
-
const actor = agent_1.Actor.createActor(commonInterface, {
|
|
91
|
-
agent: this.agent,
|
|
92
|
-
canisterId,
|
|
93
|
-
});
|
|
94
|
-
return (await actor.__get_candid_interface_tmp_hack());
|
|
95
|
-
}
|
|
96
|
-
async evaluateCandidDefinition(data) {
|
|
97
|
-
try {
|
|
98
|
-
let candidDef = "";
|
|
99
|
-
try {
|
|
100
|
-
candidDef = this.parseDidToJs(data);
|
|
101
|
-
if (candidDef === "") {
|
|
102
|
-
throw new Error("Cannot compile Candid to JavaScript");
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
catch (error) {
|
|
106
|
-
candidDef = (await this.fetchDidTojs(data))[0];
|
|
107
|
-
}
|
|
108
|
-
if (JSON.stringify(candidDef) === JSON.stringify([])) {
|
|
109
|
-
throw new Error("Cannot compile Candid to JavaScript");
|
|
110
|
-
}
|
|
111
|
-
return await (0, utils_1.importCandidDefinition)(candidDef);
|
|
112
|
-
}
|
|
113
|
-
catch (error) {
|
|
114
|
-
throw new Error(`Error evaluating Candid definition: ${error}`);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
async fetchDidTojs(candidSource, didjsCanisterId) {
|
|
118
|
-
const didjsInterface = ({ IDL }) => IDL.Service({
|
|
119
|
-
did_to_js: IDL.Func([IDL.Text], [IDL.Opt(IDL.Text)], ["query"]),
|
|
120
|
-
});
|
|
121
|
-
const didjs = agent_1.Actor.createActor(didjsInterface, {
|
|
122
|
-
agent: this.agent,
|
|
123
|
-
canisterId: didjsCanisterId || this.didjsCanisterId,
|
|
124
|
-
});
|
|
125
|
-
return didjs.did_to_js(candidSource);
|
|
126
|
-
}
|
|
127
|
-
parseDidToJs(candidSource) {
|
|
128
|
-
if (!this.parserModule) {
|
|
129
|
-
throw new Error("Parser module not available");
|
|
130
|
-
}
|
|
131
|
-
return this.parserModule.didToJs(candidSource);
|
|
132
|
-
}
|
|
133
|
-
validateIDL(candidSource) {
|
|
134
|
-
if (!this.parserModule) {
|
|
135
|
-
throw new Error("Parser module not available");
|
|
136
|
-
}
|
|
137
|
-
return this.parserModule.validateIDL(candidSource);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
exports.CandidAdapter = CandidAdapter;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { AgentManager } from "../agent";
|
|
2
|
-
import type { IDL, HttpAgent } from "../../types";
|
|
3
|
-
export interface CandidAdapterParameters {
|
|
4
|
-
agentManager?: AgentManager;
|
|
5
|
-
agent?: HttpAgent;
|
|
6
|
-
didjsCanisterId?: string;
|
|
7
|
-
}
|
|
8
|
-
export interface CandidDefenition {
|
|
9
|
-
idlFactory: IDL.InterfaceFactory;
|
|
10
|
-
init: ({ idl }: {
|
|
11
|
-
idl: typeof IDL;
|
|
12
|
-
}) => never[];
|
|
13
|
-
}
|
|
14
|
-
export type ReactorParser = typeof import("@ic-reactor/parser") | typeof import("@ic-reactor/parser/dist/nodejs") | typeof import("@ic-reactor/parser/dist/bundler");
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { HttpAgent } from "@dfinity/agent";
|
|
2
|
-
import { AuthClient } from "@dfinity/auth-client";
|
|
3
|
-
import type { AuthClientLoginOptions } from "../../types";
|
|
4
|
-
import type { AgentStore, AgentManagerParameters, UpdateAgentParameters, AuthStore } from "./types";
|
|
5
|
-
export declare class AgentManager {
|
|
6
|
-
private _agent;
|
|
7
|
-
private _auth;
|
|
8
|
-
private _subscribers;
|
|
9
|
-
agentStore: AgentStore;
|
|
10
|
-
authStore: AuthStore;
|
|
11
|
-
private updateAgentState;
|
|
12
|
-
private updateAuthState;
|
|
13
|
-
constructor(options?: AgentManagerParameters);
|
|
14
|
-
initializeAgent: () => Promise<void>;
|
|
15
|
-
subscribeAgent: (callback: (agent: HttpAgent) => void, initialize?: boolean) => () => void;
|
|
16
|
-
unsubscribeAgent: (callback: (agent: HttpAgent) => void) => void;
|
|
17
|
-
private notifySubscribers;
|
|
18
|
-
updateAgent: (options?: UpdateAgentParameters) => Promise<void>;
|
|
19
|
-
authenticate: () => Promise<import("@dfinity/agent").Identity>;
|
|
20
|
-
login: (options?: AuthClientLoginOptions) => Promise<void>;
|
|
21
|
-
logout: (options?: {
|
|
22
|
-
returnTo?: string;
|
|
23
|
-
}) => Promise<void>;
|
|
24
|
-
getAgent: () => HttpAgent;
|
|
25
|
-
getAgentHost: () => URL | undefined;
|
|
26
|
-
getAgentHostName: () => string;
|
|
27
|
-
getIsLocal: () => boolean;
|
|
28
|
-
getNetwork: () => "local" | "remote" | "ic";
|
|
29
|
-
getAgentState: AgentStore["getState"];
|
|
30
|
-
subscribeAgentState: AgentStore["subscribe"];
|
|
31
|
-
getAuthState: AuthStore["getState"];
|
|
32
|
-
subscribeAuthState: AuthStore["subscribe"];
|
|
33
|
-
getAuth: () => AuthClient | null;
|
|
34
|
-
getIdentity: () => import("@dfinity/agent").Identity | null;
|
|
35
|
-
getPrincipal: () => import("@dfinity/principal/lib/esm/principal").Principal | null;
|
|
36
|
-
}
|