@icp-sdk/core 5.4.0 → 6.0.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.
Files changed (251) hide show
  1. package/lib/cjs/agent/actor.d.ts +7 -3
  2. package/lib/cjs/agent/actor.d.ts.map +1 -1
  3. package/lib/cjs/agent/actor.js +18 -5
  4. package/lib/cjs/agent/actor.js.map +1 -1
  5. package/lib/cjs/agent/agent/api.d.ts +19 -6
  6. package/lib/cjs/agent/agent/api.d.ts.map +1 -1
  7. package/lib/cjs/agent/agent/api.js.map +1 -1
  8. package/lib/cjs/agent/agent/http/index.d.ts +8 -11
  9. package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
  10. package/lib/cjs/agent/agent/http/index.js +190 -166
  11. package/lib/cjs/agent/agent/http/index.js.map +1 -1
  12. package/lib/cjs/agent/auth.d.ts +6 -5
  13. package/lib/cjs/agent/auth.d.ts.map +1 -1
  14. package/lib/cjs/agent/auth.js +3 -3
  15. package/lib/cjs/agent/auth.js.map +1 -1
  16. package/lib/cjs/agent/canisterStatus/index.d.ts.map +1 -1
  17. package/lib/cjs/agent/canisterStatus/index.js +10 -10
  18. package/lib/cjs/agent/canisterStatus/index.js.map +1 -1
  19. package/lib/cjs/agent/cbor.d.ts +2 -1
  20. package/lib/cjs/agent/cbor.d.ts.map +1 -1
  21. package/lib/cjs/agent/cbor.js +4 -1
  22. package/lib/cjs/agent/cbor.js.map +1 -1
  23. package/lib/cjs/agent/certificate.d.ts +5 -4
  24. package/lib/cjs/agent/certificate.d.ts.map +1 -1
  25. package/lib/cjs/agent/certificate.js +14 -18
  26. package/lib/cjs/agent/certificate.js.map +1 -1
  27. package/lib/cjs/agent/der.d.ts +3 -2
  28. package/lib/cjs/agent/der.d.ts.map +1 -1
  29. package/lib/cjs/agent/der.js.map +1 -1
  30. package/lib/cjs/agent/errors.d.ts +7 -1
  31. package/lib/cjs/agent/errors.d.ts.map +1 -1
  32. package/lib/cjs/agent/errors.js +34 -16
  33. package/lib/cjs/agent/errors.js.map +1 -1
  34. package/lib/cjs/agent/polling/index.d.ts +3 -4
  35. package/lib/cjs/agent/polling/index.d.ts.map +1 -1
  36. package/lib/cjs/agent/polling/index.js +17 -10
  37. package/lib/cjs/agent/polling/index.js.map +1 -1
  38. package/lib/cjs/agent/polling/strategy.d.ts.map +1 -1
  39. package/lib/cjs/agent/polling/strategy.js +6 -6
  40. package/lib/cjs/agent/polling/strategy.js.map +1 -1
  41. package/lib/cjs/agent/polling/types.d.ts +3 -4
  42. package/lib/cjs/agent/polling/types.d.ts.map +1 -1
  43. package/lib/cjs/agent/public_key.d.ts +3 -2
  44. package/lib/cjs/agent/public_key.d.ts.map +1 -1
  45. package/lib/cjs/agent/public_key.js +2 -2
  46. package/lib/cjs/agent/public_key.js.map +1 -1
  47. package/lib/cjs/agent/request_id.js +10 -10
  48. package/lib/cjs/agent/request_id.js.map +1 -1
  49. package/lib/cjs/agent/subnetStatus/index.js +7 -7
  50. package/lib/cjs/agent/subnetStatus/index.js.map +1 -1
  51. package/lib/cjs/agent/utils/bls.d.ts.map +1 -1
  52. package/lib/cjs/agent/utils/bls.js +7 -6
  53. package/lib/cjs/agent/utils/bls.js.map +1 -1
  54. package/lib/cjs/agent/utils/readState.d.ts +2 -2
  55. package/lib/cjs/agent/utils/readState.d.ts.map +1 -1
  56. package/lib/cjs/agent/utils/readState.js +4 -4
  57. package/lib/cjs/agent/utils/readState.js.map +1 -1
  58. package/lib/cjs/agent/utils/target.d.ts +15 -0
  59. package/lib/cjs/agent/utils/target.d.ts.map +1 -0
  60. package/lib/cjs/agent/utils/target.js +27 -0
  61. package/lib/cjs/agent/utils/target.js.map +1 -0
  62. package/lib/cjs/candid/utils/buffer.d.ts +2 -1
  63. package/lib/cjs/candid/utils/buffer.d.ts.map +1 -1
  64. package/lib/cjs/candid/utils/buffer.js +5 -2
  65. package/lib/cjs/candid/utils/buffer.js.map +1 -1
  66. package/lib/cjs/identity/identity/delegation.d.ts +10 -3
  67. package/lib/cjs/identity/identity/delegation.d.ts.map +1 -1
  68. package/lib/cjs/identity/identity/delegation.js +25 -12
  69. package/lib/cjs/identity/identity/delegation.js.map +1 -1
  70. package/lib/cjs/identity/identity/ecdsa.d.ts +2 -2
  71. package/lib/cjs/identity/identity/ecdsa.d.ts.map +1 -1
  72. package/lib/cjs/identity/identity/ecdsa.js +3 -2
  73. package/lib/cjs/identity/identity/ecdsa.js.map +1 -1
  74. package/lib/cjs/identity/identity/ed25519.d.ts +3 -2
  75. package/lib/cjs/identity/identity/ed25519.d.ts.map +1 -1
  76. package/lib/cjs/identity/identity/ed25519.js +18 -15
  77. package/lib/cjs/identity/identity/ed25519.js.map +1 -1
  78. package/lib/cjs/identity/identity/webauthn.d.ts +4 -3
  79. package/lib/cjs/identity/identity/webauthn.d.ts.map +1 -1
  80. package/lib/cjs/identity/identity/webauthn.js +12 -11
  81. package/lib/cjs/identity/identity/webauthn.js.map +1 -1
  82. package/lib/cjs/identity/secp256k1/der.d.ts +2 -1
  83. package/lib/cjs/identity/secp256k1/der.d.ts.map +1 -1
  84. package/lib/cjs/identity/secp256k1/der.js.map +1 -1
  85. package/lib/cjs/identity/secp256k1/pem.d.ts +4 -3
  86. package/lib/cjs/identity/secp256k1/pem.d.ts.map +1 -1
  87. package/lib/cjs/identity/secp256k1/pem.js +2 -2
  88. package/lib/cjs/identity/secp256k1/pem.js.map +1 -1
  89. package/lib/cjs/identity/secp256k1/secp256k1.d.ts +5 -4
  90. package/lib/cjs/identity/secp256k1/secp256k1.d.ts.map +1 -1
  91. package/lib/cjs/identity/secp256k1/secp256k1.js +17 -20
  92. package/lib/cjs/identity/secp256k1/secp256k1.js.map +1 -1
  93. package/lib/cjs/principal/principal.js +5 -5
  94. package/lib/cjs/principal/principal.js.map +1 -1
  95. package/lib/esm/agent/actor.d.ts +7 -3
  96. package/lib/esm/agent/actor.d.ts.map +1 -1
  97. package/lib/esm/agent/actor.js +18 -5
  98. package/lib/esm/agent/actor.js.map +1 -1
  99. package/lib/esm/agent/agent/api.d.ts +19 -6
  100. package/lib/esm/agent/agent/api.d.ts.map +1 -1
  101. package/lib/esm/agent/agent/api.js.map +1 -1
  102. package/lib/esm/agent/agent/http/index.d.ts +8 -11
  103. package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
  104. package/lib/esm/agent/agent/http/index.js +183 -159
  105. package/lib/esm/agent/agent/http/index.js.map +1 -1
  106. package/lib/esm/agent/auth.d.ts +6 -5
  107. package/lib/esm/agent/auth.d.ts.map +1 -1
  108. package/lib/esm/agent/auth.js +1 -1
  109. package/lib/esm/agent/auth.js.map +1 -1
  110. package/lib/esm/agent/canister-env/index.js +1 -1
  111. package/lib/esm/agent/canister-env/index.js.map +1 -1
  112. package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -1
  113. package/lib/esm/agent/canisterStatus/index.js +2 -2
  114. package/lib/esm/agent/canisterStatus/index.js.map +1 -1
  115. package/lib/esm/agent/cbor.d.ts +2 -1
  116. package/lib/esm/agent/cbor.d.ts.map +1 -1
  117. package/lib/esm/agent/cbor.js +4 -1
  118. package/lib/esm/agent/cbor.js.map +1 -1
  119. package/lib/esm/agent/certificate.d.ts +5 -4
  120. package/lib/esm/agent/certificate.d.ts.map +1 -1
  121. package/lib/esm/agent/certificate.js +5 -9
  122. package/lib/esm/agent/certificate.js.map +1 -1
  123. package/lib/esm/agent/der.d.ts +3 -2
  124. package/lib/esm/agent/der.d.ts.map +1 -1
  125. package/lib/esm/agent/der.js.map +1 -1
  126. package/lib/esm/agent/errors.d.ts +7 -1
  127. package/lib/esm/agent/errors.d.ts.map +1 -1
  128. package/lib/esm/agent/errors.js +18 -1
  129. package/lib/esm/agent/errors.js.map +1 -1
  130. package/lib/esm/agent/polling/index.d.ts +3 -4
  131. package/lib/esm/agent/polling/index.d.ts.map +1 -1
  132. package/lib/esm/agent/polling/index.js +15 -8
  133. package/lib/esm/agent/polling/index.js.map +1 -1
  134. package/lib/esm/agent/polling/strategy.d.ts.map +1 -1
  135. package/lib/esm/agent/polling/strategy.js +6 -6
  136. package/lib/esm/agent/polling/strategy.js.map +1 -1
  137. package/lib/esm/agent/polling/types.d.ts +3 -4
  138. package/lib/esm/agent/polling/types.d.ts.map +1 -1
  139. package/lib/esm/agent/public_key.d.ts +3 -2
  140. package/lib/esm/agent/public_key.d.ts.map +1 -1
  141. package/lib/esm/agent/public_key.js +2 -2
  142. package/lib/esm/agent/public_key.js.map +1 -1
  143. package/lib/esm/agent/request_id.js +2 -2
  144. package/lib/esm/agent/request_id.js.map +1 -1
  145. package/lib/esm/agent/subnetStatus/index.js +1 -1
  146. package/lib/esm/agent/subnetStatus/index.js.map +1 -1
  147. package/lib/esm/agent/utils/bls.d.ts.map +1 -1
  148. package/lib/esm/agent/utils/bls.js +7 -6
  149. package/lib/esm/agent/utils/bls.js.map +1 -1
  150. package/lib/esm/agent/utils/readState.d.ts +2 -2
  151. package/lib/esm/agent/utils/readState.d.ts.map +1 -1
  152. package/lib/esm/agent/utils/readState.js +1 -1
  153. package/lib/esm/agent/utils/readState.js.map +1 -1
  154. package/lib/esm/agent/utils/target.d.ts +15 -0
  155. package/lib/esm/agent/utils/target.d.ts.map +1 -0
  156. package/lib/esm/agent/utils/target.js +23 -0
  157. package/lib/esm/agent/utils/target.js.map +1 -0
  158. package/lib/esm/candid/utils/buffer.d.ts +2 -1
  159. package/lib/esm/candid/utils/buffer.d.ts.map +1 -1
  160. package/lib/esm/candid/utils/buffer.js +5 -2
  161. package/lib/esm/candid/utils/buffer.js.map +1 -1
  162. package/lib/esm/identity/identity/delegation.d.ts +10 -3
  163. package/lib/esm/identity/identity/delegation.d.ts.map +1 -1
  164. package/lib/esm/identity/identity/delegation.js +22 -9
  165. package/lib/esm/identity/identity/delegation.js.map +1 -1
  166. package/lib/esm/identity/identity/ecdsa.d.ts +2 -2
  167. package/lib/esm/identity/identity/ecdsa.d.ts.map +1 -1
  168. package/lib/esm/identity/identity/ecdsa.js +3 -2
  169. package/lib/esm/identity/identity/ecdsa.js.map +1 -1
  170. package/lib/esm/identity/identity/ed25519.d.ts +3 -2
  171. package/lib/esm/identity/identity/ed25519.d.ts.map +1 -1
  172. package/lib/esm/identity/identity/ed25519.js +10 -7
  173. package/lib/esm/identity/identity/ed25519.js.map +1 -1
  174. package/lib/esm/identity/identity/webauthn.d.ts +4 -3
  175. package/lib/esm/identity/identity/webauthn.d.ts.map +1 -1
  176. package/lib/esm/identity/identity/webauthn.js +8 -7
  177. package/lib/esm/identity/identity/webauthn.js.map +1 -1
  178. package/lib/esm/identity/secp256k1/der.d.ts +2 -1
  179. package/lib/esm/identity/secp256k1/der.d.ts.map +1 -1
  180. package/lib/esm/identity/secp256k1/der.js.map +1 -1
  181. package/lib/esm/identity/secp256k1/pem.d.ts +4 -3
  182. package/lib/esm/identity/secp256k1/pem.d.ts.map +1 -1
  183. package/lib/esm/identity/secp256k1/pem.js +2 -2
  184. package/lib/esm/identity/secp256k1/pem.js.map +1 -1
  185. package/lib/esm/identity/secp256k1/secp256k1.d.ts +5 -4
  186. package/lib/esm/identity/secp256k1/secp256k1.d.ts.map +1 -1
  187. package/lib/esm/identity/secp256k1/secp256k1.js +10 -13
  188. package/lib/esm/identity/secp256k1/secp256k1.js.map +1 -1
  189. package/lib/esm/principal/principal.js +2 -2
  190. package/lib/esm/principal/principal.js.map +1 -1
  191. package/package.json +20 -10
  192. package/src/agent/actor.ts +0 -524
  193. package/src/agent/agent/api.ts +0 -310
  194. package/src/agent/agent/http/expiry.ts +0 -107
  195. package/src/agent/agent/http/index.ts +0 -1724
  196. package/src/agent/agent/http/transforms.ts +0 -54
  197. package/src/agent/agent/http/types.ts +0 -145
  198. package/src/agent/agent/index.ts +0 -4
  199. package/src/agent/auth.ts +0 -147
  200. package/src/agent/canister-env/index.ts +0 -192
  201. package/src/agent/canisterStatus/index.ts +0 -260
  202. package/src/agent/cbor.ts +0 -63
  203. package/src/agent/certificate.ts +0 -1058
  204. package/src/agent/constants.ts +0 -24
  205. package/src/agent/der.ts +0 -191
  206. package/src/agent/errors.ts +0 -962
  207. package/src/agent/fetch_candid.ts +0 -37
  208. package/src/agent/index.ts +0 -130
  209. package/src/agent/observable.ts +0 -49
  210. package/src/agent/polling/backoff.ts +0 -129
  211. package/src/agent/polling/index.ts +0 -238
  212. package/src/agent/polling/strategy.ts +0 -140
  213. package/src/agent/polling/types.ts +0 -28
  214. package/src/agent/public_key.ts +0 -65
  215. package/src/agent/request_id.ts +0 -99
  216. package/src/agent/subnetStatus/index.ts +0 -263
  217. package/src/agent/utils/bls.ts +0 -18
  218. package/src/agent/utils/buffer.ts +0 -64
  219. package/src/agent/utils/certificateReject.ts +0 -25
  220. package/src/agent/utils/createExpirableStore.ts +0 -31
  221. package/src/agent/utils/expirableStore.ts +0 -47
  222. package/src/agent/utils/inMemoryExpirableStore.ts +0 -49
  223. package/src/agent/utils/indexedDBExpirableStore.ts +0 -191
  224. package/src/agent/utils/leb.ts +0 -14
  225. package/src/agent/utils/random.ts +0 -30
  226. package/src/agent/utils/readState.ts +0 -180
  227. package/src/candid/candid-core.ts +0 -274
  228. package/src/candid/candid-ui.ts +0 -281
  229. package/src/candid/idl.ts +0 -2654
  230. package/src/candid/index.ts +0 -35
  231. package/src/candid/types.ts +0 -7
  232. package/src/candid/utils/bigint-math.ts +0 -26
  233. package/src/candid/utils/buffer.ts +0 -221
  234. package/src/candid/utils/hash.ts +0 -31
  235. package/src/candid/utils/leb128.ts +0 -233
  236. package/src/identity/identity/attributes.ts +0 -91
  237. package/src/identity/identity/delegation.ts +0 -411
  238. package/src/identity/identity/ecdsa.ts +0 -162
  239. package/src/identity/identity/ed25519.ts +0 -246
  240. package/src/identity/identity/partial.ts +0 -60
  241. package/src/identity/identity/webauthn.ts +0 -265
  242. package/src/identity/index.ts +0 -44
  243. package/src/identity/secp256k1/der.ts +0 -177
  244. package/src/identity/secp256k1/index.ts +0 -52
  245. package/src/identity/secp256k1/pem.ts +0 -39
  246. package/src/identity/secp256k1/secp256k1.ts +0 -276
  247. package/src/index.ts +0 -3
  248. package/src/principal/index.ts +0 -19
  249. package/src/principal/principal.ts +0 -181
  250. package/src/principal/utils/base32.ts +0 -101
  251. package/src/principal/utils/getCrc.ts +0 -52
