wasm-ast-types 0.4.0 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,6 +11,8 @@ exports.createReactQueryHooks = exports.createReactQueryHookInterface = exports.
11
11
 
12
12
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
13
 
14
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
15
+
14
16
  var t = _interopRequireWildcard(require("@babel/types"));
15
17
 
16
18
  var _case = require("case");
@@ -25,8 +27,13 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
25
27
 
26
28
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
27
29
 
30
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
31
+
32
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
33
+
28
34
  var DEFAULT_OPTIONS = {
29
- optionalClient: false
35
+ optionalClient: false,
36
+ v4: false
30
37
  };
31
38
 
32
39
  var createReactQueryHooks = function createReactQueryHooks(_ref) {
@@ -34,7 +41,9 @@ var createReactQueryHooks = function createReactQueryHooks(_ref) {
34
41
  contractName = _ref.contractName,
35
42
  QueryClient = _ref.QueryClient,
36
43
  _ref$options = _ref.options,
37
- options = _ref$options === void 0 ? DEFAULT_OPTIONS : _ref$options;
44
+ options = _ref$options === void 0 ? {} : _ref$options;
45
+ // merge the user options with the defaults
46
+ options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
38
47
  return (0, _utils.getMessageProperties)(queryMsg).reduce(function (m, schema) {
39
48
  var underscoreName = Object.keys(schema.properties)[0];
40
49
  var methodName = (0, _case.camel)(underscoreName);
@@ -73,7 +82,9 @@ var createReactQueryHook = function createReactQueryHook(_ref2) {
73
82
  methodName = _ref2.methodName,
74
83
  jsonschema = _ref2.jsonschema,
75
84
  _ref2$options = _ref2.options,
76
- options = _ref2$options === void 0 ? DEFAULT_OPTIONS : _ref2$options;
85
+ options = _ref2$options === void 0 ? {} : _ref2$options;
86
+ // merge the user options with the defaults
87
+ options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
77
88
  var keys = Object.keys((_jsonschema$propertie = jsonschema.properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {});
78
89
  var args = [];
79
90
 
@@ -91,7 +102,8 @@ var createReactQueryHook = function createReactQueryHook(_ref2) {
91
102
 
92
103
  return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(hookName), [(0, _utils.tsObjectPattern)((0, _toConsumableArray2["default"])(props.map(function (prop) {
93
104
  return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
94
- })), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName))))], t.blockStatement([t.returnStatement((0, _utils.callExpression)(t.identifier('useQuery'), [t.arrayExpression([t.stringLiteral(hookKeyName), t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, options.optionalClient)]), t.arrowFunctionExpression([], (0, _babel.optionalConditionalExpression)(t.identifier('client'), t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), t.identifier('undefined'), 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.optionalMemberExpression(t.identifier('options'), t.identifier('enabled'), false, true)))]) : t.identifier('options')], t.tsTypeParameterInstantiation([(0, _babel.typeRefOrOptionalUnion)(t.identifier(responseType), options.optionalClient), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(responseType)), t.tsArrayType(t.tsParenthesizedType(t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])))])))])));
105
+ })), 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.identifier('undefined'), 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
106
+ 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([(0, _babel.typeRefOrOptionalUnion)(t.identifier(responseType), options.optionalClient), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(responseType)), t.tsArrayType(t.tsParenthesizedType(t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])))])))])));
95
107
  };
96
108
 
97
109
  exports.createReactQueryHook = createReactQueryHook;
