@ic-reactor/candid 3.0.7-beta.1 → 3.0.8-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 +5 -1
- package/dist/display-reactor.d.ts +3 -2
- package/dist/display-reactor.d.ts.map +1 -1
- package/dist/display-reactor.js +6 -0
- package/dist/display-reactor.js.map +1 -1
- 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/dist/metadata-display-reactor.d.ts +73 -0
- package/dist/metadata-display-reactor.d.ts.map +1 -0
- package/dist/metadata-display-reactor.js +128 -0
- package/dist/metadata-display-reactor.js.map +1 -0
- package/dist/visitor/arguments/index.d.ts +69 -0
- package/dist/visitor/arguments/index.d.ts.map +1 -0
- package/dist/visitor/arguments/index.js +277 -0
- package/dist/visitor/arguments/index.js.map +1 -0
- package/dist/visitor/arguments/types.d.ts +92 -0
- package/dist/visitor/arguments/types.d.ts.map +1 -0
- package/dist/visitor/arguments/types.js +2 -0
- package/dist/visitor/arguments/types.js.map +1 -0
- package/dist/visitor/constants.d.ts +4 -0
- package/dist/visitor/constants.d.ts.map +1 -0
- package/dist/visitor/constants.js +61 -0
- package/dist/visitor/constants.js.map +1 -0
- package/dist/visitor/helpers.d.ts +30 -0
- package/dist/visitor/helpers.d.ts.map +1 -0
- package/dist/visitor/helpers.js +200 -0
- package/dist/visitor/helpers.js.map +1 -0
- package/dist/visitor/returns/index.d.ts +76 -0
- package/dist/visitor/returns/index.d.ts.map +1 -0
- package/dist/visitor/returns/index.js +425 -0
- package/dist/visitor/returns/index.js.map +1 -0
- package/dist/visitor/returns/types.d.ts +142 -0
- package/dist/visitor/returns/types.d.ts.map +1 -0
- package/dist/visitor/returns/types.js +2 -0
- package/dist/visitor/returns/types.js.map +1 -0
- package/dist/visitor/types.d.ts +6 -0
- package/dist/visitor/types.d.ts.map +1 -0
- package/dist/visitor/types.js +3 -0
- package/dist/visitor/types.js.map +1 -0
- package/package.json +4 -2
- package/src/adapter.ts +446 -0
- package/src/constants.ts +11 -0
- package/src/display-reactor.ts +332 -0
- package/src/index.ts +7 -0
- package/src/metadata-display-reactor.ts +184 -0
- package/src/reactor.ts +199 -0
- package/src/types.ts +107 -0
- package/src/utils.ts +28 -0
- package/src/visitor/arguments/index.test.ts +882 -0
- package/src/visitor/arguments/index.ts +405 -0
- package/src/visitor/arguments/types.ts +168 -0
- package/src/visitor/constants.ts +62 -0
- package/src/visitor/helpers.ts +221 -0
- package/src/visitor/returns/index.test.ts +2027 -0
- package/src/visitor/returns/index.ts +545 -0
- package/src/visitor/returns/types.ts +271 -0
- package/src/visitor/types.ts +29 -0
package/src/reactor.ts
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import type { BaseActor, ReactorParameters } from "@ic-reactor/core"
|
|
2
|
+
import type { CandidReactorParameters, DynamicMethodOptions } from "./types"
|
|
3
|
+
|
|
4
|
+
import { Reactor } from "@ic-reactor/core"
|
|
5
|
+
import { CandidAdapter } from "./adapter"
|
|
6
|
+
import { IDL } from "@icp-sdk/core/candid"
|
|
7
|
+
|
|
8
|
+
export class CandidReactor<A = BaseActor> extends Reactor<A> {
|
|
9
|
+
public adapter: CandidAdapter
|
|
10
|
+
private candidSource?: string
|
|
11
|
+
|
|
12
|
+
constructor(config: CandidReactorParameters) {
|
|
13
|
+
const superConfig = { ...config }
|
|
14
|
+
|
|
15
|
+
if (!superConfig.idlFactory) {
|
|
16
|
+
superConfig.idlFactory = ({ IDL }) => IDL.Service({})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
super(superConfig as ReactorParameters)
|
|
20
|
+
|
|
21
|
+
this.candidSource = config.candid
|
|
22
|
+
if (config.adapter) {
|
|
23
|
+
this.adapter = config.adapter
|
|
24
|
+
} else {
|
|
25
|
+
this.adapter = new CandidAdapter({
|
|
26
|
+
clientManager: this.clientManager,
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Initializes the reactor by parsing the provided Candid string or fetching it from the network.
|
|
33
|
+
* This updates the internal service definition with the actual canister interface.
|
|
34
|
+
*
|
|
35
|
+
* After initialization, all standard Reactor methods (callMethod, fetchQuery, etc.) work.
|
|
36
|
+
*/
|
|
37
|
+
public async initialize(): Promise<void> {
|
|
38
|
+
let idlFactory: IDL.InterfaceFactory
|
|
39
|
+
|
|
40
|
+
if (this.candidSource) {
|
|
41
|
+
const definition = await this.adapter.parseCandidSource(this.candidSource)
|
|
42
|
+
idlFactory = definition.idlFactory
|
|
43
|
+
} else {
|
|
44
|
+
const definition = await this.adapter.getCandidDefinition(this.canisterId)
|
|
45
|
+
idlFactory = definition.idlFactory
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
this.service = idlFactory({ IDL })
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Register a dynamic method by its Candid signature.
|
|
53
|
+
* After registration, all standard Reactor methods work with this method name.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* // Register a method
|
|
58
|
+
* await reactor.registerMethod({
|
|
59
|
+
* functionName: "icrc1_balance_of",
|
|
60
|
+
* candid: "(record { owner : principal }) -> (nat) query"
|
|
61
|
+
* })
|
|
62
|
+
*
|
|
63
|
+
* // Now use standard Reactor methods!
|
|
64
|
+
* const balance = await reactor.callMethod({
|
|
65
|
+
* functionName: "icrc1_balance_of",
|
|
66
|
+
* args: [{ owner }]
|
|
67
|
+
* })
|
|
68
|
+
*
|
|
69
|
+
* // Or with caching
|
|
70
|
+
* const cachedBalance = await reactor.fetchQuery({
|
|
71
|
+
* functionName: "icrc1_balance_of",
|
|
72
|
+
* args: [{ owner }]
|
|
73
|
+
* })
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
public async registerMethod(options: DynamicMethodOptions): Promise<void> {
|
|
77
|
+
const { functionName, candid } = options
|
|
78
|
+
|
|
79
|
+
// Check if method already registered
|
|
80
|
+
const existing = this.service._fields.find(
|
|
81
|
+
([name]) => name === functionName
|
|
82
|
+
)
|
|
83
|
+
if (existing) return
|
|
84
|
+
|
|
85
|
+
// Parse the Candid signature
|
|
86
|
+
const serviceSource = candid.includes("service :")
|
|
87
|
+
? candid
|
|
88
|
+
: `service : { ${functionName} : ${candid}; }`
|
|
89
|
+
|
|
90
|
+
const { idlFactory } = await this.adapter.parseCandidSource(serviceSource)
|
|
91
|
+
const parsedService = idlFactory({ IDL })
|
|
92
|
+
|
|
93
|
+
const funcField = parsedService._fields.find(
|
|
94
|
+
([name]) => name === functionName
|
|
95
|
+
)
|
|
96
|
+
if (!funcField) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
`Method "${functionName}" not found in the provided Candid signature`
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Inject into our service
|
|
103
|
+
this.service._fields.push(funcField)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Register multiple methods at once.
|
|
108
|
+
*/
|
|
109
|
+
public async registerMethods(methods: DynamicMethodOptions[]): Promise<void> {
|
|
110
|
+
await Promise.all(methods.map((m) => this.registerMethod(m)))
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Check if a method is registered (either from initialize or registerMethod).
|
|
115
|
+
*/
|
|
116
|
+
public hasMethod(functionName: string): boolean {
|
|
117
|
+
return this.service._fields.some(([name]) => name === functionName)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Get all registered method names.
|
|
122
|
+
*/
|
|
123
|
+
public getMethodNames(): string[] {
|
|
124
|
+
return this.service._fields.map(([name]) => name)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// ══════════════════════════════════════════════════════════════════════
|
|
128
|
+
// DYNAMIC CALL SHORTCUTS
|
|
129
|
+
// ══════════════════════════════════════════════════════════════════════
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Perform a dynamic update call in one step.
|
|
133
|
+
* Registers the method if not already registered, then calls it.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```typescript
|
|
137
|
+
* const result = await reactor.callDynamic({
|
|
138
|
+
* functionName: "transfer",
|
|
139
|
+
* candid: "(record { to : principal; amount : nat }) -> (variant { Ok : nat; Err : text })",
|
|
140
|
+
* args: [{ to: Principal.fromText("..."), amount: 100n }]
|
|
141
|
+
* })
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
public async callDynamic<T = unknown>(
|
|
145
|
+
options: DynamicMethodOptions & { args?: unknown[] }
|
|
146
|
+
): Promise<T> {
|
|
147
|
+
await this.registerMethod(options)
|
|
148
|
+
return this.callMethod({
|
|
149
|
+
functionName: options.functionName as any,
|
|
150
|
+
args: options.args as any,
|
|
151
|
+
}) as T
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Perform a dynamic query call in one step.
|
|
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: Principal.fromText("...") }]
|
|
164
|
+
* })
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
public async queryDynamic<T = unknown>(
|
|
168
|
+
options: DynamicMethodOptions & { args?: unknown[] }
|
|
169
|
+
): Promise<T> {
|
|
170
|
+
await this.registerMethod(options)
|
|
171
|
+
return this.callMethod({
|
|
172
|
+
functionName: options.functionName as any,
|
|
173
|
+
args: options.args as any,
|
|
174
|
+
}) as T
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Fetch with dynamic Candid and TanStack Query caching.
|
|
179
|
+
* Registers the method if not already registered, then fetches with caching.
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```typescript
|
|
183
|
+
* const balance = await reactor.fetchQueryDynamic({
|
|
184
|
+
* functionName: "icrc1_balance_of",
|
|
185
|
+
* candid: "(record { owner : principal }) -> (nat) query",
|
|
186
|
+
* args: [{ owner }]
|
|
187
|
+
* })
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
public async fetchQueryDynamic<T = unknown>(
|
|
191
|
+
options: DynamicMethodOptions & { args?: unknown[] }
|
|
192
|
+
): Promise<T> {
|
|
193
|
+
await this.registerMethod(options)
|
|
194
|
+
return this.fetchQuery({
|
|
195
|
+
functionName: options.functionName as any,
|
|
196
|
+
args: options.args as any,
|
|
197
|
+
}) as T
|
|
198
|
+
}
|
|
199
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { HttpAgent, Identity } from "@icp-sdk/core/agent"
|
|
2
|
+
import type { IDL } from "@icp-sdk/core/candid"
|
|
3
|
+
import type {
|
|
4
|
+
BaseActor,
|
|
5
|
+
CanisterId,
|
|
6
|
+
DisplayReactorParameters,
|
|
7
|
+
ReactorParameters,
|
|
8
|
+
} from "@ic-reactor/core"
|
|
9
|
+
import type { CandidAdapter } from "./adapter"
|
|
10
|
+
|
|
11
|
+
export interface DynamicMethodOptions {
|
|
12
|
+
/** The method name to register. */
|
|
13
|
+
functionName: string
|
|
14
|
+
/**
|
|
15
|
+
* The Candid signature for the method.
|
|
16
|
+
* Can be either a method signature like "(text) -> (text) query"
|
|
17
|
+
* or a full service definition like "service : { greet: (text) -> (text) query }".
|
|
18
|
+
*/
|
|
19
|
+
candid: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface CandidReactorParameters extends Omit<
|
|
23
|
+
ReactorParameters,
|
|
24
|
+
"idlFactory"
|
|
25
|
+
> {
|
|
26
|
+
/** The canister ID. */
|
|
27
|
+
canisterId?: CanisterId
|
|
28
|
+
/** The Candid source code. If not provided, the canister's Candid will be fetched. */
|
|
29
|
+
candid?: string
|
|
30
|
+
/** The IDL interface factory. */
|
|
31
|
+
idlFactory?: (IDL: any) => any
|
|
32
|
+
/** The Candid adapter. */
|
|
33
|
+
adapter?: CandidAdapter
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ============================================================================
|
|
37
|
+
// CandidDisplayReactor Parameters
|
|
38
|
+
// ============================================================================
|
|
39
|
+
|
|
40
|
+
export interface CandidDisplayReactorParameters<A = BaseActor> extends Omit<
|
|
41
|
+
DisplayReactorParameters<A>,
|
|
42
|
+
"idlFactory"
|
|
43
|
+
> {
|
|
44
|
+
/** The canister ID. */
|
|
45
|
+
canisterId?: CanisterId
|
|
46
|
+
/** The Candid source code. If not provided, the canister's Candid will be fetched. */
|
|
47
|
+
candid?: string
|
|
48
|
+
/** The IDL interface factory. */
|
|
49
|
+
idlFactory?: (IDL: any) => any
|
|
50
|
+
/** The Candid adapter. */
|
|
51
|
+
adapter?: CandidAdapter
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Minimal interface for ClientManager that CandidAdapter depends on.
|
|
56
|
+
* This allows the candid package to work with ClientManager without importing the full core package.
|
|
57
|
+
*/
|
|
58
|
+
export interface CandidClientManager {
|
|
59
|
+
/** The HTTP agent used for making requests. */
|
|
60
|
+
agent: HttpAgent
|
|
61
|
+
/** Whether the agent is connected to a local network. */
|
|
62
|
+
isLocal: boolean
|
|
63
|
+
/** Subscribe to identity changes. Returns an unsubscribe function. */
|
|
64
|
+
subscribe(callback: (identity: Identity) => void): () => void
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Parameters for initializing the CandidAdapter.
|
|
69
|
+
*/
|
|
70
|
+
export interface CandidAdapterParameters {
|
|
71
|
+
/** The client manager that provides agent and identity access. */
|
|
72
|
+
clientManager: CandidClientManager
|
|
73
|
+
/** The canister ID of the didjs canister for compiling Candid to JavaScript. */
|
|
74
|
+
didjsCanisterId?: CanisterId
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Represents a parsed Candid definition with IDL factory and initialization.
|
|
79
|
+
*/
|
|
80
|
+
export interface CandidDefinition {
|
|
81
|
+
/** The IDL interface factory. */
|
|
82
|
+
idlFactory: IDL.InterfaceFactory
|
|
83
|
+
/** Optional init function for the canister. */
|
|
84
|
+
init?: (args: { IDL: typeof IDL }) => IDL.Type<unknown>[]
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Interface for the optional parser module (@ic-reactor/parser).
|
|
89
|
+
*/
|
|
90
|
+
export interface ReactorParser {
|
|
91
|
+
/**
|
|
92
|
+
* Default function to initialize the WASM module.
|
|
93
|
+
*/
|
|
94
|
+
default?: () => Promise<void>
|
|
95
|
+
/**
|
|
96
|
+
* Converts Candid (DID) source to JavaScript code.
|
|
97
|
+
* @param candidSource - The Candid source code.
|
|
98
|
+
* @returns The JavaScript code.
|
|
99
|
+
*/
|
|
100
|
+
didToJs(candidSource: string): string
|
|
101
|
+
/**
|
|
102
|
+
* Validates the Candid (IDL) source.
|
|
103
|
+
* @param candidSource - The Candid source code.
|
|
104
|
+
* @returns True if valid, false otherwise.
|
|
105
|
+
*/
|
|
106
|
+
validateIDL(candidSource: string): boolean
|
|
107
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { CandidDefinition } from "./types"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Imports and evaluates a Candid definition from JavaScript code.
|
|
5
|
+
*
|
|
6
|
+
* @param candidJs - The JavaScript code containing the Candid definition.
|
|
7
|
+
* @returns A promise that resolves to the CandidDefinition.
|
|
8
|
+
* @throws Error if the import fails.
|
|
9
|
+
*/
|
|
10
|
+
export async function importCandidDefinition(
|
|
11
|
+
candidJs: string
|
|
12
|
+
): Promise<CandidDefinition> {
|
|
13
|
+
try {
|
|
14
|
+
// Create a data URL with the JavaScript code
|
|
15
|
+
const dataUri =
|
|
16
|
+
"data:text/javascript;charset=utf-8," + encodeURIComponent(candidJs)
|
|
17
|
+
|
|
18
|
+
// Dynamically import the module
|
|
19
|
+
const module = await import(/* webpackIgnore: true */ dataUri)
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
idlFactory: module.idlFactory,
|
|
23
|
+
init: module.init,
|
|
24
|
+
}
|
|
25
|
+
} catch (error) {
|
|
26
|
+
throw new Error(`Failed to import Candid definition: ${error}`)
|
|
27
|
+
}
|
|
28
|
+
}
|