wasm-ast-types 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,7 +7,9 @@ var _typeof = require("@babel/runtime/helpers/typeof");
7
7
  Object.defineProperty(exports, "__esModule", {
8
8
  value: true
9
9
  });
10
- exports.getType = exports.getPropertyType = exports.createTypedObjectParams = void 0;
10
+ exports.getParamsTypeAnnotation = exports.createTypedObjectParams = void 0;
11
+ exports.getPropertySignatureFromProp = getPropertySignatureFromProp;
12
+ exports.getTypeFromRef = exports.getType = exports.getPropertyType = void 0;
11
13
 
12
14
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
15
 
@@ -51,12 +53,22 @@ var getTypeFromRef = function getTypeFromRef($ref) {
51
53
  }
52
54
  };
53
55
 
56
+ exports.getTypeFromRef = getTypeFromRef;
57
+
54
58
  var getArrayTypeFromRef = function getArrayTypeFromRef($ref) {
55
59
  return t.tsArrayType(getTypeFromRef($ref));
56
60
  };
57
61
 
58
- var getArrayTypeFromType = function getArrayTypeFromType(type) {
59
- return t.tsArrayType(getType(type));
62
+ var getArrayTypeFromItems = function getArrayTypeFromItems(items) {
63
+ if (items.type === 'array') {
64
+ if (Array.isArray(items.items)) {
65
+ return t.tsArrayType(t.tsArrayType(getType(items.items[0].type)));
66
+ } else {
67
+ return t.tsArrayType(getArrayTypeFromItems(items.items));
68
+ }
69
+ }
70
+
71
+ return t.tsArrayType(getType(items.type));
60
72
  };
61
73
 
62
74
  var getType = function getType(type) {
@@ -69,8 +81,6 @@ var getType = function getType(type) {
69
81
 
70
82
  case 'integer':
71
83
  return t.tsNumberKeyword();
72
- // case 'object':
73
- // return t.tsObjectKeyword();
74
84
 
75
85
  default:
76
86
  throw new Error('contact maintainers [unknown type]: ' + type);
@@ -129,7 +139,7 @@ var getPropertyType = function getPropertyType(schema, prop) {
129
139
  } else if (info.items.title) {
130
140
  type = t.tsArrayType(t.tsTypeReference(t.identifier(info.items.title)));
131
141
  } else if (info.items.type) {
132
- type = getArrayTypeFromType(info.items.type);
142
+ type = getArrayTypeFromItems(info.items);
133
143
  } else {
134
144
  throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
135
145
  }
@@ -177,79 +187,95 @@ var getPropertyType = function getPropertyType(schema, prop) {
177
187
 
178
188
  exports.getPropertyType = getPropertyType;
179
189
 
180
- var createTypedObjectParams = function createTypedObjectParams(jsonschema) {
181
- var _jsonschema$propertie;
182
-
183
- var camelize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
184
- var keys = Object.keys((_jsonschema$propertie = jsonschema.properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {});
185
- if (!keys.length) return;
186
- var typedParams = keys.map(function (prop) {
187
- if (jsonschema.properties[prop].type === 'object') {
188
- if (jsonschema.properties[prop].title) {
189
- return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(jsonschema.properties[prop].title))));
190
- } else {
191
- throw new Error('createTypedObjectParams() contact maintainer');
192
- }
190
+ function getPropertySignatureFromProp(jsonschema, prop, camelize) {
191
+ if (jsonschema.properties[prop].type === 'object') {
192
+ if (jsonschema.properties[prop].title) {
193
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(jsonschema.properties[prop].title))));
194
+ } else {
195
+ throw new Error('createTypedObjectParams() contact maintainer');
193
196
  }
197
+ }
194
198
 
195
- if (Array.isArray(jsonschema.properties[prop].allOf)) {
196
- var _jsonschema$required;
199
+ if (Array.isArray(jsonschema.properties[prop].allOf)) {
200
+ var _jsonschema$required;
197
201
 
198
- var isOptional = !((_jsonschema$required = jsonschema.required) !== null && _jsonschema$required !== void 0 && _jsonschema$required.includes(prop));
199
- var unionTypes = jsonschema.properties[prop].allOf.map(function (el) {
200
- if (el.title) return el.title;
201
- if (el.$ref) return getTypeStrFromRef(el.$ref);
202
- return el.type;
203
- });
204
- var uniqUnionTypes = (0, _toConsumableArray2["default"])(new Set(unionTypes));
202
+ var isOptional = !((_jsonschema$required = jsonschema.required) !== null && _jsonschema$required !== void 0 && _jsonschema$required.includes(prop));
203
+ var unionTypes = jsonschema.properties[prop].allOf.map(function (el) {
204
+ if (el.title) return el.title;
205
+ if (el.$ref) return getTypeStrFromRef(el.$ref);
206
+ return el.type;
207
+ });
208
+ var uniqUnionTypes = (0, _toConsumableArray2["default"])(new Set(unionTypes));
205
209
 
206
- if (uniqUnionTypes.length === 1) {
207
- return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(uniqUnionTypes[0]))), isOptional);
208
- } else {
209
- return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(function (typ) {
210
- return t.tsTypeReference(t.identifier(typ));
211
- }))), isOptional);
212
- }
213
- } else if (Array.isArray(jsonschema.properties[prop].oneOf)) {
214
- var _jsonschema$required2;
210
+ if (uniqUnionTypes.length === 1) {
211
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(uniqUnionTypes[0]))), isOptional);
212
+ } else {
213
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(function (typ) {
214
+ return t.tsTypeReference(t.identifier(typ));
215
+ }))), isOptional);
216
+ }
217
+ } else if (Array.isArray(jsonschema.properties[prop].oneOf)) {
218
+ var _jsonschema$required2;
215
219
 
216
- var _isOptional = !((_jsonschema$required2 = jsonschema.required) !== null && _jsonschema$required2 !== void 0 && _jsonschema$required2.includes(prop));
220
+ var _isOptional = !((_jsonschema$required2 = jsonschema.required) !== null && _jsonschema$required2 !== void 0 && _jsonschema$required2.includes(prop));
217
221
 
218
- var _unionTypes = jsonschema.properties[prop].oneOf.map(function (el) {
219
- if (el.title) return el.title;
220
- if (el.$ref) return getTypeStrFromRef(el.$ref);
221
- return el.type;
222
- });
222
+ var _unionTypes = jsonschema.properties[prop].oneOf.map(function (el) {
223
+ if (el.title) return el.title;
224
+ if (el.$ref) return getTypeStrFromRef(el.$ref);
225
+ return el.type;
226
+ });
223
227
 
224
- var _uniqUnionTypes = (0, _toConsumableArray2["default"])(new Set(_unionTypes));
228
+ var _uniqUnionTypes = (0, _toConsumableArray2["default"])(new Set(_unionTypes));
225
229
 
226
- if (_uniqUnionTypes.length === 1) {
227
- return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(_uniqUnionTypes[0]))), _isOptional);
228
- } else {
229
- return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(_uniqUnionTypes.map(function (typ) {
230
- return t.tsTypeReference(t.identifier(typ));
231
- }))), _isOptional);
232
- }
230
+ if (_uniqUnionTypes.length === 1) {
231
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(_uniqUnionTypes[0]))), _isOptional);
232
+ } else {
233
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(_uniqUnionTypes.map(function (typ) {
234
+ return t.tsTypeReference(t.identifier(typ));
235
+ }))), _isOptional);
233
236
  }
