wasm-ast-types 0.3.7 → 0.4.2

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.
@@ -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,7 +27,23 @@ 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
 
28
- var createReactQueryHooks = function createReactQueryHooks(queryMsg, contractName, QueryClient) {
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
+
34
+ var DEFAULT_OPTIONS = {
35
+ optionalClient: false,
36
+ v4: false
37
+ };
38
+
39
+ var createReactQueryHooks = function createReactQueryHooks(_ref) {
40
+ var queryMsg = _ref.queryMsg,
41
+ contractName = _ref.contractName,
42
+ QueryClient = _ref.QueryClient,
43
+ _ref$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);
29
47
  return (0, _utils.getMessageProperties)(queryMsg).reduce(function (m, schema) {
30
48
  var underscoreName = Object.keys(schema.properties)[0];
31
49
  var methodName = (0, _case.camel)(underscoreName);
@@ -38,29 +56,35 @@ var createReactQueryHooks = function createReactQueryHooks(queryMsg, contractNam
38
56
  hookParamsTypeName: hookParamsTypeName,
39
57
  responseType: responseType,
40
58
  QueryClient: QueryClient,
41
- jsonschema: jsonschema
59
+ jsonschema: jsonschema,
60
+ options: options
42
61
  }), createReactQueryHook({
43
62
  methodName: methodName,
44
63
  hookName: hookName,
45
64
  hookParamsTypeName: hookParamsTypeName,
46
65
  responseType: responseType,
47
66
  hookKeyName: getterKey,
48
- jsonschema: jsonschema
67
+ jsonschema: jsonschema,
68
+ options: options
49
69
  })].concat((0, _toConsumableArray2["default"])(m));
50
70
  }, []);
51
71
  };
52
72
 
53
73
  exports.createReactQueryHooks = createReactQueryHooks;
54
74
 
55
- var createReactQueryHook = function createReactQueryHook(_ref) {
75
+ var createReactQueryHook = function createReactQueryHook(_ref2) {
56
76
  var _jsonschema$propertie;
57
77
 
58
- var hookName = _ref.hookName,
59
- hookParamsTypeName = _ref.hookParamsTypeName,
60
- responseType = _ref.responseType,
61
- hookKeyName = _ref.hookKeyName,
62
- methodName = _ref.methodName,
63
- jsonschema = _ref.jsonschema;
78
+ var hookName = _ref2.hookName,
79
+ hookParamsTypeName = _ref2.hookParamsTypeName,
80
+ responseType = _ref2.responseType,
81
+ hookKeyName = _ref2.hookKeyName,
82
+ methodName = _ref2.methodName,
83
+ jsonschema = _ref2.jsonschema,
84
+ _ref2$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);
64
88
  var keys = Object.keys((_jsonschema$propertie = jsonschema.properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {});
65
89
  var args = [];
66
90
 
@@ -78,17 +102,23 @@ var createReactQueryHook = function createReactQueryHook(_ref) {
78
102
 
79
103
  return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(hookName), [(0, _utils.tsObjectPattern)((0, _toConsumableArray2["default"])(props.map(function (prop) {
80
104
  return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
81
- })), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName))))], t.blockStatement([t.returnStatement((0, _utils.callExpression)(t.identifier('useQuery'), [t.arrayExpression([t.stringLiteral(hookKeyName), t.memberExpression(t.identifier('client'), t.identifier('contractAddress'))]), t.arrowFunctionExpression([], t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), false), 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()])))])))])));
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()])))])))])));
82
107
  };
83
108
 
84
109
  exports.createReactQueryHook = createReactQueryHook;
85
110
 
86
- var createReactQueryHookInterface = function createReactQueryHookInterface(_ref2) {
87
- var QueryClient = _ref2.QueryClient,
88
- hookParamsTypeName = _ref2.hookParamsTypeName,
89
- responseType = _ref2.responseType,
90
- jsonschema = _ref2.jsonschema;
91
- var body = [t.tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(QueryClient)))), (0, _utils.tsPropertySignature)(t.identifier('options'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('UseQueryOptions'), 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()])))]))), true)];
111
+ var createReactQueryHookInterface = function createReactQueryHookInterface(_ref3) {
112
+ var QueryClient = _ref3.QueryClient,
113
+ hookParamsTypeName = _ref3.hookParamsTypeName,
114
+ responseType = _ref3.responseType,
115
+ jsonschema = _ref3.jsonschema,
116
+ _ref3$options = _ref3.options,
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)];
92
122
  var props = getProps(jsonschema, true);
