wasm-ast-types 0.11.3 → 0.13.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 (55) hide show
  1. package/main/client/test/ts-client.issue-71.test.js +103 -0
  2. package/main/context/context.js +2 -1
  3. package/main/utils/types.js +20 -7
  4. package/module/client/test/ts-client.issue-71.test.js +21 -0
  5. package/module/context/context.js +2 -1
  6. package/module/utils/types.js +20 -7
  7. package/package.json +5 -3
  8. package/src/client/client.ts +665 -0
  9. package/src/client/index.ts +1 -0
  10. package/src/client/test/__snapshots__/ts-client.account-nfts.spec.ts.snap +497 -0
  11. package/src/client/test/__snapshots__/ts-client.arrays-ref.spec.ts.snap +452 -0
  12. package/src/client/test/__snapshots__/ts-client.arrays.spec.ts.snap +101 -0
  13. package/src/client/test/__snapshots__/ts-client.cw-named-groups.test.ts.snap +141 -0
  14. package/src/client/test/__snapshots__/ts-client.cw-proposal-single.test.ts.snap +341 -0
  15. package/src/client/test/__snapshots__/ts-client.empty-enums.spec.ts.snap +20 -0
  16. package/src/client/test/__snapshots__/ts-client.issue-71.test.ts.snap +432 -0
  17. package/src/client/test/__snapshots__/ts-client.issues.test.ts.snap +984 -0
  18. package/src/client/test/__snapshots__/ts-client.sg721.spec.ts.snap +350 -0
  19. package/src/client/test/__snapshots__/ts-client.spec.ts.snap +723 -0
  20. package/src/client/test/__snapshots__/ts-client.vectis.spec.ts.snap +337 -0
  21. package/src/client/test/ts-client.account-nfts.spec.ts +55 -0
  22. package/src/client/test/ts-client.arrays-ref.spec.ts +48 -0
  23. package/src/client/test/ts-client.arrays.spec.ts +58 -0
  24. package/src/client/test/ts-client.cw-named-groups.test.ts +48 -0
  25. package/src/client/test/ts-client.cw-proposal-single.test.ts +50 -0
  26. package/src/client/test/ts-client.empty-enums.spec.ts +28 -0
  27. package/src/client/test/ts-client.issue-71.test.ts +51 -0
  28. package/src/client/test/ts-client.issues.test.ts +52 -0
  29. package/src/client/test/ts-client.sg721.spec.ts +46 -0
  30. package/src/client/test/ts-client.spec.ts +166 -0
  31. package/src/client/test/ts-client.vectis.spec.ts +97 -0
  32. package/src/context/context.ts +134 -0
  33. package/src/context/imports.ts +126 -0
  34. package/src/context/index.ts +2 -0
  35. package/src/index.ts +7 -0
  36. package/src/message-composer/__snapshots__/message-composer.spec.ts.snap +271 -0
  37. package/src/message-composer/index.ts +1 -0
  38. package/src/message-composer/message-composer.spec.ts +25 -0
  39. package/src/message-composer/message-composer.ts +305 -0
  40. package/src/react-query/__snapshots__/react-query.spec.ts.snap +913 -0
  41. package/src/react-query/index.ts +1 -0
  42. package/src/react-query/react-query.spec.ts +75 -0
  43. package/src/react-query/react-query.ts +913 -0
  44. package/src/recoil/__snapshots__/recoil.spec.ts.snap +203 -0
  45. package/src/recoil/index.ts +1 -0
  46. package/src/recoil/recoil.spec.ts +38 -0
  47. package/src/recoil/recoil.ts +307 -0
  48. package/src/types.ts +44 -0
  49. package/src/utils/__snapshots__/babel.spec.ts.snap +75 -0
  50. package/src/utils/babel.spec.ts +511 -0
  51. package/src/utils/babel.ts +315 -0
  52. package/src/utils/index.ts +2 -0
  53. package/src/utils/types.ts +459 -0
  54. package/types/client/client.d.ts +1 -1
  55. package/types/context/context.d.ts +1 -0