237
+ }
234
238
 
235
- try {
236
- getPropertyType(jsonschema, prop);
237
- } catch (e) {
238
- console.log(e);
239
- console.log(jsonschema, prop);
240
- }
239
+ try {
240
+ getPropertyType(jsonschema, prop);
241
+ } catch (e) {
242
+ console.log(e);
243
+ console.log(jsonschema, prop);
244
+ }
245
+
246
+ var _getPropertyType = getPropertyType(jsonschema, prop),
247
+ type = _getPropertyType.type,
248
+ optional = _getPropertyType.optional;
241
249
 
242
- var _getPropertyType = getPropertyType(jsonschema, prop),
243
- type = _getPropertyType.type,
244
- optional = _getPropertyType.optional;
250
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional);
251
+ }
245
252
 
246
- return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional);
253
+ var getParamsTypeAnnotation = function getParamsTypeAnnotation(jsonschema) {
254
+ var _jsonschema$propertie;
255
+
256
+ var camelize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
257
+ var keys = Object.keys((_jsonschema$propertie = jsonschema.properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {});
258
+ if (!keys.length) return undefined;
259
+ var typedParams = keys.map(function (prop) {
260
+ return getPropertySignatureFromProp(jsonschema, prop, camelize);
247
261
  });
262
+ return t.tsTypeAnnotation(t.tsTypeLiteral((0, _toConsumableArray2["default"])(typedParams)));
263
+ };
264
+
265
+ exports.getParamsTypeAnnotation = getParamsTypeAnnotation;
266
+
267
+ var createTypedObjectParams = function createTypedObjectParams(jsonschema) {
268
+ var _jsonschema$propertie2;
269
+
270
+ var camelize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
271
+ var keys = Object.keys((_jsonschema$propertie2 = jsonschema.properties) !== null && _jsonschema$propertie2 !== void 0 ? _jsonschema$propertie2 : {});
272
+ if (!keys.length) return; // const typedParams = keys.map(prop => getPropertySignatureFromProp(jsonschema, prop, camelize));
273
+
248
274
  var params = keys.map(function (prop) {
249
275
  return t.objectProperty(camelize ? t.identifier((0, _case.camel)(prop)) : t.identifier(prop), camelize ? t.identifier((0, _case.camel)(prop)) : t.identifier(prop), false, true);
250
276
  });
251
277
  var obj = t.objectPattern((0, _toConsumableArray2["default"])(params));
252
- obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral((0, _toConsumableArray2["default"])(typedParams)));
278
+ obj.typeAnnotation = getParamsTypeAnnotation(jsonschema, camelize);
253
279
  return obj;
254
280
  };
