wasm-ast-types 0.26.4 → 1.1.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,403 +0,0 @@
1
- import * as t from '@babel/types';
2
- import { camel, pascal } from 'case';
3
- import { callExpression, createTypedObjectParams, getMessageProperties, identifier, OPTIONAL_FUNDS_PARAM, tsObjectPattern, tsPropertySignature } from '../utils';
4
- import { omitTypeReference, optionalConditionalExpression, propertySignature, shorthandProperty } from '../utils/babel';
5
- import { getPropertyType, getResponseType } from '../utils/types';
6
- import { OPTIONAL_FEE_PARAM, OPTIONAL_MEMO_PARAM } from '../utils/constants';
7
- export const createReactQueryHooks = ({
8
- context,
9
- queryMsg,
10
- contractName,
11
- QueryClient
12
- }) => {
13
- const options = context.options.reactQuery;
14
- const genericQueryInterfaceName = `${pascal(contractName)}ReactQuery`;
15
- const underscoreNames = getMessageProperties(queryMsg).map(schema => Object.keys(schema.properties)[0]);
16
- const body = [];
17
- const queryKeysName = `${camel(contractName)}QueryKeys`;
18
- if (options.queryKeys) {
19
- body.push(createReactQueryKeys({
20
- context,
21
- queryKeysName,
22
- camelContractName: camel(contractName),
23
- underscoreNames
24
- }));
25
- }
26
- const queryMsgs = getMessageProperties(queryMsg).map(schema => {
27
- // list_voters
28
- const underscoreName = Object.keys(schema.properties)[0];
29
- // listVoters
30
- const methodName = camel(underscoreName);
31
- // Cw3FlexMultisigListVotersQuery
32
- const hookParamsTypeName = `${pascal(contractName)}${pascal(methodName)}Query`;
33
- // useCw3FlexMultisigListVotersQuery
34
- const hookName = `use${hookParamsTypeName}`;
35
- // listVotersResponse
36
- const responseType = getResponseType(context, underscoreName);
37
- // cw3FlexMultisigListVoters
38
- const getterKey = camel(`${contractName}${pascal(methodName)}`);
39
- const jsonschema = schema.properties[underscoreName];
40
- return {
41
- underscoreName,
42
- methodName,
43
- hookParamsTypeName,
44
- hookName,
45
- responseType,
46
- getterKey,
47
- jsonschema
48
- };
49
- });
50
- const queryFactoryName = `${camel(contractName)}Queries`;
51
- if (options.queryFactory) {
52
- body.push(createReactQueryFactory({
53
- context,
54
- queryFactoryName,
55
- queryKeysName,
56
- queryMsgs
57
- }));
58
- }
59
- body.push(createReactQueryHookGenericInterface({
60
- context,
61
- QueryClient,
62
- genericQueryInterfaceName
63
- }));
64
- body.push(...queryMsgs.reduce((m, {
65
- methodName,
66
- hookParamsTypeName,
67
- hookName,
68
- responseType,
69
- getterKey,
70
- jsonschema
71
- }) => {
72
- return [createReactQueryHookInterface({
73
- context,
74
- hookParamsTypeName,
75
- responseType,
76
- queryInterfaceName: genericQueryInterfaceName,
77
- QueryClient,
78
- jsonschema
79
- }), createReactQueryHook({
80
- context,
81
- methodName,
82
- hookName,
83
- hookParamsTypeName,
84
- queryKeysName,
85
- responseType,
86
- hookKeyName: getterKey,
87
- jsonschema
88
- }), ...m];
89
- }, []));
90
- return body;
91
- };
92
- function buildQueryFn(methodName, jsonschema, options) {
93
- const keys = Object.keys(jsonschema.properties ?? {});
94
- let args = [];
95
- if (keys.length) {
96
- args = [t.objectExpression([...keys.map(prop => {
97
- return t.objectProperty(t.identifier(camel(prop)), t.memberExpression(t.identifier('args'), t.identifier(camel(prop))));
98
- })])];
99
- }
100
- const rejectInvalidClient = t.callExpression(t.memberExpression(t.identifier('Promise'), t.identifier('reject')), [t.newExpression(t.identifier('Error'), [t.stringLiteral('Invalid client')])]);
101
- return t.arrowFunctionExpression([], optionalConditionalExpression(t.identifier('client'), t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), rejectInvalidClient, options.optionalClient), false);
102
- }
103
- const ENABLED_QUERY_OPTION = t.objectProperty(t.identifier('enabled'), t.logicalExpression('&&', t.unaryExpression('!', t.unaryExpression('!', t.identifier('client'))), t.conditionalExpression(
104
- // explicitly check for undefined
105
- t.binaryExpression('!=', t.optionalMemberExpression(t.identifier('options'), t.identifier('enabled'), false, true), t.identifier('undefined')), t.memberExpression(t.identifier('options'), t.identifier('enabled')), t.booleanLiteral(true))));
106
- function buildQueryOptions(options) {
107
- return options.optionalClient ? t.objectExpression([t.spreadElement(t.identifier('options')), t.objectProperty(t.identifier('enabled'), t.logicalExpression('&&', t.unaryExpression('!', t.unaryExpression('!', t.identifier('client'))), t.conditionalExpression(
108
- // explicitly check for undefined
109
- t.binaryExpression('!=', t.optionalMemberExpression(t.identifier('options'), t.identifier('enabled'), false, true), t.identifier('undefined')), t.memberExpression(t.identifier('options'), t.identifier('enabled')), t.booleanLiteral(true))))]) : t.identifier('options');
110
- }
111
- export const createReactQueryHook = ({
112
- context,
113
- hookName,
114
- hookParamsTypeName,
115
- responseType,
116
- hookKeyName,
117
- queryKeysName,
118
- methodName,
119
- jsonschema
120
- }) => {
121
- context.addUtil('useQuery');
122
- context.addUtil('UseQueryOptions');
123
- const options = context.options.reactQuery;
124
- const keys = Object.keys(jsonschema.properties ?? {});
125
- let props = ['client', 'options'];
126
- if (keys.length) {
127
- props = ['client', 'args', 'options'];
128
- }
129
- const selectResponseGenericTypeName = GENERIC_SELECT_RESPONSE_NAME;
130
- const queryFunctionDeclaration = t.functionDeclaration(t.identifier(hookName), [tsObjectPattern([...props.map(prop => {
131
- return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
132
- })], t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(selectResponseGenericTypeName))]))))], t.blockStatement([t.returnStatement(callExpression(t.identifier('useQuery'), [generateUseQueryQueryKey({
133
- hookKeyName,
134
- queryKeysName,
135
- methodName,
136
- props,
137
- options
138
- }), buildQueryFn(methodName, jsonschema, options), buildQueryOptions(options)], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(selectResponseGenericTypeName))])))]));
139
-
140
- // Add the TData type parameters
141
- queryFunctionDeclaration.typeParameters = t.tsTypeParameterDeclaration([t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(responseType)), selectResponseGenericTypeName)]);
142
- return t.exportNamedDeclaration(queryFunctionDeclaration);
143
- };
144
- /**
145
- * Example:
146
- ```
147
- export interface Cw721RevokeMutation {
148
- client: Cw721Client;
149
- msg: {
150
- spender: string;
151
- tokenId: string;
152
- };
153
- args?: {
154
- fee?: number | StdFee | "auto";
155
- memo?: string;
156
- funds?: Coin[];
157
- };
158
- }
159
- ```
160
- */
161
- export const createReactQueryMutationArgsInterface = ({
162
- context,
163
- ExecuteClient,
164
- mutationHookParamsTypeName,
165
- useMutationTypeParameter,
166
- jsonschema
167
- }) => {
168
- const typedUseMutationOptions = t.tsTypeReference(t.identifier('UseMutationOptions'), useMutationTypeParameter);
169
- const body = [tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(ExecuteClient))), false)];
170
- const msgType = createTypedObjectParams(context, jsonschema)?.typeAnnotation;
171
- if (msgType) {
172
- body.push(t.tsPropertySignature(t.identifier('msg'),
173
- // @ts-ignore
174
- msgType));
175
- }
176
- context.addUtil('StdFee');
177
- context.addUtil('Coin');
178
- const optionalArgs = t.tsPropertySignature(t.identifier('args'), t.tsTypeAnnotation(
179
- // @ts-ignore:next-line
180
- t.tsTypeLiteral([propertySignature('fee', OPTIONAL_FEE_PARAM.typeAnnotation, true), propertySignature('memo', OPTIONAL_MEMO_PARAM.typeAnnotation, true), propertySignature('funds', OPTIONAL_FUNDS_PARAM.typeAnnotation, true)])));
181
- optionalArgs.optional = true;
182
- body.push(optionalArgs);
183
- return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(mutationHookParamsTypeName), null, [], t.tsInterfaceBody(body)));
184
- };
185
- export const createReactQueryMutationHooks = ({
186
- context,
187
- execMsg,
188
- contractName,
189
- ExecuteClient
190
- }) => {
191
- // merge the user options with the defaults
192
- return getMessageProperties(execMsg).reduce((m, schema) => {
193
- // update_members
194
- const execMethodUnderscoreName = Object.keys(schema.properties)[0];
195
- // updateMembers
196
- const execMethodName = camel(execMethodUnderscoreName);
197
- // Cw20UpdateMembersMutation
198
- const mutationHookParamsTypeName = `${pascal(contractName)}${pascal(execMethodName)}Mutation`;
199
- // useCw20UpdateMembersMutation
200
- const mutationHookName = `use${mutationHookParamsTypeName}`;
201
- const jsonschema = schema.properties[execMethodUnderscoreName];
202
- const properties = jsonschema.properties ?? {};
203
-
204
- // TODO: there should be a better way to do this
205
- const hasMsg = !!(Object.keys(properties)?.length || jsonschema?.$ref);
206
-
207
- // <ExecuteResult, Error, Cw4UpdateMembersMutation>
208
- const useMutationTypeParameter = generateMutationTypeParameter(context, mutationHookParamsTypeName);
209
- return [createReactQueryMutationArgsInterface({
210
- context,
211
- mutationHookParamsTypeName,
212
- ExecuteClient,
213
- jsonschema,
214
- useMutationTypeParameter
215
- }), createReactQueryMutationHook({
216
- context,
217
- execMethodName,
218
- mutationHookName,
219
- mutationHookParamsTypeName,
220
- hasMsg,
221
- useMutationTypeParameter
222
- }), ...m];
223
- }, []);
224
- };
225
-
226
- /**
227
- * Generates the mutation type parameter. If args exist, we use a pick. If not, we just return the params type.
228
- */
229
- const generateMutationTypeParameter = (context, mutationHookParamsTypeName) => {
230
- context.addUtil('ExecuteResult');
231
- return t.tsTypeParameterInstantiation([
232
- // Data
233
- t.tSTypeReference(t.identifier('ExecuteResult')),
234
- // Error
235
- t.tsTypeReference(t.identifier('Error')),
236
- // Variables
237
- t.tsTypeReference(t.identifier(mutationHookParamsTypeName))]);
238
- };
239
- /**
240
- *
241
- * Example:
242
- ```
243
- export const useCw4UpdateMembersMutation = ({ client, options }: Omit<Cw4UpdateMembersMutation, 'args'>) =>
244
- useMutation<ExecuteResult, Error, Pick<Cw4UpdateMembersMutation, 'args'>>(
245
- ({ args }) => client.updateMembers(args),
246
- options
247
- )
248
- ```
249
- */
250
- export const createReactQueryMutationHook = ({
251
- context,
252
- mutationHookName,
253
- mutationHookParamsTypeName,
254
- execMethodName,
255
- useMutationTypeParameter,
256
- hasMsg
257
- }) => {
258
- context.addUtil('useMutation');
259
- context.addUtil('UseMutationOptions');
260
- const useMutationFunctionArgs = [shorthandProperty('client')];
261
- if (hasMsg) useMutationFunctionArgs.push(shorthandProperty('msg'));
262
- useMutationFunctionArgs.push(t.objectProperty(t.identifier('args'), t.assignmentPattern(t.objectPattern([shorthandProperty('fee'), shorthandProperty('memo'), shorthandProperty('funds')]), t.objectExpression([]))));
263
- return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(mutationHookName), [identifier('options', t.tsTypeAnnotation(omitTypeReference(t.tsTypeReference(t.identifier('UseMutationOptions'), useMutationTypeParameter), 'mutationFn')), true)], t.blockStatement([t.returnStatement(callExpression(t.identifier('useMutation'), [t.arrowFunctionExpression([t.objectPattern(useMutationFunctionArgs)], t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(execMethodName)), (hasMsg ? [t.identifier('msg')] : []).concat([t.identifier('fee'), t.identifier('memo'), t.identifier('funds')])), false // not async
264
- ), t.identifier('options')], useMutationTypeParameter))])));
265
- };
266
- function createReactQueryKeys({
267
- context,
268
- queryKeysName,
269
- camelContractName,
270
- underscoreNames
271
- }) {
272
- const options = context.options.reactQuery;
273
- const contractAddressTypeAnnotation = t.tsTypeAnnotation(options.optionalClient ? t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()]) : t.tSStringKeyword());
274
- return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(queryKeysName), t.objectExpression([
275
- // 1: contract
276
- t.objectProperty(t.identifier('contract'), t.tSAsExpression(t.arrayExpression([t.objectExpression([t.objectProperty(t.identifier('contract'), t.stringLiteral(camelContractName))])]), t.tSTypeReference(t.identifier('const')))),
277
- // 2: address
278
- t.objectProperty(t.identifier('address'), t.arrowFunctionExpression([identifier('contractAddress', contractAddressTypeAnnotation)], t.tSAsExpression(t.arrayExpression([t.objectExpression([
279
- // 1
280
- t.spreadElement(t.memberExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier('contract')), t.numericLiteral(0), true // computed
281
- )), t.objectProperty(t.identifier('address'), t.identifier('contractAddress'))])]), t.tSTypeReference(t.identifier('const'))))),
282
- // 3: methods
283
- ...underscoreNames.map(underscoreMethodName => t.objectProperty(
284
- // key id is the camel method name
285
- t.identifier(camel(underscoreMethodName)), t.arrowFunctionExpression([identifier('contractAddress', contractAddressTypeAnnotation), identifier('args',
286
- // Record<string, unknown>
287
- t.tSTypeAnnotation(t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([t.tsStringKeyword(), t.tsUnknownKeyword()]))), true // optional
288
- )], t.tSAsExpression(t.arrayExpression([t.objectExpression([
289
- //...cw3FlexMultisigQueryKeys.address(contractAddress)[0]
290
- t.spreadElement(t.memberExpression(t.callExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier('address')), [t.identifier('contractAddress')]), t.numericLiteral(0), true // computed
291
- )),
292
- // method: list_voters
293
- t.objectProperty(t.identifier('method'), t.stringLiteral(underscoreMethodName)),
294
- // args
295
- shorthandProperty('args')])]), t.tSTypeReference(t.identifier('const'))))))]))]));
296
- }
297
- function createReactQueryFactory({
298
- context,
299
- queryFactoryName,
300
- queryKeysName,
301
- queryMsgs
302
- }) {
303
- const options = context.options.reactQuery;
304
- return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(queryFactoryName), t.objectExpression([...queryMsgs.map(({
305
- methodName,
306
- hookParamsTypeName,
307
- responseType,
308
- jsonschema
309
- }) => {
310
- const hasArgs = Object.keys(jsonschema.properties ?? {}).length > 0;
311
- const methodQueryOptionsFn = t.arrowFunctionExpression([tsObjectPattern([shorthandProperty('client'), ...(hasArgs ? [shorthandProperty('args')] : []), shorthandProperty('options')], t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))]))))], t.objectExpression([
312
- // 1: queryKey
313
- t.objectProperty(t.identifier('queryKey'), t.callExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier(methodName)), [t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, true), ...(hasArgs ? [t.identifier('args')] : [])])),
314
- // 2: queryFn
315
- t.objectProperty(t.identifier('queryFn'), buildQueryFn(methodName, jsonschema, options)),
316
- // 3: spread options
317
- t.spreadElement(t.identifier('options')),
318
- // 4. enabled
319
- ENABLED_QUERY_OPTION]));
320
- methodQueryOptionsFn.typeParameters = t.tsTypeParameterDeclaration([t.tsTypeParameter(undefined, t.tsTypeReference(t.identifier(responseType)), GENERIC_SELECT_RESPONSE_NAME)]);
321
- methodQueryOptionsFn.returnType = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))])));
322
- return t.objectProperty(
323
- // key id is the camel method name
324
- t.identifier(camel(methodName)), methodQueryOptionsFn);
325
- })]))]));
326
- }
327
- const GENERIC_SELECT_RESPONSE_NAME = 'TData';
328
- function createReactQueryHookGenericInterface({
329
- context,
330
- QueryClient,
331
- genericQueryInterfaceName
332
- }) {
333
- const options = context.options.reactQuery;
334
- const genericResponseTypeName = 'TResponse';
335
- context.addUtil('UseQueryOptions');
336
-
337
- // UseQueryOptions<TResponse, Error, TData>,
338
- const typedUseQueryOptions = t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(genericResponseTypeName)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))]));
339
- const body = [tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(options.optionalClient ? t.tsUnionType([t.tsTypeReference(t.identifier(QueryClient)), t.tsUndefinedKeyword()]) : t.tsTypeReference(t.identifier(QueryClient))), false), tsPropertySignature(t.identifier('options'), t.tsTypeAnnotation(options.version === 'v4' ? t.tSIntersectionType([omitTypeReference(typedUseQueryOptions, "'queryKey' | 'queryFn' | 'initialData'"), t.tSTypeLiteral([t.tsPropertySignature(t.identifier('initialData?'), t.tsTypeAnnotation(t.tsUndefinedKeyword()))])]) : typedUseQueryOptions), true)];
340
- return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(genericQueryInterfaceName), t.tsTypeParameterDeclaration([
341
- // 1: TResponse
342
- t.tsTypeParameter(undefined, undefined, genericResponseTypeName),
343
- // 2: TData
344
- t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(genericResponseTypeName)), GENERIC_SELECT_RESPONSE_NAME)]), [], t.tSInterfaceBody(body)));
345
- }
346
- export const createReactQueryHookInterface = ({
347
- context,
348
- QueryClient,
349
- hookParamsTypeName,
350
- queryInterfaceName,
351
- responseType,
352
- jsonschema
353
- }) => {
354
- // merge the user options with the defaults
355
- const options = context.options.reactQuery;
356
- const body = [];
357
- const props = getProps(context, jsonschema);
358
- if (props.length) {
359
- body.push(t.tsPropertySignature(t.identifier('args'), t.tsTypeAnnotation(
360
- // @ts-ignore:next-line
361
- t.tsTypeLiteral(props))));
362
- }
363
- return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(hookParamsTypeName), t.tsTypeParameterDeclaration([t.tSTypeParameter(undefined, undefined, GENERIC_SELECT_RESPONSE_NAME)]), [t.tSExpressionWithTypeArguments(t.identifier(queryInterfaceName), t.tsTypeParameterInstantiation([
364
- // 1: response
365
- t.tsTypeReference(t.identifier(responseType)),
366
- // 2: select generic
367
- t.tSTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))]))], t.tsInterfaceBody(body)));
368
- };
369
- const getProps = (context, jsonschema) => {
370
- const keys = Object.keys(jsonschema.properties ?? {});
371
- if (!keys.length) return [];
372
- return keys.map(prop => {
373
- const {
374
- type,
375
- optional
376
- } = getPropertyType(context, jsonschema, prop);
377
- return propertySignature(context.options.reactQuery.camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
378
- });
379
- };
380
- const generateUseQueryQueryKey = ({
381
- hookKeyName,
382
- queryKeysName,
383
- methodName,
384
- props,
385
- options
386
- }) => {
387
- const {
388
- optionalClient,
389
- queryKeys
390
- } = options;
391
- const hasArgs = props.includes('args');
392
- const contractAddressExpression = t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient);
393
- if (queryKeys) {
394
- const callArgs = [contractAddressExpression];
395
- if (hasArgs) callArgs.push(t.identifier('args'));
396
- return t.callExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier(camel(methodName))), callArgs);
397
- }
398
- const queryKey = [t.stringLiteral(hookKeyName), contractAddressExpression];
399
- if (hasArgs) {
400
- queryKey.push(t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.identifier('args')]));
401
- }
402
- return t.arrayExpression(queryKey);
403
- };
@@ -1,85 +0,0 @@
1
- import * as t from '@babel/types';
2
- import query_msg from '../../../../__fixtures__/basic/query_msg.json';
3
- import execute_msg from '../../../../__fixtures__/basic/execute_msg_for__empty.json';
4
- import ownership from '../../../../__fixtures__/basic/ownership.json';
5
- import { createReactQueryHooks, createReactQueryMutationHooks } from './react-query';
6
- import { expectCode, makeContext } from '../../test-utils';
7
- it('createReactQueryHooks', () => {
8
- expectCode(t.program(createReactQueryHooks({
9
- context: makeContext(query_msg, {
10
- reactQuery: {
11
- version: 'v3'
12
- }
13
- }),
14
- queryMsg: query_msg,
15
- contractName: 'Sg721',
16
- QueryClient: 'Sg721QueryClient'
17
- })));
18
- expectCode(t.program(createReactQueryHooks({
19
- context: makeContext(query_msg, {
20
- reactQuery: {
21
- version: 'v3',
22
- optionalClient: true
23
- }
24
- }),
25
- queryMsg: query_msg,
26
- contractName: 'Sg721',
27
- QueryClient: 'Sg721QueryClient'
28
- })));
29
- expectCode(t.program(createReactQueryHooks({
30
- context: makeContext(query_msg, {
31
- reactQuery: {
32
- version: 'v4'
33
- }
34
- }),
35
- queryMsg: query_msg,
36
- contractName: 'Sg721',
37
- QueryClient: 'Sg721QueryClient'
38
- })));
39
- expectCode(t.program(createReactQueryHooks({
40
- context: makeContext(query_msg, {
41
- reactQuery: {
42
- optionalClient: true,
43
- version: 'v4'
44
- }
45
- }),
46
- queryMsg: query_msg,
47
- contractName: 'Sg721',
48
- QueryClient: 'Sg721QueryClient'
49
- })));
50
- expectCode(t.program(createReactQueryHooks({
51
- context: makeContext(query_msg, {
52
- reactQuery: {
53
- optionalClient: true,
54
- version: 'v4',
55
- queryKeys: true,
56
- queryFactory: true
57
- }
58
- }),
59
- queryMsg: query_msg,
60
- contractName: 'Sg721',
61
- QueryClient: 'Sg721QueryClient'
62
- })));
63
- expectCode(t.program(createReactQueryMutationHooks({
64
- context: makeContext(execute_msg, {
65
- reactQuery: {
66
- version: 'v3'
67
- }
68
- }),
69
- execMsg: execute_msg,
70
- contractName: 'Sg721',
71
- ExecuteClient: 'Sg721Client'
72
- })));
73
- });
74
- it('ownership', () => {
75
- expectCode(t.program(createReactQueryMutationHooks({
76
- context: makeContext(ownership, {
77
- reactQuery: {
78
- version: 'v3'
79
- }
80
- }),
81
- execMsg: ownership,
82
- contractName: 'Ownership',
83
- ExecuteClient: 'OwnershipClient'
84
- })));
85
- });
@@ -1 +0,0 @@
1
- export * from './recoil';
@@ -1,46 +0,0 @@
1
- import * as t from '@babel/types';
2
- import { camel, pascal } from 'case';
3
- import { callExpression, getMessageProperties, getResponseType } from '../utils';
4
- export const createRecoilSelector = (context, keyPrefix, QueryClient, methodName, responseType) => {
5
- context.addUtil('selectorFamily');
6
- const selectorName = camel(`${methodName}Selector`);
7
- const getterKey = camel(`${keyPrefix}${pascal(methodName)}`);
8
- return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(selectorName), callExpression(t.identifier('selectorFamily'), [t.objectExpression([t.objectProperty(t.identifier('key'), t.stringLiteral(getterKey)), t.objectProperty(t.identifier('get'), t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier('params'), t.identifier('params'), false, true), t.restElement(t.identifier('queryClientParams'))])], t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier('get'), t.identifier('get'), false, true)])], t.blockStatement([t.variableDeclaration('const', [t.variableDeclarator(t.identifier('client'), t.callExpression(t.identifier('get'), [t.callExpression(t.identifier('queryClient'), [t.identifier('queryClientParams')])]))]), t.returnStatement(t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), [t.spreadElement(t.identifier('params'))])))]), true)))])], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsIntersectionType([t.tsTypeReference(t.identifier('QueryClientParams')), t.tsTypeLiteral([t.tsPropertySignature(t.identifier('params'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Parameters'), t.tsTypeParameterInstantiation([t.tsIndexedAccessType(t.tsTypeReference(t.identifier(QueryClient)), t.tsLiteralType(t.stringLiteral(methodName)))]))))])])])))]));
9
- };
10
- export const createRecoilSelectors = (context, keyPrefix, QueryClient, queryMsg) => {
11
- return getMessageProperties(queryMsg).map(schema => {
12
- const underscoreName = Object.keys(schema.properties)[0];
13
- const methodName = camel(underscoreName);
14
- const responseType = getResponseType(context, underscoreName);
15
- return createRecoilSelector(context, keyPrefix, QueryClient, methodName, responseType);
16
- });
17
- };
18
- export const createRecoilQueryClientType = () => ({
19
- "type": "TSTypeAliasDeclaration",
20
- "id": {
21
- "type": "Identifier",
22
- "name": "QueryClientParams"
23
- },
24
- "typeAnnotation": {
25
- "type": "TSTypeLiteral",
26
- "members": [{
27
- "type": "TSPropertySignature",
28
- "key": {
29
- "type": "Identifier",
30
- "name": "contractAddress"
31
- },
32
- "computed": false,
33
- "typeAnnotation": {
34
- "type": "TSTypeAnnotation",
35
- "typeAnnotation": {
36
- "type": "TSStringKeyword"
37
- }
38
- }
39
- }]
40
- }
41
- });
42
- export const createRecoilQueryClient = (context, keyPrefix, QueryClient) => {
43
- context.addUtil('selectorFamily');
44
- const getterKey = camel(`${keyPrefix}${'QueryClient'}`);
45
- return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier('queryClient'), callExpression(t.identifier('selectorFamily'), [t.objectExpression([t.objectProperty(t.identifier('key'), t.stringLiteral(getterKey)), t.objectProperty(t.identifier('get'), t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier('contractAddress'), t.identifier('contractAddress'), false, true)])], t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier('get'), t.identifier('get'), false, true)])], t.blockStatement([t.variableDeclaration('const', [t.variableDeclarator(t.identifier('client'), t.callExpression(t.identifier('get'), [t.identifier('cosmWasmClient')]))]), t.returnStatement(t.newExpression(t.identifier(QueryClient), [t.identifier('client'), t.identifier('contractAddress')]))]), false)))])], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(QueryClient)), t.tsTypeReference(t.identifier('QueryClientParams'))])))]));
46
- };
@@ -1,14 +0,0 @@
1
- import * as t from '@babel/types';
2
- import query_msg from '../../../../__fixtures__/basic/query_msg.json';
3
- import { createRecoilSelector, createRecoilSelectors, createRecoilQueryClient } from './recoil';
4
- import { expectCode, makeContext } from '../../test-utils';
5
- const ctx = makeContext(query_msg);
6
- it('selector', () => {
7
- expectCode(createRecoilSelector(ctx, 'SG721', 'SG721QueryClient', 'governanceModules', 'GovernanceModulesResponse'));
8
- });
9
- it('selectors', () => {
10
- expectCode(t.program(createRecoilSelectors(ctx, 'SG721', 'SG721QueryClient', query_msg)));
11
- });
12
- it('client', () => {
13
- expectCode(createRecoilQueryClient(ctx, 'SG721', 'SG721QueryClient'));
14
- });
package/module/types.js DELETED
@@ -1,3 +0,0 @@
1
- ;
2
- ;
3
- export {};