@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
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BaseActor,
|
|
3
|
+
FunctionName,
|
|
4
|
+
DisplayOf,
|
|
5
|
+
FunctionType,
|
|
6
|
+
ActorMethodReturnType,
|
|
7
|
+
} from "@ic-reactor/core"
|
|
8
|
+
|
|
9
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
10
|
+
// Field Type Union
|
|
11
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
12
|
+
|
|
13
|
+
export type ResultFieldType =
|
|
14
|
+
| "record"
|
|
15
|
+
| "variant"
|
|
16
|
+
| "tuple"
|
|
17
|
+
| "optional"
|
|
18
|
+
| "vector"
|
|
19
|
+
| "blob"
|
|
20
|
+
| "recursive"
|
|
21
|
+
| "principal"
|
|
22
|
+
| "number"
|
|
23
|
+
| "text"
|
|
24
|
+
| "boolean"
|
|
25
|
+
| "null"
|
|
26
|
+
| "unknown"
|
|
27
|
+
|
|
28
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
29
|
+
// Display Type (what it becomes after transformation)
|
|
30
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
31
|
+
|
|
32
|
+
export type DisplayType =
|
|
33
|
+
| "string" // Principal, nat, int, nat64, int64, blob, text
|
|
34
|
+
| "number" // float32, float64, nat8-nat32, int8-int32
|
|
35
|
+
| "boolean" // bool
|
|
36
|
+
| "null" // null
|
|
37
|
+
| "object" // record
|
|
38
|
+
| "array" // vec, tuple
|
|
39
|
+
| "variant" // variant (not Result)
|
|
40
|
+
| "result" // variant { Ok, Err } - unwrapped to Ok value
|
|
41
|
+
| "nullable" // opt T → T | null
|
|
42
|
+
| "recursive" // rec types
|
|
43
|
+
| "unknown" // fallback
|
|
44
|
+
|
|
45
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
46
|
+
// Format Hints
|
|
47
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Number-specific formatting hints derived from field names.
|
|
51
|
+
*/
|
|
52
|
+
export type NumberFormat = "timestamp" | "cycle" | "value" | "token" | "normal"
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Text-specific formatting hints derived from field names.
|
|
56
|
+
*/
|
|
57
|
+
export type TextFormat =
|
|
58
|
+
| "plain"
|
|
59
|
+
| "timestamp"
|
|
60
|
+
| "uuid"
|
|
61
|
+
| "url"
|
|
62
|
+
| "email"
|
|
63
|
+
| "phone"
|
|
64
|
+
| "btc"
|
|
65
|
+
| "eth"
|
|
66
|
+
| "account-id"
|
|
67
|
+
| "principal"
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Display hints for UI rendering.
|
|
71
|
+
*/
|
|
72
|
+
export type DisplayHint =
|
|
73
|
+
| "copyable" // Show copy button
|
|
74
|
+
| "linkable" // Can be linked
|
|
75
|
+
| "truncate" // Truncate long values
|
|
76
|
+
| "hex" // Display as hex
|
|
77
|
+
| "code" // Display as code/monospace
|
|
78
|
+
| "none" // No special formatting
|
|
79
|
+
|
|
80
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
81
|
+
// Base Field Interface
|
|
82
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
83
|
+
|
|
84
|
+
export interface ResultFieldBase {
|
|
85
|
+
/** The Candid type category */
|
|
86
|
+
type: ResultFieldType
|
|
87
|
+
/** Human-readable label from Candid */
|
|
88
|
+
label: string
|
|
89
|
+
/** Original Candid type name */
|
|
90
|
+
candidType: string
|
|
91
|
+
/** What it becomes after display transformation */
|
|
92
|
+
displayType: DisplayType
|
|
93
|
+
/**
|
|
94
|
+
* Combine metadata with value to create a render-ready tree.
|
|
95
|
+
* This allows "zipping" the static schema with dynamic runtime data.
|
|
96
|
+
*/
|
|
97
|
+
resolve(value: unknown): ResultFieldWithValue
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
101
|
+
// Compound Types
|
|
102
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
103
|
+
|
|
104
|
+
export interface RecordResultField extends ResultFieldBase {
|
|
105
|
+
type: "record"
|
|
106
|
+
displayType: "object"
|
|
107
|
+
fields: ResultField[]
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface VariantResultField extends ResultFieldBase {
|
|
111
|
+
type: "variant"
|
|
112
|
+
displayType: "variant" | "result"
|
|
113
|
+
options: string[]
|
|
114
|
+
optionFields: ResultField[]
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface TupleResultField extends ResultFieldBase {
|
|
118
|
+
type: "tuple"
|
|
119
|
+
displayType: "array"
|
|
120
|
+
fields: ResultField[]
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface OptionalResultField extends ResultFieldBase {
|
|
124
|
+
type: "optional"
|
|
125
|
+
displayType: "nullable"
|
|
126
|
+
innerField: ResultField
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export interface VectorResultField extends ResultFieldBase {
|
|
130
|
+
type: "vector"
|
|
131
|
+
displayType: "array"
|
|
132
|
+
itemField: ResultField
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface BlobResultField extends ResultFieldBase {
|
|
136
|
+
type: "blob"
|
|
137
|
+
displayType: "string"
|
|
138
|
+
displayHint: "hex"
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface RecursiveResultField extends ResultFieldBase {
|
|
142
|
+
type: "recursive"
|
|
143
|
+
displayType: "recursive"
|
|
144
|
+
typeName: string
|
|
145
|
+
/** Lazily extract the inner field to prevent infinite loops */
|
|
146
|
+
extract: () => ResultField
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
150
|
+
// Primitive Types
|
|
151
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
152
|
+
|
|
153
|
+
export interface PrincipalResultField extends ResultFieldBase {
|
|
154
|
+
type: "principal"
|
|
155
|
+
displayType: "string"
|
|
156
|
+
textFormat: TextFormat
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface NumberResultField extends ResultFieldBase {
|
|
160
|
+
type: "number"
|
|
161
|
+
displayType: "string" | "number"
|
|
162
|
+
numberFormat: NumberFormat
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export interface TextResultField extends ResultFieldBase {
|
|
166
|
+
type: "text"
|
|
167
|
+
displayType: "string"
|
|
168
|
+
textFormat: TextFormat
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export interface BooleanResultField extends ResultFieldBase {
|
|
172
|
+
type: "boolean"
|
|
173
|
+
displayType: "boolean"
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export interface NullResultField extends ResultFieldBase {
|
|
177
|
+
type: "null"
|
|
178
|
+
displayType: "null"
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export interface UnknownResultField extends ResultFieldBase {
|
|
182
|
+
type: "unknown"
|
|
183
|
+
displayType: "unknown"
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
187
|
+
// Union Type
|
|
188
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
189
|
+
|
|
190
|
+
export type ResultField =
|
|
191
|
+
| RecordResultField
|
|
192
|
+
| VariantResultField
|
|
193
|
+
| TupleResultField
|
|
194
|
+
| OptionalResultField
|
|
195
|
+
| VectorResultField
|
|
196
|
+
| BlobResultField
|
|
197
|
+
| RecursiveResultField
|
|
198
|
+
| PrincipalResultField
|
|
199
|
+
| NumberResultField
|
|
200
|
+
| TextResultField
|
|
201
|
+
| BooleanResultField
|
|
202
|
+
| NullResultField
|
|
203
|
+
| UnknownResultField
|
|
204
|
+
|
|
205
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
206
|
+
// Helper Types for Rendering
|
|
207
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* A result field paired with its transformed value for rendering.
|
|
211
|
+
* Can contain nested resolved fields for compound types.
|
|
212
|
+
*/
|
|
213
|
+
export interface ResultFieldWithValue<T = unknown> {
|
|
214
|
+
field: ResultField
|
|
215
|
+
value: DisplayOf<T>
|
|
216
|
+
raw: T
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
220
|
+
// Method & Service Level
|
|
221
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Resolved method result containing metadata and resolved field values.
|
|
225
|
+
* This is the output of `generateMetadata()`.
|
|
226
|
+
*/
|
|
227
|
+
export interface ResolvedMethodResult<A = BaseActor> {
|
|
228
|
+
functionType: FunctionType
|
|
229
|
+
functionName: FunctionName<A>
|
|
230
|
+
results: ResultFieldWithValue[]
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export interface MethodResultMeta<
|
|
234
|
+
A = BaseActor,
|
|
235
|
+
Name extends FunctionName<A> = FunctionName<A>,
|
|
236
|
+
> {
|
|
237
|
+
functionType: FunctionType
|
|
238
|
+
functionName: Name
|
|
239
|
+
resultFields: ResultField[]
|
|
240
|
+
returnCount: number
|
|
241
|
+
/**
|
|
242
|
+
* Generate metadata by resolving each result field with its corresponding display value.
|
|
243
|
+
* This "zips" the static schema with dynamic runtime data for easy rendering.
|
|
244
|
+
*
|
|
245
|
+
* @param data - Array of display-transformed return values from the canister method call
|
|
246
|
+
* @returns Resolved method result with metadata attached to each value
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```ts
|
|
250
|
+
* const result = await reactor.callMethod({ functionName: "myMethod", args: [] })
|
|
251
|
+
* const resolved = methodMeta.generateMetadata(result)
|
|
252
|
+
* // resolved.results contains fields with their display values for rendering
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
generateMetadata(
|
|
256
|
+
data: ActorMethodReturnType<A[Name]>
|
|
257
|
+
): ResolvedMethodResult<A>
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export type ServiceResultMeta<A = BaseActor> = {
|
|
261
|
+
[K in FunctionName<A>]: MethodResultMeta<A, K>
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
265
|
+
// Type Utilities
|
|
266
|
+
// ════════════════════════════════════════════════════════════════════════════
|
|
267
|
+
|
|
268
|
+
export type ResultFieldByType<T extends ResultFieldType> = Extract<
|
|
269
|
+
ResultField,
|
|
270
|
+
{ type: T }
|
|
271
|
+
>
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type FieldType =
|
|
2
|
+
| "functionRecord"
|
|
3
|
+
| "function"
|
|
4
|
+
| "record"
|
|
5
|
+
| "variant"
|
|
6
|
+
| "tuple"
|
|
7
|
+
| "optional"
|
|
8
|
+
| "vector"
|
|
9
|
+
| "table"
|
|
10
|
+
| "blob"
|
|
11
|
+
| "blob-large"
|
|
12
|
+
| "recursive"
|
|
13
|
+
| "unknown"
|
|
14
|
+
| "text"
|
|
15
|
+
| "number"
|
|
16
|
+
| "principal"
|
|
17
|
+
| "boolean"
|
|
18
|
+
| "null"
|
|
19
|
+
|
|
20
|
+
export { IDL } from "@icp-sdk/core/candid"
|
|
21
|
+
export { Principal } from "@icp-sdk/core/principal"
|
|
22
|
+
|
|
23
|
+
import { IDL } from "@icp-sdk/core/candid"
|
|
24
|
+
export type AllNumberTypes =
|
|
25
|
+
| IDL.NatClass
|
|
26
|
+
| IDL.IntClass
|
|
27
|
+
| IDL.FixedNatClass
|
|
28
|
+
| IDL.FixedIntClass
|
|
29
|
+
| IDL.FloatClass
|