255
281
 
@@ -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
+ });
package/main/wasm.js CHANGED
@@ -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.createWasmQueryMethod = exports.createWasmExecMethod = exports.createTypeOrInterface = exports.createTypeInterface = exports.createQueryInterface = exports.createQueryClass = exports.createPropertyFunctionWithObjectParamsForExec = exports.createPropertyFunctionWithObjectParams = exports.createExecuteInterface = exports.createExecuteClass = void 0;
10
+ exports.createWasmQueryMethod = exports.createWasmExecMethod = exports.createTypeOrInterface = exports.createTypeInterface = exports.createQueryInterface = exports.createQueryClass = exports.createPropertyFunctionWithObjectParamsForExec = exports.createPropertyFunctionWithObjectParams = exports.createExecuteInterface = exports.createExecuteClass = exports.FIXED_EXECUTE_PARAMS = exports.CONSTANT_EXEC_PARAMS = void 0;
11
11
 
12
12
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
13
 
@@ -60,6 +60,8 @@ var createQueryClass = function createQueryClass(className, implementsClassName,
60
60
  };
61
61
 
62
62
  exports.createQueryClass = createQueryClass;
63
+ var CONSTANT_EXEC_PARAMS = [t.assignmentPattern((0, _babel.identifier)('fee', t.tsTypeAnnotation(t.tsUnionType([t.tSNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), false), t.stringLiteral('auto')), (0, _babel.identifier)('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), (0, _babel.identifier)('funds', t.tsTypeAnnotation((0, _babel.tsTypeOperator)(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
64
+ exports.CONSTANT_EXEC_PARAMS = CONSTANT_EXEC_PARAMS;
63
65
 
64
66
  var createWasmExecMethod = function createWasmExecMethod(jsonschema) {
65
67
  var _jsonschema$propertie2;
@@ -71,9 +73,8 @@ var createWasmExecMethod = function createWasmExecMethod(jsonschema) {
71
73
  var args = Object.keys(properties).map(function (prop) {
72
74
  return t.objectProperty(t.identifier(prop), t.identifier((0, _case.camel)(prop)), false, prop === (0, _case.camel)(prop));
73
75
  });
74
- var constantParams = [t.assignmentPattern((0, _babel.identifier)('fee', t.tsTypeAnnotation(t.tsUnionType([t.tSNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), false), t.stringLiteral('auto')), (0, _babel.identifier)('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), (0, _babel.identifier)('funds', t.tsTypeAnnotation((0, _babel.tsTypeOperator)(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
75
76
  return t.classProperty(t.identifier(methodName), (0, _utils.arrowFunctionExpression)(obj ? [// props
76
- obj].concat(constantParams) : constantParams, t.blockStatement([t.returnStatement(t.awaitExpression(t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('execute')), [t.memberExpression(t.thisExpression(), t.identifier('sender')), t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.objectExpression([t.objectProperty(t.identifier(underscoreName), t.objectExpression((0, _toConsumableArray2["default"])(args)))]), t.identifier('fee'), t.identifier('memo'), t.identifier('funds')])))]), // return type
77
+ obj].concat(CONSTANT_EXEC_PARAMS) : CONSTANT_EXEC_PARAMS, t.blockStatement([t.returnStatement(t.awaitExpression(t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('execute')), [t.memberExpression(t.thisExpression(), t.identifier('sender')), t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.objectExpression([t.objectProperty(t.identifier(underscoreName), t.objectExpression((0, _toConsumableArray2["default"])(args)))]), t.identifier('fee'), t.identifier('memo'), t.identifier('funds')])))]), // return type
77
78
  t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true));
78
79
  };
79
80
 
@@ -132,14 +133,15 @@ var createPropertyFunctionWithObjectParams = function createPropertyFunctionWith
132
133
  };
133
134
 
134
135
  exports.createPropertyFunctionWithObjectParams = createPropertyFunctionWithObjectParams;
136
+ var FIXED_EXECUTE_PARAMS = [(0, _babel.identifier)('fee', t.tsTypeAnnotation(t.tsUnionType([t.tsNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), true), (0, _babel.identifier)('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), (0, _babel.identifier)('funds', t.tsTypeAnnotation((0, _babel.tsTypeOperator)(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
137
+ exports.FIXED_EXECUTE_PARAMS = FIXED_EXECUTE_PARAMS;
135
138
 
136
139
  var createPropertyFunctionWithObjectParamsForExec = function createPropertyFunctionWithObjectParamsForExec(methodName, responseType, jsonschema) {
137
140
  var obj = (0, _types2.createTypedObjectParams)(jsonschema);
138
- var fixedParams = [(0, _babel.identifier)('fee', t.tsTypeAnnotation(t.tsUnionType([t.tsNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), true), (0, _babel.identifier)('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), (0, _babel.identifier)('funds', t.tsTypeAnnotation((0, _babel.tsTypeOperator)(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
139
141
  var func = {
140
142
  type: 'TSFunctionType',
141
143
  typeAnnotation: (0, _utils.promiseTypeAnnotation)(responseType),
142
- parameters: obj ? [obj].concat(fixedParams) : fixedParams
144
+ parameters: obj ? [obj].concat(FIXED_EXECUTE_PARAMS) : FIXED_EXECUTE_PARAMS
143
145
  };
144
146
  return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(func));
145
147
  };
@@ -1,17 +1,29 @@
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
- import { tsPropertySignature, tsObjectPattern, callExpression, getMessageProperties } from './utils';
4
- import { typeRefOrOptionalUnion, propertySignature, optionalConditionalExpression } from './utils/babel';
5
- import { getPropertyType } from './utils/types';
9
+ import { callExpression, getMessageProperties, identifier, tsObjectPattern, tsPropertySignature } from './utils';
10
+ import { omitTypeReference, optionalConditionalExpression, propertySignature, shorthandProperty, typeRefOrOptionalUnion } from './utils/babel';
11
+ import { getParamsTypeAnnotation, getPropertyType, getTypeFromRef } from './utils/types';
12
+ import { FIXED_EXECUTE_PARAMS } from './wasm'; // TODO: this mutations boolean is not actually used here and only at a higher level
13
+
6
14
  const DEFAULT_OPTIONS = {
7
- optionalClient: false
15
+ optionalClient: false,
16
+ v4: false,
17
+ mutations: false
8
18
  };
9
19
  export const createReactQueryHooks = ({
10
20
  queryMsg,
11
21
  contractName,
12
22
  QueryClient,
13
- options = DEFAULT_OPTIONS
23
+ options = {}
14
24
  }) => {
25
+ // merge the user options with the defaults
26
+ options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
15
27
  return getMessageProperties(queryMsg).reduce((m, schema) => {
16
28
  const underscoreName = Object.keys(schema.properties)[0];
17
29
  const methodName = camel(underscoreName);
@@ -44,8 +56,10 @@ export const createReactQueryHook = ({
44
56
  hookKeyName,
45
57
  methodName,
46
58
  jsonschema,
47
- options = DEFAULT_OPTIONS
59
+ options = {}
48
60
  }) => {
61
+ // merge the user options with the defaults
62
+ options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
49
63
  const keys = Object.keys(jsonschema.properties ?? {});
50
64
  let args = [];
51
65
 
@@ -63,16 +77,138 @@ export const createReactQueryHook = ({
63
77
 
64
78
  return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(hookName), [tsObjectPattern([...props.map(prop => {
65
79
  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()])))])))])));
80
+ })], 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
81
+ 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()])))])))])));
82
+ };
83
+
84
+ /**
85
+ * Example:
86
+ ```
87
+ export interface Cw4UpdateMembersMutation {
88
+ client: Cw4GroupClient
89
+ args: {
90
+ tokenId: string
91
+ remove: string[]
92
+ }
93
+ options?: Omit<
94
+ UseMutationOptions<ExecuteResult, Error, Pick<Cw4UpdateMembersMutation, 'args'>>,
95
+ 'mutationFn'
96
+ >
97
+ }
98
+ ```
99
+ */
100
+ export const createReactQueryMutationArgsInterface = ({
101
+ ExecuteClient,
102
+ mutationHookParamsTypeName,
103
+ useMutationTypeParameter,
104
+ jsonschema
105
+ }) => {
106
+ const typedUseMutationOptions = t.tsTypeReference(t.identifier('UseMutationOptions'), useMutationTypeParameter);
107
+ const body = [tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(ExecuteClient))), false)];
108
+ let msgType = getParamsTypeAnnotation(jsonschema); // TODO: this should not have to be done manually.
109
+
110
+ if (!msgType && jsonschema?.$ref?.startsWith('#/definitions/')) {
111
+ let refName = jsonschema?.$ref;
112
+
113
+ if (/_for_[A-Z]/.test(refName)) {
114
+ refName = refName.replace(/_for_/, 'For');
115
+ }
116
+
117
+ msgType = t.tsTypeAnnotation(getTypeFromRef(refName));
118
+ }
119
+
120
+ if (msgType) {
121
+ body.push(t.tsPropertySignature(t.identifier('msg'), msgType));
122
+ } // fee: number | StdFee | "auto" = "auto", memo?: string, funds?: readonly Coin[]
123
+
124
+
125
+ const optionalArgs = t.tsPropertySignature(t.identifier('args'), t.tsTypeAnnotation(t.tsTypeLiteral(FIXED_EXECUTE_PARAMS.map(param => propertySignature(param.name, param.typeAnnotation, param.optional)))));
126
+ optionalArgs.optional = true;
127
+ body.push(optionalArgs);
128
+ return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(mutationHookParamsTypeName), null, [], t.tsInterfaceBody(body)));
129
+ };
130
+ export const createReactQueryMutationHooks = ({
131
+ execMsg,
132
+ contractName,
133
+ ExecuteClient,
134
+ options = {}
135
+ }) => {
136
+ // merge the user options with the defaults
137
+ return getMessageProperties(execMsg).reduce((m, schema) => {
138
+ // update_members
139
+ const execMethodUnderscoreName = Object.keys(schema.properties)[0]; // updateMembers
140
+
141
+ const execMethodName = camel(execMethodUnderscoreName); // Cw20UpdateMembersMutation
142
+
143
+ const mutationHookParamsTypeName = `${pascal(contractName)}${pascal(execMethodName)}Mutation`; // useCw20UpdateMembersMutation
144
+
145
+ const mutationHookName = `use${mutationHookParamsTypeName}`;
146
+ const jsonschema = schema.properties[execMethodUnderscoreName];
147
+ const properties = jsonschema.properties ?? {}; // TODO: there should be a better way to do this
148
+
149
+ const hasMsg = !!(Object.keys(properties)?.length || jsonschema?.$ref); // <ExecuteResult, Error, Cw4UpdateMembersMutation>
150
+
151
+ const useMutationTypeParameter = generateMutationTypeParameter(mutationHookParamsTypeName, hasMsg);
152
+ return [createReactQueryMutationArgsInterface({
153
+ mutationHookParamsTypeName,
154
+ ExecuteClient,
155
+ jsonschema,
156
+ useMutationTypeParameter
157
+ }), createReactQueryMutationHook({
158
+ execMethodName,
159
+ mutationHookName,
160
+ mutationHookParamsTypeName,
161
+ hasMsg,
162
+ useMutationTypeParameter
163
+ }), ...m];
164
+ }, []);
165
+ };
166
+ /**
167
+ * Generates the mutation type parameter. If args exist, we use a pick. If not, we just return the params type.
168
+ */
169
+
170
+ function generateMutationTypeParameter(mutationHookParamsTypeName, hasArgs) {
171
+ return t.tsTypeParameterInstantiation([// Data
172
+ t.tSTypeReference(t.identifier('ExecuteResult')), // Error
173
+ t.tsTypeReference(t.identifier('Error')), // Variables
174
+ t.tsTypeReference(t.identifier(mutationHookParamsTypeName))]);
175
+ }
176
+
177
+ /**
178
+ *
179
+ * Example:
180
+ ```
181
+ export const useCw4UpdateMembersMutation = ({ client, options }: Omit<Cw4UpdateMembersMutation, 'args'>) =>
182
+ useMutation<ExecuteResult, Error, Pick<Cw4UpdateMembersMutation, 'args'>>(
183
+ ({ args }) => client.updateMembers(args),
184
+ options
185
+ )
186
+ ```
187
+ */
188
+ export const createReactQueryMutationHook = ({
189
+ mutationHookName,
190
+ mutationHookParamsTypeName,
191
+ execMethodName,
192
+ useMutationTypeParameter,
193
+ hasMsg
194
+ }) => {
195
+ const useMutationFunctionArgs = [shorthandProperty('client')];
196
+ if (hasMsg) useMutationFunctionArgs.push(shorthandProperty('msg'));
197
+ useMutationFunctionArgs.push(t.objectProperty(t.identifier('args'), t.assignmentPattern(t.objectPattern(FIXED_EXECUTE_PARAMS.map(param => shorthandProperty(param.name))), t.objectExpression([]))));
198
+ return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(mutationHookName), [identifier('options', t.tsTypeAnnotation(omitTypeReference(t.tsTypeReference(t.identifier('UseMutationOptions'), useMutationTypeParameter), 'mutationFn')), true)], t.blockStatement([t.returnStatement(callExpression(t.identifier('useMutation'), [t.arrowFunctionExpression([t.objectPattern(useMutationFunctionArgs)], t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(execMethodName)), (hasMsg ? [t.identifier('msg')] : []).concat(FIXED_EXECUTE_PARAMS.map(param => t.identifier(param.name)))), false // not async
199
+ ), t.identifier('options')], useMutationTypeParameter))])));
67
200
  };