@@ -102,8 +114,11 @@ var createReactQueryHookInterface = function createReactQueryHookInterface(_ref3
102
114
  responseType = _ref3.responseType,
103
115
  jsonschema = _ref3.jsonschema,
104
116
  _ref3$options = _ref3.options,
105
- options = _ref3$options === void 0 ? DEFAULT_OPTIONS : _ref3$options;
106
- var body = [(0, _utils.tsPropertySignature)(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(QueryClient))), options.optionalClient), (0, _utils.tsPropertySignature)(t.identifier('options'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([(0, _babel.typeRefOrOptionalUnion)(t.identifier(responseType), options.optionalClient), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(responseType)), t.tsArrayType(t.tsParenthesizedType(t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])))]))), true)];
117
+ options = _ref3$options === void 0 ? {} : _ref3$options;
118
+ // merge the user options with the defaults
119
+ options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
120
+ var typedUseQueryOptions = t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([(0, _babel.typeRefOrOptionalUnion)(t.identifier(responseType), options.optionalClient), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(responseType)), t.tsArrayType(t.tsParenthesizedType(t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])))]));
121
+ var body = [(0, _utils.tsPropertySignature)(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(QueryClient))), options.optionalClient), (0, _utils.tsPropertySignature)(t.identifier('options'), t.tsTypeAnnotation(options.v4 ? t.tSIntersectionType([t.tsTypeReference(t.identifier('Omit'), t.tsTypeParameterInstantiation([typedUseQueryOptions, t.tsLiteralType(t.stringLiteral("'queryKey' | 'queryFn' | 'initialData'"))])), t.tSTypeLiteral([t.tsPropertySignature(t.identifier('initialData?'), t.tsTypeAnnotation(t.tsUndefinedKeyword()))])]) : typedUseQueryOptions), true)];
107
122
  var props = getProps(jsonschema, true);
108
123
 
109
124
  if (props.length) {
@@ -127,4 +142,14 @@ var getProps = function getProps(jsonschema, camelize) {
127
142
 
128
143
  return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional);
129
144
  });
130
- };
145
+ };
146
+
147
+ function generateUseQueryQueryKey(hookKeyName, props, optionalClient) {
148
+ var queryKey = [t.stringLiteral(hookKeyName), t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient)];
149
+
150
+ if (props.includes('args')) {
151
+ queryKey.push(t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.identifier('args')]));
152
+ }
153
+
154
+ return queryKey;
155
+ }
@@ -52,4 +52,21 @@ it('createReactQueryHooks', function () {
52
52
  optionalClient: true
53
53
  }
54
54
  })));
55
+ expectCode(t.program((0, _reactQuery.createReactQueryHooks)({
56
+ queryMsg: _query_msg["default"],
57
+ contractName: 'Sg721',
58
+ QueryClient: 'Sg721QueryClient',
59
+ options: {
60
+ v4: true
61
+ }
62
+ })));
63
+ expectCode(t.program((0, _reactQuery.createReactQueryHooks)({
64
+ queryMsg: _query_msg["default"],
65
+ contractName: 'Sg721',
66
+ QueryClient: 'Sg721QueryClient',
67
+ options: {
68
+ optionalClient: true,
69
+ v4: true
70
+ }
71
+ })));
55
72
  });
package/main/recoil.js CHANGED
@@ -39,8 +39,8 @@ var createRecoilSelector = function createRecoilSelector(keyPrefix, QueryClient,
39
39
  var selectorName = (0, _case.camel)("".concat(methodName, "Selector"));
40
40
  var responseType = (0, _case.pascal)("".concat(methodName, "Response"));
41
41
  var getterKey = (0, _case.camel)("".concat(keyPrefix).concat((0, _case.pascal)(methodName)));
42
- return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(selectorName), (0, _utils.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.ifStatement(t.unaryExpression('!', t.identifier('client')), t.returnStatement(null), null), t.returnStatement(t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), [// t.identifier('params')
43
- t.spreadElement(t.identifier('params'))])))]), true)))])], t.tsTypeParameterInstantiation([t.tsUnionType([t.tsTypeReference(t.identifier(responseType)), t.tsUndefinedKeyword()]), 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)))]))))])])])))]));
42
+ return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(selectorName), (0, _utils.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.identifier('params')
43
+ 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)))]))))])])])))]));
44
44
  };
45
45
 
46
46
  exports.createRecoilSelector = createRecoilSelector;
@@ -86,7 +86,7 @@ exports.createRecoilQueryClientType = createRecoilQueryClientType;
86
86
 
