wasm-ast-types 0.8.0 → 0.9.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.
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ var _arraysRef = _interopRequireDefault(require("../../../../../__fixtures__/misc/schema/arrays-ref.json"));
6
+
7
+ var _client = require("../client");
8
+
9
+ var _context = require("../../context");
10
+
11
+ var _testUtils = require("../../../test-utils");
12
+
13
+ var ctx = new _context.RenderContext(_arraysRef["default"]);
14
+ it('execute_msg_for__empty', function () {
15
+ (0, _testUtils.expectCode)((0, _client.createTypeInterface)(ctx, _arraysRef["default"]));
16
+ });
17
+ it('query classes', function () {
18
+ (0, _testUtils.expectCode)((0, _client.createQueryClass)(ctx, 'SG721QueryClient', 'SG721ReadOnlyInstance', _arraysRef["default"]));
19
+ });
20
+ it('execute classes array types', function () {
21
+ (0, _testUtils.expectCode)((0, _client.createExecuteClass)(ctx, 'SG721Client', 'SG721Instance', null, _arraysRef["default"]));
22
+ });
23
+ it('execute interfaces no extends', function () {
24
+ (0, _testUtils.expectCode)((0, _client.createExecuteInterface)(ctx, 'SG721Instance', null, _arraysRef["default"]));
25
+ });
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
6
+
7
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
8
+
9
+ var _testUtils = require("../../../test-utils");
10
+
11
+ var _client = require("../client");
12
+
13
+ var _context5 = require("../../context");
14
+
15
+ var _jestInCase = _interopRequireDefault(require("jest-in-case"));
16
+
17
+ var contracts = (0, _testUtils.globContracts)('issues/55');
18
+ (0, _jestInCase["default"])('execute_msg_for__empty', /*#__PURE__*/function () {
19
+ var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(opts) {
20
+ var ctx;
21
+ return _regenerator["default"].wrap(function _callee$(_context) {
22
+ while (1) {
23
+ switch (_context.prev = _context.next) {
24
+ case 0:
25
+ ctx = new _context5.RenderContext(opts.content);
26
+ (0, _testUtils.expectCode)((0, _client.createTypeInterface)(ctx, opts.content));
27
+
28
+ case 2:
29
+ case "end":
30
+ return _context.stop();
31
+ }
32
+ }
33
+ }, _callee);
34
+ }));
35
+
36
+ return function (_x) {
37
+ return _ref.apply(this, arguments);
38
+ };
39
+ }(), contracts);
40
+ (0, _jestInCase["default"])('query classes', /*#__PURE__*/function () {
41
+ var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(opts) {
42
+ var ctx;
43
+ return _regenerator["default"].wrap(function _callee2$(_context2) {
44
+ while (1) {
45
+ switch (_context2.prev = _context2.next) {
46
+ case 0:
47
+ ctx = new _context5.RenderContext(opts.content);
48
+ (0, _testUtils.expectCode)((0, _client.createQueryClass)(ctx, 'SG721QueryClient', 'SG721ReadOnlyInstance', opts.content));
49
+
50
+ case 2:
51
+ case "end":
52
+ return _context2.stop();
53
+ }
54
+ }
55
+ }, _callee2);
56
+ }));
57
+
58
+ return function (_x2) {
59
+ return _ref2.apply(this, arguments);
60
+ };
61
+ }(), contracts);
62
+ (0, _jestInCase["default"])('execute class', /*#__PURE__*/function () {
63
+ var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(opts) {
64
+ var ctx;
65
+ return _regenerator["default"].wrap(function _callee3$(_context3) {
66
+ while (1) {
67
+ switch (_context3.prev = _context3.next) {
68
+ case 0:
69
+ ctx = new _context5.RenderContext(opts.content);
70
+ (0, _testUtils.expectCode)((0, _client.createExecuteClass)(ctx, 'SG721Client', 'SG721Instance', null, opts.content));
71
+
72
+ case 2:
73
+ case "end":
74
+ return _context3.stop();
75
+ }
76
+ }
77
+ }, _callee3);
78
+ }));
79
+
80
+ return function (_x3) {
81
+ return _ref3.apply(this, arguments);
82
+ };
83
+ }(), contracts);
84
+ (0, _jestInCase["default"])('execute interface', /*#__PURE__*/function () {
85
+ var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(opts) {
86
+ var ctx;
87
+ return _regenerator["default"].wrap(function _callee4$(_context4) {
88
+ while (1) {
89
+ switch (_context4.prev = _context4.next) {
90
+ case 0:
91
+ ctx = new _context5.RenderContext(opts.content);
92
+ (0, _testUtils.expectCode)((0, _client.createExecuteInterface)(ctx, 'SG721Instance', null, opts.content));
93
+
94
+ case 2:
95
+ case "end":
96
+ return _context4.stop();
97
+ }
98
+ }
99
+ }, _callee4);
100
+ }));
101
+
102
+ return function (_x4) {
103
+ return _ref4.apply(this, arguments);
104
+ };
105
+ }(), contracts);
@@ -36,7 +36,8 @@ var defaultOptions = {
36
36
  optionalClient: false,
37
37
  version: 'v3',
38
38
  mutations: false,
39
- camelize: true
39
+ camelize: true,
40
+ queryKeys: false
40
41
  }