93
123
 
94
124
  if (props.length) {
@@ -112,4 +142,14 @@ var getProps = function getProps(jsonschema, camelize) {
112
142
 
113
143
  return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional);
114
144
  });
115
- };
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
+ }
@@ -34,6 +34,39 @@ var printCode = function printCode(ast) {
34
34
 
35
35
 
36
36
  it('createReactQueryHooks', function () {
37
- expectCode(t.program((0, _reactQuery.createReactQueryHooks)(_query_msg["default"], 'Sg721', 'Sg721QueryClient')));
38
- expectCode(t.program((0, _reactQuery.createReactQueryHooks)(_query_msg["default"], 'Sg721', 'Sg721QueryClient')));
37
+ expectCode(t.program((0, _reactQuery.createReactQueryHooks)({
38
+ queryMsg: _query_msg["default"],
39
+ contractName: 'Sg721',
40
+ QueryClient: 'Sg721QueryClient'
41
+ })));
42
+ expectCode(t.program((0, _reactQuery.createReactQueryHooks)({
43
+ queryMsg: _query_msg["default"],
44
+ contractName: 'Sg721',
45
+ QueryClient: 'Sg721QueryClient'
46
+ })));
47
+ expectCode(t.program((0, _reactQuery.createReactQueryHooks)({
48
+ queryMsg: _query_msg["default"],
49
+ contractName: 'Sg721',
50
+ QueryClient: 'Sg721QueryClient',
51
+ options: {
52
+ optionalClient: true
53
+ }
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
+ })));
39
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
  });
@@ -7,7 +7,7 @@ var _typeof = require("@babel/runtime/helpers/typeof");
7
7
  Object.defineProperty(exports, "__esModule", {
8
8
  value: true
9
9
  });
10
- exports.typedIdentifier = exports.tsTypeOperator = exports.tsPropertySignature = exports.tsObjectPattern = exports.shorthandProperty = exports.recursiveNamespace = exports.propertySignature = exports.promiseTypeAnnotation = exports.memberExpressionOrIdentifierSnake = exports.memberExpressionOrIdentifier = exports.importStmt = exports.importAminoMsg = exports.identifier = exports.getMessageProperties = exports.getFieldDimensionality = exports.classProperty = exports.classDeclaration = exports.callExpression = exports.bindMethod = exports.arrowFunctionExpression = exports.arrayTypeNDimensions = exports.FieldTypeAsts = void 0;
10
+ exports.typedIdentifier = exports.typeRefOrOptionalUnion = exports.tsTypeOperator = exports.tsPropertySignature = exports.tsObjectPattern = exports.shorthandProperty = exports.recursiveNamespace = exports.propertySignature = exports.promiseTypeAnnotation = exports.optionalConditionalExpression = exports.memberExpressionOrIdentifierSnake = exports.memberExpressionOrIdentifier = exports.importStmt = exports.importAminoMsg = exports.identifier = exports.getMessageProperties = exports.getFieldDimensionality = exports.classProperty = exports.classDeclaration = exports.callExpression = exports.bindMethod = exports.arrowFunctionExpression = exports.arrayTypeNDimensions = exports.FieldTypeAsts = void 0;
11
11
 
12
12
  var _toArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toArray"));
13
13
 
@@ -259,5 +259,24 @@ var memberExpressionOrIdentifierSnake = function memberExpressionOrIdentifierSna
259
259
 
260
260
  return t.memberExpression(memberExpressionOrIdentifierSnake(rest), t.identifier((0, _case.snake)(name)));
261
261
  };
262
+ /**
263
+ * If optional, return a conditional, otherwise just the expression
264
+ */
262
265
 
