@ic-reactor/core 2.0.1 → 3.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +398 -175
- 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/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 -37
- package/dist/classes/agent/index.js +0 -221
- 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,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,37 +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
|
-
isAuthClientInitialized: () => boolean;
|
|
29
|
-
getNetwork: () => "local" | "remote" | "ic";
|
|
30
|
-
getAgentState: AgentStore["getState"];
|
|
31
|
-
subscribeAgentState: AgentStore["subscribe"];
|
|
32
|
-
getAuthState: AuthStore["getState"];
|
|
33
|
-
subscribeAuthState: AuthStore["subscribe"];
|
|
34
|
-
getAuth: () => AuthClient | null;
|
|
35
|
-
getIdentity: () => import("@dfinity/agent").Identity | null;
|
|
36
|
-
getPrincipal: () => import("@dfinity/principal/lib/esm/principal").Principal | null;
|
|
37
|
-
}
|
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AgentManager = void 0;
|
|
4
|
-
/* eslint-disable no-console */
|
|
5
|
-
const agent_1 = require("@dfinity/agent");
|
|
6
|
-
const helper_1 = require("../../utils/helper");
|
|
7
|
-
const auth_client_1 = require("@dfinity/auth-client");
|
|
8
|
-
const constants_1 = require("../../utils/constants");
|
|
9
|
-
const AGENT_INITIAL_STATE = {
|
|
10
|
-
initialized: false,
|
|
11
|
-
isInitialized: false,
|
|
12
|
-
initializing: false,
|
|
13
|
-
isInitializing: false,
|
|
14
|
-
error: undefined,
|
|
15
|
-
network: undefined,
|
|
16
|
-
};
|
|
17
|
-
const AUTH_INITIAL_STATE = {
|
|
18
|
-
identity: null,
|
|
19
|
-
authenticating: false,
|
|
20
|
-
isAuthenticating: false,
|
|
21
|
-
authenticated: false,
|
|
22
|
-
isAuthenticated: false,
|
|
23
|
-
error: undefined,
|
|
24
|
-
};
|
|
25
|
-
class AgentManager {
|
|
26
|
-
_agent;
|
|
27
|
-
_auth = null;
|
|
28
|
-
_subscribers = [];
|
|
29
|
-
agentStore;
|
|
30
|
-
authStore;
|
|
31
|
-
updateAgentState = (newState, action) => {
|
|
32
|
-
this.agentStore.setState((state) => ({ ...state, ...newState }), false, action);
|
|
33
|
-
};
|
|
34
|
-
updateAuthState = (newState, action) => {
|
|
35
|
-
this.authStore.setState((state) => ({ ...state, ...newState }), false, action);
|
|
36
|
-
};
|
|
37
|
-
constructor(options) {
|
|
38
|
-
const { withDevtools, port = 4943, withLocalEnv, withProcessEnv, initializeOnCreate = true, ...agentOptions } = options || {};
|
|
39
|
-
if (withProcessEnv) {
|
|
40
|
-
const processNetwork = (0, helper_1.getProcessEnvNetwork)();
|
|
41
|
-
agentOptions.host =
|
|
42
|
-
processNetwork === "ic" ? constants_1.IC_HOST_NETWORK_URI : undefined;
|
|
43
|
-
}
|
|
44
|
-
else if (withLocalEnv) {
|
|
45
|
-
agentOptions.host = `http://127.0.0.1:${port}`;
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
agentOptions.host = agentOptions.host ?? constants_1.IC_HOST_NETWORK_URI;
|
|
49
|
-
}
|
|
50
|
-
this.agentStore = (0, helper_1.createStoreWithOptionalDevtools)(AGENT_INITIAL_STATE, {
|
|
51
|
-
withDevtools,
|
|
52
|
-
name: "reactor-agent",
|
|
53
|
-
store: "agent",
|
|
54
|
-
});
|
|
55
|
-
this.authStore = (0, helper_1.createStoreWithOptionalDevtools)(AUTH_INITIAL_STATE, {
|
|
56
|
-
withDevtools,
|
|
57
|
-
name: "reactor-agent",
|
|
58
|
-
store: "auth",
|
|
59
|
-
});
|
|
60
|
-
this._agent = agent_1.HttpAgent.createSync(agentOptions);
|
|
61
|
-
if (initializeOnCreate) {
|
|
62
|
-
this.initializeAgent();
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
initializeAgent = async () => {
|
|
66
|
-
const network = this.getNetwork();
|
|
67
|
-
this.updateAgentState({
|
|
68
|
-
initializing: true,
|
|
69
|
-
isInitializing: true,
|
|
70
|
-
error: undefined,
|
|
71
|
-
network,
|
|
72
|
-
}, "initializing");
|
|
73
|
-
if (network !== "ic") {
|
|
74
|
-
try {
|
|
75
|
-
await this._agent.fetchRootKey();
|
|
76
|
-
}
|
|
77
|
-
catch (error) {
|
|
78
|
-
this.updateAgentState({
|
|
79
|
-
error: error,
|
|
80
|
-
initializing: false,
|
|
81
|
-
isInitializing: false,
|
|
82
|
-
}, "error");
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
this.updateAgentState({
|
|
86
|
-
initialized: true,
|
|
87
|
-
isInitialized: true,
|
|
88
|
-
initializing: false,
|
|
89
|
-
isInitializing: false,
|
|
90
|
-
}, "initialized");
|
|
91
|
-
};
|
|
92
|
-
subscribeAgent = (callback, initialize = true) => {
|
|
93
|
-
if (initialize) {
|
|
94
|
-
callback(this._agent);
|
|
95
|
-
}
|
|
96
|
-
this._subscribers.push(callback);
|
|
97
|
-
return () => this.unsubscribeAgent(callback);
|
|
98
|
-
};
|
|
99
|
-
unsubscribeAgent = (callback) => {
|
|
100
|
-
this._subscribers = this._subscribers.filter((sub) => sub !== callback);
|
|
101
|
-
};
|
|
102
|
-
notifySubscribers = async () => {
|
|
103
|
-
await Promise.all(this._subscribers.map(async (callback) => callback(this._agent)));
|
|
104
|
-
};
|
|
105
|
-
updateAgent = async (options) => {
|
|
106
|
-
const { agent } = options || {};
|
|
107
|
-
if (agent) {
|
|
108
|
-
this._agent = agent;
|
|
109
|
-
}
|
|
110
|
-
else if (options) {
|
|
111
|
-
this._agent = agent_1.HttpAgent.createSync(options);
|
|
112
|
-
await this.initializeAgent();
|
|
113
|
-
}
|
|
114
|
-
await this.notifySubscribers();
|
|
115
|
-
};
|
|
116
|
-
authenticate = async () => {
|
|
117
|
-
console.log(`Authenticating on ${this.getNetwork()} network`);
|
|
118
|
-
this.updateAuthState({ isAuthenticating: true, authenticating: true }, "authenticating");
|
|
119
|
-
try {
|
|
120
|
-
this._auth = await auth_client_1.AuthClient.create();
|
|
121
|
-
const isAuthenticated = await this._auth.isAuthenticated();
|
|
122
|
-
const identity = this._auth.getIdentity();
|
|
123
|
-
this._agent.replaceIdentity(identity);
|
|
124
|
-
this.notifySubscribers();
|
|
125
|
-
this.updateAuthState({
|
|
126
|
-
authenticated: isAuthenticated,
|
|
127
|
-
isAuthenticated,
|
|
128
|
-
identity,
|
|
129
|
-
authenticating: false,
|
|
130
|
-
isAuthenticating: false,
|
|
131
|
-
}, "authenticated");
|
|
132
|
-
return identity;
|
|
133
|
-
}
|
|
134
|
-
catch (error) {
|
|
135
|
-
this.updateAuthState({
|
|
136
|
-
error: error,
|
|
137
|
-
isAuthenticating: false,
|
|
138
|
-
authenticating: false,
|
|
139
|
-
}, "error");
|
|
140
|
-
throw error;
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
login = async (options) => {
|
|
144
|
-
this.updateAuthState({ isAuthenticating: true, authenticating: true }, "login");
|
|
145
|
-
if (!this._auth) {
|
|
146
|
-
await this.authenticate();
|
|
147
|
-
}
|
|
148
|
-
if (!this._auth) {
|
|
149
|
-
throw new Error("Auth client not initialized");
|
|
150
|
-
}
|
|
151
|
-
await this._auth.login({
|
|
152
|
-
identityProvider: this.getIsLocal()
|
|
153
|
-
? constants_1.LOCAL_INTERNET_IDENTITY_PROVIDER
|
|
154
|
-
: constants_1.IC_INTERNET_IDENTITY_PROVIDER,
|
|
155
|
-
...options,
|
|
156
|
-
onSuccess: async (msg) => {
|
|
157
|
-
await this.authenticate();
|
|
158
|
-
options?.onSuccess?.(msg);
|
|
159
|
-
},
|
|
160
|
-
});
|
|
161
|
-
};
|
|
162
|
-
logout = async (options) => {
|
|
163
|
-
if (!this._auth) {
|
|
164
|
-
throw new Error("Auth client not initialized");
|
|
165
|
-
}
|
|
166
|
-
await this._auth.logout(options);
|
|
167
|
-
await this.authenticate();
|
|
168
|
-
};
|
|
169
|
-
// agent store
|
|
170
|
-
getAgent = () => {
|
|
171
|
-
return this._agent;
|
|
172
|
-
};
|
|
173
|
-
getAgentHost = () => {
|
|
174
|
-
return this._agent.host;
|
|
175
|
-
};
|
|
176
|
-
getAgentHostName = () => {
|
|
177
|
-
return this.getAgentHost()?.hostname || "";
|
|
178
|
-
};
|
|
179
|
-
getIsLocal = () => {
|
|
180
|
-
return this.getNetwork() !== "ic";
|
|
181
|
-
};
|
|
182
|
-
isAuthClientInitialized = () => {
|
|
183
|
-
return this.getAgentState().isInitialized && this._auth !== null;
|
|
184
|
-
};
|
|
185
|
-
getNetwork = () => {
|
|
186
|
-
const hostname = this.getAgentHostName();
|
|
187
|
-
return (0, helper_1.getNetworkByHostname)(hostname);
|
|
188
|
-
};
|
|
189
|
-
getAgentState = () => {
|
|
190
|
-
return this.agentStore.getState();
|
|
191
|
-
};
|
|
192
|
-
// @ts-expect-error: Overrides subscribe method signature
|
|
193
|
-
subscribeAgentState = (selectorOrListener, listener, options) => {
|
|
194
|
-
if (listener) {
|
|
195
|
-
return this.agentStore.subscribe(selectorOrListener, listener, options);
|
|
196
|
-
}
|
|
197
|
-
return this.agentStore.subscribe(selectorOrListener);
|
|
198
|
-
};
|
|
199
|
-
// auth store
|
|
200
|
-
getAuthState = () => {
|
|
201
|
-
return this.authStore.getState();
|
|
202
|
-
};
|
|
203
|
-
// @ts-expect-error: Overrides subscribe method signature
|
|
204
|
-
subscribeAuthState = (selectorOrListener, listener, options) => {
|
|
205
|
-
if (listener) {
|
|
206
|
-
return this.authStore.subscribe(selectorOrListener, listener, options);
|
|
207
|
-
}
|
|
208
|
-
return this.authStore.subscribe(selectorOrListener);
|
|
209
|
-
};
|
|
210
|
-
getAuth = () => {
|
|
211
|
-
return this._auth;
|
|
212
|
-
};
|
|
213
|
-
getIdentity = () => {
|
|
214
|
-
return this.authStore.getState().identity;
|
|
215
|
-
};
|
|
216
|
-
getPrincipal = () => {
|
|
217
|
-
const identity = this.authStore.getState().identity;
|
|
218
|
-
return identity ? identity.getPrincipal() : null;
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
exports.AgentManager = AgentManager;
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import type { AgentError, HttpAgent, HttpAgentOptions, Identity } from "@dfinity/agent";
|
|
2
|
-
import type { AuthClient } from "@dfinity/auth-client";
|
|
3
|
-
import type { StoreWithAllMiddleware } from "../../types";
|
|
4
|
-
export { HttpAgentOptions, AuthClient, Identity };
|
|
5
|
-
/**
|
|
6
|
-
* Parameters for configuring an AgentManager instance.
|
|
7
|
-
* Extends the options available in `HttpAgentOptions`.
|
|
8
|
-
*
|
|
9
|
-
* @extends HttpAgentOptions
|
|
10
|
-
*
|
|
11
|
-
* @property {number} [port] - The port number to be used by the agent.
|
|
12
|
-
* @property {boolean} [withLocalEnv] - Whether to include local environment variables.
|
|
13
|
-
* @property {boolean} [withDevtools] - Whether to enable developer tools integration.
|
|
14
|
-
* @property {boolean} [withProcessEnv] - Whether to include process environment variables.
|
|
15
|
-
* @property {boolean} [initializeOnCreate] - Whether to initialize the agent upon creation.
|
|
16
|
-
*/
|
|
17
|
-
export interface AgentManagerParameters extends HttpAgentOptions {
|
|
18
|
-
port?: number;
|
|
19
|
-
withLocalEnv?: boolean;
|
|
20
|
-
withDevtools?: boolean;
|
|
21
|
-
withProcessEnv?: boolean;
|
|
22
|
-
initializeOnCreate?: boolean;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Represents the state of an agent.
|
|
26
|
-
*/
|
|
27
|
-
export interface AgentState {
|
|
28
|
-
/**
|
|
29
|
-
* @deprecated Use `isInitialized` instead.
|
|
30
|
-
* Indicates whether the agent has been initialized.
|
|
31
|
-
*/
|
|
32
|
-
initialized: boolean;
|
|
33
|
-
/**
|
|
34
|
-
* Indicates whether the agent has been initialized.
|
|
35
|
-
*/
|
|
36
|
-
isInitialized: boolean;
|
|
37
|
-
/**
|
|
38
|
-
* @deprecated Use `isInitializing` instead.
|
|
39
|
-
* Indicates whether the agent is in the process of initializing.
|
|
40
|
-
*/
|
|
41
|
-
initializing: boolean;
|
|
42
|
-
/**
|
|
43
|
-
* Indicates whether the agent is in the process of initializing.
|
|
44
|
-
*/
|
|
45
|
-
isInitializing: boolean;
|
|
46
|
-
/**
|
|
47
|
-
* Represents an error associated with the agent, if any.
|
|
48
|
-
*/
|
|
49
|
-
error: AgentError | undefined;
|
|
50
|
-
/**
|
|
51
|
-
* Represents the network associated with the agent, if any.
|
|
52
|
-
*/
|
|
53
|
-
network: string | undefined;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Represents the authentication state of an agent.
|
|
57
|
-
*/
|
|
58
|
-
export interface AuthState {
|
|
59
|
-
identity: Identity | null;
|
|
60
|
-
/**
|
|
61
|
-
* @deprecated Use `isAuthenticating` instead.
|
|
62
|
-
* Indicates whether the authentication process is ongoing.
|
|
63
|
-
*/
|
|
64
|
-
authenticating: boolean;
|
|
65
|
-
/**
|
|
66
|
-
* Indicates whether the authentication process is ongoing.
|
|
67
|
-
*/
|
|
68
|
-
isAuthenticating: boolean;
|
|
69
|
-
/**
|
|
70
|
-
* @deprecated Use `isAuthenticated` instead.
|
|
71
|
-
* Indicates whether the agent is authenticated.
|
|
72
|
-
*/
|
|
73
|
-
authenticated: boolean;
|
|
74
|
-
/**
|
|
75
|
-
* Indicates whether the agent is authenticated.
|
|
76
|
-
*/
|
|
77
|
-
isAuthenticated: boolean;
|
|
78
|
-
/**
|
|
79
|
-
* Represents any error that occurred during authentication.
|
|
80
|
-
*/
|
|
81
|
-
error: Error | undefined;
|
|
82
|
-
}
|
|
83
|
-
export interface UpdateAgentParameters extends HttpAgentOptions {
|
|
84
|
-
agent?: HttpAgent;
|
|
85
|
-
}
|
|
86
|
-
export type AgentStore = StoreWithAllMiddleware<AgentState>;
|
|
87
|
-
export type AuthStore = StoreWithAllMiddleware<AuthState>;
|
package/dist/classes/index.d.ts
DELETED
package/dist/classes/index.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./actor"), exports);
|
|
18
|
-
__exportStar(require("./agent"), exports);
|
|
19
|
-
__exportStar(require("./adapter"), exports);
|
package/dist/classes/types.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export * from "./agent/types";
|
|
2
|
-
export * from "./actor/types";
|
|
3
|
-
export * from "./adapter/types";
|
|
4
|
-
import { type StateCreator, type StoreApi, type StoreMutatorIdentifier, type Mutate } from "zustand";
|
|
5
|
-
export type StoreWithMiddleware<T, Mis extends [StoreMutatorIdentifier, unknown][] = [], Mos extends [StoreMutatorIdentifier, unknown][] = []> = StateCreator<T, Mis, Mos>;
|
|
6
|
-
export type StoreApiWithMiddleware<T, Mos extends [StoreMutatorIdentifier, unknown][] = []> = Mutate<StoreApi<T>, Mos>;
|
|
7
|
-
export type WithDevtools = ["zustand/devtools", never];
|
|
8
|
-
export type WithSubscribeSelector = ["zustand/subscribeWithSelector", never];
|
|
9
|
-
export type StoreWithAllMiddleware<T> = StoreApiWithMiddleware<T, [
|
|
10
|
-
WithDevtools,
|
|
11
|
-
WithSubscribeSelector
|
|
12
|
-
]>;
|
|
13
|
-
export type StoreWithSubscribeOnly<T> = StoreApiWithMiddleware<T, [
|
|
14
|
-
WithSubscribeSelector
|
|
15
|
-
]>;
|