@ic-reactor/core 1.14.2 → 1.15.0
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 +1 -1
- package/dist/classes/actor/index.d.ts +1 -0
- package/dist/classes/actor/index.js +23 -0
- package/dist/classes/types.d.ts +1 -1
- package/dist/types.d.ts +8 -6
- package/dist/utils/helper.d.ts +46 -6
- package/dist/utils/helper.js +53 -4
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ yarn add @ic-reactor/core
|
|
|
19
19
|
or you can use the UMD version:
|
|
20
20
|
|
|
21
21
|
```html
|
|
22
|
-
<script src="https://github.com/B3Pay/ic-reactor/releases/download/v1.14.
|
|
22
|
+
<script src="https://github.com/B3Pay/ic-reactor/releases/download/v1.14.2/ic-reactor-core.min.js"></script>
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
### Using `createReactorCore`
|
|
@@ -24,6 +24,7 @@ export declare class ActorManager<A = BaseActor> {
|
|
|
24
24
|
private _getActorMethod;
|
|
25
25
|
callMethod: <M extends FunctionName<A>>(functionName: M, ...args: ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
|
26
26
|
callMethodWithOptions: (options: CallConfig) => <M extends FunctionName<A>>(functionName: M, ...args: ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
|
27
|
+
call: <M extends FunctionName<A>>(functionName: M, ...args: ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
|
27
28
|
get agentManager(): AgentManager;
|
|
28
29
|
getActor: () => A | null;
|
|
29
30
|
getState: ActorStore<A>["getState"];
|
|
@@ -122,6 +122,29 @@ class ActorManager {
|
|
|
122
122
|
return data;
|
|
123
123
|
});
|
|
124
124
|
};
|
|
125
|
+
this.call = (functionName, ...args) => __awaiter(this, void 0, void 0, function* () {
|
|
126
|
+
const requestHash = (0, helper_1.generateRequestHash)(args);
|
|
127
|
+
try {
|
|
128
|
+
this.updateMethodState(functionName, requestHash, {
|
|
129
|
+
loading: true,
|
|
130
|
+
error: undefined,
|
|
131
|
+
});
|
|
132
|
+
const data = yield this.callMethod(functionName, ...args);
|
|
133
|
+
this.updateMethodState(functionName, requestHash, {
|
|
134
|
+
loading: false,
|
|
135
|
+
data,
|
|
136
|
+
});
|
|
137
|
+
return data;
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
this.updateMethodState(functionName, requestHash, {
|
|
141
|
+
loading: false,
|
|
142
|
+
error: error,
|
|
143
|
+
data: undefined,
|
|
144
|
+
});
|
|
145
|
+
throw error;
|
|
146
|
+
}
|
|
147
|
+
});
|
|
125
148
|
// actor store
|
|
126
149
|
this.getActor = () => {
|
|
127
150
|
return this._actor;
|
package/dist/classes/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export * from "./agent/types";
|
|
2
2
|
export * from "./actor/types";
|
|
3
3
|
export * from "./adapter/types";
|
|
4
|
-
import { NamedSet } from "zustand/middleware";
|
|
4
|
+
import type { NamedSet } from "zustand/middleware";
|
|
5
5
|
import type { StoreApi } from "zustand";
|
|
6
6
|
export interface StoreApiWithDevtools<T> extends StoreApi<T> {
|
|
7
7
|
setState: NamedSet<T>;
|
package/dist/types.d.ts
CHANGED
|
@@ -79,17 +79,19 @@ export type ExtractOk<T> = T extends {
|
|
|
79
79
|
export type ExtractErr<T> = T extends {
|
|
80
80
|
Err: infer E;
|
|
81
81
|
} ? E : never;
|
|
82
|
-
export type
|
|
83
|
-
OkType: infer U;
|
|
84
|
-
ErrType: infer E;
|
|
85
|
-
} ? {
|
|
82
|
+
export type CompiledOkResult<U> = {
|
|
86
83
|
isOk: true;
|
|
87
84
|
isErr: false;
|
|
88
85
|
value: U;
|
|
89
86
|
error: null;
|
|
90
|
-
}
|
|
87
|
+
};
|
|
88
|
+
export type CompiledErrResult<E> = {
|
|
91
89
|
isOk: false;
|
|
92
90
|
isErr: true;
|
|
93
91
|
value: null;
|
|
94
92
|
error: E;
|
|
95
|
-
}
|
|
93
|
+
};
|
|
94
|
+
export type CompiledResult<T> = ExtractOkErr<T> extends {
|
|
95
|
+
OkType: infer U;
|
|
96
|
+
ErrType: infer E;
|
|
97
|
+
} ? CompiledOkResult<U> | CompiledErrResult<E> : never;
|
package/dist/utils/helper.d.ts
CHANGED
|
@@ -1,18 +1,58 @@
|
|
|
1
1
|
import { DevtoolsOptions } from "zustand/middleware";
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
import { StoreApi } from "zustand";
|
|
3
|
+
import type { CompiledResult, BaseActor, CandidDefenition, IDL, StoreApiWithDevtools, ExtractOk } from "../types";
|
|
4
|
+
/**
|
|
5
|
+
* Creates a Zustand store with optional DevTools middleware.
|
|
6
|
+
*
|
|
7
|
+
* @param initialState - The initial state of the store.
|
|
8
|
+
* @param config - Configuration options for DevTools.
|
|
9
|
+
* @returns A Zustand store with DevTools enabled if configured, otherwise a standard store.
|
|
10
|
+
*/
|
|
11
|
+
export declare function createStoreWithOptionalDevtools<T>(initialState: T, config: DevtoolsOptions): StoreApiWithDevtools<T> | StoreApi<T>;
|
|
8
12
|
export declare const importCandidDefinition: (candidDef: string) => Promise<CandidDefenition>;
|
|
13
|
+
/**
|
|
14
|
+
* Checks if the current environment is local or development.
|
|
15
|
+
*
|
|
16
|
+
* @returns `true` if running in a local or development environment, otherwise `false`.
|
|
17
|
+
*/
|
|
9
18
|
export declare const isInLocalOrDevelopment: () => boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Retrieves the network from the process environment variables.
|
|
21
|
+
*
|
|
22
|
+
* @returns The network name, defaulting to "ic" if not specified.
|
|
23
|
+
*/
|
|
10
24
|
export declare const getProcessEnvNetwork: () => string;
|
|
25
|
+
/**
|
|
26
|
+
* Determines the network type based on the provided hostname.
|
|
27
|
+
*
|
|
28
|
+
* @param hostname - The hostname to evaluate.
|
|
29
|
+
* @returns A string indicating the network type: "local", "remote", or "ic".
|
|
30
|
+
*/
|
|
11
31
|
export declare function getNetworkByHostname(hostname: string): "local" | "remote" | "ic";
|
|
32
|
+
/**
|
|
33
|
+
* Checks if a given IDL function is a query.
|
|
34
|
+
*
|
|
35
|
+
* @param func - The IDL function to check.
|
|
36
|
+
* @returns `true` if the function is a query or composite query, otherwise `false`.
|
|
37
|
+
*/
|
|
12
38
|
export declare function isQuery(func: IDL.FuncClass): boolean;
|
|
13
39
|
export declare const jsonToString: (json: unknown, space?: number) => string;
|
|
14
40
|
export declare const generateRequestHash: (args?: unknown[]) => `0x${string}`;
|
|
15
41
|
export declare const generateHash: (field?: unknown) => `0x${string}`;
|
|
16
42
|
export declare const generateActorHash: (actor: BaseActor) => `0x${string}`;
|
|
17
43
|
export declare const stringToHash: (str: string) => `0x${string}`;
|
|
44
|
+
/**
|
|
45
|
+
* Helper function for extracting the value from a compiled result { Ok: T } or { Err: E }
|
|
46
|
+
*
|
|
47
|
+
* @param result - The compiled result to extract from.
|
|
48
|
+
* @returns A `CompiledResult` object indicating success or failure.
|
|
49
|
+
*/
|
|
18
50
|
export declare function createCompiledResult<T>(result: T): CompiledResult<T>;
|
|
51
|
+
/**
|
|
52
|
+
* Helper function for extracting the value from a compiled result { Ok: T } or throw the error if { Err: E }
|
|
53
|
+
*
|
|
54
|
+
* @param result - The compiled result to extract from.
|
|
55
|
+
* @returns The extracted value from the compiled result.
|
|
56
|
+
* @throws The error from the compiled result.
|
|
57
|
+
*/
|
|
58
|
+
export declare function extractOkResult<T>(result: T): ExtractOk<T>;
|
package/dist/utils/helper.js
CHANGED
|
@@ -14,19 +14,27 @@ exports.createStoreWithOptionalDevtools = createStoreWithOptionalDevtools;
|
|
|
14
14
|
exports.getNetworkByHostname = getNetworkByHostname;
|
|
15
15
|
exports.isQuery = isQuery;
|
|
16
16
|
exports.createCompiledResult = createCompiledResult;
|
|
17
|
+
exports.extractOkResult = extractOkResult;
|
|
17
18
|
const agent_1 = require("@dfinity/agent");
|
|
18
19
|
const middleware_1 = require("zustand/middleware");
|
|
19
|
-
const
|
|
20
|
+
const zustand_1 = require("zustand");
|
|
20
21
|
const hash_1 = require("./hash");
|
|
21
22
|
const constants_1 = require("./constants");
|
|
23
|
+
/**
|
|
24
|
+
* Creates a Zustand store with optional DevTools middleware.
|
|
25
|
+
*
|
|
26
|
+
* @param initialState - The initial state of the store.
|
|
27
|
+
* @param config - Configuration options for DevTools.
|
|
28
|
+
* @returns A Zustand store with DevTools enabled if configured, otherwise a standard store.
|
|
29
|
+
*/
|
|
22
30
|
function createStoreWithOptionalDevtools(initialState, config) {
|
|
23
31
|
if (config.withDevtools) {
|
|
24
|
-
return (0,
|
|
32
|
+
return (0, zustand_1.createStore)((0, middleware_1.devtools)(() => initialState, Object.assign({ serialize: {
|
|
25
33
|
replacer: (_, value) => typeof value === "bigint" ? value.toString() : value,
|
|
26
34
|
} }, config)));
|
|
27
35
|
}
|
|
28
36
|
else {
|
|
29
|
-
return (0,
|
|
37
|
+
return (0, zustand_1.createStore)(() => initialState);
|
|
30
38
|
}
|
|
31
39
|
}
|
|
32
40
|
const importCandidDefinition = (candidDef) => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -58,10 +66,20 @@ const importCandidDefinition = (candidDef) => __awaiter(void 0, void 0, void 0,
|
|
|
58
66
|
}
|
|
59
67
|
});
|
|
60
68
|
exports.importCandidDefinition = importCandidDefinition;
|
|
69
|
+
/**
|
|
70
|
+
* Checks if the current environment is local or development.
|
|
71
|
+
*
|
|
72
|
+
* @returns `true` if running in a local or development environment, otherwise `false`.
|
|
73
|
+
*/
|
|
61
74
|
const isInLocalOrDevelopment = () => {
|
|
62
75
|
return typeof process !== "undefined" && process.env.DFX_NETWORK === "local";
|
|
63
76
|
};
|
|
64
77
|
exports.isInLocalOrDevelopment = isInLocalOrDevelopment;
|
|
78
|
+
/**
|
|
79
|
+
* Retrieves the network from the process environment variables.
|
|
80
|
+
*
|
|
81
|
+
* @returns The network name, defaulting to "ic" if not specified.
|
|
82
|
+
*/
|
|
65
83
|
const getProcessEnvNetwork = () => {
|
|
66
84
|
var _a;
|
|
67
85
|
if (typeof process === "undefined")
|
|
@@ -70,6 +88,12 @@ const getProcessEnvNetwork = () => {
|
|
|
70
88
|
return (_a = process.env.DFX_NETWORK) !== null && _a !== void 0 ? _a : "ic";
|
|
71
89
|
};
|
|
72
90
|
exports.getProcessEnvNetwork = getProcessEnvNetwork;
|
|
91
|
+
/**
|
|
92
|
+
* Determines the network type based on the provided hostname.
|
|
93
|
+
*
|
|
94
|
+
* @param hostname - The hostname to evaluate.
|
|
95
|
+
* @returns A string indicating the network type: "local", "remote", or "ic".
|
|
96
|
+
*/
|
|
73
97
|
function getNetworkByHostname(hostname) {
|
|
74
98
|
if (constants_1.LOCAL_HOSTS.some((host) => hostname.endsWith(host))) {
|
|
75
99
|
return "local";
|
|
@@ -81,6 +105,12 @@ function getNetworkByHostname(hostname) {
|
|
|
81
105
|
return "ic";
|
|
82
106
|
}
|
|
83
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* Checks if a given IDL function is a query.
|
|
110
|
+
*
|
|
111
|
+
* @param func - The IDL function to check.
|
|
112
|
+
* @returns `true` if the function is a query or composite query, otherwise `false`.
|
|
113
|
+
*/
|
|
84
114
|
function isQuery(func) {
|
|
85
115
|
return (func.annotations.includes("query") ||
|
|
86
116
|
func.annotations.includes("composite_query"));
|
|
@@ -112,7 +142,12 @@ exports.stringToHash = stringToHash;
|
|
|
112
142
|
function toHexString(bytes) {
|
|
113
143
|
return (0, agent_1.toHex)(bytes);
|
|
114
144
|
}
|
|
115
|
-
|
|
145
|
+
/**
|
|
146
|
+
* Helper function for extracting the value from a compiled result { Ok: T } or { Err: E }
|
|
147
|
+
*
|
|
148
|
+
* @param result - The compiled result to extract from.
|
|
149
|
+
* @returns A `CompiledResult` object indicating success or failure.
|
|
150
|
+
*/
|
|
116
151
|
function createCompiledResult(result) {
|
|
117
152
|
if (result && typeof result === "object" && "Ok" in result) {
|
|
118
153
|
return {
|
|
@@ -140,3 +175,17 @@ function createCompiledResult(result) {
|
|
|
140
175
|
};
|
|
141
176
|
}
|
|
142
177
|
}
|
|
178
|
+
/**
|
|
179
|
+
* Helper function for extracting the value from a compiled result { Ok: T } or throw the error if { Err: E }
|
|
180
|
+
*
|
|
181
|
+
* @param result - The compiled result to extract from.
|
|
182
|
+
* @returns The extracted value from the compiled result.
|
|
183
|
+
* @throws The error from the compiled result.
|
|
184
|
+
*/
|
|
185
|
+
function extractOkResult(result) {
|
|
186
|
+
const compiledResult = createCompiledResult(result);
|
|
187
|
+
if (compiledResult.isErr) {
|
|
188
|
+
throw compiledResult.error;
|
|
189
|
+
}
|
|
190
|
+
return compiledResult.value;
|
|
191
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.15.0",
|
|
4
4
|
"description": "A library for intracting with the Internet Computer canisters",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"@dfinity/identity": ">=2.1",
|
|
33
33
|
"@dfinity/principal": ">=2.1",
|
|
34
34
|
"simple-cbor": "^0.4.1",
|
|
35
|
-
"zustand": "
|
|
35
|
+
"zustand": "5.0.2"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
38
|
"@dfinity/agent": ">=2.1",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"webpack": "^5.96.1"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
|
-
"test": "NODE_OPTIONS=\"
|
|
51
|
+
"test": "NODE_OPTIONS=\"--experimental-vm-modules\" npx jest",
|
|
52
52
|
"start": "tsc watch",
|
|
53
53
|
"bundle": "yarn bundle:dev && yarn bundle:prod",
|
|
54
54
|
"bundle:dev": "npx webpack-cli --mode development",
|
|
@@ -60,5 +60,5 @@
|
|
|
60
60
|
"engines": {
|
|
61
61
|
"node": ">=10"
|
|
62
62
|
},
|
|
63
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "4c6badf97d6d2845f6bed00bf21274a72dc68134"
|
|
64
64
|
}
|