263
- exports.memberExpressionOrIdentifierSnake = memberExpressionOrIdentifierSnake;
266
+
267
+ exports.memberExpressionOrIdentifierSnake = memberExpressionOrIdentifierSnake;
268
+
269
+ var optionalConditionalExpression = function optionalConditionalExpression(test, expression, alternate) {
270
+ var optional = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
271
+ return optional ? t.conditionalExpression(test, expression, alternate) : expression;
272
+ };
273
+
274
+ exports.optionalConditionalExpression = optionalConditionalExpression;
275
+
276
+ var typeRefOrOptionalUnion = function typeRefOrOptionalUnion(identifier) {
277
+ var optional = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
278
+ var typeReference = t.tsTypeReference(identifier);
279
+ return optional ? t.tsUnionType([typeReference, t.tsUndefinedKeyword()]) : typeReference;
280
+ };
281
+
282
+ exports.typeRefOrOptionalUnion = typeRefOrOptionalUnion;
@@ -1,9 +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
- import { propertySignature } from './utils/babel';
10
+ import { typeRefOrOptionalUnion, propertySignature, optionalConditionalExpression } from './utils/babel';
5
11
  import { getPropertyType } from './utils/types';
6
- export const createReactQueryHooks = (queryMsg, contractName, QueryClient) => {
12
+ const DEFAULT_OPTIONS = {
13
+ optionalClient: false,
14
+ v4: false
15
+ };
16
+ export const createReactQueryHooks = ({
17
+ queryMsg,
18
+ contractName,
19
+ QueryClient,
20
+ options = {}
21
+ }) => {
22
+ // merge the user options with the defaults
23
+ options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
7
24
  return getMessageProperties(queryMsg).reduce((m, schema) => {
8
25
  const underscoreName = Object.keys(schema.properties)[0];
9
26
  const methodName = camel(underscoreName);
@@ -16,14 +33,16 @@ export const createReactQueryHooks = (queryMsg, contractName, QueryClient) => {
16
33
  hookParamsTypeName,
17
34
  responseType,
18
35
  QueryClient,
19
- jsonschema
36
+ jsonschema,
37
+ options
20
38
  }), createReactQueryHook({
21
39
  methodName,
22
40
  hookName,
23
41
  hookParamsTypeName,
24
42
  responseType,
25
43
  hookKeyName: getterKey,
26
- jsonschema
44
+ jsonschema,
45
+ options
27
46
  }), ...m];
28
47
  }, []);
29
48
  };
@@ -33,8 +52,11 @@ export const createReactQueryHook = ({
33
52
  responseType,
34
53
  hookKeyName,
35
54
  methodName,
36
- jsonschema
55
+ jsonschema,
56
+ options = {}
37
57
  }) => {
58
+ // merge the user options with the defaults
59
+ options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
38
60
  const keys = Object.keys(jsonschema.properties ?? {});
39
61
  let args = [];
40
62
 
@@ -52,15 +74,20 @@ export const createReactQueryHook = ({
52
74
 
53
75
  return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(hookName), [tsObjectPattern([...props.map(prop => {
54
76
  return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
55
- })], t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName))))], t.blockStatement([t.returnStatement(callExpression(t.identifier('useQuery'), [t.arrayExpression([t.stringLiteral(hookKeyName), t.memberExpression(t.identifier('client'), t.identifier('contractAddress'))]), t.arrowFunctionExpression([], t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), false), 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()])))])))])));
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()])))])))])));
56
79
  };
57
80
  export const createReactQueryHookInterface = ({
58
81
  QueryClient,
59
82
  hookParamsTypeName,
60
83
  responseType,
61
- jsonschema
84
+ jsonschema,
85
+ options = {}
62
86
  }) => {
63
- const body = [t.tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(QueryClient)))), tsPropertySignature(t.identifier('options'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('UseQueryOptions'), 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()])))]))), 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)];
64
91
  const props = getProps(jsonschema, true);
65
92
 
66
93
  if (props.length) {
@@ -80,4 +107,14 @@ const getProps = (jsonschema, camelize) => {
80
107
  } = getPropertyType(jsonschema, prop);
81
108
  return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
82
109
  });