68
201
  export const createReactQueryHookInterface = ({
69
202
  QueryClient,
70
203
  hookParamsTypeName,
71
204
  responseType,
72
205
  jsonschema,
73
- options = DEFAULT_OPTIONS
206
+ options = {}
74
207
  }) => {
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)];
208
+ // merge the user options with the defaults
209
+ options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
210
+ 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()])))]));
211
+ 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([omitTypeReference(typedUseQueryOptions, "'queryKey' | 'queryFn' | 'initialData'"), t.tSTypeLiteral([t.tsPropertySignature(t.identifier('initialData?'), t.tsTypeAnnotation(t.tsUndefinedKeyword()))])]) : typedUseQueryOptions), true)];
76
212
  const props = getProps(jsonschema, true);
77
213
 
78
214
  if (props.length) {
@@ -92,4 +228,14 @@ const getProps = (jsonschema, camelize) => {
92
228
  } = getPropertyType(jsonschema, prop);
93
229
  return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
94
230
  });
95
- };
231
+ };
232
+
233
+ function generateUseQueryQueryKey(hookKeyName, props, optionalClient) {
234
+ const queryKey = [t.stringLiteral(hookKeyName), t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient)];
235
+
236
+ if (props.includes('args')) {
237
+ queryKey.push(t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.identifier('args')]));
238
+ }
239
+
240
+ return queryKey;
241
+ }
@@ -1,7 +1,8 @@
1
1
  import generate from '@babel/generator';