@@ -1,274 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-this-alias */
2
- /* eslint-disable @typescript-eslint/no-explicit-any */
3
- import type * as IDL from './idl.ts';
4
-
5
- export interface ParseConfig {
6
- random?: boolean;
7
- }
8
-
9
- export interface UIConfig {
10
- input?: HTMLElement;
11
- form?: InputForm;
12
- parse(t: IDL.Type, config: ParseConfig, v: string): any;
13
- }
14
-
15
- export interface FormConfig {
16
- open?: HTMLElement;
17
- event?: string;
18
- labelMap?: Record<string, string>;
19
- container?: HTMLElement;
20
- render(t: IDL.Type): InputBox;
21
- }
22
-
23
- export class InputBox {
24
- public status: HTMLElement;
25
- public label: string | null = null;
26
- public value: any = undefined;
27
-
28
- constructor(
29
- public idl: IDL.Type,
30
- public ui: UIConfig,
31
- ) {
32
- const status = document.createElement('span');
33
- status.className = 'status';
34
- this.status = status;
35
-
36
- if (ui.input) {
37
- ui.input.addEventListener('blur', () => {
38
- if ((ui.input as HTMLInputElement).value === '') {
39
- return;
40
- }
41
- this.parse();
42
- });
43
- ui.input.addEventListener('input', () => {
44
- status.style.display = 'none';
45
- ui.input!.classList.remove('reject');
46
- });
47
- }
48
- }
49
- public isRejected(): boolean {
50
- return this.value === undefined;
51
- }
52
-
53
- public parse(config: ParseConfig = {}): any {
54
- if (this.ui.form) {
55
- const value = this.ui.form.parse(config);
56
- this.value = value;
57
- return value;
58
- }
59
-
60
- if (this.ui.input) {
61
- const input = this.ui.input as HTMLInputElement;
62
- try {
63
- const value = this.ui.parse(this.idl, config, input.value);
64
- if (!this.idl.covariant(value)) {
65
- throw new Error(`${input.value} is not of type ${this.idl.display()}`);
66
- }
67
- this.status.style.display = 'none';
68
- this.value = value;
69
- return value;
70
- } catch (err) {
71
- input.classList.add('reject');
72
- this.status.style.display = 'block';
73
- this.status.innerHTML = `InputError: ${(err as Error).message}`;
74
- this.value = undefined;
75
- return undefined;
76
- }
77
- }
78
- return null;
79
- }
80
- public render(dom: HTMLElement): void {
81
- const container = document.createElement('span');
82
- if (this.label) {
83
- const label = document.createElement('label');
84
- label.innerText = this.label;
85
- container.appendChild(label);
86
- }
87
- if (this.ui.input) {
88
- container.appendChild(this.ui.input);
89
- container.appendChild(this.status);
90
- }
91
-
92
- if (this.ui.form) {
93
- this.ui.form.render(container);
94
- }
95
- dom.appendChild(container);
96
- }
97
- }
98
-
99
- export abstract class InputForm {
100
- public form: InputBox[] = [];
101
- constructor(public ui: FormConfig) {}
102
-
103
- public abstract parse(config: ParseConfig): any;
104
- public abstract generateForm(): any;
105
- public renderForm(dom: HTMLElement): void {
106
- if (this.ui.container) {
107
- this.form.forEach(e => e.render(this.ui.container!));
108
- dom.appendChild(this.ui.container);
109
- } else {
110
- this.form.forEach(e => e.render(dom));
111
- }
112
- }
113
- public render(dom: HTMLElement): void {
114
- if (this.ui.open && this.ui.event) {
115
- dom.appendChild(this.ui.open);
116
- const form = this;
117
-
118
- form.ui.open!.addEventListener(form.ui.event!, () => {
119
- // Remove old form
120
- if (form.ui.container) {
121
- form.ui.container.innerHTML = '';
122
- } else {
123
- const oldContainer = form.ui.open!.nextElementSibling;
124
- if (oldContainer) {
125
- oldContainer.parentNode!.removeChild(oldContainer);
126
- }
127
- }
128
- // Render form
129
- form.generateForm();
130
- form.renderForm(dom);
131
- });
132
- } else {
133
- this.generateForm();
134
- this.renderForm(dom);
135
- }
136
- }
137
- }
138
-
139
- export class RecordForm extends InputForm {
140
- constructor(
141
- public fields: Array<[string, IDL.Type]>,
142
- public ui: FormConfig,
143
- ) {
144
- super(ui);
145
- }
146
- public generateForm(): void {
147
- this.form = this.fields.map(([key, type]) => {
148
- const input = this.ui.render(type);
149
- // eslint-disable-next-line
150
- if (this.ui.labelMap && this.ui.labelMap.hasOwnProperty(key)) {
151
- input.label = `${this.ui.labelMap[key]} `;
152
- } else {
153
- input.label = `${key} `;
154
- }
155
- return input;
156
- });
157
- }
158
- public parse(config: ParseConfig): Record<string, any> | undefined {
159
- const v: Record<string, any> = {};
160
- this.fields.forEach(([key, _], i) => {
161
- const value = this.form[i].parse(config);
162
- v[key] = value;
163
- });
164
- if (this.form.some(input => input.isRejected())) {
165
- return undefined;
166
- }
167
- return v;
168
- }
169
- }
170
-
171
- export class TupleForm extends InputForm {
172
- constructor(
173
- public components: IDL.Type[],
174
- public ui: FormConfig,
175
- ) {
176
- super(ui);
177
- }
178
- public generateForm(): void {
179
- this.form = this.components.map(type => {
180
- const input = this.ui.render(type);
181
- return input;
182
- });
183
- }
184
- public parse(config: ParseConfig): any[] | undefined {
185
- const v: any[] = [];
186
- this.components.forEach((_, i) => {
187
- const value = this.form[i].parse(config);
188
- v.push(value);
189
- });
190
- if (this.form.some(input => input.isRejected())) {
191
- return undefined;
192
- }
193
- return v;
194
- }
195
- }
196
-
197
- export class VariantForm extends InputForm {
198
- constructor(
199
- public fields: Array<[string, IDL.Type]>,
200
- public ui: FormConfig,
201
- ) {
202
- super(ui);
203
- }
204
- public generateForm(): void {
205
- const index = (this.ui.open as HTMLSelectElement).selectedIndex;
206
- const [_, type] = this.fields[index];
207
- const variant = this.ui.render(type);
208
- this.form = [variant];
209
- }
210
- public parse(config: ParseConfig): Record<string, any> | undefined {
211
- const select = this.ui.open as HTMLSelectElement;
212
- const selected = select.options[select.selectedIndex].value;
213
- const value = this.form[0].parse(config);
214
- if (value === undefined) {
215
- return undefined;
216
- }
217
- const v: Record<string, any> = {};
218
- v[selected] = value;
219
- return v;
220
- }
221
- }
222
-
223
- export class OptionForm extends InputForm {
224
- constructor(
225
- public ty: IDL.Type,
226
- public ui: FormConfig,
227
- ) {
228
- super(ui);
229
- }
230
- public generateForm(): void {
231
- if ((this.ui.open as HTMLInputElement).checked) {
232
- const opt = this.ui.render(this.ty);
233
- this.form = [opt];
234
- } else {
235
- this.form = [];
236
- }
237
- }
238
- public parse<T>(config: ParseConfig): [T] | [] | undefined {
239
- if (this.form.length === 0) {
240
- return [];
241
- }
242
- const value = this.form[0].parse(config);
243
- if (value === undefined) {
244
- return undefined;
245
- }
246
- return [value];
247
- }
248
- }
249
-
250
- export class VecForm extends InputForm {
251
- constructor(
252
- public ty: IDL.Type,
253
- public ui: FormConfig,
254
- ) {
255
- super(ui);
256
- }
257
- public generateForm(): void {
258
- const len = +(this.ui.open as HTMLInputElement).value;
259
- this.form = [];
260
- for (let i = 0; i < len; i++) {
261
- const t = this.ui.render(this.ty);
262
- this.form.push(t);
263
- }
264
- }
265
- public parse<T>(config: ParseConfig): T[] | undefined {
266
- const value = this.form.map(input => {
267
- return input.parse(config);
268
- });
269
- if (this.form.some(input => input.isRejected())) {
270
- return undefined;
271
- }
272
- return value;
273
- }
274
- }
@@ -1,281 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
-
3
- import * as IDL from './idl.ts';
4
- import { Principal } from '#principal';
5
- import * as UI from './candid-core.ts';
6
-
7
- type InputBox = UI.InputBox;
8
-
9
- const InputConfig: UI.UIConfig = { parse: parsePrimitive };
10
- const FormConfig: UI.FormConfig = { render: renderInput };
11
-
12
- export const inputBox = (t: IDL.Type, config: Partial<UI.UIConfig>) => {
13
- return new UI.InputBox(t, { ...InputConfig, ...config });
14
- };
15
- export const recordForm = (fields: Array<[string, IDL.Type]>, config: Partial<UI.FormConfig>) => {
16
- return new UI.RecordForm(fields, { ...FormConfig, ...config });
17
- };
18
- export const tupleForm = (components: IDL.Type[], config: Partial<UI.FormConfig>) => {
19
- return new UI.TupleForm(components, { ...FormConfig, ...config });
20
- };
21
- export const variantForm = (fields: Array<[string, IDL.Type]>, config: Partial<UI.FormConfig>) => {
22
- return new UI.VariantForm(fields, { ...FormConfig, ...config });
23
- };
24
- export const optForm = (ty: IDL.Type, config: Partial<UI.FormConfig>) => {
25
- return new UI.OptionForm(ty, { ...FormConfig, ...config });
26
- };
27
- export const vecForm = (ty: IDL.Type, config: Partial<UI.FormConfig>) => {
28
- return new UI.VecForm(ty, { ...FormConfig, ...config });
29
- };
30
-
31
- export class Render extends IDL.Visitor<null, InputBox> {
32
- public visitType<T>(t: IDL.Type<T>, _d: null): InputBox {
33
- const input = document.createElement('input');
34
- input.classList.add('argument');
35
- input.placeholder = t.display();
36
- return inputBox(t, { input });
37
- }
38
- public visitNull(t: IDL.NullClass, _d: null): InputBox {
39
- return inputBox(t, {});
40
- }
41
- public visitRecord(t: IDL.RecordClass, fields: Array<[string, IDL.Type]>, _d: null): InputBox {
42
- let config = {};
43
- if (fields.length > 1) {
44
- const container = document.createElement('div');
45
- container.classList.add('popup-form');
46
- config = { container };
47
- }
48
- const form = recordForm(fields, config);
49
- return inputBox(t as IDL.Type<any>, { form });
50
- }
51
- public visitTuple<T extends any[]>(
52
- t: IDL.TupleClass<T>,
53
- components: IDL.Type[],
54
- _d: null,
55
- ): InputBox {
56
- let config = {};
57
- if (components.length > 1) {
58
- const container = document.createElement('div');
59
- container.classList.add('popup-form');
60
- config = { container };
61
- }
62
- const form = tupleForm(components, config);
63
- return inputBox(t as IDL.Type<any>, { form });
64
- }
65
- public visitVariant(t: IDL.VariantClass, fields: Array<[string, IDL.Type]>, _d: null): InputBox {
66
- const select = document.createElement('select');
67
- for (const [key, _type] of fields) {
68
- const option = new Option(key);
69
- select.add(option);
70
- }
71
- select.selectedIndex = -1;
72
- select.classList.add('open');
73
- const config: Partial<UI.FormConfig> = { open: select, event: 'change' };
74
- const form = variantForm(fields, config);
75
- return inputBox(t as IDL.Type<any>, { form });
76
- }
77
- public visitOpt<T>(t: IDL.OptClass<T>, ty: IDL.Type<T>, _d: null): InputBox {
78
- const checkbox = document.createElement('input');
79
- checkbox.type = 'checkbox';
80
- checkbox.classList.add('open');
81
- const form = optForm(ty, { open: checkbox, event: 'change' });
82
- return inputBox(t as IDL.Type<any>, { form });
83
- }
84
- public visitVec<T>(t: IDL.VecClass<T>, ty: IDL.Type<T>, _d: null): InputBox {
85
- const len = document.createElement('input');
86
- len.type = 'number';
87
- len.min = '0';
88
- len.max = '100';
89
- len.style.width = '8rem';
90
- len.placeholder = 'len';
91
- len.classList.add('open');
92
- const container = document.createElement('div');
93
- container.classList.add('popup-form');
94
- const form = vecForm(ty, { open: len, event: 'change', container });
95
- return inputBox(t, { form });
96
- }
97
- public visitRec<T>(_t: IDL.RecClass<T>, ty: IDL.ConstructType<T>, _d: null): InputBox {
98
- return renderInput(ty);
99
- }
100
- }
101
-
102
- class Parse extends IDL.Visitor<string, any> {
103
- public visitNull(_t: IDL.NullClass, _v: string): null {
104
- return null;
105
- }
106
- public visitBool(_t: IDL.BoolClass, v: string): boolean {
107
- if (v === 'true') {
108
- return true;
109
- }
110
- if (v === 'false') {
111
- return false;
112
- }
113
- throw new Error(`Cannot parse ${v} as boolean`);
114
- }
115
- public visitText(_t: IDL.TextClass, v: string): string {
116
- return v;
117
- }
118
- public visitFloat(_t: IDL.FloatClass, v: string): number {
119
- return parseFloat(v);
120
- }
121
- public visitFixedInt(t: IDL.FixedIntClass, v: string): number | bigint {
122
- if (t._bits <= 32) {
123
- return parseInt(v, 10);
124
- }
125
- return BigInt(v);
126
- }
127
- public visitFixedNat(t: IDL.FixedNatClass, v: string): number | bigint {
128
- if (t._bits <= 32) {
129
- return parseInt(v, 10);
130
- }
131
- return BigInt(v);
132
- }
133
- public visitNumber(_t: IDL.PrimitiveType, v: string): bigint {
134
- return BigInt(v);
135
- }
136
- public visitPrincipal(_t: IDL.PrincipalClass, v: string): Principal {
137
- return Principal.fromText(v);
138
- }
139
- public visitService(_t: IDL.ServiceClass, v: string): Principal {
140
- return Principal.fromText(v);
141
- }
142
- public visitFunc(_t: IDL.FuncClass, v: string): [Principal, string] {
143
- const x = v.split('.', 2);
144
- return [Principal.fromText(x[0]), x[1]];
145
- }
146
- }
147
-
148
- class Random extends IDL.Visitor<string, any> {
149
- public visitNull(_t: IDL.NullClass, _v: string): null {
150
- return null;
151
- }
152
- public visitBool(_t: IDL.BoolClass, _v: string): boolean {
153
- return Math.random() < 0.5;
154
- }
155
- public visitText(_t: IDL.TextClass, _v: string): string {
156
- return Math.random().toString(36).substring(6);
157
- }
158
- public visitFloat(_t: IDL.FloatClass, _v: string): number {
159
- return Math.random();
160
- }
161
- public visitInt(_t: IDL.IntClass, _v: string): bigint {
162
- return BigInt(this.generateNumber(true));
163
- }
164
- public visitNat(_t: IDL.NatClass, _v: string): bigint {
165
- return BigInt(this.generateNumber(false));
166
- }
167
- public visitFixedInt(t: IDL.FixedIntClass, v: string): number | bigint {
168
- const x = this.generateNumber(true);
169
- if (t._bits <= 32) {
170
- return x;
171
- }
172
- return BigInt(v);
173
- }
174
- public visitFixedNat(t: IDL.FixedNatClass, v: string): number | bigint {
175
- const x = this.generateNumber(false);
176
- if (t._bits <= 32) {
177
- return x;
178
- }
179
- return BigInt(v);
180
- }
181
- private generateNumber(signed: boolean): number {
182
- const num = Math.floor(Math.random() * 100);
183
- if (signed && Math.random() < 0.5) {
184
- return -num;
185
- }
186
- return num;
187
- }
188
- }
189
-
190
- function parsePrimitive(t: IDL.Type, config: UI.ParseConfig, d: string) {
191
- if (config.random && d === '') {
192
- return t.accept(new Random(), d);
193
- }
194
- return t.accept(new Parse(), d);
195
- }
196
-
197
- /**
198
- *
199
- * @param t an IDL type
200
- * @returns an input for that type
201
- */
202
- export function renderInput(t: IDL.Type): InputBox {
203
- return t.accept(new Render(), null);
204
- }
205
-
206
- interface ValueConfig {
207
- input: InputBox;
208
- value: any;
209
- }
210
-
211
- /**
212
- *
213
- * @param t an IDL Type
214
- * @param input an InputBox
215
- * @param value any
216
- * @returns rendering that value to the provided input
217
- */
218
- export function renderValue(t: IDL.Type, input: InputBox, value: any) {
219
- return t.accept(new RenderValue(), { input, value });
220
- }
221
-
222
- class RenderValue extends IDL.Visitor<ValueConfig, void> {
223
- public visitType<T>(t: IDL.Type<T>, d: ValueConfig) {
224
- (d.input.ui.input as HTMLInputElement).value = t.valueToString(d.value);
225
- }
226
-
227
- public visitNull(_t: IDL.NullClass, _d: ValueConfig) {}
228
- public visitText(_t: IDL.TextClass, d: ValueConfig) {
229
- (d.input.ui.input as HTMLInputElement).value = d.value;
230
- }
231
- public visitRec<T>(_t: IDL.RecClass<T>, ty: IDL.ConstructType<T>, d: ValueConfig): void {
232
- renderValue(ty, d.input, d.value);
233
- }
234
- public visitOpt<T>(_t: IDL.OptClass<T>, ty: IDL.Type<T>, d: ValueConfig): void {
235
- if (d.value.length !== 0) {
236
- const form = d.input.ui.form!;
237
- const open = form.ui.open as HTMLInputElement;
238
- open.checked = true;
239
- open.dispatchEvent(new Event(form.ui.event!));
240
- renderValue(ty, form.form[0], d.value[0]);
241
- }
242
- }
243
- public visitRecord(_t: IDL.RecordClass, fields: Array<[string, IDL.Type]>, d: ValueConfig) {
244
- const form = d.input.ui.form!;
245
- fields.forEach(([key, type], i) => {
246
- renderValue(type, form.form[i], d.value[key]);
247
- });
248
- }
249
- public visitTuple<T extends any[]>(
250
- _t: IDL.TupleClass<T>,
251
- components: IDL.Type[],
252
- d: ValueConfig,
253
- ) {
254
- const form = d.input.ui.form!;
255
- components.forEach((type, i) => {
256
- renderValue(type, form.form[i], d.value[i]);
257
- });
258
- }
259
- public visitVariant(_t: IDL.VariantClass, fields: Array<[string, IDL.Type]>, d: ValueConfig) {
260
- const form = d.input.ui.form!;
261
- const selected = Object.entries(d.value)[0];
262
- fields.forEach(([key, type], i) => {
263
- if (key === selected[0]) {
264
- const open = form.ui.open as HTMLSelectElement;
265
- open.selectedIndex = i;
266
- open.dispatchEvent(new Event(form.ui.event!));
267
- renderValue(type, form.form[0], selected[1]);
268
- }
269
- });
270
- }
271
- public visitVec<T>(_t: IDL.VecClass<T>, ty: IDL.Type<T>, d: ValueConfig) {
272
- const form = d.input.ui.form!;
273
- const len = d.value.length;
274
- const open = form.ui.open as HTMLInputElement;
275
- open.value = len;
276
- open.dispatchEvent(new Event(form.ui.event!));
277
- d.value.forEach((v: T, i: number) => {
278
- renderValue(ty, form.form[i], v);
279
- });
280
- }
281
- }