41
42
  };
42
43
  exports.defaultOptions = defaultOptions;
@@ -34,17 +34,38 @@ var createReactQueryHooks = function createReactQueryHooks(_ref) {
34
34
  QueryClient = _ref.QueryClient;
35
35
  var options = context.options.reactQuery;
36
36
  var genericQueryInterfaceName = "".concat((0, _case.pascal)(contractName), "ReactQuery");
37
- var body = [createReactQueryHookGenericInterface({
37
+ var underscoreNames = (0, _utils.getMessageProperties)(queryMsg).map(function (schema) {
38
+ return Object.keys(schema.properties)[0];
39
+ });
40
+ var body = [];
41
+ var queryKeysName = "".concat((0, _case.camel)(contractName), "QueryKeys");
42
+
43
+ if (options.queryKeys) {
44
+ body.push(createReactQueryKeys({
45
+ context: context,
46
+ queryKeysName: queryKeysName,
47
+ camelContractName: (0, _case.camel)(contractName),
48
+ underscoreNames: underscoreNames
49
+ }));
50
+ }
51
+
52
+ body.push(createReactQueryHookGenericInterface({
38
53
  context: context,
39
54
  QueryClient: QueryClient,
40
55
  genericQueryInterfaceName: genericQueryInterfaceName
41
- })];
56
+ }));
42
57
  body.push.apply(body, (0, _toConsumableArray2["default"])((0, _utils.getMessageProperties)(queryMsg).reduce(function (m, schema) {
43
- var underscoreName = Object.keys(schema.properties)[0];
44
- var methodName = (0, _case.camel)(underscoreName);
45
- var hookParamsTypeName = "".concat((0, _case.pascal)(contractName)).concat((0, _case.pascal)(methodName), "Query");
46
- var hookName = "use".concat(hookParamsTypeName);
47
- var responseType = (0, _case.pascal)("".concat(methodName, "Response"));
58
+ // list_voters
59
+ var underscoreName = Object.keys(schema.properties)[0]; // listVoters
60
+
61
+ var methodName = (0, _case.camel)(underscoreName); // Cw3FlexMultisigListVotersQuery
62
+
63
+ var hookParamsTypeName = "".concat((0, _case.pascal)(contractName)).concat((0, _case.pascal)(methodName), "Query"); // useCw3FlexMultisigListVotersQuery
64
+
65
+ var hookName = "use".concat(hookParamsTypeName); // listVotersResponse
66
+
67
+ var responseType = (0, _case.pascal)("".concat(methodName, "Response")); // cw3FlexMultisigListVoters
68
+
48
69
  var getterKey = (0, _case.camel)("".concat(contractName).concat((0, _case.pascal)(methodName)));
49
70
  var jsonschema = schema.properties[underscoreName];
50
71
  return [createReactQueryHookInterface({
@@ -59,6 +80,7 @@ var createReactQueryHooks = function createReactQueryHooks(_ref) {
59
80
  methodName: methodName,
60
81
  hookName: hookName,
61
82
  hookParamsTypeName: hookParamsTypeName,
83
+ queryKeysName: queryKeysName,
62
84
  responseType: responseType,
63
85
  hookKeyName: getterKey,
64
86
  jsonschema: jsonschema
@@ -77,6 +99,7 @@ var createReactQueryHook = function createReactQueryHook(_ref2) {
77
99
  hookParamsTypeName = _ref2.hookParamsTypeName,
78
100
  responseType = _ref2.responseType,
79
101
  hookKeyName = _ref2.hookKeyName,
102
+ queryKeysName = _ref2.queryKeysName,
80
103
  methodName = _ref2.methodName,
81
104
  jsonschema = _ref2.jsonschema;
82
105
  context.addUtil('useQuery');
@@ -97,10 +120,20 @@ var createReactQueryHook = function createReactQueryHook(_ref2) {
97
120
  props = ['client', 'args', 'options'];
98
121
  }
99
122
 
100
- return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(hookName), [(0, _utils.tsObjectPattern)((0, _toConsumableArray2["default"])(props.map(function (prop) {
123
+ var selectResponseGenericTypeName = 'TData';
124
+ var queryFunctionDeclaration = t.functionDeclaration(t.identifier(hookName), [(0, _utils.tsObjectPattern)((0, _toConsumableArray2["default"])(props.map(function (prop) {
101
125
  return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
102
- })), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName))))], t.blockStatement([t.returnStatement((0, _utils.callExpression)(t.identifier('useQuery'), [t.arrayExpression(generateUseQueryQueryKey(hookKeyName, props, options.optionalClient)), t.arrowFunctionExpression([], (0, _babel.optionalConditionalExpression)(t.identifier('client'), t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), t.callExpression(t.memberExpression(t.identifier('Promise'), t.identifier('reject')), [t.newExpression(t.identifier('Error'), [t.stringLiteral('Invalid client')])]), options.optionalClient), false), 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( // explicitly check for undefined
103
- 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')], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(responseType)), t.tsArrayType(t.tsParenthesizedType(t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])))])))])));
126
+ })), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(selectResponseGenericTypeName))]))))], t.blockStatement([t.returnStatement((0, _utils.callExpression)(t.identifier('useQuery'), [generateUseQueryQueryKey({
127
+ hookKeyName: hookKeyName,
128
+ queryKeysName: queryKeysName,
129
+ methodName: methodName,
130
+ props: props,
131
+ options: options
132
+ }), t.arrowFunctionExpression([], (0, _babel.optionalConditionalExpression)(t.identifier('client'), t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), t.callExpression(t.memberExpression(t.identifier('Promise'), t.identifier('reject')), [t.newExpression(t.identifier('Error'), [t.stringLiteral('Invalid client')])]), options.optionalClient), false), 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( // explicitly check for undefined
133
+ 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')], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(selectResponseGenericTypeName))])))])); // Add the TData type parameters
134
+
135
+ queryFunctionDeclaration.typeParameters = t.tsTypeParameterDeclaration([t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(responseType)), selectResponseGenericTypeName)]);
136
+ return t.exportNamedDeclaration(queryFunctionDeclaration);
104
137
  };
105
138
 
106
139
  exports.createReactQueryHook = createReactQueryHook;
@@ -237,25 +270,52 @@ var createReactQueryMutationHook = function createReactQueryMutationHook(_ref5)
237
270
 
238
271
  exports.createReactQueryMutationHook = createReactQueryMutationHook;
239
272
 
240
- function createReactQueryHookGenericInterface(_ref6) {
273
+ function createReactQueryKeys(_ref6) {
241
274
  var context = _ref6.context,
242
- QueryClient = _ref6.QueryClient,
243
- genericQueryInterfaceName = _ref6.genericQueryInterfaceName;
275
+ queryKeysName = _ref6.queryKeysName,
276
+ camelContractName = _ref6.camelContractName,
277
+ underscoreNames = _ref6.underscoreNames;
244
278
  var options = context.options.reactQuery;
245
- var genericTypeName = 'TResponse';
246
- context.addUtil('UseQueryOptions');
247
- var typedUseQueryOptions = t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(genericTypeName)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(genericTypeName)), t.tsArrayType(t.tsParenthesizedType(t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])))]));
248
- var body = [(0, _utils.tsPropertySignature)(t.identifier('client'), t.tsTypeAnnotation(options.optionalClient ? t.tsUnionType([t.tsTypeReference(t.identifier(QueryClient)), t.tsUndefinedKeyword()]) : t.tsTypeReference(t.identifier(QueryClient))), false), (0, _utils.tsPropertySignature)(t.identifier('options'), t.tsTypeAnnotation(options.version === 'v4' ? t.tSIntersectionType([(0, _babel.omitTypeReference)(typedUseQueryOptions, "'queryKey' | 'queryFn' | 'initialData'"), t.tSTypeLiteral([t.tsPropertySignature(t.identifier('initialData?'), t.tsTypeAnnotation(t.tsUndefinedKeyword()))])]) : typedUseQueryOptions), true)];
249
- return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(genericQueryInterfaceName), t.tsTypeParameterDeclaration([t.tsTypeParameter(undefined, undefined, genericTypeName)]), [], t.tSInterfaceBody(body)));
279
+ var contractAddressTypeAnnotation = t.tsTypeAnnotation(options.optionalClient ? t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()]) : t.tSStringKeyword());
280
+ return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(queryKeysName), t.objectExpression([// 1: contract
281
+ t.objectProperty(t.identifier('contract'), t.tSAsExpression(t.arrayExpression([t.objectExpression([t.objectProperty(t.identifier('contract'), t.stringLiteral(camelContractName))])]), t.tSTypeReference(t.identifier('const')))), // 2: address
282
+ t.objectProperty(t.identifier('address'), t.arrowFunctionExpression([(0, _utils.identifier)('contractAddress', contractAddressTypeAnnotation)], t.tSAsExpression(t.arrayExpression([t.objectExpression([// 1
283
+ t.spreadElement(t.memberExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier('contract')), t.numericLiteral(0), true // computed
284
+ )), t.objectProperty(t.identifier('address'), t.identifier('contractAddress'))])]), t.tSTypeReference(t.identifier('const')))))].concat((0, _toConsumableArray2["default"])(underscoreNames.map(function (underscoreMethodName) {
285
+ return t.objectProperty( // key id is the camel method name
286
+ t.identifier((0, _case.camel)(underscoreMethodName)), t.arrowFunctionExpression([(0, _utils.identifier)('contractAddress', contractAddressTypeAnnotation), (0, _utils.identifier)('args', // 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([//...cw3FlexMultisigQueryKeys.address(contractAddress)[0]
289
+ t.spreadElement(t.memberExpression(t.callExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier('address')), [t.identifier('contractAddress')]), t.numericLiteral(0), true // computed
290
+ )), // method: list_voters
291
+ t.objectProperty(t.identifier('method'), t.stringLiteral(underscoreMethodName)), // args
292
+ (0, _babel.shorthandProperty)('args')])]), t.tSTypeReference(t.identifier('const')))));
293
+ })))))]));
250
294
  }