87
87
  var createRecoilQueryClient = function createRecoilQueryClient(keyPrefix, QueryClient) {
88
88
  var getterKey = (0, _case.camel)("".concat(keyPrefix, 'QueryClient'));
89
- return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier('queryClient'), (0, _utils.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.ifStatement(t.unaryExpression('!', t.identifier('client')), t.returnStatement(null), null), t.returnStatement(t.newExpression(t.identifier(QueryClient), [t.identifier('client'), t.identifier('contractAddress')]))]), false)))])], t.tsTypeParameterInstantiation([t.tsUnionType([t.tsTypeReference(t.identifier(QueryClient)), t.tsUndefinedKeyword()]), t.tsTypeReference(t.identifier('QueryClientParams'))])))]));
89
+ return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier('queryClient'), (0, _utils.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'))])))]));
90
90
  };
91
91
 
92
92
  exports.createRecoilQueryClient = createRecoilQueryClient;
@@ -31,5 +31,5 @@ it('selectors', function () {
31
31
  expectCode(t.program((0, _recoil.createRecoilSelectors)('SG721', 'SG721QueryClient', _query_msg["default"])));
32
32
  });
33
33
  it('client', function () {
34
- printCode((0, _recoil.createRecoilQueryClient)('SG721', 'SG721QueryClient'));
34
+ expectCode((0, _recoil.createRecoilQueryClient)('SG721', 'SG721QueryClient'));
35
35
  });
@@ -55,8 +55,16 @@ var getArrayTypeFromRef = function getArrayTypeFromRef($ref) {
55
55
  return t.tsArrayType(getTypeFromRef($ref));
56
56
  };
57
57
 