2
2
  import * as t from '@babel/types';
3
3
  import query_msg from './../../../__fixtures__/basic/query_msg.json';
4
- import { createReactQueryHooks } from './react-query';
4
+ import execute_msg from './../../../__fixtures__/basic/execute_msg_for__empty.json';
5
+ import { createReactQueryHooks, createReactQueryMutationHooks } from './react-query';
5
6
 
6
7
  const expectCode = ast => {
7
8
  expect(generate(ast).code).toMatchSnapshot();
@@ -39,4 +40,26 @@ it('createReactQueryHooks', () => {
39
40
  optionalClient: true
40
41
  }
41
42
  })));
43
+ expectCode(t.program(createReactQueryHooks({
44
+ queryMsg: query_msg,
45
+ contractName: 'Sg721',
46
+ QueryClient: 'Sg721QueryClient',
47
+ options: {
48
+ v4: true
49
+ }
50
+ })));
51
+ expectCode(t.program(createReactQueryHooks({
52
+ queryMsg: query_msg,
53
+ contractName: 'Sg721',
54
+ QueryClient: 'Sg721QueryClient',
55
+ options: {
56
+ optionalClient: true,
57
+ v4: true
58
+ }
59
+ })));
60
+ expectCode(t.program(createReactQueryMutationHooks({
61
+ execMsg: execute_msg,
62
+ contractName: 'Sg721',
63
+ ExecuteClient: 'Sg721Client'
64
+ })));
42
65
  });
