wasm-ast-types 0.26.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (218) hide show
  1. package/LICENSE-Apache +201 -0
  2. package/LICENSE-MIT +21 -0
  3. package/README.md +1 -1
  4. package/{types/client → client}/client.d.ts +2 -2
  5. package/client/client.js +275 -0
  6. package/client/index.js +17 -0
  7. package/{types/context → context}/context.d.ts +9 -22
  8. package/context/context.js +114 -0
  9. package/{types/context → context}/imports.d.ts +9 -8
  10. package/context/imports.js +166 -0
  11. package/context/index.js +18 -0
  12. package/esm/client/client.js +238 -0
  13. package/esm/client/index.js +1 -0
  14. package/esm/context/context.js +104 -0
  15. package/esm/context/imports.js +136 -0
  16. package/{module → esm}/context/index.js +1 -1
  17. package/{src/index.ts → esm/index.js} +0 -1
  18. package/esm/message-builder/message-builder.js +64 -0
  19. package/esm/message-composer/message-composer.js +105 -0
  20. package/esm/provider/provider.js +97 -0
  21. package/esm/react-query/react-query.js +458 -0
  22. package/esm/recoil/recoil.js +110 -0
  23. package/esm/utils/babel.js +231 -0
  24. package/esm/utils/constants.js +20 -0
  25. package/esm/utils/ref.js +4 -0
  26. package/esm/utils/types.js +343 -0
  27. package/{types/index.d.ts → index.d.ts} +0 -1
  28. package/index.js +24 -0
  29. package/message-builder/index.js +17 -0
  30. package/{types/message-builder → message-builder}/message-builder.d.ts +1 -1
  31. package/message-builder/message-builder.js +91 -0
  32. package/message-composer/index.d.ts +1 -0
  33. package/message-composer/index.js +17 -0
  34. package/{types/message-composer → message-composer}/message-composer.d.ts +1 -1
  35. package/message-composer/message-composer.js +133 -0
  36. package/package.json +19 -68
  37. package/provider/index.d.ts +1 -0
  38. package/provider/index.js +17 -0
  39. package/provider/provider.js +127 -0
  40. package/react-query/index.d.ts +1 -0
  41. package/react-query/index.js +17 -0
  42. package/{types/react-query → react-query}/react-query.d.ts +2 -3
  43. package/react-query/react-query.js +490 -0
  44. package/recoil/index.d.ts +1 -0
  45. package/recoil/index.js +17 -0
  46. package/{types/recoil → recoil}/recoil.d.ts +1 -1
  47. package/recoil/recoil.js +140 -0
  48. package/{types/utils → utils}/babel.d.ts +9 -8
  49. package/utils/babel.js +285 -0
  50. package/utils/constants.js +46 -0
  51. package/utils/index.js +26 -0
  52. package/{types/utils → utils}/ref.d.ts +1 -1
  53. package/utils/ref.js +8 -0
  54. package/{types/utils → utils}/types.d.ts +8 -8
  55. package/utils/types.js +379 -0
  56. package/main/client/client.js +0 -196
  57. package/main/client/index.js +0 -16
  58. package/main/client/test/ts-client.account-nfts.spec.js +0 -28
  59. package/main/client/test/ts-client.arrays-ref.spec.js +0 -19
  60. package/main/client/test/ts-client.arrays.spec.js +0 -26
  61. package/main/client/test/ts-client.cw-named-groups.test.js +0 -19
  62. package/main/client/test/ts-client.cw-proposal-single.test.js +0 -23
  63. package/main/client/test/ts-client.empty-enums.spec.js +0 -13
  64. package/main/client/test/ts-client.issue-101.spec.js +0 -23
  65. package/main/client/test/ts-client.issue-103.test.js +0 -44
  66. package/main/client/test/ts-client.issue-71.test.js +0 -81
  67. package/main/client/test/ts-client.issue-98.test.js +0 -28
  68. package/main/client/test/ts-client.issues.test.js +0 -81
  69. package/main/client/test/ts-client.overrides.spec.js +0 -33
  70. package/main/client/test/ts-client.sg721.spec.js +0 -19
  71. package/main/client/test/ts-client.spec.js +0 -87
  72. package/main/client/test/ts-client.vectis.spec.js +0 -46
  73. package/main/client/test/ts-client.wager.spec.js +0 -39
  74. package/main/context/context.js +0 -162
  75. package/main/context/imports.js +0 -159
  76. package/main/context/index.js +0 -27
  77. package/main/index.js +0 -104
  78. package/main/message-builder/index.js +0 -16
  79. package/main/message-builder/message-builder.js +0 -62
  80. package/main/message-builder/message-builder.spec.js +0 -20
  81. package/main/message-composer/index.js +0 -16
  82. package/main/message-composer/message-composer.js +0 -86
  83. package/main/message-composer/message-composer.spec.js +0 -23
  84. package/main/provider/index.js +0 -16
  85. package/main/provider/provider.js +0 -71
  86. package/main/provider/provider.spec.js +0 -60
  87. package/main/react-query/index.js +0 -16
  88. package/main/react-query/react-query.js +0 -413
  89. package/main/react-query/react-query.spec.js +0 -91
  90. package/main/recoil/index.js +0 -16
  91. package/main/recoil/recoil.js +0 -61
  92. package/main/recoil/recoil.spec.js +0 -20
  93. package/main/types.js +0 -4
  94. package/main/utils/babel.js +0 -282
  95. package/main/utils/babel.spec.js +0 -109
  96. package/main/utils/constants.js +0 -26
  97. package/main/utils/index.js +0 -65
  98. package/main/utils/ref.js +0 -12
  99. package/main/utils/types.js +0 -343
  100. package/module/client/client.js +0 -166
  101. package/module/client/index.js +0 -1
  102. package/module/client/test/ts-client.account-nfts.spec.js +0 -25
  103. package/module/client/test/ts-client.arrays-ref.spec.js +0 -16
  104. package/module/client/test/ts-client.arrays.spec.js +0 -23
  105. package/module/client/test/ts-client.cw-named-groups.test.js +0 -16
  106. package/module/client/test/ts-client.cw-proposal-single.test.js +0 -20
  107. package/module/client/test/ts-client.empty-enums.spec.js +0 -10
  108. package/module/client/test/ts-client.issue-101.spec.js +0 -22
  109. package/module/client/test/ts-client.issue-103.test.js +0 -41
  110. package/module/client/test/ts-client.issue-71.test.js +0 -21
  111. package/module/client/test/ts-client.issue-98.test.js +0 -25
  112. package/module/client/test/ts-client.issues.test.js +0 -21
  113. package/module/client/test/ts-client.overrides.spec.js +0 -30
  114. package/module/client/test/ts-client.sg721.spec.js +0 -16
  115. package/module/client/test/ts-client.spec.js +0 -84
  116. package/module/client/test/ts-client.vectis.spec.js +0 -43
  117. package/module/client/test/ts-client.wager.spec.js +0 -36
  118. package/module/context/context.js +0 -116
  119. package/module/context/imports.js +0 -127
  120. package/module/index.js +0 -9
  121. package/module/message-builder/index.js +0 -1
  122. package/module/message-builder/message-builder.js +0 -52
  123. package/module/message-builder/message-builder.spec.js +0 -17
  124. package/module/message-composer/index.js +0 -1
  125. package/module/message-composer/message-composer.js +0 -70
  126. package/module/message-composer/message-composer.spec.js +0 -20
  127. package/module/provider/index.js +0 -1
  128. package/module/provider/provider.js +0 -45
  129. package/module/provider/provider.spec.js +0 -58
  130. package/module/react-query/index.js +0 -1
  131. package/module/react-query/react-query.js +0 -403
  132. package/module/react-query/react-query.spec.js +0 -85
  133. package/module/recoil/index.js +0 -1
  134. package/module/recoil/recoil.js +0 -46
  135. package/module/recoil/recoil.spec.js +0 -14
  136. package/module/types.js +0 -3
  137. package/module/utils/babel.js +0 -203
  138. package/module/utils/babel.spec.js +0 -70
  139. package/module/utils/constants.js +0 -12
  140. package/module/utils/index.js +0 -6
  141. package/module/utils/ref.js +0 -4
  142. package/module/utils/types.js +0 -301
  143. package/src/client/client.ts +0 -584
  144. package/src/client/index.ts +0 -1
  145. package/src/client/test/__snapshots__/ts-client.account-nfts.spec.ts.snap +0 -497
  146. package/src/client/test/__snapshots__/ts-client.arrays-ref.spec.ts.snap +0 -452
  147. package/src/client/test/__snapshots__/ts-client.arrays.spec.ts.snap +0 -101
  148. package/src/client/test/__snapshots__/ts-client.cw-named-groups.test.ts.snap +0 -141
  149. package/src/client/test/__snapshots__/ts-client.cw-proposal-single.test.ts.snap +0 -341
  150. package/src/client/test/__snapshots__/ts-client.empty-enums.spec.ts.snap +0 -20
  151. package/src/client/test/__snapshots__/ts-client.issue-101.spec.ts.snap +0 -47
  152. package/src/client/test/__snapshots__/ts-client.issue-103.test.ts.snap +0 -79
  153. package/src/client/test/__snapshots__/ts-client.issue-71.test.ts.snap +0 -432
  154. package/src/client/test/__snapshots__/ts-client.issue-98.test.ts.snap +0 -117
  155. package/src/client/test/__snapshots__/ts-client.issues.test.ts.snap +0 -984
  156. package/src/client/test/__snapshots__/ts-client.overrides.spec.ts.snap +0 -709
  157. package/src/client/test/__snapshots__/ts-client.sg721.spec.ts.snap +0 -350
  158. package/src/client/test/__snapshots__/ts-client.spec.ts.snap +0 -723
  159. package/src/client/test/__snapshots__/ts-client.vectis.spec.ts.snap +0 -337
  160. package/src/client/test/__snapshots__/ts-client.wager.spec.ts.snap +0 -132
  161. package/src/client/test/ts-client.account-nfts.spec.ts +0 -55
  162. package/src/client/test/ts-client.arrays-ref.spec.ts +0 -48
  163. package/src/client/test/ts-client.arrays.spec.ts +0 -58
  164. package/src/client/test/ts-client.cw-named-groups.test.ts +0 -48
  165. package/src/client/test/ts-client.cw-proposal-single.test.ts +0 -50
  166. package/src/client/test/ts-client.empty-enums.spec.ts +0 -28
  167. package/src/client/test/ts-client.issue-101.spec.ts +0 -37
  168. package/src/client/test/ts-client.issue-103.test.ts +0 -106
  169. package/src/client/test/ts-client.issue-71.test.ts +0 -51
  170. package/src/client/test/ts-client.issue-98.test.ts +0 -55
  171. package/src/client/test/ts-client.issues.test.ts +0 -52
  172. package/src/client/test/ts-client.overrides.spec.ts +0 -74
  173. package/src/client/test/ts-client.sg721.spec.ts +0 -46
  174. package/src/client/test/ts-client.spec.ts +0 -166
  175. package/src/client/test/ts-client.vectis.spec.ts +0 -97
  176. package/src/client/test/ts-client.wager.spec.ts +0 -63
  177. package/src/context/context.ts +0 -229
  178. package/src/context/imports.ts +0 -195
  179. package/src/context/index.ts +0 -2
  180. package/src/message-builder/__snapshots__/message-builder.spec.ts.snap +0 -270
  181. package/src/message-builder/message-builder.spec.ts +0 -24
  182. package/src/message-builder/message-builder.ts +0 -119
  183. package/src/message-composer/__snapshots__/message-composer.spec.ts.snap +0 -331
  184. package/src/message-composer/index.ts +0 -1
  185. package/src/message-composer/message-composer.spec.ts +0 -46
  186. package/src/message-composer/message-composer.ts +0 -262
  187. package/src/provider/__snapshots__/provider.spec.ts.snap +0 -49
  188. package/src/provider/index.ts +0 -1
  189. package/src/provider/provider.spec.ts +0 -81
  190. package/src/provider/provider.ts +0 -237
  191. package/src/react-query/__snapshots__/react-query.spec.ts.snap +0 -1359
  192. package/src/react-query/index.ts +0 -1
  193. package/src/react-query/react-query.spec.ts +0 -116
  194. package/src/react-query/react-query.ts +0 -1072
  195. package/src/recoil/__snapshots__/recoil.spec.ts.snap +0 -203
  196. package/src/recoil/index.ts +0 -1
  197. package/src/recoil/recoil.spec.ts +0 -38
  198. package/src/recoil/recoil.ts +0 -307
  199. package/src/types.ts +0 -44
  200. package/src/utils/__snapshots__/babel.spec.ts.snap +0 -75
  201. package/src/utils/babel.spec.ts +0 -511
  202. package/src/utils/babel.ts +0 -352
  203. package/src/utils/constants.ts +0 -37
  204. package/src/utils/ref.ts +0 -6
  205. package/src/utils/types.ts +0 -478
  206. package/types/types.d.ts +0 -41
  207. /package/{types/client → client}/index.d.ts +0 -0
  208. /package/{types/context → context}/index.d.ts +0 -0
  209. /package/{src/message-builder/index.ts → esm/message-builder/index.js} +0 -0
  210. /package/{types/message-composer/index.d.ts → esm/message-composer/index.js} +0 -0
  211. /package/{types/provider/index.d.ts → esm/provider/index.js} +0 -0
  212. /package/{types/react-query/index.d.ts → esm/react-query/index.js} +0 -0
  213. /package/{types/recoil/index.d.ts → esm/recoil/index.js} +0 -0
  214. /package/{src/utils/index.ts → esm/utils/index.js} +0 -0
  215. /package/{types/message-builder → message-builder}/index.d.ts +0 -0
  216. /package/{types/provider → provider}/provider.d.ts +0 -0
  217. /package/{types/utils → utils}/constants.d.ts +0 -0
  218. /package/{types/utils → utils}/index.d.ts +0 -0