58
- var getArrayTypeFromType = function getArrayTypeFromType(type) {
59
- return t.tsArrayType(getType(type));
58
+ var getArrayTypeFromItems = function getArrayTypeFromItems(items) {
59
+ if (items.type === 'array') {
60
+ if (Array.isArray(items.items)) {
61
+ return t.tsArrayType(t.tsArrayType(getType(items.items[0].type)));
62
+ } else {
63
+ return t.tsArrayType(getArrayTypeFromItems(items.items));
64
+ }
65
+ }
66
+
67
+ return t.tsArrayType(getType(items.type));
60
68
  };
61
69
 
62
70
  var getType = function getType(type) {
@@ -69,8 +77,6 @@ var getType = function getType(type) {
69
77
 
70
78
  case 'integer':
71
79
  return t.tsNumberKeyword();
72
- // case 'object':
73
- // return t.tsObjectKeyword();
74
80
 
75
81
  default:
76
82
  throw new Error('contact maintainers [unknown type]: ' + type);
@@ -129,7 +135,7 @@ var getPropertyType = function getPropertyType(schema, prop) {
129
135
  } else if (info.items.title) {
130
136
  type = t.tsArrayType(t.tsTypeReference(t.identifier(info.items.title)));
131
137
  } else if (info.items.type) {
132
- type = getArrayTypeFromType(info.items.type);
138
+ type = getArrayTypeFromItems(info.items);
133
139
  } else {
134
140
  throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
135
141
  }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ var _generator = _interopRequireDefault(require("@babel/generator"));
6
+
7
+ var _schema = _interopRequireDefault(require("./../../../__fixtures__/arrays/schema/schema.json"));
8
+
9
+ var _wasm = require("./wasm");
10
+
11
+ var expectCode = function expectCode(ast) {
12
+ expect((0, _generator["default"])(ast).code).toMatchSnapshot();
13
+ };
14
+
15
+ var printCode = function printCode(ast) {
16
+ console.log((0, _generator["default"])(ast).code);
17
+ };
18
+
19
+ it('execute_msg_for__empty', function () {
20
+ expectCode((0, _wasm.createTypeInterface)(_schema["default"]));
21
+ });
22
+ it('query classes', function () {
23
+ expectCode((0, _wasm.createQueryClass)('SG721QueryClient', 'SG721ReadOnlyInstance', _schema["default"]));
24
+ });
25
+ it('execute classes array types', function () {
26
+ expectCode((0, _wasm.createExecuteClass)('SG721Client', 'SG721Instance', null, _schema["default"]));
27
+ });
28
+ it('execute interfaces no extends', function () {
29
+ expectCode((0, _wasm.createExecuteInterface)('SG721Instance', null, _schema["default"]));
30
+ });
@@ -1,17 +1,26 @@
1
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
+
3
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
+
5
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
+
1
7
  import * as t from '@babel/types';
2
8
  import { camel, pascal } from 'case';
3
9
  import { tsPropertySignature, tsObjectPattern, callExpression, getMessageProperties } from './utils';
4
10
  import { typeRefOrOptionalUnion, propertySignature, optionalConditionalExpression } from './utils/babel';
5
11
  import { getPropertyType } from './utils/types';
6
12
  const DEFAULT_OPTIONS = {
7
- optionalClient: false
13
+ optionalClient: false,
14
+ v4: false
8
15
  };
9
16
  export const createReactQueryHooks = ({
10
17
  queryMsg,
11
18
  contractName,
12
19
  QueryClient,
13
- options = DEFAULT_OPTIONS
20
+ options = {}
14
21
  }) => {
22
+ // merge the user options with the defaults
23
+ options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
15
24
  return getMessageProperties(queryMsg).reduce((m, schema) => {
16
25
  const underscoreName = Object.keys(schema.properties)[0];
17
26
  const methodName = camel(underscoreName);
@@ -44,8 +53,10 @@ export const createReactQueryHook = ({
44
53
  hookKeyName,
45
54
  methodName,
46
55
  jsonschema,
47
- options = DEFAULT_OPTIONS
56
+ options = {}
48
57
  }) => {
58
+ // merge the user options with the defaults
59
+ options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
49
60
  const keys = Object.keys(jsonschema.properties ?? {});
50
61
  let args = [];
51
62
 
@@ -63,16 +74,20 @@ export const createReactQueryHook = ({
63
74
 
64
75
  return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(hookName), [tsObjectPattern([...props.map(prop => {
65
76
  return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
66
- })], t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName))))], t.blockStatement([t.returnStatement(callExpression(t.identifier('useQuery'), [t.arrayExpression([t.stringLiteral(hookKeyName), t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, options.optionalClient)]), t.arrowFunctionExpression([], optionalConditionalExpression(t.identifier('client'), t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), t.identifier('undefined'), 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.optionalMemberExpression(t.identifier('options'), t.identifier('enabled'), false, true)))]) : t.identifier('options')], t.tsTypeParameterInstantiation([typeRefOrOptionalUnion(t.identifier(responseType), options.optionalClient), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(responseType)), t.tsArrayType(t.tsParenthesizedType(t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])))])))])));
77
+ })], 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.identifier('undefined'), 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
78
+ 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([typeRefOrOptionalUnion(t.identifier(responseType), options.optionalClient), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(responseType)), t.tsArrayType(t.tsParenthesizedType(t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])))])))])));
67
79
  };
68
80
  export const createReactQueryHookInterface = ({
69
81
  QueryClient,
70
82
  hookParamsTypeName,
71
83
  responseType,
72
84
  jsonschema,
73
- options = DEFAULT_OPTIONS
85
+ options = {}
74
86
  }) => {
75
- const body = [tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(QueryClient))), options.optionalClient), tsPropertySignature(t.identifier('options'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([typeRefOrOptionalUnion(t.identifier(responseType), options.optionalClient), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(responseType)), t.tsArrayType(t.tsParenthesizedType(t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])))]))), true)];
87
+ // merge the user options with the defaults
88
+ options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
89
+ const typedUseQueryOptions = t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([typeRefOrOptionalUnion(t.identifier(responseType), options.optionalClient), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(responseType)), t.tsArrayType(t.tsParenthesizedType(t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])))]));
90
+ const body = [tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(QueryClient))), options.optionalClient), tsPropertySignature(t.identifier('options'), t.tsTypeAnnotation(options.v4 ? t.tSIntersectionType([t.tsTypeReference(t.identifier('Omit'), t.tsTypeParameterInstantiation([typedUseQueryOptions, t.tsLiteralType(t.stringLiteral("'queryKey' | 'queryFn' | 'initialData'"))])), t.tSTypeLiteral([t.tsPropertySignature(t.identifier('initialData?'), t.tsTypeAnnotation(t.tsUndefinedKeyword()))])]) : typedUseQueryOptions), true)];
76
91
  const props = getProps(jsonschema, true);