251
295
 
252
- var createReactQueryHookInterface = function createReactQueryHookInterface(_ref7) {
296
+ function createReactQueryHookGenericInterface(_ref7) {
253
297
  var context = _ref7.context,
254
298
  QueryClient = _ref7.QueryClient,
255
- hookParamsTypeName = _ref7.hookParamsTypeName,
256
- queryInterfaceName = _ref7.queryInterfaceName,
257
- responseType = _ref7.responseType,
258
- jsonschema = _ref7.jsonschema;
299
+ genericQueryInterfaceName = _ref7.genericQueryInterfaceName;
300
+ var options = context.options.reactQuery;
301
+ var genericResponseTypeName = 'TResponse';
302
+ var genericSelectResponseTypeName = 'TData';
303
+ context.addUtil('UseQueryOptions'); // UseQueryOptions<TResponse, Error, TData>,
304
+
305
+ var typedUseQueryOptions = t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(genericResponseTypeName)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(genericSelectResponseTypeName))]));
306
+ var body = [(0, _utils.tsPropertySignature)(t.identifier('client'), t.tsTypeAnnotation(options.optionalClient ? t.tsUnionType([t.tsTypeReference(t.identifier(QueryClient)), t.tsUndefinedKeyword()]) : t.tsTypeReference(t.identifier(QueryClient))), false), (0, _utils.tsPropertySignature)(t.identifier('options'), t.tsTypeAnnotation(options.version === 'v4' ? t.tSIntersectionType([(0, _babel.omitTypeReference)(typedUseQueryOptions, "'queryKey' | 'queryFn' | 'initialData'"), t.tSTypeLiteral([t.tsPropertySignature(t.identifier('initialData?'), t.tsTypeAnnotation(t.tsUndefinedKeyword()))])]) : typedUseQueryOptions), true)];
307
+ return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(genericQueryInterfaceName), t.tsTypeParameterDeclaration([// 1: TResponse
308
+ t.tsTypeParameter(undefined, undefined, genericResponseTypeName), // 2: TData
309
+ t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(genericResponseTypeName)), genericSelectResponseTypeName)]), [], t.tSInterfaceBody(body)));
310
+ }
311
+
312
+ var createReactQueryHookInterface = function createReactQueryHookInterface(_ref8) {
313
+ var context = _ref8.context,
314
+ QueryClient = _ref8.QueryClient,
315
+ hookParamsTypeName = _ref8.hookParamsTypeName,
316
+ queryInterfaceName = _ref8.queryInterfaceName,
317
+ responseType = _ref8.responseType,
318
+ jsonschema = _ref8.jsonschema;
259
319
  // merge the user options with the defaults
260
320
  var options = context.options.reactQuery;
261
321
  var body = [];
@@ -266,7 +326,9 @@ var createReactQueryHookInterface = function createReactQueryHookInterface(_ref7
266
326
  t.tsTypeLiteral(props))));