83
- };
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
+ }
@@ -21,6 +21,39 @@ const printCode = ast => {
21
21
 
22
22
 
23
23
  it('createReactQueryHooks', () => {
24
- expectCode(t.program(createReactQueryHooks(query_msg, 'Sg721', 'Sg721QueryClient')));
25
- expectCode(t.program(createReactQueryHooks(query_msg, 'Sg721', 'Sg721QueryClient')));
24
+ expectCode(t.program(createReactQueryHooks({
25
+ queryMsg: query_msg,
26
+ contractName: 'Sg721',
27
+ QueryClient: 'Sg721QueryClient'
28
+ })));
29
+ expectCode(t.program(createReactQueryHooks({
30
+ queryMsg: query_msg,
31
+ contractName: 'Sg721',
32
+ QueryClient: 'Sg721QueryClient'
33
+ })));
34
+ expectCode(t.program(createReactQueryHooks({
35
+ queryMsg: query_msg,
36
+ contractName: 'Sg721',
37
+ QueryClient: 'Sg721QueryClient',
38
+ options: {
39
+ optionalClient: true
40
+ }
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
+ })));
26
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
  });
@@ -153,4 +153,15 @@ export const memberExpressionOrIdentifierSnake = names => {
153
153
 
154
154
  const [name, ...rest] = names;
155
155
  return t.memberExpression(memberExpressionOrIdentifierSnake(rest), t.identifier(snake(name)));
156
+ };
157
+ /**
158
+ * If optional, return a conditional, otherwise just the expression
159
+ */
160
+
161
+ export const optionalConditionalExpression = (test, expression, alternate, optional = false) => {
162
+ return optional ? t.conditionalExpression(test, expression, alternate) : expression;
163
+ };
164
+ export const typeRefOrOptionalUnion = (identifier, optional = false) => {
165
+ const typeReference = t.tsTypeReference(identifier);
166
+ return optional ? t.tsUnionType([typeReference, t.tsUndefinedKeyword()]) : typeReference;
156
167
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.3.7",
3
+ "version": "0.4.2",
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": "1f32ebfe7996630272adbbb54a324ea4a0699267"
87
+ "gitHead": "aafba6dadee2e5a14b304f0dd53858e8b509046d"
88
88
  }
@@ -1,5 +1,12 @@
1
1
  import * as t from '@babel/types';
2
2
  import { QueryMsg } from './types';
3
+
4
+ interface ReactQueryHooks {
5
+ queryMsg: QueryMsg
6
+ contractName: string
7
+ QueryClient: string
8
+ options?: ReactQueryOptions
9
+ }
3
10
  interface ReactQueryHookQuery {
4
11
  hookName: string;
5
12
  hookParamsTypeName: string;
@@ -8,7 +15,12 @@ interface ReactQueryHookQuery {
8
15
  methodName: string;
9
16
  jsonschema: any;
10
17
  }
11
- export declare const createReactQueryHooks: (queryMsg: QueryMsg, contractName: string, QueryClient: string) => any;
18
+
19
+ export declare interface ReactQueryOptions {
20
+ optionalClient?: boolean
21
+ v4?: boolean
22
+ }
23
+ export declare const createReactQueryHooks: ({ queryMsg, contractName, QueryClient, options }: ReactQueryHooks) => any;
12
24
  export declare const createReactQueryHook: ({ hookName, hookParamsTypeName, responseType, hookKeyName, methodName, jsonschema }: ReactQueryHookQuery) => t.ExportNamedDeclaration;
13
25
  interface ReactQueryHookQueryInterface {
14
26
  QueryClient: string;
@@ -17,4 +29,4 @@ interface ReactQueryHookQueryInterface {
17
29
  jsonschema: any;
18
30
  }
19
31
  export declare const createReactQueryHookInterface: ({ QueryClient, hookParamsTypeName, responseType, jsonschema }: ReactQueryHookQueryInterface) => t.ExportNamedDeclaration;
20
- export {};
32
+ export { };