77
92
 
78
93
  if (props.length) {
@@ -92,4 +107,14 @@ const getProps = (jsonschema, camelize) => {
92
107
  } = getPropertyType(jsonschema, prop);
93
108
  return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
94
109
  });
95
- };
110
+ };
111
+
112
+ function generateUseQueryQueryKey(hookKeyName, props, optionalClient) {
113
+ const queryKey = [t.stringLiteral(hookKeyName), t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient)];
114
+
115
+ if (props.includes('args')) {
116
+ queryKey.push(t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.identifier('args')]));
117
+ }
118
+
119
+ return queryKey;
120
+ }
@@ -39,4 +39,21 @@ it('createReactQueryHooks', () => {
39
39
  optionalClient: true
40
40
  }
41
41
  })));
42
+ expectCode(t.program(createReactQueryHooks({
43
+ queryMsg: query_msg,
44
+ contractName: 'Sg721',
45
+ QueryClient: 'Sg721QueryClient',
46
+ options: {
47
+ v4: true
48
+ }
49
+ })));
50
+ expectCode(t.program(createReactQueryHooks({
51
+ queryMsg: query_msg,
52
+ contractName: 'Sg721',
53
+ QueryClient: 'Sg721QueryClient',
54
+ options: {
55
+ optionalClient: true,
56
+ v4: true
57
+ }
58
+ })));
42
59
  });
package/module/recoil.js CHANGED
@@ -18,8 +18,8 @@ export const createRecoilSelector = (keyPrefix, QueryClient, methodName) => {
18
18
  const selectorName = camel(`${methodName}Selector`);
19
19
  const responseType = pascal(`${methodName}Response`);
20
20
  const getterKey = camel(`${keyPrefix}${pascal(methodName)}`);
21
- 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.ifStatement(t.unaryExpression('!', t.identifier('client')), t.returnStatement(null), null), t.returnStatement(t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), [// t.identifier('params')
22
- t.spreadElement(t.identifier('params'))])))]), true)))])], t.tsTypeParameterInstantiation([t.tsUnionType([t.tsTypeReference(t.identifier(responseType)), t.tsUndefinedKeyword()]), 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)))]))))])])])))]));
21
+ 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.identifier('params')
22
+ 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)))]))))])])])))]));
23
23
  };
24
24
  export const createRecoilSelectors = (keyPrefix, QueryClient, queryMsg) => {
25
25
  return getMessageProperties(queryMsg).map(schema => {
@@ -54,5 +54,5 @@ export const createRecoilQueryClientType = () => ({
54
54
  });
55
55
  export const createRecoilQueryClient = (keyPrefix, QueryClient) => {
56
56
  const getterKey = camel(`${keyPrefix}${'QueryClient'}`);
57
- 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.ifStatement(t.unaryExpression('!', t.identifier('client')), t.returnStatement(null), null), t.returnStatement(t.newExpression(t.identifier(QueryClient), [t.identifier('client'), t.identifier('contractAddress')]))]), false)))])], t.tsTypeParameterInstantiation([t.tsUnionType([t.tsTypeReference(t.identifier(QueryClient)), t.tsUndefinedKeyword()]), t.tsTypeReference(t.identifier('QueryClientParams'))])))]));
57
+ 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'))])))]));
58
58
  };
@@ -18,5 +18,5 @@ it('selectors', () => {
18
18
  expectCode(t.program(createRecoilSelectors('SG721', 'SG721QueryClient', query_msg)));
19
19
  });