267
327
  }
268
328
 
269
- return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(hookParamsTypeName), null, [t.tSExpressionWithTypeArguments(t.identifier(queryInterfaceName), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType))]))], t.tsInterfaceBody(body)));
329
+ return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(hookParamsTypeName), t.tsTypeParameterDeclaration([t.tSTypeParameter(undefined, undefined, 'TData')]), [t.tSExpressionWithTypeArguments(t.identifier(queryInterfaceName), t.tsTypeParameterInstantiation([// 1: response
330
+ t.tsTypeReference(t.identifier(responseType)), // 2: select generic
331
+ t.tSTypeReference(t.identifier('TData'))]))], t.tsInterfaceBody(body)));
270
332
  };
271
333
 
272
334
  exports.createReactQueryHookInterface = createReactQueryHookInterface;
@@ -285,12 +347,28 @@ var getProps = function getProps(context, jsonschema) {
285
347
  });
286
348
  };
287
349
 
288
- var generateUseQueryQueryKey = function generateUseQueryQueryKey(hookKeyName, props, optionalClient) {
289
- var queryKey = [t.stringLiteral(hookKeyName), t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient)];
350
+ var generateUseQueryQueryKey = function generateUseQueryQueryKey(_ref9) {
351
+ var hookKeyName = _ref9.hookKeyName,
352
+ queryKeysName = _ref9.queryKeysName,
353
+ methodName = _ref9.methodName,
354
+ props = _ref9.props,
355
+ options = _ref9.options;
356
+ var optionalClient = options.optionalClient,
357
+ queryKeys = options.queryKeys;
358
+ var hasArgs = props.includes('args');
359
+ var contractAddressExpression = t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient);
360
+
361
+ if (queryKeys) {
362
+ var callArgs = [contractAddressExpression];
363
+ if (hasArgs) callArgs.push(t.identifier('args'));
364
+ return t.callExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier((0, _case.camel)(methodName))), callArgs);
365
+ }
366
+
367
+ var queryKey = [t.stringLiteral(hookKeyName), contractAddressExpression];
290
368
 
