@ic-reactor/core 1.2.3 → 1.3.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 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.2.2/ic-reactor-core.min.js"></script>
22
+ <script src="https://github.com/B3Pay/ic-reactor/releases/download/v1.3.0/ic-reactor-core.min.js"></script>
23
23
  ```
24
24
 
25
25
  ### Using `createReactorCore`
@@ -1,4 +1,4 @@
1
- import type { CanisterId, ActorMethodParameters, ActorMethodReturnType, ActorStore, ActorManagerParameters, FunctionName, VisitService, BaseActor, ActorMethodState } from "./types";
1
+ import type { CanisterId, ActorMethodParameters, ActorMethodReturnType, ActorStore, ActorManagerParameters, FunctionName, VisitService, BaseActor, ActorMethodState, MethodAttributes } from "./types";
2
2
  import { IDL } from "@dfinity/candid";
3
3
  import type { AgentManager } from "../agent";
4
4
  import type { UpdateAgentParameters } from "../../types";
@@ -11,6 +11,7 @@ export declare class ActorManager<A = BaseActor> {
11
11
  canisterId: CanisterId;
12
12
  actorStore: ActorStore<A>;
13
13
  visitFunction: VisitService<A>;
14
+ methodAttributes: MethodAttributes<A>;
14
15
  private initialState;
15
16
  private updateState;
16
17
  updateMethodState: (method: FunctionName<A>, hash: string, newState: Partial<{
@@ -20,9 +21,9 @@ export declare class ActorManager<A = BaseActor> {
20
21
  }>) => void;
21
22
  constructor(actorConfig: ActorManagerParameters);
22
23
  initialize: (options?: UpdateAgentParameters) => Promise<void>;
23
- extractMethodNames: () => Array<FunctionName<A>>;
24
- extractService: () => VisitService<A>;
25
24
  extractInterface: () => IDL.ServiceClass;
25
+ extractMethodAttributes: () => MethodAttributes<A>;
26
+ extractVisitor: () => VisitService<A>;
26
27
  private initializeActor;
27
28
  callMethod: <M extends FunctionName<A>>(functionName: M, ...args: ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
28
29
  get agentManager(): AgentManager;
@@ -13,6 +13,7 @@ exports.ActorManager = void 0;
13
13
  /* eslint-disable no-console */
14
14
  const agent_1 = require("@dfinity/agent");
15
15
  const helper_1 = require("../../utils/helper");
16
+ const candid_1 = require("@dfinity/candid");
16
17
  class ActorManager {
17
18
  constructor(actorConfig) {
18
19
  this._actor = null;
@@ -37,11 +38,31 @@ class ActorManager {
37
38
  this.initialize = (options) => __awaiter(this, void 0, void 0, function* () {
38
39
  yield this._agentManager.updateAgent(options);
39
40
  });
40
- this.extractMethodNames = () => {
41
+ this.extractInterface = () => {
42
+ return this._idlFactory({ IDL: candid_1.IDL });
43
+ };
44
+ this.extractMethodAttributes = () => {
41
45
  const iface = this.extractInterface();
42
- return iface._fields.map((service) => service[0]);
46
+ const methodAttributesArray = iface._fields.map(([name, func]) => ({
47
+ name: name,
48
+ attributes: {
49
+ numberOfArgs: func.argTypes.length,
50
+ type: ((0, helper_1.isQuery)(func) ? "query" : "update"),
51
+ validate: (arg) => func.argTypes.some((t, i) => t.covariant(arg[i])),
52
+ },
53
+ }));
54
+ methodAttributesArray.sort((a, b) => {
55
+ if (a.attributes.type === b.attributes.type) {
56
+ return a.attributes.numberOfArgs - b.attributes.numberOfArgs;
57
+ }
58
+ return a.attributes.type === "query" ? -1 : 1;
59
+ });
60
+ return methodAttributesArray.reduce((acc, { name, attributes }) => {
61
+ acc[name] = attributes;
62
+ return acc;
63
+ }, {});
43
64
  };
44
- this.extractService = () => {
65
+ this.extractVisitor = () => {
45
66
  const iface = this.extractInterface();
46
67
  return iface._fields.reduce((acc, service) => {
47
68
  const functionName = service[0];
@@ -53,12 +74,6 @@ class ActorManager {
53
74
  return acc;
54
75
  }, {});
55
76
  };
56
- this.extractInterface = () => {
57
- if (this._actor === null) {
58
- throw new Error("For extracting interface, actor must be initialized");
59
- }
60
- return agent_1.Actor.interfaceOf(this._actor);
61
- };
62
77
  this.initializeActor = (agent) => {
63
78
  console.info(`Initializing actor ${this.canisterId} on ${agent.isLocal() ? "local" : "ic"} network`);
64
79
  const { _idlFactory, canisterId } = this;
@@ -123,9 +138,16 @@ class ActorManager {
123
138
  if (!canisterId) {
124
139
  throw new Error("CanisterId is required!");
125
140
  }
126
- this._agentManager = agentManager;
127
141
  this.canisterId = canisterId;
142
+ if (!idlFactory) {
143
+ throw new Error("IDL factory is required!");
144
+ }
128
145
  this._idlFactory = idlFactory;
146
+ this.methodAttributes = this.extractMethodAttributes();
147
+ if (!agentManager) {
148
+ throw new Error("Agent manager is required!");
149
+ }
150
+ this._agentManager = agentManager;
129
151
  // Initialize stores
130
152
  this.actorStore = (0, helper_1.createStoreWithOptionalDevtools)(this.initialState, {
131
153
  withDevtools,
@@ -134,7 +156,7 @@ class ActorManager {
134
156
  });
135
157
  this._unsubscribeAgent = this._agentManager.subscribeAgent(this.initializeActor, initializeOnCreate);
136
158
  if (withVisitor) {
137
- this.visitFunction = this.extractService();
159
+ this.visitFunction = this.extractVisitor();
138
160
  }
139
161
  else {
140
162
  this.visitFunction = emptyVisitor;
@@ -44,3 +44,8 @@ export type ActorState<A = BaseActor> = {
44
44
  };
45
45
  export type ActorStore<A = BaseActor> = StoreApiWithDevtools<ActorState<A>>;
46
46
  export type CallActorMethod<A = BaseActor> = <M extends FunctionName<A> = FunctionName<A>>(functionName: M, ...args: ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
47
+ export type MethodAttributes<A = BaseActor> = Record<FunctionName<A>, {
48
+ type: FunctionType;
49
+ numberOfArgs: number;
50
+ validate: (arg: never) => boolean;
51
+ }>;
@@ -91,7 +91,7 @@ class CandidAdapter {
91
91
  });
92
92
  const js = yield didjs.did_to_js(candidSource);
93
93
  if (JSON.stringify(js) === JSON.stringify([])) {
94
- throw new Error("Cannot fetch candid file");
94
+ throw new Error("Cannot compile Candid to JavaScript");
95
95
  }
96
96
  const dataUri = "data:text/javascript;charset=utf-8," +
97
97
  encodeURIComponent(js[0]);
@@ -1,11 +1,12 @@
1
1
  import { DevtoolsOptions } from "zustand/middleware";
2
- import type { BaseActor } from "../types";
2
+ import type { BaseActor, IDL } from "../types";
3
3
  export declare function createStoreWithOptionalDevtools<T>(initialState: T, config: DevtoolsOptions): Omit<import("zustand/vanilla").StoreApi<T>, "setState"> & {
4
4
  setState<A extends string | {
5
5
  type: string;
6
6
  }>(partial: T | Partial<T> | ((state: T) => T | Partial<T>), replace?: boolean | undefined, action?: A | undefined): void;
7
7
  };
8
8
  export declare const isInLocalOrDevelopment: () => boolean;
9
+ export declare function isQuery(func: IDL.FuncClass): boolean;
9
10
  export declare const jsonToString: (json: unknown, space?: number) => string;
10
11
  export declare const generateRequestHash: (args?: unknown[]) => `0x${string}`;
11
12
  export declare const generateHash: (field?: unknown) => `0x${string}`;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.stringToHash = exports.generateActorHash = exports.generateHash = exports.generateRequestHash = exports.jsonToString = exports.isInLocalOrDevelopment = exports.createStoreWithOptionalDevtools = void 0;
3
+ exports.stringToHash = exports.generateActorHash = exports.generateHash = exports.generateRequestHash = exports.jsonToString = exports.isQuery = exports.isInLocalOrDevelopment = exports.createStoreWithOptionalDevtools = void 0;
4
4
  const agent_1 = require("@dfinity/agent");
5
5
  const middleware_1 = require("zustand/middleware");
6
6
  const vanilla_1 = require("zustand/vanilla");
@@ -21,6 +21,11 @@ const isInLocalOrDevelopment = () => {
21
21
  process.env.NODE_ENV === "development"));
22
22
  };
23
23
  exports.isInLocalOrDevelopment = isInLocalOrDevelopment;
24
+ function isQuery(func) {
25
+ return (func.annotations.includes("query") ||
26
+ func.annotations.includes("composite_query"));
27
+ }
28
+ exports.isQuery = isQuery;
24
29
  const jsonToString = (json, space = 2) => {
25
30
  return JSON.stringify(json, (_, value) => (typeof value === "bigint" ? `BigInt(${value})` : value), space);
26
31
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/core",
3
- "version": "1.2.3",
3
+ "version": "1.3.1",
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",
@@ -52,5 +52,5 @@
52
52
  "engines": {
53
53
  "node": ">=10"
54
54
  },
55
- "gitHead": "ae71ae0c7eee0ebe8a97751d4222054668a6b994"
55
+ "gitHead": "05130e2581edfafd6390cf04ba618cb084bafc31"
56
56
  }