20
20
  it('client', () => {
21
- printCode(createRecoilQueryClient('SG721', 'SG721QueryClient'));
21
+ expectCode(createRecoilQueryClient('SG721', 'SG721QueryClient'));
22
22
  });
@@ -34,8 +34,16 @@ const getArrayTypeFromRef = $ref => {
34
34
  return t.tsArrayType(getTypeFromRef($ref));
35
35
  };
36
36
 
37
- const getArrayTypeFromType = type => {
38
- return t.tsArrayType(getType(type));
37
+ const getArrayTypeFromItems = items => {
38
+ if (items.type === 'array') {
39
+ if (Array.isArray(items.items)) {
40
+ return t.tsArrayType(t.tsArrayType(getType(items.items[0].type)));
41
+ } else {
42
+ return t.tsArrayType(getArrayTypeFromItems(items.items));
43
+ }
44
+ }
45
+
46
+ return t.tsArrayType(getType(items.type));
39
47
  };
40
48
 
41
49
  export const getType = type => {
@@ -48,8 +56,6 @@ export const getType = type => {
48
56
 
49
57
  case 'integer':
50
58
  return t.tsNumberKeyword();
51
- // case 'object':
52
- // return t.tsObjectKeyword();
53
59
 
54
60
  default:
55
61
  throw new Error('contact maintainers [unknown type]: ' + type);
@@ -101,7 +107,7 @@ export const getPropertyType = (schema, prop) => {
101
107
  } else if (info.items.title) {
102
108
  type = t.tsArrayType(t.tsTypeReference(t.identifier(info.items.title)));
103
109
  } else if (info.items.type) {
104
- type = getArrayTypeFromType(info.items.type);
110
+ type = getArrayTypeFromItems(info.items);
105
111
  } else {
106
112
  throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
107
113
  }
@@ -0,0 +1,24 @@
1
+ import generate from '@babel/generator';
2
+ import message from './../../../__fixtures__/arrays/schema/schema.json';
3
+ import { createQueryClass, createExecuteClass, createExecuteInterface, createTypeInterface } from './wasm';
4
+
5
+ const expectCode = ast => {
6
+ expect(generate(ast).code).toMatchSnapshot();
7
+ };
8
+
9
+ const printCode = ast => {
10
+ console.log(generate(ast).code);
11
+ };
12
+
13
+ it('execute_msg_for__empty', () => {
14
+ expectCode(createTypeInterface(message));
15
+ });
16
+ it('query classes', () => {
17
+ expectCode(createQueryClass('SG721QueryClient', 'SG721ReadOnlyInstance', message));
18
+ });
19
+ it('execute classes array types', () => {
20
+ expectCode(createExecuteClass('SG721Client', 'SG721Instance', null, message));
21
+ });
22
+ it('execute interfaces no extends', () => {
23
+ expectCode(createExecuteInterface('SG721Instance', null, message));
24
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.4.0",
3
+ "version": "0.4.3",
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",
@@ -84,5 +84,5 @@
84
84
  "ast-stringify": "0.1.0",
85
85
  "case": "1.6.3"
86
86
  },
87
- "gitHead": "bf5da4cc0e89f0d718d4d02fcc00d3db5424321b"
87
+ "gitHead": "c5fc4992d9da5f1c9da1f2c34469994dee7801df"
88
88
  }
@@ -18,6 +18,7 @@ interface ReactQueryHookQuery {
18
18
 
19
19
  export declare interface ReactQueryOptions {
20
20
  optionalClient?: boolean
21
+ v4?: boolean
21
22
  }
22
23
  export declare const createReactQueryHooks: ({ queryMsg, contractName, QueryClient, options }: ReactQueryHooks) => any;
23
24
  export declare const createReactQueryHook: ({ hookName, hookParamsTypeName, responseType, hookKeyName, methodName, jsonschema }: ReactQueryHookQuery) => t.ExportNamedDeclaration;