@@ -0,0 +1,665 @@
1
+ import * as t from '@babel/types';
2
+ import { camel, pascal } from 'case';
3
+ import {
4
+ bindMethod,
5
+ typedIdentifier,
6
+ promiseTypeAnnotation,
7
+ classDeclaration,
8
+ classProperty,
9
+ arrowFunctionExpression,
10
+ getMessageProperties
11
+ } from '../utils'
12
+
13
+ import {
14
+ QueryMsg,
15
+ ExecuteMsg
16
+ } from '../types';
17
+
18
+ import { getPropertyType, getType, createTypedObjectParams, getResponseType } from '../utils/types';
19
+ import { RenderContext } from '../context';
20
+ import { JSONSchema } from '../types';
21
+ import { identifier, propertySignature } from '../utils/babel';
22
+
23
+ export const CONSTANT_EXEC_PARAMS = [
24
+ t.assignmentPattern(
25
+ identifier(
26
+ 'fee',
27
+ t.tsTypeAnnotation(
28
+ t.tsUnionType(
29
+ [
30
+ t.tSNumberKeyword(),
31
+ t.tsTypeReference(
32
+ t.identifier('StdFee')
33
+ ),
34
+ t.tsLiteralType(
35
+ t.stringLiteral('auto')
36
+ )
37
+ ]
38
+ )
39
+ ),
40
+ false
41
+ ),
42
+ t.stringLiteral('auto')
43
+ ),
44
+ identifier('memo', t.tsTypeAnnotation(
45
+ t.tsStringKeyword()
46
+ ), true),
47
+ identifier('funds', t.tsTypeAnnotation(
48
+ t.tsArrayType(
49
+ t.tsTypeReference(
50
+ t.identifier('Coin')
51
+ )
52
+ )
53
+ ), true)
54
+ ];
55
+
56
+ export const FIXED_EXECUTE_PARAMS = [
57
+ identifier('fee', t.tsTypeAnnotation(
58
+ t.tsUnionType(
59
+ [
60
+ t.tsNumberKeyword(),
61
+ t.tsTypeReference(
62
+ t.identifier('StdFee')
63
+ ),
64
+ t.tsLiteralType(
65
+ t.stringLiteral('auto')
66
+ )
67
+ ]
68
+ )
69
+ ), true),
70
+ identifier('memo', t.tsTypeAnnotation(
71
+ t.tsStringKeyword()
72
+ ), true),
73
+ identifier('funds', t.tsTypeAnnotation(
74
+ t.tsArrayType(
75
+ t.tsTypeReference(
76
+ t.identifier('Coin')
77
+ )
78
+ )
79
+ ), true)
80
+ ];
81
+
82
+
83
+ export const createWasmQueryMethod = (
84
+ context: RenderContext,
85
+ jsonschema: any
86
+ ) => {
87
+
88
+ const underscoreName = Object.keys(jsonschema.properties)[0];
89
+ const methodName = camel(underscoreName);
90
+ const responseType = getResponseType(context, underscoreName);
91
+
92
+ const obj = createTypedObjectParams(
93
+ context,
94
+ jsonschema.properties[underscoreName]
95
+ );
96
+
97
+ const args = getWasmMethodArgs(
98
+ context,
99
+ jsonschema.properties[underscoreName]
100
+ );
101
+
102
+ const actionArg =
103
+ t.objectProperty(t.identifier(underscoreName), t.objectExpression(args));
104
+
105
+ return t.classProperty(
106
+ t.identifier(methodName),
107
+ arrowFunctionExpression(
108
+ obj ? [obj] : [],
109
+ t.blockStatement(
110
+ [
111
+ t.returnStatement(
112
+ t.callExpression(
113
+ t.memberExpression(
114
+ t.memberExpression(
115
+ t.thisExpression(),
116
+ t.identifier('client')
117
+ ),
118
+ t.identifier('queryContractSmart')
119
+ ),
120
+ [
121
+ t.memberExpression(t.thisExpression(), t.identifier('contractAddress')),
122
+ t.objectExpression([
123
+ actionArg
124
+ ])
125
+ ]
126
+ )
127
+ )
128
+ ]
129
+ ),
130
+ t.tsTypeAnnotation(
131
+ t.tsTypeReference(
132
+ t.identifier('Promise'),
133
+ t.tsTypeParameterInstantiation(
134
+ [
135
+ t.tSTypeReference(
136
+ t.identifier(responseType)
137
+ )
138
+ ]
139
+ )
140
+ )
141
+ ),
142
+ true
143
+ )
144
+ );
145
+ }
146
+
147
+ export const createQueryClass = (
148
+ context: RenderContext,
149
+ className: string,
150
+ implementsClassName: string,
151
+ queryMsg: QueryMsg
152
+ ) => {
153
+
154
+ context.addUtil('CosmWasmClient');
155
+
156
+ const propertyNames = getMessageProperties(queryMsg)
157
+ .map(method => Object.keys(method.properties)?.[0])
158
+ .filter(Boolean);
159
+
160
+ const bindings = propertyNames
161
+ .map(camel)
162
+ .map(bindMethod);
163
+
164
+ const methods = getMessageProperties(queryMsg)
165
+ .map(schema => {
166
+ return createWasmQueryMethod(context, schema)
167
+ });
168
+
169
+ return t.exportNamedDeclaration(
170
+ classDeclaration(className,
171
+ [
172
+ // client
173
+ classProperty('client', t.tsTypeAnnotation(
174
+ t.tsTypeReference(t.identifier('CosmWasmClient'))
175
+ )),
176
+
177
+ // contractAddress
178
+ classProperty('contractAddress', t.tsTypeAnnotation(
179
+ t.tsStringKeyword()
180
+ )),
181
+
182
+ // constructor
183
+ t.classMethod('constructor',
184
+ t.identifier('constructor'),
185
+ [
186
+ typedIdentifier('client', t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CosmWasmClient')))),
187
+ typedIdentifier('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword()))
188
+
189
+ ],
190
+ t.blockStatement(
191
+ [
192
+
193
+ // client/contract set
194
+ t.expressionStatement(
195
+ t.assignmentExpression(
196
+ '=',
197
+ t.memberExpression(
198
+ t.thisExpression(),
199
+ t.identifier('client')
200
+ ),
201
+ t.identifier('client')
202
+ )
203
+ ),
204
+ t.expressionStatement(
205
+ t.assignmentExpression(
206
+ '=',
207
+ t.memberExpression(
208
+ t.thisExpression(),
209
+ t.identifier('contractAddress')
210
+ ),
211
+ t.identifier('contractAddress')
212
+ )
213
+ ),
214
+
215
+ ...bindings
216
+
217
+ ]
218
+ )),
219
+
220
+ ...methods
221
+
222
+ ],
223
+ [
224
+ t.tSExpressionWithTypeArguments(
225
+ t.identifier(implementsClassName)
226
+ )
227
+ ])
228
+ );
229
+ };
230
+
231
+ export const getWasmMethodArgs = (
232
+ context: RenderContext,
233
+ jsonschema: JSONSchema
234
+ ) => {
235
+ let keys = Object.keys(jsonschema.properties ?? {});
236
+
237
+ // only 1 degree $ref-lookup
238
+ if (!keys.length && jsonschema.$ref) {
239
+ const obj = context.refLookup(jsonschema.$ref);
240
+ if (obj) {
241
+ keys = Object.keys(obj.properties ?? {})
242
+ }
243
+ }
244
+
245
+ const args = keys.map(prop => {
246
+ return t.objectProperty(
247
+ t.identifier(prop),
248
+ t.identifier(camel(prop)),
249
+ false,
250
+ prop === camel(prop)
251
+ );
252
+ });
253
+
254
+ return args;
255
+ };
256
+
257
+ export const createWasmExecMethod = (
258
+ context: RenderContext,
259
+ jsonschema: JSONSchema
260
+ ) => {
261
+
262
+ context.addUtil('ExecuteResult');
263
+ context.addUtil('StdFee');
264
+ context.addUtil('Coin');
265
+
266
+ const underscoreName = Object.keys(jsonschema.properties)[0];
267
+ const methodName = camel(underscoreName);
268
+ const obj = createTypedObjectParams(
269
+ context,
270
+ jsonschema.properties[underscoreName]
271
+ );
272
+ const args = getWasmMethodArgs(
273
+ context,
274
+ jsonschema.properties[underscoreName]
275
+ );
276
+
277
+ return t.classProperty(
278
+ t.identifier(methodName),
279
+ arrowFunctionExpression(
280
+ obj ? [
281
+ // props
282
+ obj,
283
+ ...CONSTANT_EXEC_PARAMS
284
+ ] : CONSTANT_EXEC_PARAMS,
285
+ t.blockStatement(
286
+ [
287
+ t.returnStatement(
288
+ t.awaitExpression(
289
+ t.callExpression(
290
+ t.memberExpression(
291
+ t.memberExpression(
292
+ t.thisExpression(),
293
+ t.identifier('client')
294
+ ),
295
+ t.identifier('execute')
296
+ ),
297
+ [
298
+ t.memberExpression(
299
+ t.thisExpression(),
300
+ t.identifier('sender')
301
+ ),
302
+ t.memberExpression(
303
+ t.thisExpression(),
304
+ t.identifier('contractAddress')
305
+ ),
306
+ t.objectExpression(
307
+ [
308
+ t.objectProperty(
309
+ t.identifier(underscoreName),
310
+ t.objectExpression([
311
+ ...args
312
+ ])
313
+ )
314
+
315
+ ]
316
+ ),
317
+ t.identifier('fee'),
318
+ t.identifier('memo'),
319
+ t.identifier('funds')
320
+ ]
321
+ )
322
+ )
323
+ )
324
+ ]
325
+ ),
326
+ // return type
327
+ t.tsTypeAnnotation(
328
+ t.tsTypeReference(
329
+ t.identifier('Promise'),
330
+ t.tsTypeParameterInstantiation(
331
+ [
332
+ t.tSTypeReference(
333
+ t.identifier('ExecuteResult')
334
+ )
335
+ ]
336
+ )
337
+ )
338
+ ),
339
+ true
340
+ )
341
+ );
342
+
343
+ }
344
+
345
+ export const createExecuteClass = (
346
+ context: RenderContext,
347
+ className: string,
348
+ implementsClassName: string,
349
+ extendsClassName: string | null,
350
+ execMsg: ExecuteMsg
351
+ ) => {
352
+
353
+ context.addUtil('SigningCosmWasmClient');
354
+
355
+ const propertyNames = getMessageProperties(execMsg)
356
+ .map(method => Object.keys(method.properties)?.[0])
357
+ .filter(Boolean);
358
+
359
+ const bindings = propertyNames
360
+ .map(camel)
361
+ .map(bindMethod);
362
+
363
+ const methods = getMessageProperties(execMsg)
364
+ .map(schema => {
365
+ return createWasmExecMethod(context, schema)
366
+ });
367
+
368
+ const blockStmt = [];
369
+
370
+ if (extendsClassName) {
371
+ blockStmt.push( // super()
372
+ t.expressionStatement(t.callExpression(
373
+ t.super(),
374
+ [
375
+ t.identifier('client'),
376
+ t.identifier('contractAddress')
377
+ ]
378
+ ))
379
+ );
380
+ }
381
+
382
+ [].push.apply(blockStmt, [
383
+ // client/contract set
384
+ t.expressionStatement(
385
+ t.assignmentExpression(
386
+ '=',
387
+ t.memberExpression(
388
+ t.thisExpression(),
389
+ t.identifier('client')
390
+ ),
391
+ t.identifier('client')
392
+ )
393
+ ),
394
+ t.expressionStatement(
395
+ t.assignmentExpression(
396
+ '=',
397
+ t.memberExpression(
398
+ t.thisExpression(),
399
+ t.identifier('sender')
400
+ ),
401
+ t.identifier('sender')
402
+ )
403
+ ),
404
+ t.expressionStatement(
405
+ t.assignmentExpression(
406
+ '=',
407
+ t.memberExpression(
408
+ t.thisExpression(),
409
+ t.identifier('contractAddress')
410
+ ),
411
+ t.identifier('contractAddress')
412
+ )
413
+ ),
414
+ ...bindings
415
+ ]);
416
+
417
+ return t.exportNamedDeclaration(
418
+ classDeclaration(className,
419
+ [
420
+ // client
421
+ classProperty('client', t.tsTypeAnnotation(
422
+ t.tsTypeReference(t.identifier('SigningCosmWasmClient'))
423
+ )),
424
+
425
+ // sender
426
+ classProperty('sender', t.tsTypeAnnotation(
427
+ t.tsStringKeyword()
428
+ )),
429
+
430
+ // contractAddress
431
+ classProperty('contractAddress', t.tsTypeAnnotation(
432
+ t.tsStringKeyword()
433
+ )),
434
+
435
+ // constructor
436
+ t.classMethod('constructor',
437
+ t.identifier('constructor'),
438
+ [
439
+ typedIdentifier('client', t.tsTypeAnnotation(t.tsTypeReference(t.identifier('SigningCosmWasmClient')))),
440
+ typedIdentifier('sender', t.tsTypeAnnotation(t.tsStringKeyword())),
441
+ typedIdentifier('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword()))
442
+ ],
443
+ t.blockStatement(
444
+ blockStmt
445
+ )),
446
+ ...methods
447
+ ],
448
+ [
449
+ t.tSExpressionWithTypeArguments(
450
+ t.identifier(implementsClassName)
451
+ )
452
+ ],
453
+ extendsClassName ? t.identifier(extendsClassName) : null
454
+ )
455
+ );
456
+ }
457
+
458
+ export const createExecuteInterface = (
459
+ context: RenderContext,
460
+ className: string,
461
+ extendsClassName: string | null,
462
+ execMsg: ExecuteMsg
463
+ ) => {
464
+
465
+ const methods = getMessageProperties(execMsg)
466
+ .map(jsonschema => {
467
+ const underscoreName = Object.keys(jsonschema.properties)[0];
468
+ const methodName = camel(underscoreName);
469
+ return createPropertyFunctionWithObjectParamsForExec(
470
+ context,
471
+ methodName,
472
+ 'ExecuteResult',
473
+ jsonschema.properties[underscoreName]
474
+ );
475
+ });
476
+
477
+ const extendsAst = extendsClassName ? [t.tSExpressionWithTypeArguments(
478
+ t.identifier(extendsClassName)
479
+ )] : []
480
+
481
+ return t.exportNamedDeclaration(
482
+ t.tsInterfaceDeclaration(
483
+ t.identifier(className),
484
+ null,
485
+ extendsAst,
486
+ t.tSInterfaceBody(
487
+ [
488
+
489
+ // contract address
490
+ t.tSPropertySignature(
491
+ t.identifier('contractAddress'),
492
+ t.tsTypeAnnotation(
493
+ t.tsStringKeyword()
494
+ )
495
+ ),
496
+
497
+ // contract address
498
+ t.tSPropertySignature(
499
+ t.identifier('sender'),
500
+ t.tsTypeAnnotation(
501
+ t.tsStringKeyword()
502
+ )
503
+ ),
504
+
505
+ ...methods,
506
+ ]
507
+ )
508
+ )
509
+ );
510
+ };
511
+
512
+ export const createPropertyFunctionWithObjectParams = (
513
+ context: RenderContext,
514
+ methodName: string,
515
+ responseType: string,
516
+ jsonschema: JSONSchema
517
+ ) => {
518
+ const obj = createTypedObjectParams(context, jsonschema);
519
+
520
+ const func = {
521
+ type: 'TSFunctionType',
522
+ typeAnnotation: promiseTypeAnnotation(responseType),
523
+ parameters: obj ? [
524
+ obj
525
+ ] : []
526
+ }
527
+
528
+ return t.tSPropertySignature(
529
+ t.identifier(methodName),
530
+ t.tsTypeAnnotation(
531
+ // @ts-ignore:next-line
532
+ func
533
+ )
534
+ );
535
+ };
536
+
537
+ export const createPropertyFunctionWithObjectParamsForExec = (
538
+ context: RenderContext,
539
+ methodName: string,
540
+ responseType: string,
541
+ jsonschema: JSONSchema
542
+ ) => {
543
+
544
+ context.addUtil('Coin');
545
+
546
+ const obj = createTypedObjectParams(context, jsonschema);
547
+
548
+ const func = {
549
+ type: 'TSFunctionType',
550
+ typeAnnotation: promiseTypeAnnotation(responseType),
551
+ parameters: obj ? [
552
+ obj,
553
+ ...FIXED_EXECUTE_PARAMS
554
+
555
+ ] : FIXED_EXECUTE_PARAMS
556
+ }
557
+
558
+ return t.tSPropertySignature(
559
+ t.identifier(methodName),
560
+ t.tsTypeAnnotation(
561
+ // @ts-ignore:next-line
562
+ func
563
+ )
564
+ );
565
+ };
566
+
567
+ export const createQueryInterface = (
568
+ context: RenderContext,
569
+ className: string,
570
+ queryMsg: QueryMsg
571
+ ) => {
572
+ const methods = getMessageProperties(queryMsg)
573
+ .map(jsonschema => {
574
+ const underscoreName = Object.keys(jsonschema.properties)[0];
575
+ const methodName = camel(underscoreName);
576
+ const responseType = getResponseType(context, underscoreName);
577
+ return createPropertyFunctionWithObjectParams(
578
+ context,
579
+ methodName,
580
+ responseType,
581
+ jsonschema.properties[underscoreName]
582
+ );
583
+ });
584
+
585
+ return t.exportNamedDeclaration(
586
+ t.tsInterfaceDeclaration(
587
+ t.identifier(className),
588
+ null,
589
+ [],
590
+ t.tSInterfaceBody(
591
+ [
592
+ t.tSPropertySignature(
593
+ t.identifier('contractAddress'),
594
+ t.tsTypeAnnotation(
595
+ t.tsStringKeyword()
596
+ )
597
+ ),
598
+ ...methods
599
+ ]
600
+ )
601
+ )
602
+ );
603
+ };
604
+
605
+
606
+ export const createTypeOrInterface = (
607
+ context: RenderContext,
608
+ Type: string,
609
+ jsonschema: JSONSchema
610
+ ) => {
611
+ if (jsonschema.type !== 'object') {
612
+
613
+ if (!jsonschema.type) {
614
+ return t.exportNamedDeclaration(
615
+ t.tsTypeAliasDeclaration(
616
+ t.identifier(Type),
617
+ null,
618
+ t.tsTypeReference(t.identifier(jsonschema.title))
619
+ )
620
+ )
621
+ }
622
+
623
+ return t.exportNamedDeclaration(
624
+ t.tsTypeAliasDeclaration(
625
+ t.identifier(Type),
626
+ null,
627
+ getType(jsonschema.type)
628
+ )
629
+ );
630
+ }
631
+ const props = Object.keys(jsonschema.properties ?? {})
632
+ .map(prop => {
633
+ const { type, optional } = getPropertyType(context, jsonschema, prop);
634
+ return propertySignature(camel(prop), t.tsTypeAnnotation(
635
+ type
636
+ ), optional);
637
+ });
638
+
639
+
640
+ return t.exportNamedDeclaration(
641
+ t.tsInterfaceDeclaration(
642
+ t.identifier(Type),
643
+ null,
644
+ [],
645
+ t.tsInterfaceBody(
646
+ // @ts-ignore:next-line
647
+ [
648
+ ...props
649
+ ]
650
+ )
651
+ )
652
+ )
653
+ };
654
+
655
+ export const createTypeInterface = (
656
+ context: RenderContext,
657
+ jsonschema: JSONSchema
658
+ ) => {
659
+ const Type = jsonschema.title;
660
+ return createTypeOrInterface(
661
+ context,
662
+ Type,
663
+ jsonschema
664
+ );
665
+ };
@@ -0,0 +1 @@
1
+ export * from './client';