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,1072 +0,0 @@
1
- import type { Expression } from '@babel/types';
2
- import * as t from '@babel/types';
3
- import { camel, pascal } from 'case';
4
- import { ExecuteMsg, QueryMsg } from '../types';
5
- import {
6
- callExpression,
7
- createTypedObjectParams,
8
- FIXED_EXECUTE_PARAMS,
9
- getMessageProperties,
10
- identifier,
11
- OPTIONAL_FUNDS_PARAM,
12
- tsObjectPattern,
13
- tsPropertySignature
14
- } from '../utils';
15
-
16
- import {
17
- omitTypeReference,
18
- optionalConditionalExpression,
19
- propertySignature,
20
- shorthandProperty
21
- } from '../utils/babel';
22
- import {
23
- getParamsTypeAnnotation,
24
- getPropertyType,
25
- getResponseType
26
- } from '../utils/types';
27
- import { ReactQueryOptions, RenderContext } from '../context';
28
- import { JSONSchema } from '../types';
29
- import { ArrowFunctionExpression, objectExpression } from '@babel/types';
30
- import { OPTIONAL_FEE_PARAM, OPTIONAL_MEMO_PARAM } from '../utils/constants';
31
-
32
- interface ReactQueryHookQuery {
33
- context: RenderContext;
34
- hookName: string;
35
- hookParamsTypeName: string;
36
- hookKeyName: string;
37
- queryKeysName: string;
38
- responseType: string;
39
- methodName: string;
40
- jsonschema: any;
41
- }
42
-
43
- interface ParsedQueryMsg {
44
- underscoreName: string;
45
- methodName: string;
46
- hookParamsTypeName: string;
47
- hookName: string;
48
- responseType: string;
49
- getterKey: string;
50
- jsonschema: any;
51
- // jsonschema: JSONSchema;
52
- }
53
-
54
- interface ReactQueryHooks {
55
- context: RenderContext;
56
- queryMsg: QueryMsg;
57
- contractName: string;
58
- QueryClient: string;
59
- }
60
-
61
- export const createReactQueryHooks = ({
62
- context,
63
- queryMsg,
64
- contractName,
65
- QueryClient
66
- }: ReactQueryHooks) => {
67
- const options = context.options.reactQuery;
68
-
69
- const genericQueryInterfaceName = `${pascal(contractName)}ReactQuery`;
70
- const underscoreNames: string[] = getMessageProperties(queryMsg).map(
71
- (schema) => Object.keys(schema.properties)[0]
72
- );
73
-
74
- const body = [];
75
-
76
- const queryKeysName = `${camel(contractName)}QueryKeys`;
77
- if (options.queryKeys) {
78
- body.push(
79
- createReactQueryKeys({
80
- context,
81
- queryKeysName,
82
- camelContractName: camel(contractName),
83
- underscoreNames
84
- })
85
- );
86
- }
87
-
88
- const queryMsgs: ParsedQueryMsg[] = getMessageProperties(queryMsg).map(
89
- (schema) => {
90
- // list_voters
91
- const underscoreName = Object.keys(schema.properties)[0];
92
- // listVoters
93
- const methodName = camel(underscoreName);
94
- // Cw3FlexMultisigListVotersQuery
95
- const hookParamsTypeName = `${pascal(contractName)}${pascal(
96
- methodName
97
- )}Query`;
98
- // useCw3FlexMultisigListVotersQuery
99
- const hookName = `use${hookParamsTypeName}`;
100
- // listVotersResponse
101
- const responseType = getResponseType(context, underscoreName);
102
- // cw3FlexMultisigListVoters
103
- const getterKey = camel(`${contractName}${pascal(methodName)}`);
104
- const jsonschema = schema.properties[underscoreName];
105
-
106
- return {
107
- underscoreName,
108
- methodName,
109
- hookParamsTypeName,
110
- hookName,
111
- responseType,
112
- getterKey,
113
- jsonschema
114
- };
115
- }
116
- );
117
-
118
- const queryFactoryName = `${camel(contractName)}Queries`;
119
- if (options.queryFactory) {
120
- body.push(
121
- createReactQueryFactory({
122
- context,
123
- queryFactoryName,
124
- queryKeysName,
125
- queryMsgs
126
- })
127
- );
128
- }
129
-
130
- body.push(
131
- createReactQueryHookGenericInterface({
132
- context,
133
- QueryClient,
134
- genericQueryInterfaceName
135
- })
136
- );
137
-
138
- body.push(
139
- ...queryMsgs.reduce(
140
- (
141
- m,
142
- {
143
- methodName,
144
- hookParamsTypeName,
145
- hookName,
146
- responseType,
147
- getterKey,
148
- jsonschema
149
- }
150
- ) => {
151
- return [
152
- createReactQueryHookInterface({
153
- context,
154
- hookParamsTypeName,
155
- responseType,
156
- queryInterfaceName: genericQueryInterfaceName,
157
- QueryClient,
158
- jsonschema
159
- }),
160
- createReactQueryHook({
161
- context,
162
- methodName,
163
- hookName,
164
- hookParamsTypeName,
165
- queryKeysName,
166
- responseType,
167
- hookKeyName: getterKey,
168
- jsonschema
169
- }),
170
- ...m
171
- ];
172
- },
173
- []
174
- )
175
- );
176
-
177
- return body;
178
- };
179
-
180
- function buildQueryFn(
181
- methodName: string,
182
- jsonschema: any,
183
- options: ReactQueryOptions
184
- ): ArrowFunctionExpression {
185
- const keys = Object.keys(jsonschema.properties ?? {});
186
- let args = [];
187
- if (keys.length) {
188
- args = [
189
- t.objectExpression([
190
- ...keys.map((prop) => {
191
- return t.objectProperty(
192
- t.identifier(camel(prop)),
193
- t.memberExpression(t.identifier('args'), t.identifier(camel(prop)))
194
- );
195
- })
196
- ])
197
- ];
198
- }
199
-
200
- const rejectInvalidClient = t.callExpression(
201
- t.memberExpression(t.identifier('Promise'), t.identifier('reject')),
202
- [
203
- t.newExpression(t.identifier('Error'), [
204
- t.stringLiteral('Invalid client')
205
- ])
206
- ]
207
- );
208
-
209
- return t.arrowFunctionExpression(
210
- [],
211
- optionalConditionalExpression(
212
- t.identifier('client'),
213
- t.callExpression(
214
- t.memberExpression(t.identifier('client'), t.identifier(methodName)),
215
- args
216
- ),
217
- rejectInvalidClient,
218
- options.optionalClient
219
- ),
220
- false
221
- );
222
- }
223
-
224
- const ENABLED_QUERY_OPTION = t.objectProperty(
225
- t.identifier('enabled'),
226
- t.logicalExpression(
227
- '&&',
228
- t.unaryExpression('!', t.unaryExpression('!', t.identifier('client'))),
229
- t.conditionalExpression(
230
- // explicitly check for undefined
231
- t.binaryExpression(
232
- '!=',
233
- t.optionalMemberExpression(
234
- t.identifier('options'),
235
- t.identifier('enabled'),
236
- false,
237
- true
238
- ),
239
- t.identifier('undefined')
240
- ),
241
- t.memberExpression(t.identifier('options'), t.identifier('enabled')),
242
- t.booleanLiteral(true)
243
- )
244
- )
245
- );
246
-
247
- function buildQueryOptions(options: ReactQueryOptions) {
248
- return options.optionalClient
249
- ? t.objectExpression([
250
- t.spreadElement(t.identifier('options')),
251
- t.objectProperty(
252
- t.identifier('enabled'),
253
- t.logicalExpression(
254
- '&&',
255
- t.unaryExpression(
256
- '!',
257
- t.unaryExpression('!', t.identifier('client'))
258
- ),
259
- t.conditionalExpression(
260
- // explicitly check for undefined
261
- t.binaryExpression(
262
- '!=',
263
- t.optionalMemberExpression(
264
- t.identifier('options'),
265
- t.identifier('enabled'),
266
- false,
267
- true
268
- ),
269
- t.identifier('undefined')
270
- ),
271
- t.memberExpression(
272
- t.identifier('options'),
273
- t.identifier('enabled')
274
- ),
275
- t.booleanLiteral(true)
276
- )
277
- )
278
- )
279
- ])
280
- : t.identifier('options');
281
- }
282
-
283
- export const createReactQueryHook = ({
284
- context,
285
- hookName,
286
- hookParamsTypeName,
287
- responseType,
288
- hookKeyName,
289
- queryKeysName,
290
- methodName,
291
- jsonschema
292
- }: ReactQueryHookQuery) => {
293
- context.addUtil('useQuery');
294
- context.addUtil('UseQueryOptions');
295
-
296
- const options = context.options.reactQuery;
297
- const keys = Object.keys(jsonschema.properties ?? {});
298
-
299
- let props = ['client', 'options'];
300
- if (keys.length) {
301
- props = ['client', 'args', 'options'];
302
- }
303
-
304
- const selectResponseGenericTypeName = GENERIC_SELECT_RESPONSE_NAME;
305
-
306
- const queryFunctionDeclaration = t.functionDeclaration(
307
- t.identifier(hookName),
308
- [
309
- tsObjectPattern(
310
- [
311
- ...props.map((prop) => {
312
- return t.objectProperty(
313
- t.identifier(prop),
314
- t.identifier(prop),
315
- false,
316
- true
317
- );
318
- })
319
- ],
320
- t.tsTypeAnnotation(
321
- t.tsTypeReference(
322
- t.identifier(hookParamsTypeName),
323
- t.tsTypeParameterInstantiation([
324
- t.tsTypeReference(t.identifier(selectResponseGenericTypeName))
325
- ])
326
- )
327
- )
328
- )
329
- ],
330
- t.blockStatement([
331
- t.returnStatement(
332
- callExpression(
333
- t.identifier('useQuery'),
334
- [
335
- generateUseQueryQueryKey({
336
- hookKeyName,
337
- queryKeysName,
338
- methodName,
339
- props,
340
- options
341
- }),
342
- buildQueryFn(methodName, jsonschema, options),
343
- buildQueryOptions(options)
344
- ],
345
- t.tsTypeParameterInstantiation([
346
- t.tsTypeReference(t.identifier(responseType)),
347
- t.tsTypeReference(t.identifier('Error')),
348
- t.tsTypeReference(t.identifier(selectResponseGenericTypeName))
349
- ])
350
- )
351
- )
352
- ])
353
- );
354
-
355
- // Add the TData type parameters
356
- queryFunctionDeclaration.typeParameters = t.tsTypeParameterDeclaration([
357
- t.tsTypeParameter(
358
- undefined,
359
- t.tSTypeReference(t.identifier(responseType)),
360
- selectResponseGenericTypeName
361
- )
362
- ]);
363
-
364
- return t.exportNamedDeclaration(queryFunctionDeclaration);
365
- };
366
-
367
- interface ReactQueryMutationHookInterface {
368
- context: RenderContext;
369
- ExecuteClient: string;
370
- mutationHookParamsTypeName: string;
371
- jsonschema: JSONSchema;
372
- useMutationTypeParameter: t.TSTypeParameterInstantiation;
373
- }
374
-
375
- /**
376
- * Example:
377
- ```
378
- export interface Cw721RevokeMutation {
379
- client: Cw721Client;
380
- msg: {
381
- spender: string;
382
- tokenId: string;
383
- };
384
- args?: {
385
- fee?: number | StdFee | "auto";
386
- memo?: string;
387
- funds?: Coin[];
388
- };
389
- }
390
- ```
391
- */
392
- export const createReactQueryMutationArgsInterface = ({
393
- context,
394
- ExecuteClient,
395
- mutationHookParamsTypeName,
396
- useMutationTypeParameter,
397
- jsonschema
398
- }: ReactQueryMutationHookInterface) => {
399
- const typedUseMutationOptions = t.tsTypeReference(
400
- t.identifier('UseMutationOptions'),
401
- useMutationTypeParameter
402
- );
403
-
404
- const body = [
405
- tsPropertySignature(
406
- t.identifier('client'),
407
- t.tsTypeAnnotation(t.tsTypeReference(t.identifier(ExecuteClient))),
408
- false
409
- )
410
- ];
411
-
412
- const msgType = createTypedObjectParams(context, jsonschema)?.typeAnnotation;
413
-
414
- if (msgType) {
415
- body.push(
416
- t.tsPropertySignature(
417
- t.identifier('msg'),
418
- // @ts-ignore
419
- msgType
420
- )
421
- );
422
- }
423
-
424
- context.addUtil('StdFee');
425
- context.addUtil('Coin');
426
-
427
- const optionalArgs = t.tsPropertySignature(
428
- t.identifier('args'),
429
- t.tsTypeAnnotation(
430
- // @ts-ignore:next-line
431
- t.tsTypeLiteral([
432
- propertySignature('fee', OPTIONAL_FEE_PARAM.typeAnnotation as t.TSTypeAnnotation, true),
433
- propertySignature('memo', OPTIONAL_MEMO_PARAM.typeAnnotation as t.TSTypeAnnotation, true),
434
- propertySignature('funds', OPTIONAL_FUNDS_PARAM.typeAnnotation as t.TSTypeAnnotation, true)
435
- ])
436
- )
437
- );
438
-
439
- optionalArgs.optional = true;
440
-
441
- body.push(optionalArgs);
442
-
443
- return t.exportNamedDeclaration(
444
- t.tsInterfaceDeclaration(
445
- t.identifier(mutationHookParamsTypeName),
446
- null,
447
- [],
448
- t.tsInterfaceBody(body)
449
- )
450
- );
451
- };
452
-
453
- interface ReactQueryMutationHooks {
454
- context: RenderContext;
455
- execMsg: ExecuteMsg;
456
- contractName: string;
457
- ExecuteClient: string;
458
- }
459
-
460
- export const createReactQueryMutationHooks = ({
461
- context,
462
- execMsg,
463
- contractName,
464
- ExecuteClient
465
- }: ReactQueryMutationHooks) => {
466
- // merge the user options with the defaults
467
- return getMessageProperties(execMsg).reduce((m, schema) => {
468
- // update_members
469
- const execMethodUnderscoreName = Object.keys(schema.properties)[0];
470
- // updateMembers
471
- const execMethodName = camel(execMethodUnderscoreName);
472
- // Cw20UpdateMembersMutation
473
- const mutationHookParamsTypeName = `${pascal(contractName)}${pascal(
474
- execMethodName
475
- )}Mutation`;
476
- // useCw20UpdateMembersMutation
477
- const mutationHookName = `use${mutationHookParamsTypeName}`;
478
-
479
- const jsonschema = schema.properties[execMethodUnderscoreName];
480
-
481
- const properties = jsonschema.properties ?? {};
482
-
483
- // TODO: there should be a better way to do this
484
- const hasMsg = !!(Object.keys(properties)?.length || jsonschema?.$ref);
485
-
486
- // <ExecuteResult, Error, Cw4UpdateMembersMutation>
487
- const useMutationTypeParameter = generateMutationTypeParameter(
488
- context,
489
- mutationHookParamsTypeName
490
- );
491
-
492
- return [
493
- createReactQueryMutationArgsInterface({
494
- context,
495
- mutationHookParamsTypeName,
496
- ExecuteClient,
497
- jsonschema,
498
- useMutationTypeParameter
499
- }),
500
- createReactQueryMutationHook({
501
- context,
502
- execMethodName,
503
- mutationHookName,
504
- mutationHookParamsTypeName,
505
- hasMsg,
506
- useMutationTypeParameter
507
- }),
508
- ...m
509
- ];
510
- }, []);
511
- };
512
-
513
- /**
514
- * Generates the mutation type parameter. If args exist, we use a pick. If not, we just return the params type.
515
- */
516
- const generateMutationTypeParameter = (
517
- context: RenderContext,
518
- mutationHookParamsTypeName: string
519
- ) => {
520
- context.addUtil('ExecuteResult');
521
-
522
- return t.tsTypeParameterInstantiation([
523
- // Data
524
- t.tSTypeReference(t.identifier('ExecuteResult')),
525
- // Error
526
- t.tsTypeReference(t.identifier('Error')),
527
- // Variables
528
- t.tsTypeReference(t.identifier(mutationHookParamsTypeName))
529
- ]);
530
- };
531
-
532
- interface ReactQueryMutationHook {
533
- context: RenderContext;
534
- mutationHookName: string;
535
- mutationHookParamsTypeName: string;
536
- execMethodName: string;
537
- useMutationTypeParameter: t.TSTypeParameterInstantiation;
538
- hasMsg: boolean;
539
- }
540
-
541
- /**
542
- *
543
- * Example:
544
- ```
545
- export const useCw4UpdateMembersMutation = ({ client, options }: Omit<Cw4UpdateMembersMutation, 'args'>) =>
546
- useMutation<ExecuteResult, Error, Pick<Cw4UpdateMembersMutation, 'args'>>(
547
- ({ args }) => client.updateMembers(args),
548
- options
549
- )
550
- ```
551
- */
552
- export const createReactQueryMutationHook = ({
553
- context,
554
- mutationHookName,
555
- mutationHookParamsTypeName,
556
- execMethodName,
557
- useMutationTypeParameter,
558
- hasMsg
559
- }: ReactQueryMutationHook) => {
560
- context.addUtil('useMutation');
561
- context.addUtil('UseMutationOptions');
562
-
563
- const useMutationFunctionArgs = [shorthandProperty('client')];
564
- if (hasMsg) useMutationFunctionArgs.push(shorthandProperty('msg'));
565
- useMutationFunctionArgs.push(
566
- t.objectProperty(
567
- t.identifier('args'),
568
- t.assignmentPattern(
569
- t.objectPattern([
570
- shorthandProperty('fee'),
571
- shorthandProperty('memo'),
572
- shorthandProperty('funds')
573
- ]),
574
- t.objectExpression([])
575
- )
576
- )
577
- );
578
-
579
- return t.exportNamedDeclaration(
580
- t.functionDeclaration(
581
- t.identifier(mutationHookName),
582
- [
583
- identifier(
584
- 'options',
585
- t.tsTypeAnnotation(
586
- omitTypeReference(
587
- t.tsTypeReference(
588
- t.identifier('UseMutationOptions'),
589
- useMutationTypeParameter
590
- ),
591
- 'mutationFn'
592
- )
593
- ),
594
- true
595
- )
596
- ],
597
- t.blockStatement([
598
- t.returnStatement(
599
- callExpression(
600
- t.identifier('useMutation'),
601
- [
602
- t.arrowFunctionExpression(
603
- [t.objectPattern(useMutationFunctionArgs)],
604
- t.callExpression(
605
- t.memberExpression(
606
- t.identifier('client'),
607
- t.identifier(execMethodName)
608
- ),
609
- (hasMsg ? [t.identifier('msg')] : []).concat([
610
- t.identifier('fee'),
611
- t.identifier('memo'),
612
- t.identifier('funds')
613
- ])
614
- ),
615
- false // not async
616
- ),
617
- t.identifier('options')
618
- ],
619
- useMutationTypeParameter
620
- )
621
- )
622
- ])
623
- )
624
- );
625
- };
626
-
627
- function createReactQueryKeys({
628
- context,
629
- queryKeysName,
630
- camelContractName,
631
- underscoreNames
632
- }: {
633
- context: RenderContext;
634
- queryKeysName: string;
635
- camelContractName: string;
636
- underscoreNames: string[];
637
- }) {
638
- const options = context.options.reactQuery;
639
-
640
- const contractAddressTypeAnnotation = t.tsTypeAnnotation(
641
- options.optionalClient
642
- ? t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])
643
- : t.tSStringKeyword()
644
- );
645
-
646
- return t.exportNamedDeclaration(
647
- t.variableDeclaration('const', [
648
- t.variableDeclarator(
649
- t.identifier(queryKeysName),
650
- t.objectExpression([
651
- // 1: contract
652
- t.objectProperty(
653
- t.identifier('contract'),
654
- t.tSAsExpression(
655
- t.arrayExpression([
656
- t.objectExpression([
657
- t.objectProperty(
658
- t.identifier('contract'),
659
- t.stringLiteral(camelContractName)
660
- )
661
- ])
662
- ]),
663
- t.tSTypeReference(t.identifier('const'))
664
- )
665
- ),
666
- // 2: address
667
- t.objectProperty(
668
- t.identifier('address'),
669
- t.arrowFunctionExpression(
670
- [identifier('contractAddress', contractAddressTypeAnnotation)],
671
- t.tSAsExpression(
672
- t.arrayExpression([
673
- t.objectExpression([
674
- // 1
675
- t.spreadElement(
676
- t.memberExpression(
677
- t.memberExpression(
678
- t.identifier(queryKeysName),
679
- t.identifier('contract')
680
- ),
681
- t.numericLiteral(0),
682
- true // computed
683
- )
684
- ),
685
- t.objectProperty(
686
- t.identifier('address'),
687
- t.identifier('contractAddress')
688
- )
689
- ])
690
- ]),
691
- t.tSTypeReference(t.identifier('const'))
692
- )
693
- )
694
- ),
695
- // 3: methods
696
- ...underscoreNames.map((underscoreMethodName) =>
697
- t.objectProperty(
698
- // key id is the camel method name
699
- t.identifier(camel(underscoreMethodName)),
700
- t.arrowFunctionExpression(
701
- [
702
- identifier('contractAddress', contractAddressTypeAnnotation),
703
- identifier(
704
- 'args',
705
- // Record<string, unknown>
706
- t.tSTypeAnnotation(
707
- t.tsTypeReference(
708
- t.identifier('Record'),
709
- t.tsTypeParameterInstantiation([
710
- t.tsStringKeyword(),
711
- t.tsUnknownKeyword()
712
- ])
713
- )
714
- ),
715
- true // optional
716
- )
717
- ],
718
- t.tSAsExpression(
719
- t.arrayExpression([
720
- t.objectExpression([
721
- //...cw3FlexMultisigQueryKeys.address(contractAddress)[0]
722
- t.spreadElement(
723
- t.memberExpression(
724
- t.callExpression(
725
- t.memberExpression(
726
- t.identifier(queryKeysName),
727
- t.identifier('address')
728
- ),
729
- [t.identifier('contractAddress')]
730
- ),
731
- t.numericLiteral(0),
732
- true // computed
733
- )
734
- ),
735
- // method: list_voters
736
- t.objectProperty(
737
- t.identifier('method'),
738
- t.stringLiteral(underscoreMethodName)
739
- ),
740
- // args
741
- shorthandProperty('args')
742
- ])
743
- ]),
744
- t.tSTypeReference(t.identifier('const'))
745
- )
746
- )
747
- )
748
- )
749
- ])
750
- )
751
- ])
752
- );
753
- }
754
-
755
- function createReactQueryFactory({
756
- context,
757
- queryFactoryName,
758
- queryKeysName,
759
- queryMsgs
760
- }: {
761
- context: RenderContext;
762
- queryFactoryName: string;
763
- queryKeysName: string;
764
- queryMsgs: ParsedQueryMsg[];
765
- }) {
766
- const options = context.options.reactQuery;
767
-
768
- return t.exportNamedDeclaration(
769
- t.variableDeclaration('const', [
770
- t.variableDeclarator(
771
- t.identifier(queryFactoryName),
772
- t.objectExpression([
773
- ...queryMsgs.map(
774
- ({ methodName, hookParamsTypeName, responseType, jsonschema }) => {
775
- const hasArgs =
776
- Object.keys(jsonschema.properties ?? {}).length > 0;
777
-
778
- const methodQueryOptionsFn = t.arrowFunctionExpression(
779
- [
780
- tsObjectPattern(
781
- [
782
- shorthandProperty('client'),
783
- ...(hasArgs ? [shorthandProperty('args')] : []),
784
- shorthandProperty('options')
785
- ],
786
- t.tsTypeAnnotation(
787
- t.tsTypeReference(
788
- t.identifier(hookParamsTypeName),
789
- t.tsTypeParameterInstantiation([
790
- t.tsTypeReference(
791
- t.identifier(GENERIC_SELECT_RESPONSE_NAME)
792
- )
793
- ])
794
- )
795
- )
796
- )
797
- ],
798
- t.objectExpression([
799
- // 1: queryKey
800
- t.objectProperty(
801
- t.identifier('queryKey'),
802
- t.callExpression(
803
- t.memberExpression(
804
- t.identifier(queryKeysName),
805
- t.identifier(methodName)
806
- ),
807
- [
808
- t.optionalMemberExpression(
809
- t.identifier('client'),
810
- t.identifier('contractAddress'),
811
- false,
812
- true
813
- ),
814
- ...(hasArgs ? [t.identifier('args')] : [])
815
- ]
816
- )
817
- ),
818
- // 2: queryFn
819
- t.objectProperty(
820
- t.identifier('queryFn'),
821
- buildQueryFn(methodName, jsonschema, options)
822
- ),
823
- // 3: spread options
824
- t.spreadElement(t.identifier('options')),
825
- // 4. enabled
826
- ENABLED_QUERY_OPTION
827
- ])
828
- );
829
-
830
- methodQueryOptionsFn.typeParameters =
831
- t.tsTypeParameterDeclaration([
832
- t.tsTypeParameter(
833
- undefined,
834
- t.tsTypeReference(t.identifier(responseType)),
835
- GENERIC_SELECT_RESPONSE_NAME
836
- )
837
- ]);
838
-
839
- methodQueryOptionsFn.returnType = t.tsTypeAnnotation(
840
- t.tsTypeReference(
841
- t.identifier('UseQueryOptions'),
842
- t.tsTypeParameterInstantiation([
843
- t.tsTypeReference(t.identifier(responseType)),
844
- t.tsTypeReference(t.identifier('Error')),
845
- t.tsTypeReference(
846
- t.identifier(GENERIC_SELECT_RESPONSE_NAME)
847
- )
848
- ])
849
- )
850
- );
851
-
852
- return t.objectProperty(
853
- // key id is the camel method name
854
- t.identifier(camel(methodName)),
855
- methodQueryOptionsFn
856
- );
857
- }
858
- )
859
- ])
860
- )
861
- ])
862
- );
863
- }
864
-
865
- interface ReactQueryHookGenericInterface {
866
- context: RenderContext;
867
- QueryClient: string;
868
- genericQueryInterfaceName: string;
869
- }
870
-
871
- const GENERIC_SELECT_RESPONSE_NAME = 'TData';
872
-
873
- function createReactQueryHookGenericInterface({
874
- context,
875
- QueryClient,
876
- genericQueryInterfaceName
877
- }: ReactQueryHookGenericInterface) {
878
- const options = context.options.reactQuery;
879
- const genericResponseTypeName = 'TResponse';
880
-
881
- context.addUtil('UseQueryOptions');
882
-
883
- // UseQueryOptions<TResponse, Error, TData>,
884
- const typedUseQueryOptions = t.tsTypeReference(
885
- t.identifier('UseQueryOptions'),
886
- t.tsTypeParameterInstantiation([
887
- t.tsTypeReference(t.identifier(genericResponseTypeName)),
888
- t.tsTypeReference(t.identifier('Error')),
889
- t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))
890
- ])
891
- );
892
-
893
- const body = [
894
- tsPropertySignature(
895
- t.identifier('client'),
896
- t.tsTypeAnnotation(
897
- options.optionalClient
898
- ? t.tsUnionType([
899
- t.tsTypeReference(t.identifier(QueryClient)),
900
- t.tsUndefinedKeyword()
901
- ])
902
- : t.tsTypeReference(t.identifier(QueryClient))
903
- ),
904
- false
905
- ),
906
- tsPropertySignature(
907
- t.identifier('options'),
908
- t.tsTypeAnnotation(
909
- options.version === 'v4'
910
- ? t.tSIntersectionType([
911
- omitTypeReference(
912
- typedUseQueryOptions,
913
- "'queryKey' | 'queryFn' | 'initialData'"
914
- ),
915
- t.tSTypeLiteral([
916
- t.tsPropertySignature(
917
- t.identifier('initialData?'),
918
- t.tsTypeAnnotation(t.tsUndefinedKeyword())
919
- )
920
- ])
921
- ])
922
- : typedUseQueryOptions
923
- ),
924
- true
925
- )
926
- ];
927
-
928
- return t.exportNamedDeclaration(
929
- t.tsInterfaceDeclaration(
930
- t.identifier(genericQueryInterfaceName),
931
- t.tsTypeParameterDeclaration([
932
- // 1: TResponse
933
- t.tsTypeParameter(undefined, undefined, genericResponseTypeName),
934
- // 2: TData
935
- t.tsTypeParameter(
936
- undefined,
937
- t.tSTypeReference(t.identifier(genericResponseTypeName)),
938
- GENERIC_SELECT_RESPONSE_NAME
939
- )
940
- ]),
941
- [],
942
- t.tSInterfaceBody(body)
943
- )
944
- );
945
- }
946
-
947
- interface ReactQueryHookQueryInterface {
948
- context: RenderContext;
949
- QueryClient: string;
950
- hookParamsTypeName: string;
951
- queryInterfaceName: string;
952
- responseType: string;
953
- jsonschema: any;
954
- }
955
-
956
- export const createReactQueryHookInterface = ({
957
- context,
958
- QueryClient,
959
- hookParamsTypeName,
960
- queryInterfaceName,
961
- responseType,
962
- jsonschema
963
- }: ReactQueryHookQueryInterface) => {
964
- // merge the user options with the defaults
965
- const options = context.options.reactQuery;
966
-
967
- const body = [];
968
-
969
- const props = getProps(context, jsonschema);
970
- if (props.length) {
971
- body.push(
972
- t.tsPropertySignature(
973
- t.identifier('args'),
974
- t.tsTypeAnnotation(
975
- // @ts-ignore:next-line
976
- t.tsTypeLiteral(props)
977
- )
978
- )
979
- );
980
- }
981
-
982
- return t.exportNamedDeclaration(
983
- t.tsInterfaceDeclaration(
984
- t.identifier(hookParamsTypeName),
985
- t.tsTypeParameterDeclaration([
986
- t.tSTypeParameter(undefined, undefined, GENERIC_SELECT_RESPONSE_NAME)
987
- ]),
988
- [
989
- t.tSExpressionWithTypeArguments(
990
- t.identifier(queryInterfaceName),
991
- t.tsTypeParameterInstantiation([
992
- // 1: response
993
- t.tsTypeReference(t.identifier(responseType)),
994
- // 2: select generic
995
- t.tSTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))
996
- ])
997
- )
998
- ],
999
- t.tsInterfaceBody(body)
1000
- )
1001
- );
1002
- };
1003
-
1004
- const getProps = (context: RenderContext, jsonschema: JSONSchema) => {
1005
- const keys = Object.keys(jsonschema.properties ?? {});
1006
- if (!keys.length) return [];
1007
-
1008
- return keys.map((prop) => {
1009
- const { type, optional } = getPropertyType(context, jsonschema, prop);
1010
- return propertySignature(
1011
- context.options.reactQuery.camelize ? camel(prop) : prop,
1012
- t.tsTypeAnnotation(type),
1013
- optional
1014
- );
1015
- });
1016
- };
1017
-
1018
- interface GenerateUseQueryQueryKeyParams {
1019
- hookKeyName: string;
1020
- queryKeysName: string;
1021
- methodName: string;
1022
- props: string[];
1023
- options: ReactQueryOptions;
1024
- }
1025
-
1026
- const generateUseQueryQueryKey = ({
1027
- hookKeyName,
1028
- queryKeysName,
1029
- methodName,
1030
- props,
1031
- options
1032
- }: GenerateUseQueryQueryKeyParams): t.ArrayExpression | t.CallExpression => {
1033
- const { optionalClient, queryKeys } = options;
1034
-
1035
- const hasArgs = props.includes('args');
1036
-
1037
- const contractAddressExpression = t.optionalMemberExpression(
1038
- t.identifier('client'),
1039
- t.identifier('contractAddress'),
1040
- false,
1041
- optionalClient
1042
- );
1043
-
1044
- if (queryKeys) {
1045
- const callArgs: Array<Expression> = [contractAddressExpression];
1046
-
1047
- if (hasArgs) callArgs.push(t.identifier('args'));
1048
-
1049
- return t.callExpression(
1050
- t.memberExpression(
1051
- t.identifier(queryKeysName),
1052
- t.identifier(camel(methodName))
1053
- ),
1054
- callArgs
1055
- );
1056
- }
1057
-
1058
- const queryKey: Array<Expression> = [
1059
- t.stringLiteral(hookKeyName),
1060
- contractAddressExpression
1061
- ];
1062
-
1063
- if (hasArgs) {
1064
- queryKey.push(
1065
- t.callExpression(
1066
- t.memberExpression(t.identifier('JSON'), t.identifier('stringify')),
1067
- [t.identifier('args')]
1068
- )
1069
- );
1070
- }
1071
- return t.arrayExpression(queryKey);
1072
- };