@ic-reactor/candid 3.0.0-beta.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 +358 -0
- package/dist/adapter.d.ts +197 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +384 -0
- package/dist/adapter.js.map +1 -0
- package/dist/constants.d.ts +11 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +11 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/reactor.d.ts +103 -0
- package/dist/reactor.d.ts.map +1 -0
- package/dist/reactor.js +173 -0
- package/dist/reactor.js.map +1 -0
- package/dist/types.d.ts +77 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +14 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +27 -0
- package/dist/utils.js.map +1 -0
- package/package.json +72 -0
package/dist/adapter.js
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import { CanisterStatus } from "@icp-sdk/core/agent";
|
|
2
|
+
import { IDL } from "@icp-sdk/core/candid";
|
|
3
|
+
import { DEFAULT_IC_DIDJS_ID, DEFAULT_LOCAL_DIDJS_ID } from "./constants";
|
|
4
|
+
import { importCandidDefinition, noop } from "./utils";
|
|
5
|
+
/**
|
|
6
|
+
* CandidAdapter provides functionality to fetch and parse Candid definitions
|
|
7
|
+
* from Internet Computer canisters.
|
|
8
|
+
*
|
|
9
|
+
* It supports multiple methods for retrieving Candid definitions:
|
|
10
|
+
* 1. From canister metadata (preferred)
|
|
11
|
+
* 2. From the `__get_candid_interface_tmp_hack` method (fallback)
|
|
12
|
+
*
|
|
13
|
+
* It also supports parsing Candid to JavaScript using:
|
|
14
|
+
* 1. Local WASM parser (@ic-reactor/parser) - faster, no network request
|
|
15
|
+
* 2. Remote didjs canister - always available fallback
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { CandidAdapter } from "@ic-reactor/candid"
|
|
20
|
+
* import { ClientManager } from "@ic-reactor/core"
|
|
21
|
+
* import { QueryClient } from "@tanstack/query-core"
|
|
22
|
+
*
|
|
23
|
+
* const queryClient = new QueryClient()
|
|
24
|
+
* const clientManager = new ClientManager({ queryClient })
|
|
25
|
+
* await clientManager.initialize()
|
|
26
|
+
*
|
|
27
|
+
* const adapter = new CandidAdapter({ clientManager })
|
|
28
|
+
*
|
|
29
|
+
* // Optionally load the local parser for faster processing
|
|
30
|
+
* await adapter.loadParser()
|
|
31
|
+
*
|
|
32
|
+
* // Get the Candid definition for a canister
|
|
33
|
+
* const { idlFactory } = await adapter.getCandidDefinition("ryjl3-tyaaa-aaaaa-aaaba-cai")
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export class CandidAdapter {
|
|
37
|
+
/**
|
|
38
|
+
* Creates a new CandidAdapter instance.
|
|
39
|
+
*
|
|
40
|
+
* @param params - The adapter parameters.
|
|
41
|
+
*/
|
|
42
|
+
constructor({ clientManager, didjsCanisterId }) {
|
|
43
|
+
/** The client manager providing agent and identity access. */
|
|
44
|
+
Object.defineProperty(this, "clientManager", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
writable: true,
|
|
48
|
+
value: void 0
|
|
49
|
+
});
|
|
50
|
+
/** The canister ID of the didjs canister for remote Candid compilation. */
|
|
51
|
+
Object.defineProperty(this, "didjsCanisterId", {
|
|
52
|
+
enumerable: true,
|
|
53
|
+
configurable: true,
|
|
54
|
+
writable: true,
|
|
55
|
+
value: void 0
|
|
56
|
+
});
|
|
57
|
+
/** The optional local parser module. */
|
|
58
|
+
Object.defineProperty(this, "parserModule", {
|
|
59
|
+
enumerable: true,
|
|
60
|
+
configurable: true,
|
|
61
|
+
writable: true,
|
|
62
|
+
value: void 0
|
|
63
|
+
});
|
|
64
|
+
/** Whether parser auto-loading has been attempted. */
|
|
65
|
+
Object.defineProperty(this, "parserLoadAttempted", {
|
|
66
|
+
enumerable: true,
|
|
67
|
+
configurable: true,
|
|
68
|
+
writable: true,
|
|
69
|
+
value: false
|
|
70
|
+
});
|
|
71
|
+
/** Function to unsubscribe from identity updates. */
|
|
72
|
+
Object.defineProperty(this, "unsubscribe", {
|
|
73
|
+
enumerable: true,
|
|
74
|
+
configurable: true,
|
|
75
|
+
writable: true,
|
|
76
|
+
value: noop
|
|
77
|
+
});
|
|
78
|
+
this.clientManager = clientManager;
|
|
79
|
+
this.didjsCanisterId = didjsCanisterId || this.getDefaultDidJsId();
|
|
80
|
+
// Subscribe to identity changes to update didjs canister ID if needed
|
|
81
|
+
this.unsubscribe = clientManager.subscribe(() => {
|
|
82
|
+
if (!didjsCanisterId) {
|
|
83
|
+
this.didjsCanisterId = this.getDefaultDidJsId();
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The HTTP agent from the client manager.
|
|
89
|
+
*/
|
|
90
|
+
get agent() {
|
|
91
|
+
return this.clientManager.agent;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Whether the local parser is available.
|
|
95
|
+
*/
|
|
96
|
+
get hasParser() {
|
|
97
|
+
return this.parserModule !== undefined;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Loads the local parser module for converting Candid to JavaScript.
|
|
101
|
+
* If no module is provided, attempts to dynamically load @ic-reactor/parser.
|
|
102
|
+
*
|
|
103
|
+
* @param module - Optional parser module to use.
|
|
104
|
+
* @throws Error if the parser loading fails.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* // Load the default parser
|
|
109
|
+
* await adapter.loadParser()
|
|
110
|
+
*
|
|
111
|
+
* // Or provide a custom parser
|
|
112
|
+
* import * as parser from "@ic-reactor/parser"
|
|
113
|
+
* await adapter.loadParser(parser)
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
async loadParser(module) {
|
|
117
|
+
if (module !== undefined) {
|
|
118
|
+
this.parserModule = module;
|
|
119
|
+
this.parserLoadAttempted = true;
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (this.parserLoadAttempted) {
|
|
123
|
+
return; // Already tried loading
|
|
124
|
+
}
|
|
125
|
+
this.parserLoadAttempted = true;
|
|
126
|
+
try {
|
|
127
|
+
this.parserModule = require("@ic-reactor/parser");
|
|
128
|
+
if (this.parserModule?.default) {
|
|
129
|
+
await this.parserModule.default();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
throw new Error(`Error loading parser: ${error}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Attempts to load the parser silently (no error if not available).
|
|
138
|
+
* Useful for optional parser initialization.
|
|
139
|
+
*/
|
|
140
|
+
async tryLoadParser() {
|
|
141
|
+
if (this.parserModule || this.parserLoadAttempted) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
this.parserLoadAttempted = true;
|
|
145
|
+
try {
|
|
146
|
+
this.parserModule = require("@ic-reactor/parser");
|
|
147
|
+
if (this.parserModule?.default) {
|
|
148
|
+
await this.parserModule.default();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
// Silently fail - parser is optional
|
|
153
|
+
this.parserModule = undefined;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Gets the default didjs canister ID based on whether the agent is local or not.
|
|
158
|
+
*/
|
|
159
|
+
getDefaultDidJsId() {
|
|
160
|
+
return this.clientManager.isLocal
|
|
161
|
+
? DEFAULT_LOCAL_DIDJS_ID
|
|
162
|
+
: DEFAULT_IC_DIDJS_ID;
|
|
163
|
+
}
|
|
164
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
165
|
+
// MAIN API - High-level methods for fetching Candid definitions
|
|
166
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
167
|
+
/**
|
|
168
|
+
* Gets the parsed Candid definition for a canister, ready for use with Actor.createActor.
|
|
169
|
+
* This is the main entry point for fetching a canister's interface.
|
|
170
|
+
*
|
|
171
|
+
* @param canisterId - The canister ID to get the Candid definition for.
|
|
172
|
+
* @returns The parsed Candid definition with idlFactory and optional init.
|
|
173
|
+
* @throws Error if fetching or parsing fails.
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```typescript
|
|
177
|
+
* const { idlFactory } = await adapter.getCandidDefinition("ryjl3-tyaaa-aaaaa-aaaba-cai")
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
async getCandidDefinition(canisterId) {
|
|
181
|
+
try {
|
|
182
|
+
const candidSource = await this.fetchCandidSource(canisterId);
|
|
183
|
+
return await this.parseCandidSource(candidSource);
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
throw new Error(`Error fetching canister ${canisterId}: ${error}`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Fetches the raw Candid source string for a canister.
|
|
191
|
+
* First attempts to get it from metadata, then falls back to the tmp hack method.
|
|
192
|
+
*
|
|
193
|
+
* @param canisterId - The canister ID to fetch the Candid source for.
|
|
194
|
+
* @returns The raw Candid source string (.did file contents).
|
|
195
|
+
* @throws Error if both methods fail.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```typescript
|
|
199
|
+
* const candidSource = await adapter.fetchCandidSource("ryjl3-tyaaa-aaaaa-aaaba-cai")
|
|
200
|
+
* console.log(candidSource) // service { greet: (text) -> (text) query; }
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
async fetchCandidSource(canisterId) {
|
|
204
|
+
// First attempt: Try getting Candid from metadata
|
|
205
|
+
const fromMetadata = await this.fetchFromMetadata(canisterId).catch(() => undefined);
|
|
206
|
+
if (fromMetadata) {
|
|
207
|
+
return fromMetadata;
|
|
208
|
+
}
|
|
209
|
+
// Second attempt: Try the temporary hack method
|
|
210
|
+
const fromTmpHack = await this.fetchFromTmpHack(canisterId).catch(() => undefined);
|
|
211
|
+
if (fromTmpHack) {
|
|
212
|
+
return fromTmpHack;
|
|
213
|
+
}
|
|
214
|
+
throw new Error("Failed to retrieve Candid source by any method.");
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Parses Candid source string and returns the definition with idlFactory.
|
|
218
|
+
* First attempts to use the local parser, then falls back to the remote didjs canister.
|
|
219
|
+
*
|
|
220
|
+
* @param candidSource - The raw Candid source string.
|
|
221
|
+
* @returns The parsed Candid definition.
|
|
222
|
+
* @throws Error if parsing fails.
|
|
223
|
+
*/
|
|
224
|
+
async parseCandidSource(candidSource) {
|
|
225
|
+
// Try to auto-load parser if not already loaded
|
|
226
|
+
await this.tryLoadParser();
|
|
227
|
+
let compiledJs;
|
|
228
|
+
// First attempt: Try local parser (faster, no network)
|
|
229
|
+
if (this.parserModule) {
|
|
230
|
+
try {
|
|
231
|
+
compiledJs = this.compileLocal(candidSource);
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
// Fall through to remote compilation
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
// Second attempt: Try remote didjs canister
|
|
238
|
+
if (!compiledJs) {
|
|
239
|
+
compiledJs = await this.compileRemote(candidSource);
|
|
240
|
+
}
|
|
241
|
+
if (!compiledJs) {
|
|
242
|
+
throw new Error("Failed to compile Candid to JavaScript");
|
|
243
|
+
}
|
|
244
|
+
return importCandidDefinition(compiledJs);
|
|
245
|
+
}
|
|
246
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
247
|
+
// FETCH METHODS - Low-level methods for fetching Candid source
|
|
248
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
249
|
+
/**
|
|
250
|
+
* Fetches Candid source from the canister's metadata.
|
|
251
|
+
*
|
|
252
|
+
* @param canisterId - The canister ID to query.
|
|
253
|
+
* @returns The Candid source string, or undefined if not available.
|
|
254
|
+
*/
|
|
255
|
+
async fetchFromMetadata(canisterId) {
|
|
256
|
+
const status = await CanisterStatus.request({
|
|
257
|
+
agent: this.agent,
|
|
258
|
+
canisterId: canisterId,
|
|
259
|
+
paths: ["candid"],
|
|
260
|
+
});
|
|
261
|
+
return status.get("candid");
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Fetches Candid source using the temporary hack method.
|
|
265
|
+
* This calls the `__get_candid_interface_tmp_hack` query method on the canister.
|
|
266
|
+
*
|
|
267
|
+
* @param canisterId - The canister ID to query.
|
|
268
|
+
* @returns The Candid source string.
|
|
269
|
+
*/
|
|
270
|
+
async fetchFromTmpHack(canisterId) {
|
|
271
|
+
const canisterIdStr = typeof canisterId === "string" ? canisterId : canisterId.toString();
|
|
272
|
+
// Use raw agent.query instead of Actor.createActor
|
|
273
|
+
const response = await this.agent.query(canisterIdStr, {
|
|
274
|
+
methodName: "__get_candid_interface_tmp_hack",
|
|
275
|
+
arg: IDL.encode([], []),
|
|
276
|
+
});
|
|
277
|
+
if ("reply" in response && response.reply) {
|
|
278
|
+
const [candidSource] = IDL.decode([IDL.Text], response.reply.arg);
|
|
279
|
+
return candidSource;
|
|
280
|
+
}
|
|
281
|
+
throw new Error(`Query failed: ${JSON.stringify(response)}`);
|
|
282
|
+
}
|
|
283
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
284
|
+
// COMPILE METHODS - Methods for compiling Candid to JavaScript
|
|
285
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
286
|
+
/**
|
|
287
|
+
* Compiles Candid source to JavaScript using the local WASM parser.
|
|
288
|
+
*
|
|
289
|
+
* @param candidSource - The Candid source to compile.
|
|
290
|
+
* @returns The compiled JavaScript code.
|
|
291
|
+
* @throws Error if the parser is not loaded.
|
|
292
|
+
*/
|
|
293
|
+
compileLocal(candidSource) {
|
|
294
|
+
if (!this.parserModule) {
|
|
295
|
+
throw new Error("Parser not loaded. Call loadParser() first.");
|
|
296
|
+
}
|
|
297
|
+
return this.parserModule.didToJs(candidSource);
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Compiles Candid source to JavaScript using the remote didjs canister.
|
|
301
|
+
*
|
|
302
|
+
* @param candidSource - The Candid source to compile.
|
|
303
|
+
* @param didjsCanisterId - Optional custom didjs canister ID.
|
|
304
|
+
* @returns The compiled JavaScript code, or undefined if compilation fails.
|
|
305
|
+
*/
|
|
306
|
+
async compileRemote(candidSource, didjsCanisterId) {
|
|
307
|
+
const canisterId = didjsCanisterId || this.didjsCanisterId;
|
|
308
|
+
// Use raw agent.query instead of Actor.createActor
|
|
309
|
+
const response = await this.agent.query(canisterId, {
|
|
310
|
+
methodName: "did_to_js",
|
|
311
|
+
arg: IDL.encode([IDL.Text], [candidSource]),
|
|
312
|
+
});
|
|
313
|
+
if ("reply" in response && response.reply) {
|
|
314
|
+
const [result] = IDL.decode([IDL.Opt(IDL.Text)], response.reply.arg);
|
|
315
|
+
return result[0];
|
|
316
|
+
}
|
|
317
|
+
throw new Error(`Query failed: ${JSON.stringify(response)}`);
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Validates Candid source using the local parser.
|
|
321
|
+
*
|
|
322
|
+
* @param candidSource - The Candid source to validate.
|
|
323
|
+
* @returns True if the source is valid, false otherwise.
|
|
324
|
+
* @throws Error if the parser is not loaded.
|
|
325
|
+
*/
|
|
326
|
+
validateCandid(candidSource) {
|
|
327
|
+
if (!this.parserModule) {
|
|
328
|
+
throw new Error("Parser not loaded. Call loadParser() first.");
|
|
329
|
+
}
|
|
330
|
+
return this.parserModule.validateIDL(candidSource);
|
|
331
|
+
}
|
|
332
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
333
|
+
// DEPRECATED ALIASES - For backwards compatibility
|
|
334
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
335
|
+
/**
|
|
336
|
+
* @deprecated Use `loadParser()` instead.
|
|
337
|
+
*/
|
|
338
|
+
async initializeParser(module) {
|
|
339
|
+
return this.loadParser(module);
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* @deprecated Use `fetchCandidSource()` instead.
|
|
343
|
+
*/
|
|
344
|
+
async fetchCandidDefinition(canisterId) {
|
|
345
|
+
return this.fetchCandidSource(canisterId);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* @deprecated Use `fetchFromMetadata()` instead.
|
|
349
|
+
*/
|
|
350
|
+
async getFromMetadata(canisterId) {
|
|
351
|
+
return this.fetchFromMetadata(canisterId);
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* @deprecated Use `fetchFromTmpHack()` instead.
|
|
355
|
+
*/
|
|
356
|
+
async getFromTmpHack(canisterId) {
|
|
357
|
+
return this.fetchFromTmpHack(canisterId);
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* @deprecated Use `parseCandidSource()` instead.
|
|
361
|
+
*/
|
|
362
|
+
async evaluateCandidDefinition(data) {
|
|
363
|
+
return this.parseCandidSource(data);
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* @deprecated Use `compileRemote()` instead.
|
|
367
|
+
*/
|
|
368
|
+
async fetchDidTojs(candidSource, didjsCanisterId) {
|
|
369
|
+
return this.compileRemote(candidSource, didjsCanisterId);
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* @deprecated Use `compileLocal()` instead.
|
|
373
|
+
*/
|
|
374
|
+
parseDidToJs(candidSource) {
|
|
375
|
+
return this.compileLocal(candidSource);
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* @deprecated Use `validateCandid()` instead.
|
|
379
|
+
*/
|
|
380
|
+
validateIDL(candidSource) {
|
|
381
|
+
return this.validateCandid(candidSource);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
//# sourceMappingURL=adapter.js.map
|
|
@@ -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|
|
|
1
|
+
/**
|
|
2
|
+
* Default didjs canister ID for the IC mainnet.
|
|
3
|
+
* This canister is used to compile Candid to JavaScript.
|
|
4
|
+
*/
|
|
5
|
+
export declare const DEFAULT_IC_DIDJS_ID = "a4gq6-oaaaa-aaaab-qaa4q-cai";
|
|
6
|
+
/**
|
|
7
|
+
* Default didjs canister ID for local development.
|
|
8
|
+
* This canister is used to compile Candid to JavaScript locally.
|
|
9
|
+
*/
|
|
10
|
+
export declare const DEFAULT_LOCAL_DIDJS_ID = "bd3sg-teaaa-aaaaa-qaaba-cai";
|
|
11
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,mBAAmB,gCAAgC,CAAA;AAEhE;;;GAGG;AACH,eAAO,MAAM,sBAAsB,gCAAgC,CAAA"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default didjs canister ID for the IC mainnet.
|
|
3
|
+
* This canister is used to compile Candid to JavaScript.
|
|
4
|
+
*/
|
|
5
|
+
export const DEFAULT_IC_DIDJS_ID = "a4gq6-oaaaa-aaaab-qaa4q-cai";
|
|
6
|
+
/**
|
|
7
|
+
* Default didjs canister ID for local development.
|
|
8
|
+
* This canister is used to compile Candid to JavaScript locally.
|
|
9
|
+
*/
|
|
10
|
+
export const DEFAULT_LOCAL_DIDJS_ID = "bd3sg-teaaa-aaaaa-qaaba-cai";
|
|
11
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,6BAA6B,CAAA;AAEhE;;;GAGG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,6BAA6B,CAAA"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +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"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { BaseActor } from "@ic-reactor/core";
|
|
2
|
+
import type { CandidReactorParameters, DynamicMethodOptions } from "./types";
|
|
3
|
+
import { Reactor } from "@ic-reactor/core";
|
|
4
|
+
import { CandidAdapter } from "./adapter";
|
|
5
|
+
export declare class CandidReactor<A = BaseActor> extends Reactor<A> {
|
|
6
|
+
adapter: CandidAdapter;
|
|
7
|
+
private candidSource?;
|
|
8
|
+
constructor(config: CandidReactorParameters<A>);
|
|
9
|
+
/**
|
|
10
|
+
* Initializes the reactor by parsing the provided Candid string or fetching it from the network.
|
|
11
|
+
* This updates the internal service definition with the actual canister interface.
|
|
12
|
+
*
|
|
13
|
+
* After initialization, all standard Reactor methods (callMethod, fetchQuery, etc.) work.
|
|
14
|
+
*/
|
|
15
|
+
initialize(): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Register a dynamic method by its Candid signature.
|
|
18
|
+
* After registration, all standard Reactor methods work with this method name.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* // Register a method
|
|
23
|
+
* await reactor.registerMethod({
|
|
24
|
+
* functionName: "icrc1_balance_of",
|
|
25
|
+
* candid: "(record { owner : principal }) -> (nat) query"
|
|
26
|
+
* })
|
|
27
|
+
*
|
|
28
|
+
* // Now use standard Reactor methods!
|
|
29
|
+
* const balance = await reactor.callMethod({
|
|
30
|
+
* functionName: "icrc1_balance_of",
|
|
31
|
+
* args: [{ owner }]
|
|
32
|
+
* })
|
|
33
|
+
*
|
|
34
|
+
* // Or with caching
|
|
35
|
+
* const cachedBalance = await reactor.fetchQuery({
|
|
36
|
+
* functionName: "icrc1_balance_of",
|
|
37
|
+
* args: [{ owner }]
|
|
38
|
+
* })
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
registerMethod(options: DynamicMethodOptions): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Register multiple methods at once.
|
|
44
|
+
*/
|
|
45
|
+
registerMethods(methods: DynamicMethodOptions[]): Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Check if a method is registered (either from initialize or registerMethod).
|
|
48
|
+
*/
|
|
49
|
+
hasMethod(functionName: string): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Get all registered method names.
|
|
52
|
+
*/
|
|
53
|
+
getMethodNames(): string[];
|
|
54
|
+
/**
|
|
55
|
+
* Perform a dynamic update call in one step.
|
|
56
|
+
* Registers the method if not already registered, then calls it.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* const result = await reactor.callDynamic({
|
|
61
|
+
* functionName: "transfer",
|
|
62
|
+
* candid: "(record { to : principal; amount : nat }) -> (variant { Ok : nat; Err : text })",
|
|
63
|
+
* args: [{ to: Principal.fromText("..."), amount: 100n }]
|
|
64
|
+
* })
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
callDynamic<T = unknown>(options: DynamicMethodOptions & {
|
|
68
|
+
args?: unknown[];
|
|
69
|
+
}): Promise<T>;
|
|
70
|
+
/**
|
|
71
|
+
* Perform a dynamic query call in one step.
|
|
72
|
+
* Registers the method if not already registered, then calls it.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const balance = await reactor.queryDynamic({
|
|
77
|
+
* functionName: "icrc1_balance_of",
|
|
78
|
+
* candid: "(record { owner : principal }) -> (nat) query",
|
|
79
|
+
* args: [{ owner: Principal.fromText("...") }]
|
|
80
|
+
* })
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
queryDynamic<T = unknown>(options: DynamicMethodOptions & {
|
|
84
|
+
args?: unknown[];
|
|
85
|
+
}): Promise<T>;
|
|
86
|
+
/**
|
|
87
|
+
* Fetch with dynamic Candid and TanStack Query caching.
|
|
88
|
+
* Registers the method if not already registered, then fetches with caching.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```typescript
|
|
92
|
+
* const balance = await reactor.fetchQueryDynamic({
|
|
93
|
+
* functionName: "icrc1_balance_of",
|
|
94
|
+
* candid: "(record { owner : principal }) -> (nat) query",
|
|
95
|
+
* args: [{ owner }]
|
|
96
|
+
* })
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
fetchQueryDynamic<T = unknown>(options: DynamicMethodOptions & {
|
|
100
|
+
args?: unknown[];
|
|
101
|
+
}): Promise<T>;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=reactor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reactor.d.ts","sourceRoot":"","sources":["../src/reactor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAqB,MAAM,kBAAkB,CAAA;AACpE,OAAO,KAAK,EAAE,uBAAuB,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAE5E,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AAGzC,qBAAa,aAAa,CAAC,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