@@ -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
  });
@@ -1,5 +1,6 @@
1
1
  import * as t from '@babel/types';
2
2
  import { snake } from "case";
3
+ // t.TSPropertySignature - kind?
3
4
  export const propertySignature = (name, typeAnnotation, optional = false) => {
4
5
  return {
5
6
  type: 'TSPropertySignature',
@@ -164,4 +165,18 @@ export const optionalConditionalExpression = (test, expression, alternate, optio
164
165
  export const typeRefOrOptionalUnion = (identifier, optional = false) => {
165
166
  const typeReference = t.tsTypeReference(identifier);
166
167
  return optional ? t.tsUnionType([typeReference, t.tsUndefinedKeyword()]) : typeReference;
168
+ };
169
+ export const parameterizedTypeReference = (identifier, from, omit) => {
170
+ return t.tsTypeReference(t.identifier(identifier), t.tsTypeParameterInstantiation([from, typeof omit === 'string' ? t.tsLiteralType(t.stringLiteral(omit)) : t.tsUnionType(omit.map(o => t.tsLiteralType(t.stringLiteral(o))))]));
171
+ };
172
+ /**
173
+ * omitTypeReference(t.tsTypeReference(t.identifier('Cw4UpdateMembersMutation'),),'args').....
174
+ * Omit<Cw4UpdateMembersMutation, 'args'>
175
+ */
176
+
177
+ export const omitTypeReference = (from, omit) => {
178
+ return parameterizedTypeReference('Omit', from, omit);
179
+ };
180
+ export const pickTypeReference = (from, pick) => {
181
+ return parameterizedTypeReference('Pick', from, pick);
167
182
  };