@@ -1,203 +0,0 @@
1
- import * as t from '@babel/types';
2
- import { snake } from "case";
3
- import { refLookup } from './ref';
4
-
5
- // t.TSPropertySignature - kind?
6
- export const propertySignature = (name, typeAnnotation, optional = false) => {
7
- return {
8
- type: 'TSPropertySignature',
9
- key: t.identifier(name),
10
- kind: 'get',
11
- typeAnnotation,
12
- optional
13
- };
14
- };
15
- export const identifier = (name, typeAnnotation, optional = false) => {
16
- const type = t.identifier(name);
17
- type.typeAnnotation = typeAnnotation;
18
- type.optional = optional;
19
- return type;
20
- };
21
- export const tsTypeOperator = (typeAnnotation, operator) => {
22
- const obj = t.tsTypeOperator(typeAnnotation);
23
- obj.operator = operator;
24
- return obj;
25
- };
26
- export const getMessageProperties = msg => {
27
- let results = [];
28
- let objs = [];
29
- if (msg.anyOf) {
30
- objs = msg.anyOf;
31
- } else if (msg.oneOf) {
32
- objs = msg.oneOf;
33
- } else if (msg.allOf) {
34
- objs = msg.allOf;
35
- }
36
- for (const obj of objs) {
37
- if (obj.properties) {
38
- results.push(obj);
39
- } else {
40
- if (obj.$ref) {
41
- const ref = refLookup(obj.$ref, msg);
42
- const refProps = getMessageProperties(ref);
43
- results = [...results, ...refProps];
44
- }
45
- }
46
- }
47
- return results;
48
- };
49
- export const tsPropertySignature = (key, typeAnnotation, optional) => {
50
- const obj = t.tsPropertySignature(key, typeAnnotation);
51
- obj.optional = optional;
52
- return obj;
53
- };
54
- export const tsObjectPattern = (properties, typeAnnotation) => {
55
- const obj = t.objectPattern(properties);
56
- obj.typeAnnotation = typeAnnotation;
57
- return obj;
58
- };
59
- export const callExpression = (callee, _arguments, typeParameters) => {
60
- const callExpr = t.callExpression(callee, _arguments);
61
- callExpr.typeParameters = typeParameters;
62
- return callExpr;
63
- };
64
- export const bindMethod = name => {
65
- return t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier(name)), t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier(name)), t.identifier('bind')), [t.thisExpression()])));
66
- };
67
- export const typedIdentifier = (name, typeAnnotation, optional = false) => {
68
- const type = t.identifier(name);
69
- type.typeAnnotation = typeAnnotation;
70
- type.optional = optional;
71
- return type;
72
- };
73
- export const promiseTypeAnnotation = name => {
74
- return t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(name))])));
75
- };
76
- export const abstractClassDeclaration = (name, body, implementsExressions = [], superClass = null) => {
77
- const declaration = classDeclaration(name, body, implementsExressions, superClass);
78
- declaration.abstract = true;
79
- return declaration;
80
- };
81
- export const classDeclaration = (name, body, implementsExressions = [], superClass = null) => {
82
- const declaration = t.classDeclaration(t.identifier(name), superClass, t.classBody(body));
83
- if (implementsExressions.length) {
84
- declaration.implements = implementsExressions;
85
- }
86
- return declaration;
87
- };
88
- export const classProperty = (name, typeAnnotation = null, isReadonly = false, isStatic = false, noImplicitOverride = false) => {
89
- const prop = t.classProperty(t.identifier(name));
90
- if (isReadonly) prop.readonly = true;
91
- if (isStatic) prop.static = true;
92
- if (typeAnnotation) prop.typeAnnotation = typeAnnotation;
93
- if (noImplicitOverride) prop.override = true;
94
- return prop;
95
- };
96
- export const arrowFunctionExpression = (params, body, returnType, isAsync = false) => {
97
- const func = t.arrowFunctionExpression(params, body, isAsync);
98
- if (returnType) func.returnType = returnType;
99
- return func;
100
- };
101
- export const recursiveNamespace = (names, moduleBlockBody) => {
102
- if (!names || !names.length) return moduleBlockBody;
103
- const name = names.pop();
104
- const body = [t.exportNamedDeclaration(t.tsModuleDeclaration(t.identifier(name), t.tsModuleBlock(recursiveNamespace(names, moduleBlockBody))))];
105
- return body;
106
- };
107
- export const arrayTypeNDimensions = (body, n) => {
108
- if (!n) return t.tsArrayType(body);
109
- return t.tsArrayType(arrayTypeNDimensions(body, n - 1));
110
- };
111
- export const FieldTypeAsts = {
112
- string: () => {
113
- return t.tsStringKeyword();
114
- },
115
- array: type => {
116
- return t.tsArrayType(FieldTypeAsts[type]());
117
- },
118
- Duration: () => {
119
- return t.tsTypeReference(t.identifier('Duration'));
120
- },
121
- Height: () => {
122
- return t.tsTypeReference(t.identifier('Height'));
123
- },
124
- Coin: () => {
125
- return t.tsTypeReference(t.identifier('Coin'));
126
- },
127
- Long: () => {
128
- return t.tsTypeReference(t.identifier('Long'));
129
- }
130
- };
131
- export const shorthandProperty = prop => {
132
- return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
133
- };
134
- export const importStmt = (names, path) => {
135
- return t.importDeclaration(names.map(name => t.importSpecifier(t.identifier(name), t.identifier(name))), t.stringLiteral(path));
136
- };
137
- export const importAs = (name, importAs, importPath) => {
138
- return t.importDeclaration([t.importSpecifier(t.identifier(importAs), t.identifier(name))], t.stringLiteral(importPath));
139
- };
140
- export const importAminoMsg = () => {
141
- return importStmt(['AminoMsg'], '@cosmjs/amino');
142
- };
143
- export const getFieldDimensionality = field => {
144
- let typeName = field.type;
145
- const isArray = typeName.endsWith('[]');
146
- let dimensions = 0;
147
- if (isArray) {
148
- dimensions = typeName.match(/\[\]/g).length - 1;
149
- typeName = typeName.replace(/\[\]/g, '');
150
- }
151
- return {
152
- typeName,
153
- dimensions,
154
- isArray
155
- };
156
- };
157
- export const memberExpressionOrIdentifier = names => {
158
- if (names.length === 1) {
159
- return t.identifier(names[0]);
160
- }
161
- if (names.length === 2) {
162
- const [b, a] = names;
163
- return t.memberExpression(t.identifier(a), t.identifier(b));
164
- }
165
- const [name, ...rest] = names;
166
- return t.memberExpression(memberExpressionOrIdentifier(rest), t.identifier(name));
167
- };
168
- export const memberExpressionOrIdentifierSnake = names => {
169
- if (names.length === 1) {
170
- return t.identifier(snake(names[0]));
171
- }
172
- if (names.length === 2) {
173
- const [b, a] = names;
174
- return t.memberExpression(t.identifier(snake(a)), t.identifier(snake(b)));
175
- }
176
- const [name, ...rest] = names;
177
- return t.memberExpression(memberExpressionOrIdentifierSnake(rest), t.identifier(snake(name)));
178
- };
179
-
180
- /**
181
- * If optional, return a conditional, otherwise just the expression
182
- */
183
- export const optionalConditionalExpression = (test, expression, alternate, optional = false) => {
184
- return optional ? t.conditionalExpression(test, expression, alternate) : expression;
185
- };
186
- export const typeRefOrUnionWithUndefined = (identifier, optional = false) => {
187
- const typeReference = t.tsTypeReference(identifier);
188
- return optional ? t.tsUnionType([typeReference, t.tsUndefinedKeyword()]) : typeReference;
189
- };
190
- export const parameterizedTypeReference = (identifier, from, omit) => {
191
- return t.tsTypeReference(t.identifier(identifier), t.tsTypeParameterInstantiation([from, typeof omit === 'string' ? t.tsLiteralType(t.stringLiteral(omit)) : t.tsUnionType(omit.map(o => t.tsLiteralType(t.stringLiteral(o))))]));
192
- };
193
-
194
- /**
195
- * omitTypeReference(t.tsTypeReference(t.identifier('Cw4UpdateMembersMutation'),),'args').....
196
- * Omit<Cw4UpdateMembersMutation, 'args'>
197
- */
198
- export const omitTypeReference = (from, omit) => {
199
- return parameterizedTypeReference('Omit', from, omit);
200
- };
201
- export const pickTypeReference = (from, pick) => {
202
- return parameterizedTypeReference('Pick', from, pick);
203
- };
@@ -1,70 +0,0 @@
1
- import { importStmt } from './babel';
2
- import * as t from '@babel/types';
3
- import { bindMethod, typedIdentifier, promiseTypeAnnotation, classDeclaration, classProperty, arrowFunctionExpression } from './babel';
4
- import { expectCode } from '../../test-utils';
5
- it('top import', async () => {
6
- expectCode(importStmt(['CosmWasmClient', 'ExecuteResult', 'SigningCosmWasmClient'], '@cosmjs/cosmwasm-stargate'));
7
- });
8
- it('interfaces', async () => {
9
- expectCode(t.program([t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier('SG721ReadOnlyInstance'), null, [], t.tSInterfaceBody([t.tSPropertySignature(t.identifier('contractAddress'), t.tsTypeAnnotation(t.tsStringKeyword())),
10
- // methods
11
-
12
- t.tSPropertySignature(t.identifier('tokens'), t.tsTypeAnnotation(t.tsFunctionType(null, [typedIdentifier('owner', t.tsTypeAnnotation(t.tsStringKeyword())), typedIdentifier('startAfter', t.tsTypeAnnotation(t.tsStringKeyword()), true), typedIdentifier('limit', t.tsTypeAnnotation(t.tsStringKeyword()), true)], promiseTypeAnnotation('TokensResponse'))))]))),
13
- // extends
14
-
15
- t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier('SG721Instance'), null, [t.tSExpressionWithTypeArguments(t.identifier('SG721ReadOnlyInstance'))], t.tSInterfaceBody([
16
- // contract address
17
-
18
- t.tSPropertySignature(t.identifier('contractAddress'), t.tsTypeAnnotation(t.tsStringKeyword())),
19
- // METHOD
20
- t.tSPropertySignature(t.identifier('mint'), t.tsTypeAnnotation(t.tsFunctionType(null, [typedIdentifier('sender', t.tsTypeAnnotation(t.tsStringKeyword())), typedIdentifier('anotherProp', t.tsTypeAnnotation(t.tsStringKeyword())), typedIdentifier('prop3', t.tsTypeAnnotation(t.tsStringKeyword()))], promiseTypeAnnotation('ExecuteResult'))))])))]));
21
- });
22
- it('readonly classes', async () => {
23
- expectCode(t.program([t.exportNamedDeclaration(classDeclaration('SG721QueryClient', [
24
- // client
25
- classProperty('client', t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CosmWasmClient')))),
26
- // contractAddress
27
- classProperty('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword())),
28
- // constructor
29
- t.classMethod('constructor', t.identifier('constructor'), [typedIdentifier('client', t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CosmWasmClient')))), typedIdentifier('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword()))], t.blockStatement([
30
- // client/contract set
31
- t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('client'))), t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.identifier('contractAddress'))),
32
- // bindings
33
- bindMethod('approval'), bindMethod('otherProp'), bindMethod('hello'), bindMethod('mintme')])),
34
- // methods:
35
- t.classProperty(t.identifier('approval'), arrowFunctionExpression([
36
- // props
37
- typedIdentifier('owner', t.tsTypeAnnotation(t.tsStringKeyword())),
38
- //
39
- typedIdentifier('spender', t.tsTypeAnnotation(t.tsStringKeyword()))], t.blockStatement([t.returnStatement(t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('queryContractSmart')), [t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.objectExpression([
40
- // props
41
- t.objectProperty(t.identifier('owner'), t.identifier('owner'), false, true), t.objectProperty(t.identifier('spender'), t.identifier('spender'), false, true)])]))]), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ApprovalResponse'))]))), true))], [t.tSExpressionWithTypeArguments(t.identifier('SG721ReadOnlyInstance'))]))]));
42
- });
43
- it('mutation classes', async () => {
44
- expectCode(t.program([t.exportNamedDeclaration(classDeclaration('SG721Client', [
45
- // client
46
- classProperty('client', t.tsTypeAnnotation(t.tsTypeReference(t.identifier('SigningCosmWasmClient')))),
47
- // contractAddress
48
- classProperty('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword())),
49
- // constructor
50
- t.classMethod('constructor', t.identifier('constructor'), [typedIdentifier('client', t.tsTypeAnnotation(t.tsTypeReference(t.identifier('SigningCosmWasmClient')))), typedIdentifier('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword()))], t.blockStatement([
51
- // super()
52
- t.expressionStatement(t.callExpression(t.super(), [t.identifier('client'), t.identifier('contractAddress')])),
53
- // client/contract set
54
- t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('client'))), t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.identifier('contractAddress'))),
55
- // bindings
56
- bindMethod('approval'), bindMethod('otherProp'), bindMethod('hello'), bindMethod('mintme')])),
57
- // methods:
58
- t.classProperty(t.identifier('mint'), arrowFunctionExpression([
59
- // props
60
- typedIdentifier('sender', t.tsTypeAnnotation(t.tsStringKeyword())), typedIdentifier('tokenId', t.tsTypeAnnotation(t.tsStringKeyword())), typedIdentifier('owner', t.tsTypeAnnotation(t.tsStringKeyword())), typedIdentifier('token_uri', t.tsTypeAnnotation(t.tsStringKeyword()))], t.blockStatement([t.returnStatement(t.awaitExpression(t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('execute')), [t.identifier('sender'), t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.objectExpression([t.objectProperty(t.identifier('mint'), t.objectExpression([t.objectProperty(t.identifier('token_id'), t.identifier('tokenId')), t.objectProperty(t.identifier('owner'), t.identifier('owner')), t.objectProperty(t.identifier('token_uri'), t.identifier('token_uri')), t.objectProperty(t.identifier('expression'), t.objectExpression([]))]))]), t.stringLiteral('auto')])))]),
61
- // return type
62
- t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true))], [t.tSExpressionWithTypeArguments(t.identifier('SG721ReadOnlyInstance'))], t.identifier('SG721QueryClient')))]));
63
- });
64
- it('object parms', () => {
65
- const obj = t.objectPattern([t.objectProperty(t.identifier('includeExpired'), t.identifier('includeExpired'), false, true), t.objectProperty(t.identifier('tokenId'), t.identifier('tokenId'), false, true)]);
66
- obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral([t.tsPropertySignature(t.identifier('includeExpired'), t.tsTypeAnnotation(t.tsBooleanKeyword())), t.tsPropertySignature(t.identifier('tokenId'), t.tsTypeAnnotation(t.tsStringKeyword()))]));
67
- expectCode(t.program([t.expressionStatement(t.assignmentExpression('=', t.identifier('ownerOf'), arrowFunctionExpression([obj], t.blockStatement([
68
- // body
69
- ], []), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier('OwnerOfResponse'))]))))))]));
70
- });
@@ -1,12 +0,0 @@
1
- import { identifier } from './babel';
2
- import * as t from '@babel/types';
3
- export const OPTIONAL_FUNDS_PARAM = identifier('_funds', t.tsTypeAnnotation(t.tsArrayType(t.tsTypeReference(t.identifier('Coin')))), true);
4
- export const OPTIONAL_FEE_PARAM = identifier('fee', t.tsTypeAnnotation(t.tsUnionType([t.tsNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), true);
5
- export const OPTIONAL_MEMO_PARAM = identifier('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true);
6
- export const FIXED_EXECUTE_PARAMS = [OPTIONAL_FEE_PARAM, OPTIONAL_MEMO_PARAM, OPTIONAL_FUNDS_PARAM];
7
- export const PROVIDER_TYPES = {
8
- SIGNING_CLIENT_TYPE: "client",
9
- QUERY_CLIENT_TYPE: "queryClient",
10
- MESSAGE_COMPOSER_TYPE: 'message-composer',
11
- PROVIDER_TYPE: 'provider'
12
- };
@@ -1,6 +0,0 @@
1
- export * from './babel';
2
- export * from './types';
3
- export * from './ref';
4
- export { OPTIONAL_FUNDS_PARAM } from './constants';
5
- export { FIXED_EXECUTE_PARAMS } from './constants';
6
- export { PROVIDER_TYPES } from './constants';
@@ -1,4 +0,0 @@
1
- export const refLookup = ($ref, schema) => {
2
- const refName = $ref.replace('#/definitions/', '');
3
- return schema.definitions?.[refName];
4
- };
@@ -1,301 +0,0 @@
1
- import * as t from '@babel/types';
2
- import { camel, pascal } from 'case';
3
- import { propertySignature } from './babel';
4
- export function getResponseType(context, underscoreName) {
5
- const methodName = camel(underscoreName);
6
- return pascal(context.contract?.responses?.[underscoreName]?.title ??
7
- // after v1.1 is adopted, we can deprecate this and require the above response
8
- `${methodName}Response`);
9
- }
10
- ;
11
- const getTypeStrFromRef = $ref => {
12
- if ($ref?.startsWith('#/definitions/')) {
13
- return $ref.replace('#/definitions/', '');
14
- }
15
- throw new Error('what is $ref: ' + $ref);
16
- };
17
- export const getTypeFromRef = $ref => {
18
- return t.tsTypeReference(t.identifier(getTypeStrFromRef($ref)));
19
- };
20
- const getArrayTypeFromRef = $ref => {
21
- return t.tsArrayType(getTypeFromRef($ref));
22
- };
23
- const getTypeOrRef = obj => {
24
- if (obj.type) {
25
- return getType(obj.type);
26
- }
27
- if (obj.$ref) {
28
- return getTypeFromRef(obj.$ref);
29
- }
30
- throw new Error('contact maintainers cannot find type for ' + obj);
31
- };
32
- const getArrayTypeFromItems = items => {
33
- // passing in [{"type":"string"}]
34
- if (Array.isArray(items)) {
35
- if (items[0]?.type === 'array') {
36
- return getArrayTypeFromItems(items[0]);
37
- }
38
- return t.tsArrayType(t.tsArrayType(getTypeOrRef(items[0])));
39
- }
40
-
41
- // passing in {"items": [{"type":"string"}]}
42
- const detect = detectType(items.type);
43
- if (detect.type === 'array') {
44
- if (Array.isArray(items.items)) {
45
- return t.tsArrayType(t.tsArrayType(getTypeOrRef(items.items[0])));
46
- } else {
47
- return t.tsArrayType(getArrayTypeFromItems(items.items));
48
- }
49
- }
50
- return t.tsArrayType(getType(detect.type));
51
- };
52
- export const detectType = type => {
53
- let optional = false;
54
- let theType = '';
55
- if (Array.isArray(type)) {
56
- if (type.length !== 2) {
57
- throw new Error('[getType(array length)] case not handled by transpiler. contact maintainers.');
58
- }
59
- const [nullableType, nullType] = type;
60
- if (nullType !== 'null') {
61
- throw new Error('[getType(null)] case not handled by transpiler. contact maintainers.');
62
- }
63
- theType = nullableType;
64
- optional = true;
65
- } else {
66
- theType = type;
67
- }
68
- return {
69
- type: theType,
70
- optional
71
- };
72
- };
73
- export const getTypeInfo = info => {
74
- let type = undefined;
75
- let optional = undefined;
76
- if (Array.isArray(info.anyOf)) {
77
- // assuming 2nd is null, but let's check to ensure
78
- if (info.anyOf.length !== 2) {
79
- throw new Error('case not handled by transpiler. contact maintainers.');
80
- }
81
- const [nullableType, nullType] = info.anyOf;
82
- if (nullType?.type !== 'null') {
83
- throw new Error('[nullableType.type]: case not handled by transpiler. contact maintainers.');
84
- }
85
- if (!nullableType?.$ref) {
86
- if (nullableType.title) {
87
- type = t.tsTypeReference(t.identifier(nullableType.title));
88
- } else {
89
- throw new Error('[nullableType.title] case not handled by transpiler. contact maintainers.');
90
- }
91
- } else {
92
- type = getTypeFromRef(nullableType?.$ref);
93
- }
94
- optional = true;
95
- }
96
- if (typeof info.type === 'string') {
97
- if (info.type === 'array') {
98
- if (typeof info.items === 'object' && !Array.isArray(info.items)) {
99
- if (info.items.$ref) {
100
- type = getArrayTypeFromRef(info.items.$ref);
101
- } else if (info.items.title) {
102
- type = t.tsArrayType(t.tsTypeReference(t.identifier(info.items.title)));
103
- } else if (info.items.type) {
104
- type = getArrayTypeFromItems(info.items);
105
- } else {
106
- throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
107
- }
108
- } else {
109
- if (Array.isArray(info.items)) {
110
- type = getArrayTypeFromItems(info.items);
111
- // console.log(typeof info.items === 'object');
112
- // console.log(Array.isArray(info.items));
113
- // console.log(info);
114
- } else {
115
- throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
116
- }
117
- }
118
- } else {
119
- const detect = detectType(info.type);
120
- type = getType(detect.type);
121
- optional = detect.optional;
122
- }
123
- }
124
- if (Array.isArray(info.type)) {
125
- // assuming 2nd is null, but let's check to ensure
126
- if (info.type.length !== 2) {
127
- throw new Error('please report this to maintainers (field type): ' + JSON.stringify(info, null, 2));
128
- }
129
- const [nullableType, nullType] = info.type;
130
- if (nullType !== 'null') {
131
- throw new Error('please report this to maintainers (field type): ' + JSON.stringify(info, null, 2));
132
- }
133
- if (nullableType === 'array' && typeof info.items === 'object' && !Array.isArray(info.items)) {
134
- if (info.items.type) {
135
- const detect = detectType(info.items.type);
136
- if (detect.type === 'array') {
137
- // wen recursion?
138
- type = t.tsArrayType(getArrayTypeFromItems(info.items));
139
- } else {
140
- type = t.tsArrayType(getType(detect.type));
141
- }
142
- optional = detect.optional;
143
- } else if (info.items.$ref) {
144
- type = getArrayTypeFromRef(info.items.$ref);
145
- // } else if (info.items.title) {
146
- // type = t.tsArrayType(
147
- // t.tsTypeReference(
148
- // t.identifier(info.items.title)
149
- // )
150
- // );
151
- } else if (info.items.type) {
152
- type = getArrayTypeFromItems(info.items);
153
- } else {
154
- throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
155
- }
156
- } else {
157
- const detect = detectType(nullableType);
158
- optional = detect.optional;
159
- if (detect.type === 'array') {
160
- type = getArrayTypeFromItems(info.items);
161
- } else {
162
- type = getType(detect.type);
163
- }
164
- }
165
- optional = true;
166
- }
167
- return {
168
- type,
169
- optional
170
- };
171
- };
172
- export const getType = type => {
173
- switch (type) {
174
- case 'string':
175
- return t.tsStringKeyword();
176
- case 'boolean':
177
- return t.tSBooleanKeyword();
178
- case 'integer':
179
- return t.tsNumberKeyword();
180
- default:
181
- throw new Error('contact maintainers [unknown type]: ' + type);
182
- }
183
- };
184
- export const getPropertyType = (context, schema, prop) => {
185
- const props = schema.properties ?? {};
186
- let info = props[prop];
187
- let type = null;
188
- let optional = !schema.required?.includes(prop);
189
- if (info.allOf && info.allOf.length === 1) {
190
- info = info.allOf[0];
191
- }
192
- if (typeof info.$ref === 'string') {
193
- type = getTypeFromRef(info.$ref);
194
- }
195
- const typeInfo = getTypeInfo(info);
196
- if (typeof typeInfo.optional !== 'undefined') {
197
- optional = typeInfo.optional;
198
- }
199
- if (typeof typeInfo.type !== 'undefined') {
200
- type = typeInfo.type;
201
- }
202
- if (!type) {
203
- throw new Error('cannot find type for ' + JSON.stringify(info));
204
- }
205
- if (schema.required?.includes(prop)) {
206
- optional = false;
207
- }
208
- return {
209
- type,
210
- optional
211
- };
212
- };
213
- export function getPropertySignatureFromProp(context, jsonschema, prop, camelize) {
214
- if (jsonschema.properties[prop].type === 'object') {
215
- if (jsonschema.properties[prop].title) {
216
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(jsonschema.properties[prop].title))));
217
- } else {
218
- throw new Error('getPropertySignatureFromProp() contact maintainer');
219
- }
220
- }
221
- if (Array.isArray(jsonschema.properties[prop].allOf)) {
222
- const isOptional = !jsonschema.required?.includes(prop);
223
- const unionTypes = jsonschema.properties[prop].allOf.map(el => {
224
- if (el.title) return el.title;
225
- if (el.$ref) return getTypeStrFromRef(el.$ref);
226
- return el.type;
227
- });
228
- // @ts-ignore:next-line
229
- const uniqUnionTypes = [...new Set(unionTypes)];
230
- if (uniqUnionTypes.length === 1) {
231
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(uniqUnionTypes[0]))), isOptional);
232
- } else {
233
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(typ))))), isOptional);
234
- }
235
- } else if (Array.isArray(jsonschema.properties[prop].oneOf)) {
236
- const isOptional = !jsonschema.required?.includes(prop);
237
- const unionTypes = jsonschema.properties[prop].oneOf.map(el => {
238
- if (el.title) return el.title;
239
- if (el.$ref) return getTypeStrFromRef(el.$ref);
240
- return el.type;
241
- });
242
- // @ts-ignore:next-line
243
- const uniqUnionTypes = [...new Set(unionTypes)];
244
- if (uniqUnionTypes.length === 1) {
245
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(uniqUnionTypes[0]))), isOptional);
246
- } else {
247
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(typ))))), isOptional);
248
- }
249
- }
250
- try {
251
- getPropertyType(context, jsonschema, prop);
252
- } catch (e) {
253
- console.log(e);
254
- console.log(JSON.stringify(jsonschema, null, 2), prop);
255
- }
256
- const {
257
- type,
258
- optional
259
- } = getPropertyType(context, jsonschema, prop);
260
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
261
- }
262
- export const getParamsTypeAnnotation = (context, jsonschema, camelize = true) => {
263
- const keys = Object.keys(jsonschema.properties ?? {});
264
- if (!keys.length && jsonschema.$ref) {
265
- return t.tsTypeAnnotation(getTypeFromRef(jsonschema.$ref));
266
- }
267
- if (!keys.length) return undefined;
268
- const typedParams = keys.map(prop => getPropertySignatureFromProp(context, jsonschema, prop, camelize));
269
- return t.tsTypeAnnotation(t.tsTypeLiteral(
270
- // @ts-ignore:next-line
271
- [...typedParams]));
272
- };
273
- export const createTypedObjectParams = (context, jsonschema, camelize = true) => {
274
- const keys = Object.keys(jsonschema.properties ?? {});
275
- if (!keys.length) {
276
- // is there a ref?
277
- if (jsonschema.$ref) {
278
- const obj = context.refLookup(jsonschema.$ref);
279
- // If there is a oneOf, then we need to create a type for it
280
- if (obj?.oneOf) {
281
- // the actual type of the ref
282
- const refType = jsonschema.$ref.split('/').pop();
283
- const refName = camel(refType);
284
- const id = t.identifier(refName);
285
- id.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier(refType)));
286
- return id;
287
- } else if (obj) {
288
- return createTypedObjectParams(context, obj, camelize);
289
- }
290
- }
291
-
292
- // no results...
293
- return;
294
- }
295
- const params = keys.map(prop => {
296
- return t.objectProperty(camelize ? t.identifier(camel(prop)) : t.identifier(prop), camelize ? t.identifier(camel(prop)) : t.identifier(prop), false, true);
297
- });
298
- const obj = t.objectPattern([...params]);
299
- obj.typeAnnotation = getParamsTypeAnnotation(context, jsonschema, camelize);
300
- return obj;
301
- };