@ic-reactor/candid 3.0.1-beta.0 → 3.0.2-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/display-reactor.d.ts +190 -0
- package/dist/display-reactor.d.ts.map +1 -0
- package/dist/display-reactor.js +280 -0
- package/dist/display-reactor.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import type { BaseActor, DisplayReactorParameters } from "@ic-reactor/core";
|
|
2
|
+
import type { DynamicMethodOptions } from "./types";
|
|
3
|
+
import { DisplayReactor } from "@ic-reactor/core";
|
|
4
|
+
import { CandidAdapter } from "./adapter";
|
|
5
|
+
import { IDL } from "@icp-sdk/core/candid";
|
|
6
|
+
export interface CandidDisplayReactorParameters<A = BaseActor> extends Omit<DisplayReactorParameters<A>, "idlFactory" | "actor"> {
|
|
7
|
+
/** The canister ID. */
|
|
8
|
+
canisterId: string;
|
|
9
|
+
/** The Candid source code. If not provided, the canister's Candid will be fetched. */
|
|
10
|
+
candid?: string;
|
|
11
|
+
/** The IDL interface factory. */
|
|
12
|
+
idlFactory?: IDL.InterfaceFactory;
|
|
13
|
+
/** The actor instance. */
|
|
14
|
+
actor?: A;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* CandidDisplayReactor combines the display transformation capabilities of
|
|
18
|
+
* DisplayReactor with dynamic Candid parsing from CandidReactor.
|
|
19
|
+
*
|
|
20
|
+
* This class provides:
|
|
21
|
+
* - **Display transformations**: Automatic type conversion between Candid and
|
|
22
|
+
* display-friendly types (bigint ↔ string, Principal ↔ string, etc.)
|
|
23
|
+
* - **Validation**: Optional argument validation with display types
|
|
24
|
+
* - **Dynamic Candid parsing**: Initialize from Candid source or fetch from network
|
|
25
|
+
* - **Dynamic method registration**: Register methods at runtime with Candid signatures
|
|
26
|
+
*
|
|
27
|
+
* @typeParam A - The actor service type
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import { CandidDisplayReactor } from "@ic-reactor/candid"
|
|
32
|
+
*
|
|
33
|
+
* const reactor = new CandidDisplayReactor({
|
|
34
|
+
* clientManager,
|
|
35
|
+
* canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai",
|
|
36
|
+
* })
|
|
37
|
+
*
|
|
38
|
+
* // Initialize from network (fetches Candid from canister)
|
|
39
|
+
* await reactor.initialize()
|
|
40
|
+
*
|
|
41
|
+
* // Or provide Candid source directly
|
|
42
|
+
* const reactor2 = new CandidDisplayReactor({
|
|
43
|
+
* clientManager,
|
|
44
|
+
* canisterId: "...",
|
|
45
|
+
* candid: `service : { greet : (text) -> (text) query }`
|
|
46
|
+
* })
|
|
47
|
+
* await reactor2.initialize()
|
|
48
|
+
*
|
|
49
|
+
* // Call methods with display types (strings instead of bigint/Principal)
|
|
50
|
+
* const result = await reactor.callMethod({
|
|
51
|
+
* functionName: "transfer",
|
|
52
|
+
* args: [{ to: "aaaaa-aa", amount: "1000000" }] // strings!
|
|
53
|
+
* })
|
|
54
|
+
*
|
|
55
|
+
* // Add validation
|
|
56
|
+
* reactor.registerValidator("transfer", ([input]) => {
|
|
57
|
+
* if (!input.to) {
|
|
58
|
+
* return { success: false, issues: [{ path: ["to"], message: "Required" }] }
|
|
59
|
+
* }
|
|
60
|
+
* return { success: true }
|
|
61
|
+
* })
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export declare class CandidDisplayReactor<A = BaseActor> extends DisplayReactor<A> {
|
|
65
|
+
adapter: CandidAdapter;
|
|
66
|
+
private candidSource?;
|
|
67
|
+
constructor(config: CandidDisplayReactorParameters<A>);
|
|
68
|
+
/**
|
|
69
|
+
* Initializes the reactor by parsing the provided Candid string or fetching it from the network.
|
|
70
|
+
* This updates the internal service definition with the actual canister interface.
|
|
71
|
+
*
|
|
72
|
+
* After initialization, all DisplayReactor methods work with display type transformations.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const reactor = new CandidDisplayReactor({
|
|
77
|
+
* clientManager,
|
|
78
|
+
* canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai",
|
|
79
|
+
* })
|
|
80
|
+
*
|
|
81
|
+
* // Fetches Candid from the canister and initializes
|
|
82
|
+
* await reactor.initialize()
|
|
83
|
+
*
|
|
84
|
+
* // Now you can call methods with display types
|
|
85
|
+
* const balance = await reactor.callMethod({
|
|
86
|
+
* functionName: "icrc1_balance_of",
|
|
87
|
+
* args: [{ owner: "aaaaa-aa" }] // Principal as string!
|
|
88
|
+
* })
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
initialize(): Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* Re-initialize the display codecs after the service has been updated.
|
|
94
|
+
* This is called automatically after initialize() or registerMethod().
|
|
95
|
+
*/
|
|
96
|
+
private reinitializeCodecs;
|
|
97
|
+
/**
|
|
98
|
+
* Register a dynamic method by its Candid signature.
|
|
99
|
+
* After registration, all DisplayReactor methods work with display type transformations.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* // Register a method
|
|
104
|
+
* await reactor.registerMethod({
|
|
105
|
+
* functionName: "icrc1_balance_of",
|
|
106
|
+
* candid: "(record { owner : principal }) -> (nat) query"
|
|
107
|
+
* })
|
|
108
|
+
*
|
|
109
|
+
* // Now use with display types!
|
|
110
|
+
* const balance = await reactor.callMethod({
|
|
111
|
+
* functionName: "icrc1_balance_of",
|
|
112
|
+
* args: [{ owner: "aaaaa-aa" }] // Principal as string
|
|
113
|
+
* })
|
|
114
|
+
* // balance is string (not bigint) due to display transformation
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
registerMethod(options: DynamicMethodOptions): Promise<void>;
|
|
118
|
+
/**
|
|
119
|
+
* Register multiple methods at once.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* await reactor.registerMethods([
|
|
124
|
+
* { functionName: "icrc1_balance_of", candid: "(record { owner : principal }) -> (nat) query" },
|
|
125
|
+
* { functionName: "icrc1_transfer", candid: "(record { to : principal; amount : nat }) -> (variant { Ok : nat; Err : text })" }
|
|
126
|
+
* ])
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
registerMethods(methods: DynamicMethodOptions[]): Promise<void>;
|
|
130
|
+
/**
|
|
131
|
+
* Check if a method is registered (either from initialize or registerMethod).
|
|
132
|
+
*/
|
|
133
|
+
hasMethod(functionName: string): boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Get all registered method names.
|
|
136
|
+
*/
|
|
137
|
+
getMethodNames(): string[];
|
|
138
|
+
/**
|
|
139
|
+
* Perform a dynamic update call in one step with display type transformations.
|
|
140
|
+
* Registers the method if not already registered, then calls it.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```typescript
|
|
144
|
+
* const result = await reactor.callDynamic({
|
|
145
|
+
* functionName: "transfer",
|
|
146
|
+
* candid: "(record { to : principal; amount : nat }) -> (variant { Ok : nat; Err : text })",
|
|
147
|
+
* args: [{ to: "aaaaa-aa", amount: "100" }] // Display types!
|
|
148
|
+
* })
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
callDynamic<T = unknown>(options: DynamicMethodOptions & {
|
|
152
|
+
args?: unknown[];
|
|
153
|
+
}): Promise<T>;
|
|
154
|
+
/**
|
|
155
|
+
* Perform a dynamic query call in one step with display type transformations.
|
|
156
|
+
* Registers the method if not already registered, then calls it.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* const balance = await reactor.queryDynamic({
|
|
161
|
+
* functionName: "icrc1_balance_of",
|
|
162
|
+
* candid: "(record { owner : principal }) -> (nat) query",
|
|
163
|
+
* args: [{ owner: "aaaaa-aa" }] // Display types!
|
|
164
|
+
* })
|
|
165
|
+
* // balance is string (not BigInt)
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
queryDynamic<T = unknown>(options: DynamicMethodOptions & {
|
|
169
|
+
args?: unknown[];
|
|
170
|
+
}): Promise<T>;
|
|
171
|
+
/**
|
|
172
|
+
* Fetch with dynamic Candid and TanStack Query caching.
|
|
173
|
+
* Registers the method if not already registered, then fetches with caching.
|
|
174
|
+
* Results are transformed to display types.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* const balance = await reactor.fetchQueryDynamic({
|
|
179
|
+
* functionName: "icrc1_balance_of",
|
|
180
|
+
* candid: "(record { owner : principal }) -> (nat) query",
|
|
181
|
+
* args: [{ owner: "aaaaa-aa" }]
|
|
182
|
+
* })
|
|
183
|
+
* // Subsequent calls with same args return cached result
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
fetchQueryDynamic<T = unknown>(options: DynamicMethodOptions & {
|
|
187
|
+
args?: unknown[];
|
|
188
|
+
}): Promise<T>;
|
|
189
|
+
}
|
|
190
|
+
//# sourceMappingURL=display-reactor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"display-reactor.d.ts","sourceRoot":"","sources":["../src/display-reactor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,wBAAwB,EAAE,MAAM,kBAAkB,CAAA;AAC3E,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAEnD,OAAO,EACL,cAAc,EAGf,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AACzC,OAAO,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAA;AAM1C,MAAM,WAAW,8BAA8B,CAAC,CAAC,GAAG,SAAS,CAAE,SAAQ,IAAI,CACzE,wBAAwB,CAAC,CAA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AC,EAAE,OAAO,EAAE,CAAA;KAAE,GACnD,OAAO,CAAC,CAAC,CAAC;IAQb;;;;;;;;;;;;;;OAcG;IACU,iBAAiB,CAAC,CAAC,GAAG,OAAO,EACxC,OAAO,EAAE,oBAAoB,GAAG;QAAE,IAAI,CAAC,EAAE,OAAO,EAAE,CAAA;KAAE,GACnD,OAAO,CAAC,CAAC,CAAC;CAOd"}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { DisplayReactor, didToDisplayCodec, didTypeFromArray, } from "@ic-reactor/core";
|
|
2
|
+
import { CandidAdapter } from "./adapter";
|
|
3
|
+
import { IDL } from "@icp-sdk/core/candid";
|
|
4
|
+
// ============================================================================
|
|
5
|
+
// CandidDisplayReactor
|
|
6
|
+
// ============================================================================
|
|
7
|
+
/**
|
|
8
|
+
* CandidDisplayReactor combines the display transformation capabilities of
|
|
9
|
+
* DisplayReactor with dynamic Candid parsing from CandidReactor.
|
|
10
|
+
*
|
|
11
|
+
* This class provides:
|
|
12
|
+
* - **Display transformations**: Automatic type conversion between Candid and
|
|
13
|
+
* display-friendly types (bigint ↔ string, Principal ↔ string, etc.)
|
|
14
|
+
* - **Validation**: Optional argument validation with display types
|
|
15
|
+
* - **Dynamic Candid parsing**: Initialize from Candid source or fetch from network
|
|
16
|
+
* - **Dynamic method registration**: Register methods at runtime with Candid signatures
|
|
17
|
+
*
|
|
18
|
+
* @typeParam A - The actor service type
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* import { CandidDisplayReactor } from "@ic-reactor/candid"
|
|
23
|
+
*
|
|
24
|
+
* const reactor = new CandidDisplayReactor({
|
|
25
|
+
* clientManager,
|
|
26
|
+
* canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai",
|
|
27
|
+
* })
|
|
28
|
+
*
|
|
29
|
+
* // Initialize from network (fetches Candid from canister)
|
|
30
|
+
* await reactor.initialize()
|
|
31
|
+
*
|
|
32
|
+
* // Or provide Candid source directly
|
|
33
|
+
* const reactor2 = new CandidDisplayReactor({
|
|
34
|
+
* clientManager,
|
|
35
|
+
* canisterId: "...",
|
|
36
|
+
* candid: `service : { greet : (text) -> (text) query }`
|
|
37
|
+
* })
|
|
38
|
+
* await reactor2.initialize()
|
|
39
|
+
*
|
|
40
|
+
* // Call methods with display types (strings instead of bigint/Principal)
|
|
41
|
+
* const result = await reactor.callMethod({
|
|
42
|
+
* functionName: "transfer",
|
|
43
|
+
* args: [{ to: "aaaaa-aa", amount: "1000000" }] // strings!
|
|
44
|
+
* })
|
|
45
|
+
*
|
|
46
|
+
* // Add validation
|
|
47
|
+
* reactor.registerValidator("transfer", ([input]) => {
|
|
48
|
+
* if (!input.to) {
|
|
49
|
+
* return { success: false, issues: [{ path: ["to"], message: "Required" }] }
|
|
50
|
+
* }
|
|
51
|
+
* return { success: true }
|
|
52
|
+
* })
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export class CandidDisplayReactor extends DisplayReactor {
|
|
56
|
+
constructor(config) {
|
|
57
|
+
// If idlFactory/actor are missing, use a dummy one to satisfy DisplayReactor constructor
|
|
58
|
+
const superConfig = { ...config };
|
|
59
|
+
if (!superConfig.idlFactory && !superConfig.actor) {
|
|
60
|
+
// Use `any` for IDL to avoid type compatibility issues across package versions
|
|
61
|
+
superConfig.idlFactory = (config) => config.IDL.Service({});
|
|
62
|
+
}
|
|
63
|
+
super(superConfig);
|
|
64
|
+
Object.defineProperty(this, "adapter", {
|
|
65
|
+
enumerable: true,
|
|
66
|
+
configurable: true,
|
|
67
|
+
writable: true,
|
|
68
|
+
value: void 0
|
|
69
|
+
});
|
|
70
|
+
Object.defineProperty(this, "candidSource", {
|
|
71
|
+
enumerable: true,
|
|
72
|
+
configurable: true,
|
|
73
|
+
writable: true,
|
|
74
|
+
value: void 0
|
|
75
|
+
});
|
|
76
|
+
this.candidSource = config.candid;
|
|
77
|
+
this.adapter = new CandidAdapter({
|
|
78
|
+
clientManager: this.clientManager,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
// ══════════════════════════════════════════════════════════════════════════
|
|
82
|
+
// INITIALIZATION
|
|
83
|
+
// ══════════════════════════════════════════════════════════════════════════
|
|
84
|
+
/**
|
|
85
|
+
* Initializes the reactor by parsing the provided Candid string or fetching it from the network.
|
|
86
|
+
* This updates the internal service definition with the actual canister interface.
|
|
87
|
+
*
|
|
88
|
+
* After initialization, all DisplayReactor methods work with display type transformations.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```typescript
|
|
92
|
+
* const reactor = new CandidDisplayReactor({
|
|
93
|
+
* clientManager,
|
|
94
|
+
* canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai",
|
|
95
|
+
* })
|
|
96
|
+
*
|
|
97
|
+
* // Fetches Candid from the canister and initializes
|
|
98
|
+
* await reactor.initialize()
|
|
99
|
+
*
|
|
100
|
+
* // Now you can call methods with display types
|
|
101
|
+
* const balance = await reactor.callMethod({
|
|
102
|
+
* functionName: "icrc1_balance_of",
|
|
103
|
+
* args: [{ owner: "aaaaa-aa" }] // Principal as string!
|
|
104
|
+
* })
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
async initialize() {
|
|
108
|
+
let idlFactory;
|
|
109
|
+
if (this.candidSource) {
|
|
110
|
+
const definition = await this.adapter.parseCandidSource(this.candidSource);
|
|
111
|
+
idlFactory = definition.idlFactory;
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
const definition = await this.adapter.getCandidDefinition(this.canisterId);
|
|
115
|
+
idlFactory = definition.idlFactory;
|
|
116
|
+
}
|
|
117
|
+
this.service = idlFactory({ IDL });
|
|
118
|
+
// Re-initialize codecs after service is updated
|
|
119
|
+
this.reinitializeCodecs();
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Re-initialize the display codecs after the service has been updated.
|
|
123
|
+
* This is called automatically after initialize() or registerMethod().
|
|
124
|
+
*/
|
|
125
|
+
reinitializeCodecs() {
|
|
126
|
+
const fields = this.getServiceInterface()?._fields;
|
|
127
|
+
if (!fields)
|
|
128
|
+
return;
|
|
129
|
+
// Access the private codecs map from DisplayReactor
|
|
130
|
+
const codecs = this.codecs;
|
|
131
|
+
for (const [methodName, funcType] of fields) {
|
|
132
|
+
// Skip if already exists
|
|
133
|
+
if (codecs.has(methodName))
|
|
134
|
+
continue;
|
|
135
|
+
const argsIdlType = didTypeFromArray(funcType.argTypes);
|
|
136
|
+
const retIdlType = didTypeFromArray(funcType.retTypes);
|
|
137
|
+
codecs.set(methodName, {
|
|
138
|
+
args: didToDisplayCodec(argsIdlType),
|
|
139
|
+
result: didToDisplayCodec(retIdlType),
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
// ══════════════════════════════════════════════════════════════════════════
|
|
144
|
+
// DYNAMIC METHOD REGISTRATION
|
|
145
|
+
// ══════════════════════════════════════════════════════════════════════════
|
|
146
|
+
/**
|
|
147
|
+
* Register a dynamic method by its Candid signature.
|
|
148
|
+
* After registration, all DisplayReactor methods work with display type transformations.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* // Register a method
|
|
153
|
+
* await reactor.registerMethod({
|
|
154
|
+
* functionName: "icrc1_balance_of",
|
|
155
|
+
* candid: "(record { owner : principal }) -> (nat) query"
|
|
156
|
+
* })
|
|
157
|
+
*
|
|
158
|
+
* // Now use with display types!
|
|
159
|
+
* const balance = await reactor.callMethod({
|
|
160
|
+
* functionName: "icrc1_balance_of",
|
|
161
|
+
* args: [{ owner: "aaaaa-aa" }] // Principal as string
|
|
162
|
+
* })
|
|
163
|
+
* // balance is string (not bigint) due to display transformation
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
async registerMethod(options) {
|
|
167
|
+
const { functionName, candid } = options;
|
|
168
|
+
// Check if method already registered
|
|
169
|
+
const existing = this.service._fields.find(([name]) => name === functionName);
|
|
170
|
+
if (existing)
|
|
171
|
+
return;
|
|
172
|
+
// Parse the Candid signature
|
|
173
|
+
const serviceSource = candid.includes("service :")
|
|
174
|
+
? candid
|
|
175
|
+
: `service : { ${functionName} : ${candid}; }`;
|
|
176
|
+
const { idlFactory } = await this.adapter.parseCandidSource(serviceSource);
|
|
177
|
+
const parsedService = idlFactory({ IDL });
|
|
178
|
+
const funcField = parsedService._fields.find(([name]) => name === functionName);
|
|
179
|
+
if (!funcField) {
|
|
180
|
+
throw new Error(`Method "${functionName}" not found in the provided Candid signature`);
|
|
181
|
+
}
|
|
182
|
+
// Inject into our service
|
|
183
|
+
this.service._fields.push(funcField);
|
|
184
|
+
// Re-initialize codecs for the new method
|
|
185
|
+
this.reinitializeCodecs();
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Register multiple methods at once.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```typescript
|
|
192
|
+
* await reactor.registerMethods([
|
|
193
|
+
* { functionName: "icrc1_balance_of", candid: "(record { owner : principal }) -> (nat) query" },
|
|
194
|
+
* { functionName: "icrc1_transfer", candid: "(record { to : principal; amount : nat }) -> (variant { Ok : nat; Err : text })" }
|
|
195
|
+
* ])
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
async registerMethods(methods) {
|
|
199
|
+
await Promise.all(methods.map((m) => this.registerMethod(m)));
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Check if a method is registered (either from initialize or registerMethod).
|
|
203
|
+
*/
|
|
204
|
+
hasMethod(functionName) {
|
|
205
|
+
return this.service._fields.some(([name]) => name === functionName);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Get all registered method names.
|
|
209
|
+
*/
|
|
210
|
+
getMethodNames() {
|
|
211
|
+
return this.service._fields.map(([name]) => name);
|
|
212
|
+
}
|
|
213
|
+
// ══════════════════════════════════════════════════════════════════════════
|
|
214
|
+
// DYNAMIC CALL SHORTCUTS
|
|
215
|
+
// ══════════════════════════════════════════════════════════════════════════
|
|
216
|
+
/**
|
|
217
|
+
* Perform a dynamic update call in one step with display type transformations.
|
|
218
|
+
* Registers the method if not already registered, then calls it.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```typescript
|
|
222
|
+
* const result = await reactor.callDynamic({
|
|
223
|
+
* functionName: "transfer",
|
|
224
|
+
* candid: "(record { to : principal; amount : nat }) -> (variant { Ok : nat; Err : text })",
|
|
225
|
+
* args: [{ to: "aaaaa-aa", amount: "100" }] // Display types!
|
|
226
|
+
* })
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
async callDynamic(options) {
|
|
230
|
+
await this.registerMethod(options);
|
|
231
|
+
return this.callMethod({
|
|
232
|
+
functionName: options.functionName,
|
|
233
|
+
args: options.args,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Perform a dynamic query call in one step with display type transformations.
|
|
238
|
+
* Registers the method if not already registered, then calls it.
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```typescript
|
|
242
|
+
* const balance = await reactor.queryDynamic({
|
|
243
|
+
* functionName: "icrc1_balance_of",
|
|
244
|
+
* candid: "(record { owner : principal }) -> (nat) query",
|
|
245
|
+
* args: [{ owner: "aaaaa-aa" }] // Display types!
|
|
246
|
+
* })
|
|
247
|
+
* // balance is string (not BigInt)
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
async queryDynamic(options) {
|
|
251
|
+
await this.registerMethod(options);
|
|
252
|
+
return this.callMethod({
|
|
253
|
+
functionName: options.functionName,
|
|
254
|
+
args: options.args,
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Fetch with dynamic Candid and TanStack Query caching.
|
|
259
|
+
* Registers the method if not already registered, then fetches with caching.
|
|
260
|
+
* Results are transformed to display types.
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* ```typescript
|
|
264
|
+
* const balance = await reactor.fetchQueryDynamic({
|
|
265
|
+
* functionName: "icrc1_balance_of",
|
|
266
|
+
* candid: "(record { owner : principal }) -> (nat) query",
|
|
267
|
+
* args: [{ owner: "aaaaa-aa" }]
|
|
268
|
+
* })
|
|
269
|
+
* // Subsequent calls with same args return cached result
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
async fetchQueryDynamic(options) {
|
|
273
|
+
await this.registerMethod(options);
|
|
274
|
+
return this.fetchQuery({
|
|
275
|
+
functionName: options.functionName,
|
|
276
|
+
args: options.args,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
//# sourceMappingURL=display-reactor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"display-reactor.js","sourceRoot":"","sources":["../src/display-reactor.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,cAAc,EACd,iBAAiB,EACjB,gBAAgB,GACjB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AACzC,OAAO,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAA;AAoB1C,+EAA+E;AAC/E,uBAAuB;AACvB,+EAA+E;AAE/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,MAAM,OAAO,oBAAoC,SAAQ,cAAiB;IAIxE,YAAY,MAAyC;QACnD,yFAAyF;QACzF,MAAM,WAAW,GAAG,EAAE,GAAG,MAAM,EAAE,CAAA;QAEjC,IAAI,CAAC,WAAW,CAAC,UAAU,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC;YAClD,+EAA+E;YAC/E,WAAW,CAAC,UAAU,GAAG,CAAC,MAAoB,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;QAC3E,CAAC;QAED,KAAK,CAAC,WAA0C,CAAC,CAAA;QAZ5C;;;;;WAAsB;QACrB;;;;;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ACI,KAAK,CAAC,iBAAiB,CAC5B,OAAoD;QAEpD,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,CAAA;QAClC,OAAO,IAAI,CAAC,UAAU,CAAC;YACrB,YAAY,EAAE,OAAO,CAAC,YAAmB;YACzC,IAAI,EAAE,OAAO,CAAC,IAAW;SAC1B,CAAM,CAAA;IACT,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AACzC,cAAc,SAAS,CAAA;AACvB,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AACzC,cAAc,SAAS,CAAA;AACvB,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,cAAc,mBAAmB,CAAA"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AACzC,cAAc,SAAS,CAAA;AACvB,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AACzC,cAAc,SAAS,CAAA;AACvB,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,cAAc,mBAAmB,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/candid",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2-beta.1",
|
|
4
4
|
"description": "IC Reactor Candid Adapter - Fetch and parse Candid definitions from Internet Computer canisters",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"author": "Behrad Deylami",
|
|
42
42
|
"license": "MIT",
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@ic-reactor/core": "^3.0.
|
|
44
|
+
"@ic-reactor/core": "^3.0.2-beta.1"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"@icp-sdk/core": "^5.0.0"
|
|
@@ -56,9 +56,9 @@
|
|
|
56
56
|
},
|
|
57
57
|
"size-limit": [
|
|
58
58
|
{
|
|
59
|
-
"name": "Candid
|
|
59
|
+
"name": "Candid Package",
|
|
60
60
|
"path": "dist/index.js",
|
|
61
|
-
"limit": "
|
|
61
|
+
"limit": "30 KB",
|
|
62
62
|
"gzip": true
|
|
63
63
|
}
|
|
64
64
|
],
|