@ic-reactor/core 0.5.2 → 1.0.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 +166 -64
- package/dist/actor/index.d.ts +31 -0
- package/dist/actor/index.js +157 -0
- package/dist/actor/types.d.ts +46 -0
- package/dist/actor/types.js +2 -0
- package/dist/agent/index.d.ts +32 -0
- package/dist/agent/index.js +187 -0
- package/dist/agent/types.d.ts +26 -0
- package/dist/agent/types.js +2 -0
- package/dist/index.d.ts +56 -20
- package/dist/index.js +146 -47
- package/dist/tools/candid.d.ts +15 -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 +3 -0
- package/dist/tools/index.js +19 -0
- package/dist/tools/types.d.ts +13 -0
- package/dist/tools/types.js +2 -0
- package/dist/types.d.ts +36 -19
- package/package.json +14 -17
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { BaseActor } from "../actor";
|
|
2
|
+
interface StoreOptions {
|
|
3
|
+
withDevtools?: boolean;
|
|
4
|
+
store: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function createStoreWithOptionalDevtools<T>(initialState: T, options: StoreOptions): Omit<import("zustand/vanilla").StoreApi<T>, "setState"> & {
|
|
7
|
+
setState<A extends string | {
|
|
8
|
+
type: string;
|
|
9
|
+
}>(partial: T | Partial<T> | ((state: T) => T | Partial<T>), replace?: boolean | undefined, action?: A | undefined): void;
|
|
10
|
+
};
|
|
11
|
+
export declare function jsonToString(json: unknown): string;
|
|
12
|
+
export declare const generateRequestHash: (args?: unknown[]) => `0x${string}`;
|
|
13
|
+
export declare const generateHash: (field?: unknown) => `0x${string}`;
|
|
14
|
+
export declare const generateActorHash: (actor: BaseActor) => `0x${string}`;
|
|
15
|
+
export declare const stringToHash: (str: string) => `0x${string}`;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.stringToHash = exports.generateActorHash = exports.generateHash = exports.generateRequestHash = exports.jsonToString = exports.createStoreWithOptionalDevtools = void 0;
|
|
4
|
+
const agent_1 = require("@dfinity/agent");
|
|
5
|
+
const candid_1 = require("@dfinity/candid");
|
|
6
|
+
const middleware_1 = require("zustand/middleware");
|
|
7
|
+
const vanilla_1 = require("zustand/vanilla");
|
|
8
|
+
function createStoreWithOptionalDevtools(initialState, options) {
|
|
9
|
+
if (options.withDevtools) {
|
|
10
|
+
return (0, vanilla_1.createStore)((0, middleware_1.devtools)(() => initialState, {
|
|
11
|
+
name: "ReActor",
|
|
12
|
+
store: options.store,
|
|
13
|
+
}));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
return (0, vanilla_1.createStore)(() => initialState);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.createStoreWithOptionalDevtools = createStoreWithOptionalDevtools;
|
|
20
|
+
function jsonToString(json) {
|
|
21
|
+
return JSON.stringify(json, (_, value) => (typeof value === "bigint" ? `BigInt(${value})` : value), 2);
|
|
22
|
+
}
|
|
23
|
+
exports.jsonToString = jsonToString;
|
|
24
|
+
const generateRequestHash = (args = []) => {
|
|
25
|
+
const serializedArgs = args
|
|
26
|
+
.map((arg) => {
|
|
27
|
+
if (typeof arg === "bigint") {
|
|
28
|
+
return arg.toString();
|
|
29
|
+
}
|
|
30
|
+
return JSON.stringify(arg);
|
|
31
|
+
})
|
|
32
|
+
.join("|");
|
|
33
|
+
return (0, exports.stringToHash)(serializedArgs !== null && serializedArgs !== void 0 ? serializedArgs : "");
|
|
34
|
+
};
|
|
35
|
+
exports.generateRequestHash = generateRequestHash;
|
|
36
|
+
const generateHash = (field) => {
|
|
37
|
+
const serializedArgs = JSON.stringify(field);
|
|
38
|
+
return (0, exports.stringToHash)(serializedArgs !== null && serializedArgs !== void 0 ? serializedArgs : "");
|
|
39
|
+
};
|
|
40
|
+
exports.generateHash = generateHash;
|
|
41
|
+
const generateActorHash = (actor) => {
|
|
42
|
+
const serializedArgs = JSON.stringify(actor);
|
|
43
|
+
return (0, exports.stringToHash)(serializedArgs !== null && serializedArgs !== void 0 ? serializedArgs : "");
|
|
44
|
+
};
|
|
45
|
+
exports.generateActorHash = generateActorHash;
|
|
46
|
+
const stringToHash = (str) => {
|
|
47
|
+
const hashBytes = (0, agent_1.hash)(new TextEncoder().encode(str));
|
|
48
|
+
return `0x${(0, candid_1.toHexString)(hashBytes)}`;
|
|
49
|
+
};
|
|
50
|
+
exports.stringToHash = stringToHash;
|
|
@@ -0,0 +1,19 @@
|
|
|
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("./candid"), exports);
|
|
18
|
+
__exportStar(require("./helper"), exports);
|
|
19
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { AgentManager } from "../agent";
|
|
2
|
+
import type { IDL, HttpAgent } from "../types";
|
|
3
|
+
export interface CandidAdapterOptions {
|
|
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
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,42 +1,59 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import type { ActorMethod, ActorSubclass,
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import type { ActorMethod, ActorSubclass, HttpAgentOptions, HttpAgent, Identity } from "@dfinity/agent";
|
|
3
|
+
import type { Principal } from "@dfinity/principal";
|
|
4
|
+
import type { IDL } from "@dfinity/candid";
|
|
5
|
+
import type { ActorManager, ActorManagerOptions, ActorMethodArgs, ActorMethodReturnType, ActorMethodState, BaseActor, FunctionName } from "./actor";
|
|
6
|
+
import type { AgentManager } from "./agent";
|
|
7
|
+
import type { AuthClientLoginOptions } from "@dfinity/auth-client";
|
|
8
|
+
export type { ActorMethod, HttpAgentOptions, ActorSubclass, Principal, HttpAgent, Identity, IDL, };
|
|
9
|
+
export interface CreateReActorOptions extends CreateReActorStoreOptions {
|
|
10
|
+
}
|
|
11
|
+
export interface CreateReActorStoreOptions extends HttpAgentOptions, Omit<ActorManagerOptions, "agentManager"> {
|
|
12
|
+
agentManager?: AgentManager;
|
|
13
|
+
withProcessEnv?: boolean;
|
|
14
|
+
isLocalEnv?: boolean;
|
|
15
|
+
port?: number;
|
|
16
|
+
}
|
|
17
|
+
export type ActorGetStateFunction<A, M extends FunctionName<A>> = {
|
|
18
|
+
(key: "data"): ActorMethodReturnType<A[M]>;
|
|
5
19
|
(key: "loading"): boolean;
|
|
6
20
|
(key: "error"): Error | undefined;
|
|
7
21
|
(): ActorMethodState<A, M>[string];
|
|
8
22
|
};
|
|
9
|
-
export type ActorSubscribeFunction<A, M extends
|
|
10
|
-
export type ActorCallFunction<A, M extends
|
|
11
|
-
export type ActorQueryReturn<A, M extends
|
|
23
|
+
export type ActorSubscribeFunction<A, M extends FunctionName<A>> = (callback: (state: ActorMethodState<A, M>[string]) => void) => () => void;
|
|
24
|
+
export type ActorCallFunction<A, M extends FunctionName<A>> = (replaceArgs?: ActorMethodArgs<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
|
25
|
+
export type ActorQueryReturn<A, M extends FunctionName<A>> = {
|
|
12
26
|
intervalId: NodeJS.Timeout | null;
|
|
13
27
|
requestHash: string;
|
|
14
28
|
getState: ActorGetStateFunction<A, M>;
|
|
15
29
|
subscribe: ActorSubscribeFunction<A, M>;
|
|
16
30
|
call: ActorCallFunction<A, M>;
|
|
17
|
-
|
|
31
|
+
dataPromise: Promise<ActorMethodReturnType<A[M]>>;
|
|
18
32
|
};
|
|
19
|
-
export type ActorUpdateReturn<A, M extends
|
|
33
|
+
export type ActorUpdateReturn<A, M extends FunctionName<A>> = {
|
|
20
34
|
requestHash: string;
|
|
21
35
|
getState: ActorGetStateFunction<A, M>;
|
|
22
36
|
subscribe: ActorSubscribeFunction<A, M>;
|
|
23
37
|
call: ActorCallFunction<A, M>;
|
|
24
38
|
};
|
|
25
|
-
export type ActorQueryArgs<A, M extends
|
|
39
|
+
export type ActorQueryArgs<A, M extends FunctionName<A>> = {
|
|
26
40
|
functionName: M;
|
|
27
|
-
args?:
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
refreshInterval?: number;
|
|
41
|
+
args?: ActorMethodArgs<A[M]>;
|
|
42
|
+
refetchOnMount?: boolean;
|
|
43
|
+
refetchInterval?: number | false;
|
|
31
44
|
};
|
|
32
|
-
export type ActorUpdateArgs<A, M extends
|
|
45
|
+
export type ActorUpdateArgs<A, M extends FunctionName<A>> = {
|
|
33
46
|
functionName: M;
|
|
34
|
-
args?:
|
|
47
|
+
args?: ActorMethodArgs<A[M]>;
|
|
35
48
|
};
|
|
36
|
-
export type ActorMethodCall<A = Record<string, ActorMethod>> = <M extends
|
|
37
|
-
export type ActorQuery<A = Record<string, ActorMethod>> = <M extends
|
|
38
|
-
export type ActorUpdate<A = Record<string, ActorMethod>> = <M extends
|
|
39
|
-
export interface ActorCoreActions<A extends
|
|
49
|
+
export type ActorMethodCall<A = Record<string, ActorMethod>> = <M extends FunctionName<A>>(functionName: M, ...args: ActorMethodArgs<A[M]>) => ActorUpdateReturn<A, M>;
|
|
50
|
+
export type ActorQuery<A = Record<string, ActorMethod>> = <M extends FunctionName<A>>(options: ActorQueryArgs<A, M>) => ActorQueryReturn<A, M>;
|
|
51
|
+
export type ActorUpdate<A = Record<string, ActorMethod>> = <M extends FunctionName<A>>(options: ActorUpdateArgs<A, M>) => ActorUpdateReturn<A, M>;
|
|
52
|
+
export interface ActorCoreActions<A = BaseActor> extends AgentManager, Omit<ActorManager<A>, "updateMethodState"> {
|
|
53
|
+
login: (options?: AuthClientLoginOptions) => Promise<void>;
|
|
54
|
+
logout: (options?: {
|
|
55
|
+
returnTo?: string;
|
|
56
|
+
}) => Promise<void>;
|
|
40
57
|
queryCall: ActorQuery<A>;
|
|
41
58
|
updateCall: ActorUpdate<A>;
|
|
42
59
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "A
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A library for intracting with the Internet Computer canisters",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
],
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
|
-
"url": "git@github.com:
|
|
12
|
+
"url": "git@github.com:B3Pay/ic-reactor.git"
|
|
13
13
|
},
|
|
14
14
|
"keywords": [
|
|
15
15
|
"actor",
|
|
@@ -25,8 +25,17 @@
|
|
|
25
25
|
"url": "https://github.com/b3hr4d/ic-reactor/issues"
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/b3hr4d/ic-reactor/tree/main/packages/core#readme",
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@dfinity/agent": ">=1.0",
|
|
30
|
+
"@dfinity/auth-client": ">=1.0",
|
|
31
|
+
"@dfinity/candid": ">=1.0",
|
|
32
|
+
"@dfinity/principal": ">=1.0"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"zustand": "^4.5"
|
|
36
|
+
},
|
|
28
37
|
"scripts": {
|
|
29
|
-
"test": "npx jest",
|
|
38
|
+
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest",
|
|
30
39
|
"start": "tsc watch",
|
|
31
40
|
"build": "npx tsc",
|
|
32
41
|
"clean": "npx rimraf dist"
|
|
@@ -34,17 +43,5 @@
|
|
|
34
43
|
"engines": {
|
|
35
44
|
"node": ">=10"
|
|
36
45
|
},
|
|
37
|
-
"
|
|
38
|
-
"@ic-reactor/store": "^0.5.2"
|
|
39
|
-
},
|
|
40
|
-
"peerDependencies": {
|
|
41
|
-
"@dfinity/agent": "^0.20",
|
|
42
|
-
"@dfinity/auth-client": "^0.20",
|
|
43
|
-
"@dfinity/candid": "0.20",
|
|
44
|
-
"@dfinity/identity": "^0.20",
|
|
45
|
-
"@dfinity/principal": "^0.20",
|
|
46
|
-
"@peculiar/webcrypto": "1.4",
|
|
47
|
-
"zustand": "4.4"
|
|
48
|
-
},
|
|
49
|
-
"gitHead": "581df4947caeb34fcdf2b773e5f8543fdb5ceaaa"
|
|
46
|
+
"gitHead": "ca9e2f3438a4566c26d5ce56b0f0b953aa93df53"
|
|
50
47
|
}
|