wasm-ast-types 0.24.0 → 0.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/main/client/client.js +46 -72
- package/main/client/index.js +0 -2
- package/main/client/test/ts-client.account-nfts.spec.js +3 -5
- package/main/client/test/ts-client.arrays-ref.spec.js +0 -4
- package/main/client/test/ts-client.arrays.spec.js +3 -6
- package/main/client/test/ts-client.cw-named-groups.test.js +0 -4
- package/main/client/test/ts-client.cw-proposal-single.test.js +0 -5
- package/main/client/test/ts-client.empty-enums.spec.js +0 -4
- package/main/client/test/ts-client.issue-101.spec.js +0 -4
- package/main/client/test/ts-client.issue-103.test.js +0 -4
- package/main/client/test/ts-client.issue-71.test.js +28 -50
- package/main/client/test/ts-client.issue-98.test.js +3 -5
- package/main/client/test/ts-client.issues.test.js +28 -50
- package/main/client/test/ts-client.overrides.spec.js +0 -4
- package/main/client/test/ts-client.sg721.spec.js +0 -4
- package/main/client/test/ts-client.spec.js +0 -17
- package/main/client/test/ts-client.vectis.spec.js +0 -8
- package/main/client/test/ts-client.wager.spec.js +4 -5
- package/main/context/context.js +6 -33
- package/main/context/imports.js +7 -43
- package/main/context/index.js +0 -4
- package/main/index.js +4 -22
- package/main/{msg-builder → message-builder}/index.js +4 -6
- package/main/{msg-builder/msg-builder.js → message-builder/message-builder.js} +24 -30
- package/main/{msg-builder/msg-builder.spec.js → message-builder/message-builder.spec.js} +4 -10
- package/main/message-composer/index.js +0 -2
- package/main/message-composer/message-composer.js +18 -28
- package/main/message-composer/message-composer.spec.js +0 -5
- package/main/provider/index.js +0 -2
- package/main/provider/provider.js +2 -29
- package/main/provider/provider.spec.js +0 -3
- package/main/react-query/index.js +0 -2
- package/main/react-query/react-query.js +125 -152
- package/main/react-query/react-query.spec.js +0 -10
- package/main/recoil/index.js +0 -2
- package/main/recoil/recoil.js +0 -14
- package/main/recoil/recoil.spec.js +0 -8
- package/main/utils/babel.js +12 -98
- package/main/utils/babel.spec.js +72 -67
- package/main/utils/constants.js +0 -6
- package/main/utils/index.js +0 -7
- package/main/utils/ref.js +0 -3
- package/main/utils/types.js +22 -109
- package/module/client/client.js +46 -32
- package/module/client/test/ts-client.account-nfts.spec.js +3 -1
- package/module/client/test/ts-client.arrays.spec.js +3 -1
- package/module/client/test/ts-client.issue-101.spec.js +3 -1
- package/module/client/test/ts-client.issue-98.test.js +3 -1
- package/module/client/test/ts-client.wager.spec.js +4 -1
- package/module/context/context.js +9 -23
- package/module/context/imports.js +5 -24
- package/module/index.js +1 -1
- package/module/message-builder/index.js +1 -0
- package/module/{msg-builder/msg-builder.js → message-builder/message-builder.js} +22 -17
- package/module/{msg-builder/msg-builder.spec.js → message-builder/message-builder.spec.js} +4 -4
- package/module/message-composer/message-composer.js +18 -14
- package/module/provider/provider.js +0 -8
- package/module/react-query/react-query.js +77 -76
- package/module/utils/babel.js +4 -14
- package/module/utils/babel.spec.js +49 -23
- package/module/utils/types.js +18 -66
- package/package.json +2 -2
- package/src/context/context.ts +158 -158
- package/src/index.ts +1 -1
- package/src/{msg-builder/__snapshots__/msg-builder.spec.ts.snap → message-builder/__snapshots__/message-builder.spec.ts.snap} +2 -2
- package/src/message-builder/index.ts +1 -0
- package/src/{msg-builder/msg-builder.spec.ts → message-builder/message-builder.spec.ts} +6 -7
- package/src/{msg-builder/msg-builder.ts → message-builder/message-builder.ts} +5 -5
- package/src/react-query/react-query.spec.ts +1 -1
- package/types/context/context.d.ts +2 -2
- package/types/index.d.ts +1 -1
- package/types/message-builder/index.d.ts +1 -0
- package/types/message-builder/message-builder.d.ts +4 -0
- package/module/msg-builder/index.js +0 -1
- package/src/msg-builder/index.ts +0 -1
- package/types/msg-builder/index.d.ts +0 -1
- package/types/msg-builder/msg-builder.d.ts +0 -4
@@ -15,7 +15,6 @@ export const createReactQueryHooks = ({
|
|
15
15
|
const underscoreNames = getMessageProperties(queryMsg).map(schema => Object.keys(schema.properties)[0]);
|
16
16
|
const body = [];
|
17
17
|
const queryKeysName = `${camel(contractName)}QueryKeys`;
|
18
|
-
|
19
18
|
if (options.queryKeys) {
|
20
19
|
body.push(createReactQueryKeys({
|
21
20
|
context,
|
@@ -24,19 +23,18 @@ export const createReactQueryHooks = ({
|
|
24
23
|
underscoreNames
|
25
24
|
}));
|
26
25
|
}
|
27
|
-
|
28
26
|
const queryMsgs = getMessageProperties(queryMsg).map(schema => {
|
29
27
|
// list_voters
|
30
|
-
const underscoreName = Object.keys(schema.properties)[0];
|
31
|
-
|
32
|
-
const methodName = camel(underscoreName);
|
33
|
-
|
34
|
-
const hookParamsTypeName = `${pascal(contractName)}${pascal(methodName)}Query`;
|
35
|
-
|
36
|
-
const hookName = `use${hookParamsTypeName}`;
|
37
|
-
|
38
|
-
const responseType = getResponseType(context, underscoreName);
|
39
|
-
|
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
|
40
38
|
const getterKey = camel(`${contractName}${pascal(methodName)}`);
|
41
39
|
const jsonschema = schema.properties[underscoreName];
|
42
40
|
return {
|
@@ -50,7 +48,6 @@ export const createReactQueryHooks = ({
|
|
50
48
|
};
|
51
49
|
});
|
52
50
|
const queryFactoryName = `${camel(contractName)}Queries`;
|
53
|
-
|
54
51
|
if (options.queryFactory) {
|
55
52
|
body.push(createReactQueryFactory({
|
56
53
|
context,
|
@@ -59,7 +56,6 @@ export const createReactQueryHooks = ({
|
|
59
56
|
queryMsgs
|
60
57
|
}));
|
61
58
|
}
|
62
|
-
|
63
59
|
body.push(createReactQueryHookGenericInterface({
|
64
60
|
context,
|
65
61
|
QueryClient,
|
@@ -93,29 +89,25 @@ export const createReactQueryHooks = ({
|
|
93
89
|
}, []));
|
94
90
|
return body;
|
95
91
|
};
|
96
|
-
|
97
92
|
function buildQueryFn(methodName, jsonschema, options) {
|
98
93
|
const keys = Object.keys(jsonschema.properties ?? {});
|
99
94
|
let args = [];
|
100
|
-
|
101
95
|
if (keys.length) {
|
102
96
|
args = [t.objectExpression([...keys.map(prop => {
|
103
97
|
return t.objectProperty(t.identifier(camel(prop)), t.memberExpression(t.identifier('args'), t.identifier(camel(prop))));
|
104
98
|
})])];
|
105
99
|
}
|
106
|
-
|
107
100
|
const rejectInvalidClient = t.callExpression(t.memberExpression(t.identifier('Promise'), t.identifier('reject')), [t.newExpression(t.identifier('Error'), [t.stringLiteral('Invalid client')])]);
|
108
101
|
return t.arrowFunctionExpression([], optionalConditionalExpression(t.identifier('client'), t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), rejectInvalidClient, options.optionalClient), false);
|
109
102
|
}
|
110
|
-
|
111
|
-
|
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
|
112
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))));
|
113
|
-
|
114
106
|
function buildQueryOptions(options) {
|
115
|
-
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(
|
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
|
116
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');
|
117
110
|
}
|
118
|
-
|
119
111
|
export const createReactQueryHook = ({
|
120
112
|
context,
|
121
113
|
hookName,
|
@@ -131,11 +123,9 @@ export const createReactQueryHook = ({
|
|
131
123
|
const options = context.options.reactQuery;
|
132
124
|
const keys = Object.keys(jsonschema.properties ?? {});
|
133
125
|
let props = ['client', 'options'];
|
134
|
-
|
135
126
|
if (keys.length) {
|
136
127
|
props = ['client', 'args', 'options'];
|
137
128
|
}
|
138
|
-
|
139
129
|
const selectResponseGenericTypeName = GENERIC_SELECT_RESPONSE_NAME;
|
140
130
|
const queryFunctionDeclaration = t.functionDeclaration(t.identifier(hookName), [tsObjectPattern([...props.map(prop => {
|
141
131
|
return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
|
@@ -145,12 +135,12 @@ export const createReactQueryHook = ({
|
|
145
135
|
methodName,
|
146
136
|
props,
|
147
137
|
options
|
148
|
-
}), buildQueryFn(methodName, jsonschema, options), buildQueryOptions(options)], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(selectResponseGenericTypeName))])))]));
|
138
|
+
}), buildQueryFn(methodName, jsonschema, options), buildQueryOptions(options)], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(selectResponseGenericTypeName))])))]));
|
149
139
|
|
140
|
+
// Add the TData type parameters
|
150
141
|
queryFunctionDeclaration.typeParameters = t.tsTypeParameterDeclaration([t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(responseType)), selectResponseGenericTypeName)]);
|
151
142
|
return t.exportNamedDeclaration(queryFunctionDeclaration);
|
152
143
|
};
|
153
|
-
|
154
144
|
/**
|
155
145
|
* Example:
|
156
146
|
```
|
@@ -178,15 +168,15 @@ export const createReactQueryMutationArgsInterface = ({
|
|
178
168
|
const typedUseMutationOptions = t.tsTypeReference(t.identifier('UseMutationOptions'), useMutationTypeParameter);
|
179
169
|
const body = [tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(ExecuteClient))), false)];
|
180
170
|
const msgType = createTypedObjectParams(context, jsonschema)?.typeAnnotation;
|
181
|
-
|
182
171
|
if (msgType) {
|
183
|
-
body.push(t.tsPropertySignature(t.identifier('msg'),
|
172
|
+
body.push(t.tsPropertySignature(t.identifier('msg'),
|
173
|
+
// @ts-ignore
|
184
174
|
msgType));
|
185
175
|
}
|
186
|
-
|
187
176
|
context.addUtil('StdFee');
|
188
177
|
context.addUtil('Coin');
|
189
|
-
const optionalArgs = t.tsPropertySignature(t.identifier('args'), t.tsTypeAnnotation(
|
178
|
+
const optionalArgs = t.tsPropertySignature(t.identifier('args'), t.tsTypeAnnotation(
|
179
|
+
// @ts-ignore:next-line
|
190
180
|
t.tsTypeLiteral([propertySignature('fee', OPTIONAL_FEE_PARAM.typeAnnotation, true), propertySignature('memo', OPTIONAL_MEMO_PARAM.typeAnnotation, true), propertySignature('funds', OPTIONAL_FUNDS_PARAM.typeAnnotation, true)])));
|
191
181
|
optionalArgs.optional = true;
|
192
182
|
body.push(optionalArgs);
|
@@ -201,18 +191,20 @@ export const createReactQueryMutationHooks = ({
|
|
201
191
|
// merge the user options with the defaults
|
202
192
|
return getMessageProperties(execMsg).reduce((m, schema) => {
|
203
193
|
// update_members
|
204
|
-
const execMethodUnderscoreName = Object.keys(schema.properties)[0];
|
205
|
-
|
206
|
-
const execMethodName = camel(execMethodUnderscoreName);
|
207
|
-
|
208
|
-
const mutationHookParamsTypeName = `${pascal(contractName)}${pascal(execMethodName)}Mutation`;
|
209
|
-
|
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
|
210
200
|
const mutationHookName = `use${mutationHookParamsTypeName}`;
|
211
201
|
const jsonschema = schema.properties[execMethodUnderscoreName];
|
212
|
-
const properties = jsonschema.properties ?? {};
|
202
|
+
const properties = jsonschema.properties ?? {};
|
213
203
|
|
214
|
-
|
204
|
+
// TODO: there should be a better way to do this
|
205
|
+
const hasMsg = !!(Object.keys(properties)?.length || jsonschema?.$ref);
|
215
206
|
|
207
|
+
// <ExecuteResult, Error, Cw4UpdateMembersMutation>
|
216
208
|
const useMutationTypeParameter = generateMutationTypeParameter(context, mutationHookParamsTypeName);
|
217
209
|
return [createReactQueryMutationArgsInterface({
|
218
210
|
context,
|
@@ -230,18 +222,20 @@ export const createReactQueryMutationHooks = ({
|
|
230
222
|
}), ...m];
|
231
223
|
}, []);
|
232
224
|
};
|
225
|
+
|
233
226
|
/**
|
234
227
|
* Generates the mutation type parameter. If args exist, we use a pick. If not, we just return the params type.
|
235
228
|
*/
|
236
|
-
|
237
229
|
const generateMutationTypeParameter = (context, mutationHookParamsTypeName) => {
|
238
230
|
context.addUtil('ExecuteResult');
|
239
|
-
return t.tsTypeParameterInstantiation([
|
240
|
-
|
241
|
-
t.
|
231
|
+
return t.tsTypeParameterInstantiation([
|
232
|
+
// Data
|
233
|
+
t.tSTypeReference(t.identifier('ExecuteResult')),
|
234
|
+
// Error
|
235
|
+
t.tsTypeReference(t.identifier('Error')),
|
236
|
+
// Variables
|
242
237
|
t.tsTypeReference(t.identifier(mutationHookParamsTypeName))]);
|
243
238
|
};
|
244
|
-
|
245
239
|
/**
|
246
240
|
*
|
247
241
|
* Example:
|
@@ -269,7 +263,6 @@ export const createReactQueryMutationHook = ({
|
|
269
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
|
270
264
|
), t.identifier('options')], useMutationTypeParameter))])));
|
271
265
|
};
|
272
|
-
|
273
266
|
function createReactQueryKeys({
|
274
267
|
context,
|
275
268
|
queryKeysName,
|
@@ -278,21 +271,29 @@ function createReactQueryKeys({
|
|
278
271
|
}) {
|
279
272
|
const options = context.options.reactQuery;
|
280
273
|
const contractAddressTypeAnnotation = t.tsTypeAnnotation(options.optionalClient ? t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()]) : t.tSStringKeyword());
|
281
|
-
return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(queryKeysName), t.objectExpression([
|
282
|
-
|
283
|
-
t.objectProperty(t.identifier('
|
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
|
284
280
|
t.spreadElement(t.memberExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier('contract')), t.numericLiteral(0), true // computed
|
285
|
-
)), t.objectProperty(t.identifier('address'), t.identifier('contractAddress'))])]), t.tSTypeReference(t.identifier('const'))))),
|
286
|
-
|
287
|
-
|
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>
|
288
287
|
t.tSTypeAnnotation(t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([t.tsStringKeyword(), t.tsUnknownKeyword()]))), true // optional
|
289
|
-
)], t.tSAsExpression(t.arrayExpression([t.objectExpression([
|
288
|
+
)], t.tSAsExpression(t.arrayExpression([t.objectExpression([
|
289
|
+
//...cw3FlexMultisigQueryKeys.address(contractAddress)[0]
|
290
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
|
-
|
291
|
+
)),
|
292
|
+
// method: list_voters
|
293
|
+
t.objectProperty(t.identifier('method'), t.stringLiteral(underscoreMethodName)),
|
294
|
+
// args
|
293
295
|
shorthandProperty('args')])]), t.tSTypeReference(t.identifier('const'))))))]))]));
|
294
296
|
}
|
295
|
-
|
296
297
|
function createReactQueryFactory({
|
297
298
|
context,
|
298
299
|
queryFactoryName,
|
@@ -307,20 +308,23 @@ function createReactQueryFactory({
|
|
307
308
|
jsonschema
|
308
309
|
}) => {
|
309
310
|
const hasArgs = Object.keys(jsonschema.properties ?? {}).length > 0;
|
310
|
-
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([
|
311
|
-
|
312
|
-
t.objectProperty(t.identifier('
|
313
|
-
|
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
|
314
319
|
ENABLED_QUERY_OPTION]));
|
315
320
|
methodQueryOptionsFn.typeParameters = t.tsTypeParameterDeclaration([t.tsTypeParameter(undefined, t.tsTypeReference(t.identifier(responseType)), GENERIC_SELECT_RESPONSE_NAME)]);
|
316
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))])));
|
317
|
-
return t.objectProperty(
|
322
|
+
return t.objectProperty(
|
323
|
+
// key id is the camel method name
|
318
324
|
t.identifier(camel(methodName)), methodQueryOptionsFn);
|
319
325
|
})]))]));
|
320
326
|
}
|
321
|
-
|
322
327
|
const GENERIC_SELECT_RESPONSE_NAME = 'TData';
|
323
|
-
|
324
328
|
function createReactQueryHookGenericInterface({
|
325
329
|
context,
|
326
330
|
QueryClient,
|
@@ -328,15 +332,17 @@ function createReactQueryHookGenericInterface({
|
|
328
332
|
}) {
|
329
333
|
const options = context.options.reactQuery;
|
330
334
|
const genericResponseTypeName = 'TResponse';
|
331
|
-
context.addUtil('UseQueryOptions');
|
335
|
+
context.addUtil('UseQueryOptions');
|
332
336
|
|
337
|
+
// UseQueryOptions<TResponse, Error, TData>,
|
333
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))]));
|
334
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)];
|
335
|
-
return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(genericQueryInterfaceName), t.tsTypeParameterDeclaration([
|
336
|
-
|
340
|
+
return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(genericQueryInterfaceName), t.tsTypeParameterDeclaration([
|
341
|
+
// 1: TResponse
|
342
|
+
t.tsTypeParameter(undefined, undefined, genericResponseTypeName),
|
343
|
+
// 2: TData
|
337
344
|
t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(genericResponseTypeName)), GENERIC_SELECT_RESPONSE_NAME)]), [], t.tSInterfaceBody(body)));
|
338
345
|
}
|
339
|
-
|
340
346
|
export const createReactQueryHookInterface = ({
|
341
347
|
context,
|
342
348
|
QueryClient,
|
@@ -349,17 +355,17 @@ export const createReactQueryHookInterface = ({
|
|
349
355
|
const options = context.options.reactQuery;
|
350
356
|
const body = [];
|
351
357
|
const props = getProps(context, jsonschema);
|
352
|
-
|
353
358
|
if (props.length) {
|
354
|
-
body.push(t.tsPropertySignature(t.identifier('args'), t.tsTypeAnnotation(
|
359
|
+
body.push(t.tsPropertySignature(t.identifier('args'), t.tsTypeAnnotation(
|
360
|
+
// @ts-ignore:next-line
|
355
361
|
t.tsTypeLiteral(props))));
|
356
362
|
}
|
357
|
-
|
358
|
-
|
359
|
-
t.tsTypeReference(t.identifier(responseType)),
|
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
|
360
367
|
t.tSTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))]))], t.tsInterfaceBody(body)));
|
361
368
|
};
|
362
|
-
|
363
369
|
const getProps = (context, jsonschema) => {
|
364
370
|
const keys = Object.keys(jsonschema.properties ?? {});
|
365
371
|
if (!keys.length) return [];
|
@@ -371,7 +377,6 @@ const getProps = (context, jsonschema) => {
|
|
371
377
|
return propertySignature(context.options.reactQuery.camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
|
372
378
|
});
|
373
379
|
};
|
374
|
-
|
375
380
|
const generateUseQueryQueryKey = ({
|
376
381
|
hookKeyName,
|
377
382
|
queryKeysName,
|
@@ -385,18 +390,14 @@ const generateUseQueryQueryKey = ({
|
|
385
390
|
} = options;
|
386
391
|
const hasArgs = props.includes('args');
|
387
392
|
const contractAddressExpression = t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient);
|
388
|
-
|
389
393
|
if (queryKeys) {
|
390
394
|
const callArgs = [contractAddressExpression];
|
391
395
|
if (hasArgs) callArgs.push(t.identifier('args'));
|
392
396
|
return t.callExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier(camel(methodName))), callArgs);
|
393
397
|
}
|
394
|
-
|
395
398
|
const queryKey = [t.stringLiteral(hookKeyName), contractAddressExpression];
|
396
|
-
|
397
399
|
if (hasArgs) {
|
398
400
|
queryKey.push(t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.identifier('args')]));
|
399
401
|
}
|
400
|
-
|
401
402
|
return t.arrayExpression(queryKey);
|
402
403
|
};
|
package/module/utils/babel.js
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
import * as t from '@babel/types';
|
2
2
|
import { snake } from "case";
|
3
|
-
import { refLookup } from './ref';
|
3
|
+
import { refLookup } from './ref';
|
4
4
|
|
5
|
+
// t.TSPropertySignature - kind?
|
5
6
|
export const propertySignature = (name, typeAnnotation, optional = false) => {
|
6
7
|
return {
|
7
8
|
type: 'TSPropertySignature',
|
@@ -25,7 +26,6 @@ export const tsTypeOperator = (typeAnnotation, operator) => {
|
|
25
26
|
export const getMessageProperties = msg => {
|
26
27
|
let results = [];
|
27
28
|
let objs = [];
|
28
|
-
|
29
29
|
if (msg.anyOf) {
|
30
30
|
objs = msg.anyOf;
|
31
31
|
} else if (msg.oneOf) {
|
@@ -33,7 +33,6 @@ export const getMessageProperties = msg => {
|
|
33
33
|
} else if (msg.allOf) {
|
34
34
|
objs = msg.allOf;
|
35
35
|
}
|
36
|
-
|
37
36
|
for (const obj of objs) {
|
38
37
|
if (obj.properties) {
|
39
38
|
results.push(obj);
|
@@ -45,7 +44,6 @@ export const getMessageProperties = msg => {
|
|
45
44
|
}
|
46
45
|
}
|
47
46
|
}
|
48
|
-
|
49
47
|
return results;
|
50
48
|
};
|
51
49
|
export const tsPropertySignature = (key, typeAnnotation, optional) => {
|
@@ -82,11 +80,9 @@ export const abstractClassDeclaration = (name, body, implementsExressions = [],
|
|
82
80
|
};
|
83
81
|
export const classDeclaration = (name, body, implementsExressions = [], superClass = null) => {
|
84
82
|
const declaration = t.classDeclaration(t.identifier(name), superClass, t.classBody(body));
|
85
|
-
|
86
83
|
if (implementsExressions.length) {
|
87
84
|
declaration.implements = implementsExressions;
|
88
85
|
}
|
89
|
-
|
90
86
|
return declaration;
|
91
87
|
};
|
92
88
|
export const classProperty = (name, typeAnnotation = null, isReadonly = false, isStatic = false, noImplicitOverride = false) => {
|
@@ -148,12 +144,10 @@ export const getFieldDimensionality = field => {
|
|
148
144
|
let typeName = field.type;
|
149
145
|
const isArray = typeName.endsWith('[]');
|
150
146
|
let dimensions = 0;
|
151
|
-
|
152
147
|
if (isArray) {
|
153
148
|
dimensions = typeName.match(/\[\]/g).length - 1;
|
154
149
|
typeName = typeName.replace(/\[\]/g, '');
|
155
150
|
}
|
156
|
-
|
157
151
|
return {
|
158
152
|
typeName,
|
159
153
|
dimensions,
|
@@ -164,12 +158,10 @@ export const memberExpressionOrIdentifier = names => {
|
|
164
158
|
if (names.length === 1) {
|
165
159
|
return t.identifier(names[0]);
|
166
160
|
}
|
167
|
-
|
168
161
|
if (names.length === 2) {
|
169
162
|
const [b, a] = names;
|
170
163
|
return t.memberExpression(t.identifier(a), t.identifier(b));
|
171
164
|
}
|
172
|
-
|
173
165
|
const [name, ...rest] = names;
|
174
166
|
return t.memberExpression(memberExpressionOrIdentifier(rest), t.identifier(name));
|
175
167
|
};
|
@@ -177,19 +169,17 @@ export const memberExpressionOrIdentifierSnake = names => {
|
|
177
169
|
if (names.length === 1) {
|
178
170
|
return t.identifier(snake(names[0]));
|
179
171
|
}
|
180
|
-
|
181
172
|
if (names.length === 2) {
|
182
173
|
const [b, a] = names;
|
183
174
|
return t.memberExpression(t.identifier(snake(a)), t.identifier(snake(b)));
|
184
175
|
}
|
185
|
-
|
186
176
|
const [name, ...rest] = names;
|
187
177
|
return t.memberExpression(memberExpressionOrIdentifierSnake(rest), t.identifier(snake(name)));
|
188
178
|
};
|
179
|
+
|
189
180
|
/**
|
190
181
|
* If optional, return a conditional, otherwise just the expression
|
191
182
|
*/
|
192
|
-
|
193
183
|
export const optionalConditionalExpression = (test, expression, alternate, optional = false) => {
|
194
184
|
return optional ? t.conditionalExpression(test, expression, alternate) : expression;
|
195
185
|
};
|
@@ -200,11 +190,11 @@ export const typeRefOrUnionWithUndefined = (identifier, optional = false) => {
|
|
200
190
|
export const parameterizedTypeReference = (identifier, from, omit) => {
|
201
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))))]));
|
202
192
|
};
|
193
|
+
|
203
194
|
/**
|
204
195
|
* omitTypeReference(t.tsTypeReference(t.identifier('Cw4UpdateMembersMutation'),),'args').....
|
205
196
|
* Omit<Cw4UpdateMembersMutation, 'args'>
|
206
197
|
*/
|
207
|
-
|
208
198
|
export const omitTypeReference = (from, omit) => {
|
209
199
|
return parameterizedTypeReference('Omit', from, omit);
|
210
200
|
};
|
@@ -6,39 +6,65 @@ it('top import', async () => {
|
|
6
6
|
expectCode(importStmt(['CosmWasmClient', 'ExecuteResult', 'SigningCosmWasmClient'], '@cosmjs/cosmwasm-stargate'));
|
7
7
|
});
|
8
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
|
-
|
11
|
-
|
12
|
-
t.tSPropertySignature(t.identifier('
|
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
|
13
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'))))])))]));
|
14
21
|
});
|
15
22
|
it('readonly classes', async () => {
|
16
|
-
expectCode(t.program([t.exportNamedDeclaration(classDeclaration('SG721QueryClient', [
|
17
|
-
|
18
|
-
classProperty('
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
t.
|
23
|
-
|
24
|
-
|
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
|
25
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'))]))]));
|
26
42
|
});
|
27
43
|
it('mutation classes', async () => {
|
28
|
-
expectCode(t.program([t.exportNamedDeclaration(classDeclaration('SG721Client', [
|
29
|
-
|
30
|
-
classProperty('
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
37
62
|
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true))], [t.tSExpressionWithTypeArguments(t.identifier('SG721ReadOnlyInstance'))], t.identifier('SG721QueryClient')))]));
|
38
63
|
});
|
39
64
|
it('object parms', () => {
|
40
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)]);
|
41
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()))]));
|
42
|
-
expectCode(t.program([t.expressionStatement(t.assignmentExpression('=', t.identifier('ownerOf'), arrowFunctionExpression([obj], t.blockStatement([
|
67
|
+
expectCode(t.program([t.expressionStatement(t.assignmentExpression('=', t.identifier('ownerOf'), arrowFunctionExpression([obj], t.blockStatement([
|
68
|
+
// body
|
43
69
|
], []), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier('OwnerOfResponse'))]))))))]));
|
44
70
|
});
|