291
- if (props.includes('args')) {
369
+ if (hasArgs) {
292
370
  queryKey.push(t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.identifier('args')]));
293
371
  }
294
372
 
295
- return queryKey;
373
+ return t.arrayExpression(queryKey);
296
374
  };
@@ -45,10 +45,22 @@ var getArrayTypeFromRef = function getArrayTypeFromRef($ref) {
45
45
  return t.tsArrayType(getTypeFromRef($ref));
46
46
  };
47
47
 
48
+ var getTypeOrRef = function getTypeOrRef(obj) {
49
+ if (obj.type) {
50
+ return getType(obj.type);
51
+ }
52
+
53
+ if (obj.$ref) {
54
+ return getTypeFromRef(obj.$ref);
55
+ }
56
+
57
+ throw new Error('contact maintainers cannot find type for ' + obj);
58
+ };
59
+
48
60
  var getArrayTypeFromItems = function getArrayTypeFromItems(items) {
49
61
  if (items.type === 'array') {
50
62
  if (Array.isArray(items.items)) {
51
- return t.tsArrayType(t.tsArrayType(getType(items.items[0].type)));
63
+ return t.tsArrayType(t.tsArrayType(getTypeOrRef(items.items[0])));
52
64
  } else {
53
65
  return t.tsArrayType(getArrayTypeFromItems(items.items));
54
66
  }
@@ -0,0 +1,17 @@
1
+ import message from '../../../../../__fixtures__/misc/schema/arrays-ref.json';
2
+ import { createQueryClass, createExecuteClass, createExecuteInterface, createTypeInterface } from '../client';
3
+ import { RenderContext } from '../../context';
4
+ import { expectCode } from '../../../test-utils';
5
+ const ctx = new RenderContext(message);
6
+ it('execute_msg_for__empty', () => {
7
+ expectCode(createTypeInterface(ctx, message));
8
+ });
9
+ it('query classes', () => {
10
+ expectCode(createQueryClass(ctx, 'SG721QueryClient', 'SG721ReadOnlyInstance', message));
11
+ });
12
+ it('execute classes array types', () => {
13
+ expectCode(createExecuteClass(ctx, 'SG721Client', 'SG721Instance', null, message));
14
+ });
15
+ it('execute interfaces no extends', () => {
16
+ expectCode(createExecuteInterface(ctx, 'SG721Instance', null, message));
17
+ });
@@ -0,0 +1,22 @@
1
+ import { globContracts } from '../../../test-utils';
2
+ import { createQueryClass, createExecuteClass, createExecuteInterface, createTypeInterface } from '../client';
3
+ import { RenderContext } from '../../context';
4
+ import { expectCode } from '../../../test-utils';
5
+ import cases from 'jest-in-case';
6
+ const contracts = globContracts('issues/55');
7
+ cases('execute_msg_for__empty', async opts => {
8
+ const ctx = new RenderContext(opts.content);
9
+ expectCode(createTypeInterface(ctx, opts.content));
10
+ }, contracts);
11
+ cases('query classes', async opts => {
12
+ const ctx = new RenderContext(opts.content);
13
+ expectCode(createQueryClass(ctx, 'SG721QueryClient', 'SG721ReadOnlyInstance', opts.content));
14
+ }, contracts);
15
+ cases('execute class', async opts => {
16
+ const ctx = new RenderContext(opts.content);
17
+ expectCode(createExecuteClass(ctx, 'SG721Client', 'SG721Instance', null, opts.content));
18
+ }, contracts);
19
+ cases('execute interface', async opts => {
20
+ const ctx = new RenderContext(opts.content);
21
+ expectCode(createExecuteInterface(ctx, 'SG721Instance', null, opts.content));
22
+ }, contracts);
@@ -21,7 +21,8 @@ export const defaultOptions = {
21
21
  optionalClient: false,
22
22
  version: 'v3',
23
23
  mutations: false,
24
- camelize: true
24
+ camelize: true,
25
+ queryKeys: false
25
26
  }
26
27
  };
27
28
  export class RenderContext {
@@ -12,17 +12,36 @@ export const createReactQueryHooks = ({
12
12
  }) => {
13
13
  const options = context.options.reactQuery;
14
14
  const genericQueryInterfaceName = `${pascal(contractName)}ReactQuery`;
15
- const body = [createReactQueryHookGenericInterface({
15
+ const underscoreNames = getMessageProperties(queryMsg).map(schema => Object.keys(schema.properties)[0]);
16
+ const body = [];
17
+ const queryKeysName = `${camel(contractName)}QueryKeys`;
18
+
19
+ if (options.queryKeys) {
20
+ body.push(createReactQueryKeys({
21
+ context,
22
+ queryKeysName,
23
+ camelContractName: camel(contractName),
24
+ underscoreNames
25
+ }));
26
+ }
27
+
28
+ body.push(createReactQueryHookGenericInterface({
16
29
  context,
17
30
  QueryClient,
18
31
  genericQueryInterfaceName
19
- })];
32
+ }));
20
33
  body.push(...getMessageProperties(queryMsg).reduce((m, schema) => {
21
- const underscoreName = Object.keys(schema.properties)[0];
22
- const methodName = camel(underscoreName);
23
- const hookParamsTypeName = `${pascal(contractName)}${pascal(methodName)}Query`;
24
- const hookName = `use${hookParamsTypeName}`;
25
- const responseType = pascal(`${methodName}Response`);
34
+ // list_voters
35
+ const underscoreName = Object.keys(schema.properties)[0]; // listVoters
36
+
37
+ const methodName = camel(underscoreName); // Cw3FlexMultisigListVotersQuery
38
+
39
+ const hookParamsTypeName = `${pascal(contractName)}${pascal(methodName)}Query`; // useCw3FlexMultisigListVotersQuery
40
+
41
+ const hookName = `use${hookParamsTypeName}`; // listVotersResponse
42
+
43
+ const responseType = pascal(`${methodName}Response`); // cw3FlexMultisigListVoters
44
+
26
45
  const getterKey = camel(`${contractName}${pascal(methodName)}`);
27
46
  const jsonschema = schema.properties[underscoreName];
28
47
  return [createReactQueryHookInterface({
@@ -37,6 +56,7 @@ export const createReactQueryHooks = ({
37
56
  methodName,
38
57
  hookName,
39
58
  hookParamsTypeName,
59
+ queryKeysName,
40
60
  responseType,
41
61
  hookKeyName: getterKey,
42
62
  jsonschema
@@ -50,6 +70,7 @@ export const createReactQueryHook = ({
50
70
  hookParamsTypeName,
51
71
  responseType,
52
72
  hookKeyName,
73
+ queryKeysName,
53
74
  methodName,
54
75
  jsonschema
55
76
  }) => {
@@ -71,10 +92,20 @@ export const createReactQueryHook = ({
71
92
  props = ['client', 'args', 'options'];
72
93
  }
73
94
 
74
- return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(hookName), [tsObjectPattern([...props.map(prop => {
95
+ const selectResponseGenericTypeName = 'TData';
96
+ const queryFunctionDeclaration = t.functionDeclaration(t.identifier(hookName), [tsObjectPattern([...props.map(prop => {
75
97
  return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
76
- })], t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName))))], t.blockStatement([t.returnStatement(callExpression(t.identifier('useQuery'), [t.arrayExpression(generateUseQueryQueryKey(hookKeyName, props, options.optionalClient)), t.arrowFunctionExpression([], optionalConditionalExpression(t.identifier('client'), t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), t.callExpression(t.memberExpression(t.identifier('Promise'), t.identifier('reject')), [t.newExpression(t.identifier('Error'), [t.stringLiteral('Invalid client')])]), options.optionalClient), false), 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( // explicitly check for undefined
77
- 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')], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(responseType)), t.tsArrayType(t.tsParenthesizedType(t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])))])))])));
98
+ })], t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(selectResponseGenericTypeName))]))))], t.blockStatement([t.returnStatement(callExpression(t.identifier('useQuery'), [generateUseQueryQueryKey({
99
+ hookKeyName,
100
+ queryKeysName,
101
+ methodName,
102
+ props,
103
+ options
104
+ }), t.arrowFunctionExpression([], optionalConditionalExpression(t.identifier('client'), t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), t.callExpression(t.memberExpression(t.identifier('Promise'), t.identifier('reject')), [t.newExpression(t.identifier('Error'), [t.stringLiteral('Invalid client')])]), options.optionalClient), false), 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( // 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))))]) : t.identifier('options')], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(selectResponseGenericTypeName))])))])); // Add the TData type parameters
106
+
107
+ queryFunctionDeclaration.typeParameters = t.tsTypeParameterDeclaration([t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(responseType)), selectResponseGenericTypeName)]);
108
+ return t.exportNamedDeclaration(queryFunctionDeclaration);
78
109
  };
79
110
 
80
111
  /**
@@ -196,17 +227,44 @@ export const createReactQueryMutationHook = ({
196
227
  ), t.identifier('options')], useMutationTypeParameter))])));
197
228
  };
198
229
 
230
+ function createReactQueryKeys({
231
+ context,
232
+ queryKeysName,
233
+ camelContractName,
234
+ underscoreNames
235
+ }) {
236
+ const options = context.options.reactQuery;
237
+ const contractAddressTypeAnnotation = t.tsTypeAnnotation(options.optionalClient ? t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()]) : t.tSStringKeyword());
238
+ return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(queryKeysName), t.objectExpression([// 1: contract
239
+ t.objectProperty(t.identifier('contract'), t.tSAsExpression(t.arrayExpression([t.objectExpression([t.objectProperty(t.identifier('contract'), t.stringLiteral(camelContractName))])]), t.tSTypeReference(t.identifier('const')))), // 2: address
240
+ t.objectProperty(t.identifier('address'), t.arrowFunctionExpression([identifier('contractAddress', contractAddressTypeAnnotation)], t.tSAsExpression(t.arrayExpression([t.objectExpression([// 1
241
+ t.spreadElement(t.memberExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier('contract')), t.numericLiteral(0), true // computed
242
+ )), t.objectProperty(t.identifier('address'), t.identifier('contractAddress'))])]), t.tSTypeReference(t.identifier('const'))))), // 3: methods
243
+ ...underscoreNames.map(underscoreMethodName => t.objectProperty( // key id is the camel method name
244
+ t.identifier(camel(underscoreMethodName)), t.arrowFunctionExpression([identifier('contractAddress', contractAddressTypeAnnotation), identifier('args', // Record<string, unknown>
245
+ t.tSTypeAnnotation(t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([t.tsStringKeyword(), t.tsUnknownKeyword()]))), true // optional
246
+ )], t.tSAsExpression(t.arrayExpression([t.objectExpression([//...cw3FlexMultisigQueryKeys.address(contractAddress)[0]
247
+ t.spreadElement(t.memberExpression(t.callExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier('address')), [t.identifier('contractAddress')]), t.numericLiteral(0), true // computed
248
+ )), // method: list_voters
249
+ t.objectProperty(t.identifier('method'), t.stringLiteral(underscoreMethodName)), // args
250
+ shorthandProperty('args')])]), t.tSTypeReference(t.identifier('const'))))))]))]));
251
+ }
252
+
199
253
  function createReactQueryHookGenericInterface({
200
254
  context,
201
255
  QueryClient,
202
256
  genericQueryInterfaceName
203
257
  }) {
204
258
  const options = context.options.reactQuery;
205
- const genericTypeName = 'TResponse';
206
- context.addUtil('UseQueryOptions');
207
- const typedUseQueryOptions = t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(genericTypeName)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(genericTypeName)), t.tsArrayType(t.tsParenthesizedType(t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])))]));
259
+ const genericResponseTypeName = 'TResponse';
260
+ const genericSelectResponseTypeName = 'TData';
261
+ context.addUtil('UseQueryOptions'); // UseQueryOptions<TResponse, Error, TData>,
262
+
263
+ const typedUseQueryOptions = t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(genericResponseTypeName)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(genericSelectResponseTypeName))]));
208
264
  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)];
209
- return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(genericQueryInterfaceName), t.tsTypeParameterDeclaration([t.tsTypeParameter(undefined, undefined, genericTypeName)]), [], t.tSInterfaceBody(body)));
265
+ return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(genericQueryInterfaceName), t.tsTypeParameterDeclaration([// 1: TResponse
266
+ t.tsTypeParameter(undefined, undefined, genericResponseTypeName), // 2: TData
267
+ t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(genericResponseTypeName)), genericSelectResponseTypeName)]), [], t.tSInterfaceBody(body)));
210
268
  }
211
269
 
212
270
  export const createReactQueryHookInterface = ({
@@ -227,7 +285,9 @@ export const createReactQueryHookInterface = ({
227
285
  t.tsTypeLiteral(props))));
228
286
  }
229
287
 
230
- return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(hookParamsTypeName), null, [t.tSExpressionWithTypeArguments(t.identifier(queryInterfaceName), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType))]))], t.tsInterfaceBody(body)));
288
+ return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(hookParamsTypeName), t.tsTypeParameterDeclaration([t.tSTypeParameter(undefined, undefined, 'TData')]), [t.tSExpressionWithTypeArguments(t.identifier(queryInterfaceName), t.tsTypeParameterInstantiation([// 1: response
289
+ t.tsTypeReference(t.identifier(responseType)), // 2: select generic
290
+ t.tSTypeReference(t.identifier('TData'))]))], t.tsInterfaceBody(body)));
231
291
  };
232
292
 
233
293
  const getProps = (context, jsonschema) => {
@@ -242,12 +302,31 @@ const getProps = (context, jsonschema) => {
242
302
  });
243
303
  };
244
304
 
245
- const generateUseQueryQueryKey = (hookKeyName, props, optionalClient) => {
246
- const queryKey = [t.stringLiteral(hookKeyName), t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient)];
305
+ const generateUseQueryQueryKey = ({
306
+ hookKeyName,
307
+ queryKeysName,
308
+ methodName,
309
+ props,
310
+ options
311
+ }) => {
312
+ const {
313
+ optionalClient,
314
+ queryKeys
315
+ } = options;
316
+ const hasArgs = props.includes('args');
317
+ const contractAddressExpression = t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient);
318
+
319
+ if (queryKeys) {
320
+ const callArgs = [contractAddressExpression];
321
+ if (hasArgs) callArgs.push(t.identifier('args'));
322
+ return t.callExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier(camel(methodName))), callArgs);
323
+ }
324
+
325
+ const queryKey = [t.stringLiteral(hookKeyName), contractAddressExpression];
247
326
 
248
- if (props.includes('args')) {
327
+ if (hasArgs) {
249
328
  queryKey.push(t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.identifier('args')]));
250
329
  }
251
330
 
252
- return queryKey;
331
+ return t.arrayExpression(queryKey);
253
332
  };
@@ -18,10 +18,22 @@ const getArrayTypeFromRef = $ref => {
18
18
  return t.tsArrayType(getTypeFromRef($ref));
19
19
  };
20
20
 
21
+ const getTypeOrRef = obj => {
22
+ if (obj.type) {
23
+ return getType(obj.type);
24
+ }
25
+
26
+ if (obj.$ref) {
27
+ return getTypeFromRef(obj.$ref);
28
+ }
29
+
30
+ throw new Error('contact maintainers cannot find type for ' + obj);
31
+ };
32
+
21
33
  const getArrayTypeFromItems = items => {
22
34
  if (items.type === 'array') {
23
35
  if (Array.isArray(items.items)) {
24
- return t.tsArrayType(t.tsArrayType(getType(items.items[0].type)));
36
+ return t.tsArrayType(t.tsArrayType(getTypeOrRef(items.items[0])));
25
37
  } else {
26
38
  return t.tsArrayType(getArrayTypeFromItems(items.items));
27
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "CosmWasm TypeScript AST generation",
5
5
  "author": "Dan Lynch <pyramation@gmail.com>",
6
6
  "homepage": "https://github.com/pyramation/cosmwasm-typescript-gen/tree/master/packages/wasm-ast-types#readme",
@@ -86,5 +86,5 @@
86
86
  "case": "1.6.3",
87
87
  "deepmerge": "4.2.2"
88
88
  },
89
- "gitHead": "93256e4c8579d792ca727ef8f1e9ac47a5231726"
89
+ "gitHead": "3b7ca02c9283c346a365d8426d3034af355afcac"
90
90
  }
@@ -5,6 +5,7 @@ export interface ReactQueryOptions {
5
5
  version?: 'v3' | 'v4';
6
6
  mutations?: boolean;
7
7
  camelize?: boolean;
8
+ queryKeys?: boolean;
8
9
  }
9
10
  export interface TSClientOptions {
10
11
  enabled?: boolean;