wasm-ast-types 0.1.3 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/main/index.js CHANGED
@@ -28,4 +28,30 @@ Object.keys(_wasm).forEach(function (key) {
28
28
  return _wasm[key];
29
29
  }
30
30
  });
31
+ });
32
+
33
+ var _recoil = require("./recoil");
34
+
35
+ Object.keys(_recoil).forEach(function (key) {
36
+ if (key === "default" || key === "__esModule") return;
37
+ if (key in exports && exports[key] === _recoil[key]) return;
38
+ Object.defineProperty(exports, key, {
39
+ enumerable: true,
40
+ get: function get() {
41
+ return _recoil[key];
42
+ }
43
+ });
44
+ });
45
+
46
+ var _reactQuery = require("./react-query");
47
+
48
+ Object.keys(_reactQuery).forEach(function (key) {
49
+ if (key === "default" || key === "__esModule") return;
50
+ if (key in exports && exports[key] === _reactQuery[key]) return;
51
+ Object.defineProperty(exports, key, {
52
+ enumerable: true,
53
+ get: function get() {
54
+ return _reactQuery[key];
55
+ }
56
+ });
31
57
  });
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ var _typeof = require("@babel/runtime/helpers/typeof");
6
+
7
+ Object.defineProperty(exports, "__esModule", {
8
+ value: true
9
+ });
10
+ exports.createReactQueryHooks = exports.createReactQueryHookInterface = exports.createReactQueryHook = void 0;
11
+
12
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
+
14
+ var t = _interopRequireWildcard(require("@babel/types"));
15
+
16
+ var _case = require("case");
17
+
18
+ var _utils = require("./utils");
19
+
20
+ var _wasm = require("./wasm");
21
+
22
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
23
+
24
+ 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; }
25
+
26
+ var createReactQueryHooks = function createReactQueryHooks(queryMsg, contractName, QueryClient) {
27
+ return (0, _utils.getMessageProperties)(queryMsg).reduce(function (m, schema) {
28
+ var underscoreName = Object.keys(schema.properties)[0];
29
+ var methodName = (0, _case.camel)(underscoreName);
30
+ var hookName = "use".concat((0, _case.pascal)(contractName)).concat((0, _case.pascal)(methodName), "Query");
31
+ var hookParamsTypeName = "".concat((0, _case.pascal)(contractName)).concat((0, _case.pascal)(methodName), "Query");
32
+ var responseType = (0, _case.pascal)("".concat(methodName, "Response"));
33
+ var getterKey = (0, _case.camel)("".concat(contractName).concat((0, _case.pascal)(methodName)));
34
+ var jsonschema = schema.properties[underscoreName];
35
+ return [createReactQueryHookInterface({
36
+ hookParamsTypeName: hookParamsTypeName,
37
+ responseType: responseType,
38
+ QueryClient: QueryClient,
39
+ jsonschema: jsonschema
40
+ }), createReactQueryHook({
41
+ methodName: methodName,
42
+ hookName: hookName,
43
+ hookParamsTypeName: hookParamsTypeName,
44
+ responseType: responseType,
45
+ hookKeyName: getterKey,
46
+ jsonschema: jsonschema
47
+ })].concat((0, _toConsumableArray2["default"])(m));
48
+ }, []);
49
+ };
50
+
51
+ exports.createReactQueryHooks = createReactQueryHooks;
52
+
53
+ var createReactQueryHook = function createReactQueryHook(_ref) {
54
+ var _jsonschema$propertie;
55
+
56
+ var hookName = _ref.hookName,
57
+ hookParamsTypeName = _ref.hookParamsTypeName,
58
+ responseType = _ref.responseType,
59
+ hookKeyName = _ref.hookKeyName,
60
+ methodName = _ref.methodName,
61
+ jsonschema = _ref.jsonschema;
62
+ var keys = Object.keys((_jsonschema$propertie = jsonschema.properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {});
63
+ var args = [];
64
+
65
+ if (keys.length) {
66
+ args = [t.objectExpression((0, _toConsumableArray2["default"])(keys.map(function (prop) {
67
+ return t.objectProperty(t.identifier((0, _case.camel)(prop)), t.memberExpression(t.identifier('args'), t.identifier((0, _case.camel)(prop))));
68
+ })))];
69
+ }
70
+
71
+ var props = ['client', 'options'];
72
+
73
+ if (keys.length) {
74
+ props = ['client', 'args', 'options'];
75
+ }
76
+
77
+ return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(hookName), [(0, _utils.tsObjectPattern)((0, _toConsumableArray2["default"])(props.map(function (prop) {
78
+ return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
79
+ })), 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()])))])))])));
80
+ };
81
+
82
+ exports.createReactQueryHook = createReactQueryHook;
83
+
84
+ var createReactQueryHookInterface = function createReactQueryHookInterface(_ref2) {
85
+ var QueryClient = _ref2.QueryClient,
86
+ hookParamsTypeName = _ref2.hookParamsTypeName,
87
+ responseType = _ref2.responseType,
88
+ jsonschema = _ref2.jsonschema;
89
+ 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)];
90
+ var props = getProps(jsonschema, true);
91
+
92
+ if (props.length) {
93
+ body.push(t.tsPropertySignature(t.identifier('args'), t.tsTypeAnnotation(t.tsTypeLiteral(props))));
94
+ }
95
+
96
+ return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(hookParamsTypeName), null, [], t.tsInterfaceBody(body)));
97
+ };
98
+
99
+ exports.createReactQueryHookInterface = createReactQueryHookInterface;
100
+
101
+ var getProps = function getProps(jsonschema, camelize) {
102
+ var _jsonschema$propertie2;
103
+
104
+ var keys = Object.keys((_jsonschema$propertie2 = jsonschema.properties) !== null && _jsonschema$propertie2 !== void 0 ? _jsonschema$propertie2 : {});
105
+ if (!keys.length) return [];
106
+ return keys.map(function (prop) {
107
+ var _getPropertyType = (0, _wasm.getPropertyType)(jsonschema, prop),
108
+ type = _getPropertyType.type,
109
+ optional = _getPropertyType.optional;
110
+
111
+ return (0, _wasm.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional);
112
+ });
113
+ };
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ var _typeof = require("@babel/runtime/helpers/typeof");
6
+
7
+ var _generator = _interopRequireDefault(require("@babel/generator"));
8
+
9
+ var t = _interopRequireWildcard(require("@babel/types"));
10
+
11
+ var _query_msg = _interopRequireDefault(require("./__fixtures__/schema/query_msg.json"));
12
+
13
+ var _reactQuery = require("./react-query");
14
+
15
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
16
+
17
+ 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; }
18
+
19
+ var expectCode = function expectCode(ast) {
20
+ expect((0, _generator["default"])(ast).code).toMatchSnapshot();
21
+ };
22
+
23
+ var printCode = function printCode(ast) {
24
+ console.log((0, _generator["default"])(ast).code);
25
+ }; // it('createReactQueryHook', () => {
26
+ // printCode(createReactQueryHook({
27
+ // methodName: 'allNftInfo',
28
+ // hookName: 'useSg721AllNftInfoQuery',
29
+ // hookParamsTypeName: 'Sg721CollectionInfoQuery',
30
+ // responseType: 'AllNftInfoResponse',
31
+ // hookKeyName: 'Sg721AllNftInfo'
32
+ // }))
33
+ // });
34
+
35
+
36
+ it('createReactQueryHooks', function () {
37
+ printCode(t.program((0, _reactQuery.createReactQueryHooks)(_query_msg["default"], 'Sg721', 'Sg721QueryClient')));
38
+ expectCode(t.program((0, _reactQuery.createReactQueryHooks)(_query_msg["default"], 'Sg721', 'Sg721QueryClient')));
39
+ });
package/main/recoil.js ADDED
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+
3
+ var _typeof = require("@babel/runtime/helpers/typeof");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.createWasmRecoilMethod = exports.createRecoilSelectors = exports.createRecoilSelector = exports.createRecoilQueryClientType = exports.createRecoilQueryClient = void 0;
9
+
10
+ var t = _interopRequireWildcard(require("@babel/types"));
11
+
12
+ var _case = require("case");
13
+
14
+ var _utils = require("./utils");
15
+
16
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
17
+
18
+ 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; }
19
+
20
+ var createWasmRecoilMethod = function createWasmRecoilMethod(jsonschema) {
21
+ var _jsonschema$propertie;
22
+
23
+ var underscoreName = Object.keys(jsonschema.properties)[0];
24
+ var methodName = (0, _case.camel)(underscoreName);
25
+ var responseType = (0, _case.pascal)("".concat(methodName, "Response"));
26
+ var properties = (_jsonschema$propertie = jsonschema.properties[underscoreName].properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {};
27
+
28
+ if (Object.keys(properties).length > 0) {} else {}
29
+ };
30
+
31
+ exports.createWasmRecoilMethod = createWasmRecoilMethod;
32
+
33
+ var createRecoilSelector = function createRecoilSelector(keyPrefix, QueryClient, methodName) {
34
+ // const propertyNames = getMessageProperties(queryMsg)
35
+ // .map(method => Object.keys(method.properties)?.[0])
36
+ // .filter(Boolean);
37
+ // const methods = getMessageProperties(queryMsg)
38
+ // .map(schema => createWasmRecoilMethod(schema))
39
+ var selectorName = (0, _case.camel)("".concat(methodName, "Selector"));
40
+ var responseType = (0, _case.pascal)("".concat(methodName, "Response"));
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)))]))))])])])))]));
44
+ };
45
+
46
+ exports.createRecoilSelector = createRecoilSelector;
47
+
48
+ var createRecoilSelectors = function createRecoilSelectors(keyPrefix, QueryClient, queryMsg) {
49
+ return (0, _utils.getMessageProperties)(queryMsg).map(function (schema) {
50
+ var underscoreName = Object.keys(schema.properties)[0];
51
+ var methodName = (0, _case.camel)(underscoreName);
52
+ return createRecoilSelector(keyPrefix, QueryClient, methodName);
53
+ });
54
+ };
55
+
56
+ exports.createRecoilSelectors = createRecoilSelectors;
57
+
58
+ var createRecoilQueryClientType = function createRecoilQueryClientType() {
59
+ return {
60
+ "type": "TSTypeAliasDeclaration",
61
+ "id": {
62
+ "type": "Identifier",
63
+ "name": "QueryClientParams"
64
+ },
65
+ "typeAnnotation": {
66
+ "type": "TSTypeLiteral",
67
+ "members": [{
68
+ "type": "TSPropertySignature",
69
+ "key": {
70
+ "type": "Identifier",
71
+ "name": "contractAddress"
72
+ },
73
+ "computed": false,
74
+ "typeAnnotation": {
75
+ "type": "TSTypeAnnotation",
76
+ "typeAnnotation": {
77
+ "type": "TSStringKeyword"
78
+ }
79
+ }
80
+ }]
81
+ }
82
+ };
83
+ };
84
+
85
+ exports.createRecoilQueryClientType = createRecoilQueryClientType;
86
+
87
+ var createRecoilQueryClient = function createRecoilQueryClient(keyPrefix, QueryClient) {
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'))])))]));
90
+ };
91
+
92
+ exports.createRecoilQueryClient = createRecoilQueryClient;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ var _typeof = require("@babel/runtime/helpers/typeof");
6
+
7
+ var _generator = _interopRequireDefault(require("@babel/generator"));
8
+
9
+ var t = _interopRequireWildcard(require("@babel/types"));
10
+
11
+ var _query_msg = _interopRequireDefault(require("./__fixtures__/schema/query_msg.json"));
12
+
13
+ var _recoil = require("./recoil");
14
+
15
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
16
+
17
+ 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; }
18
+
19
+ var expectCode = function expectCode(ast) {
20
+ expect((0, _generator["default"])(ast).code).toMatchSnapshot();
21
+ };
22
+
23
+ var printCode = function printCode(ast) {
24
+ console.log((0, _generator["default"])(ast).code);
25
+ };
26
+
27
+ it('selector', function () {
28
+ expectCode((0, _recoil.createRecoilSelector)('SG721', 'SG721QueryClient', 'governanceModules'));
29
+ });
30
+ it('selectors', function () {
31
+ expectCode(t.program((0, _recoil.createRecoilSelectors)('SG721', 'SG721QueryClient', _query_msg["default"])));
32
+ });
33
+ it('client', function () {
34
+ printCode((0, _recoil.createRecoilQueryClient)('SG721', 'SG721QueryClient'));
35
+ });
package/main/types.js ADDED
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+
3
+ ;
4
+ ;
package/main/utils.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.typedIdentifier = exports.shorthandProperty = exports.recursiveNamespace = exports.promiseTypeAnnotation = exports.memberExpressionOrIdentifierSnake = exports.memberExpressionOrIdentifier = exports.importStmt = exports.importAminoMsg = exports.getFieldDimensionality = exports.classProperty = exports.classDeclaration = exports.bindMethod = exports.arrowFunctionExpression = exports.arrayTypeNDimensions = exports.FieldTypeAsts = void 0;
10
+ exports.typedIdentifier = exports.tsPropertySignature = exports.tsObjectPattern = exports.shorthandProperty = exports.recursiveNamespace = exports.promiseTypeAnnotation = exports.memberExpressionOrIdentifierSnake = exports.memberExpressionOrIdentifier = exports.importStmt = exports.importAminoMsg = 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
 
@@ -21,6 +21,39 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
21
21
 
22
22
  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; }
23
23
 
24
+ var getMessageProperties = function getMessageProperties(msg) {
25
+ if (msg.anyOf) return msg.anyOf;
26
+ if (msg.oneOf) return msg.oneOf;
27
+ if (msg.allOf) return msg.allOf;
28
+ return [];
29
+ };
30
+
31
+ exports.getMessageProperties = getMessageProperties;
32
+
33
+ var tsPropertySignature = function tsPropertySignature(key, typeAnnotation, optional) {
34
+ var obj = t.tsPropertySignature(key, typeAnnotation);
35
+ obj.optional = optional;
36
+ return obj;
37
+ };
38
+
39
+ exports.tsPropertySignature = tsPropertySignature;
40
+
41
+ var tsObjectPattern = function tsObjectPattern(properties, typeAnnotation) {
42
+ var obj = t.objectPattern(properties);
43
+ obj.typeAnnotation = typeAnnotation;
44
+ return obj;
45
+ };
46
+
47
+ exports.tsObjectPattern = tsObjectPattern;
48
+
49
+ var callExpression = function callExpression(callee, _arguments, typeParameters) {
50
+ var callExpr = t.callExpression(callee, _arguments);
51
+ callExpr.typeParameters = typeParameters;
52
+ return callExpr;
53
+ };
54
+
55
+ exports.callExpression = callExpression;
56
+
24
57
  var bindMethod = function bindMethod(name) {
25
58
  return t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier(name)), t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier(name)), t.identifier('bind')), [t.thisExpression()])));
26
59
  };
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.propertySignature = exports.getMessageProperties = exports.createWasmQueryMethod = exports.createWasmExecMethod = exports.createTypedObjectParams = exports.createTypeOrInterface = exports.createTypeInterface = exports.createQueryInterface = exports.createQueryClass = exports.createPropertyFunctionWithObjectParams = exports.createExecuteInterface = exports.createExecuteClass = void 0;
10
+ exports.propertySignature = exports.identifier = exports.getPropertyType = exports.createWasmQueryMethod = exports.createWasmExecMethod = exports.createTypedObjectParams = exports.createTypeOrInterface = exports.createTypeInterface = exports.createQueryInterface = exports.createQueryClass = exports.createPropertyFunctionWithObjectParamsForExec = exports.createPropertyFunctionWithObjectParams = exports.createExecuteInterface = exports.createExecuteClass = void 0;
11
11
 
12
12
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
13
 
@@ -23,19 +23,10 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
23
23
 
24
24
  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; }
25
25
 
26
- var getMessageProperties = function getMessageProperties(msg) {
27
- if (msg.anyOf) return msg.anyOf;
28
- if (msg.oneOf) return msg.oneOf;
29
- if (msg.allOf) return msg.allOf;
30
- return [];
31
- };
32
-
33
- exports.getMessageProperties = getMessageProperties;
34
-
35
26
  var getTypeFromRef = function getTypeFromRef($ref) {
36
27
  switch ($ref) {
37
28
  case '#/definitions/Binary':
38
- return t.tsTypeReference(t.identifier('BinaryType'));
29
+ return t.tsTypeReference(t.identifier('Binary'));
39
30
 
40
31
  case '#/definitions/Expiration':
41
32
  return t.tsTypeReference(t.identifier('Expiration'));
@@ -57,6 +48,16 @@ var getArrayTypeFromType = function getArrayTypeFromType(type) {
57
48
  return t.tsArrayType(getType(type));
58
49
  };
59
50
 
51
+ var identifier = function identifier(name, typeAnnotation) {
52
+ var optional = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
53
+ var type = t.identifier(name);
54
+ type.typeAnnotation = typeAnnotation;
55
+ type.optional = optional;
56
+ return type;
57
+ };
58
+
59
+ exports.identifier = identifier;
60
+
60
61
  var getType = function getType(type) {
61
62
  switch (type) {
62
63
  case 'string':
@@ -151,6 +152,8 @@ var getPropertyType = function getPropertyType(schema, prop) {
151
152
  };
152
153
  };
153
154
 
155
+ exports.getPropertyType = getPropertyType;
156
+
154
157
  var createWasmQueryMethod = function createWasmQueryMethod(jsonschema) {
155
158
  var _jsonschema$propertie;
156
159
 
@@ -169,13 +172,13 @@ var createWasmQueryMethod = function createWasmQueryMethod(jsonschema) {
169
172
  exports.createWasmQueryMethod = createWasmQueryMethod;
170
173
 
171
174
  var createQueryClass = function createQueryClass(className, implementsClassName, queryMsg) {
172
- var propertyNames = getMessageProperties(queryMsg).map(function (method) {
175
+ var propertyNames = (0, _utils.getMessageProperties)(queryMsg).map(function (method) {
173
176
  var _Object$keys;
174
177
 
175
178
  return (_Object$keys = Object.keys(method.properties)) === null || _Object$keys === void 0 ? void 0 : _Object$keys[0];
176
179
  }).filter(Boolean);
177
180
  var bindings = propertyNames.map(_case.camel).map(_utils.bindMethod);
178
- var methods = getMessageProperties(queryMsg).map(function (schema) {
181
+ var methods = (0, _utils.getMessageProperties)(queryMsg).map(function (schema) {
179
182
  return createWasmQueryMethod(schema);
180
183
  });
181
184
  return t.exportNamedDeclaration((0, _utils.classDeclaration)(className, [// client
@@ -187,6 +190,12 @@ var createQueryClass = function createQueryClass(className, implementsClassName,
187
190
 
188
191
  exports.createQueryClass = createQueryClass;
189
192
 
193
+ var tsTypeOperator = function tsTypeOperator(typeAnnotation, operator) {
194
+ var obj = t.tsTypeOperator(typeAnnotation);
195
+ obj.operator = operator;
196
+ return obj;
197
+ };
198
+
190
199
  var createWasmExecMethod = function createWasmExecMethod(jsonschema) {
191
200
  var _jsonschema$propertie2;
192
201
 
@@ -198,20 +207,20 @@ var createWasmExecMethod = function createWasmExecMethod(jsonschema) {
198
207
  return t.objectProperty(t.identifier(prop), t.identifier((0, _case.camel)(prop)), false, prop === (0, _case.camel)(prop));
199
208
  });
200
209
  return t.classProperty(t.identifier(methodName), (0, _utils.arrowFunctionExpression)(obj ? [// props
201
- obj] : [], 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.stringLiteral('auto')])))]), // return type
210
+ obj, t.assignmentPattern(identifier('fee', t.tsTypeAnnotation(t.tsUnionType([t.tSNumberKeyword(), t.tsTypeReference(t.identifier('StdFee'))])), false), t.stringLiteral('auto')), identifier('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), identifier('funds', t.tsTypeAnnotation(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)] : [], 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
202
211
  t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true));
203
212
  };
204
213
 
205
214
  exports.createWasmExecMethod = createWasmExecMethod;
206
215
 
207
216
  var createExecuteClass = function createExecuteClass(className, implementsClassName, extendsClassName, execMsg) {
208
- var propertyNames = getMessageProperties(execMsg).map(function (method) {
217
+ var propertyNames = (0, _utils.getMessageProperties)(execMsg).map(function (method) {
209
218
  var _Object$keys2;
210
219
 
211
220
  return (_Object$keys2 = Object.keys(method.properties)) === null || _Object$keys2 === void 0 ? void 0 : _Object$keys2[0];
212
221
  }).filter(Boolean);
213
222
  var bindings = propertyNames.map(_case.camel).map(_utils.bindMethod);
214
- var methods = getMessageProperties(execMsg).map(function (schema) {
223
+ var methods = (0, _utils.getMessageProperties)(execMsg).map(function (schema) {
215
224
  return createWasmExecMethod(schema);
216
225
  });
217
226
  var blockStmt = [];
@@ -233,10 +242,10 @@ var createExecuteClass = function createExecuteClass(className, implementsClassN
233
242
  exports.createExecuteClass = createExecuteClass;
234
243
 
235
244
  var createExecuteInterface = function createExecuteInterface(className, extendsClassName, execMsg) {
236
- var methods = getMessageProperties(execMsg).map(function (jsonschema) {
245
+ var methods = (0, _utils.getMessageProperties)(execMsg).map(function (jsonschema) {
237
246
  var underscoreName = Object.keys(jsonschema.properties)[0];
238
247
  var methodName = (0, _case.camel)(underscoreName);
239
- return createPropertyFunctionWithObjectParams(methodName, 'ExecuteResult', jsonschema.properties[underscoreName]);
248
+ return createPropertyFunctionWithObjectParamsForExec(methodName, 'ExecuteResult', jsonschema.properties[underscoreName]);
240
249
  });
241
250
  var extendsAst = extendsClassName ? [t.tSExpressionWithTypeArguments(t.identifier(extendsClassName))] : [];
242
251
  return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(className), null, extendsAst, t.tSInterfaceBody([// contract address
@@ -301,8 +310,21 @@ var createPropertyFunctionWithObjectParams = function createPropertyFunctionWith
301
310
 
302
311
  exports.createPropertyFunctionWithObjectParams = createPropertyFunctionWithObjectParams;
303
312
 
313
+ var createPropertyFunctionWithObjectParamsForExec = function createPropertyFunctionWithObjectParamsForExec(methodName, responseType, jsonschema) {
314
+ var obj = createTypedObjectParams(jsonschema);
315
+ var fixedParams = [identifier('fee', t.tsTypeAnnotation(t.tsUnionType([t.tsNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), true), identifier('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), identifier('funds', t.tsTypeAnnotation(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
316
+ var func = {
317
+ type: 'TSFunctionType',
318
+ typeAnnotation: (0, _utils.promiseTypeAnnotation)(responseType),
319
+ parameters: obj ? [obj].concat(fixedParams) : fixedParams
320
+ };
321
+ return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(func));
322
+ };
323
+
324
+ exports.createPropertyFunctionWithObjectParamsForExec = createPropertyFunctionWithObjectParamsForExec;
325
+
304
326
  var createQueryInterface = function createQueryInterface(className, queryMsg) {
305
- var methods = getMessageProperties(queryMsg).map(function (jsonschema) {
327
+ var methods = (0, _utils.getMessageProperties)(queryMsg).map(function (jsonschema) {
306
328
  var underscoreName = Object.keys(jsonschema.properties)[0];
307
329
  var methodName = (0, _case.camel)(underscoreName);
308
330
  var responseType = (0, _case.pascal)("".concat(methodName, "Response"));
package/module/index.js CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from './utils';
2
- export * from './wasm';
2
+ export * from './wasm';
3
+ export * from './recoil';
4
+ export * from './react-query';
@@ -0,0 +1,82 @@
1
+ import * as t from '@babel/types';
2
+ import { camel, pascal } from 'case';
3
+ import { tsPropertySignature, tsObjectPattern, callExpression, getMessageProperties } from './utils';
4
+ import { propertySignature, getPropertyType } from './wasm';
5
+ export const createReactQueryHooks = (queryMsg, contractName, QueryClient) => {
6
+ return getMessageProperties(queryMsg).reduce((m, schema) => {
7
+ const underscoreName = Object.keys(schema.properties)[0];
8
+ const methodName = camel(underscoreName);
9
+ const hookName = `use${pascal(contractName)}${pascal(methodName)}Query`;
10
+ const hookParamsTypeName = `${pascal(contractName)}${pascal(methodName)}Query`;
11
+ const responseType = pascal(`${methodName}Response`);
12
+ const getterKey = camel(`${contractName}${pascal(methodName)}`);
13
+ const jsonschema = schema.properties[underscoreName];
14
+ return [createReactQueryHookInterface({
15
+ hookParamsTypeName,
16
+ responseType,
17
+ QueryClient,
18
+ jsonschema
19
+ }), createReactQueryHook({
20
+ methodName,
21
+ hookName,
22
+ hookParamsTypeName,
23
+ responseType,
24
+ hookKeyName: getterKey,
25
+ jsonschema
26
+ }), ...m];
27
+ }, []);
28
+ };
29
+ export const createReactQueryHook = ({
30
+ hookName,
31
+ hookParamsTypeName,
32
+ responseType,
33
+ hookKeyName,
34
+ methodName,
35
+ jsonschema
36
+ }) => {
37
+ const keys = Object.keys(jsonschema.properties ?? {});
38
+ let args = [];
39
+
40
+ if (keys.length) {
41
+ args = [t.objectExpression([...keys.map(prop => {
42
+ return t.objectProperty(t.identifier(camel(prop)), t.memberExpression(t.identifier('args'), t.identifier(camel(prop))));
43
+ })])];
44
+ }
45
+
46
+ let props = ['client', 'options'];
47
+
48
+ if (keys.length) {
49
+ props = ['client', 'args', 'options'];
50
+ }
51
+
52
+ return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(hookName), [tsObjectPattern([...props.map(prop => {
53
+ return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
54
+ })], 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()])))])))])));
55
+ };
56
+ export const createReactQueryHookInterface = ({
57
+ QueryClient,
58
+ hookParamsTypeName,
59
+ responseType,
60
+ jsonschema
61
+ }) => {
62
+ 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)];
63
+ const props = getProps(jsonschema, true);
64
+
65
+ if (props.length) {
66
+ body.push(t.tsPropertySignature(t.identifier('args'), t.tsTypeAnnotation(t.tsTypeLiteral(props))));
67
+ }
68
+
69
+ return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(hookParamsTypeName), null, [], t.tsInterfaceBody(body)));
70
+ };
71
+
72
+ const getProps = (jsonschema, camelize) => {
73
+ const keys = Object.keys(jsonschema.properties ?? {});
74
+ if (!keys.length) return [];
75
+ return keys.map(prop => {
76
+ const {
77
+ type,
78
+ optional
79
+ } = getPropertyType(jsonschema, prop);
80
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
81
+ });
82
+ };
@@ -0,0 +1,26 @@
1
+ import generate from '@babel/generator';
2
+ import * as t from '@babel/types';
3
+ import query_msg from './__fixtures__/schema/query_msg.json';
4
+ import { createReactQueryHooks } from './react-query';
5
+
6
+ const expectCode = ast => {
7
+ expect(generate(ast).code).toMatchSnapshot();
8
+ };
9
+
10
+ const printCode = ast => {
11
+ console.log(generate(ast).code);
12
+ }; // it('createReactQueryHook', () => {
13
+ // printCode(createReactQueryHook({
14
+ // methodName: 'allNftInfo',
15
+ // hookName: 'useSg721AllNftInfoQuery',
16
+ // hookParamsTypeName: 'Sg721CollectionInfoQuery',
17
+ // responseType: 'AllNftInfoResponse',
18
+ // hookKeyName: 'Sg721AllNftInfo'
19
+ // }))
20
+ // });
21
+
22
+
23
+ it('createReactQueryHooks', () => {
24
+ printCode(t.program(createReactQueryHooks(query_msg, 'Sg721', 'Sg721QueryClient')));
25
+ expectCode(t.program(createReactQueryHooks(query_msg, 'Sg721', 'Sg721QueryClient')));
26
+ });
@@ -0,0 +1,58 @@
1
+ import * as t from '@babel/types';
2
+ import { camel, pascal } from 'case';
3
+ import { callExpression, getMessageProperties } from './utils';
4
+ export const createWasmRecoilMethod = jsonschema => {
5
+ const underscoreName = Object.keys(jsonschema.properties)[0];
6
+ const methodName = camel(underscoreName);
7
+ const responseType = pascal(`${methodName}Response`);
8
+ const properties = jsonschema.properties[underscoreName].properties ?? {};
9
+
10
+ if (Object.keys(properties).length > 0) {} else {}
11
+ };
12
+ export const createRecoilSelector = (keyPrefix, QueryClient, methodName) => {
13
+ // const propertyNames = getMessageProperties(queryMsg)
14
+ // .map(method => Object.keys(method.properties)?.[0])
15
+ // .filter(Boolean);
16
+ // const methods = getMessageProperties(queryMsg)
17
+ // .map(schema => createWasmRecoilMethod(schema))
18
+ const selectorName = camel(`${methodName}Selector`);
19
+ const responseType = pascal(`${methodName}Response`);
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)))]))))])])])))]));
23
+ };
24
+ export const createRecoilSelectors = (keyPrefix, QueryClient, queryMsg) => {
25
+ return getMessageProperties(queryMsg).map(schema => {
26
+ const underscoreName = Object.keys(schema.properties)[0];
27
+ const methodName = camel(underscoreName);
28
+ return createRecoilSelector(keyPrefix, QueryClient, methodName);
29
+ });
30
+ };
31
+ export const createRecoilQueryClientType = () => ({
32
+ "type": "TSTypeAliasDeclaration",
33
+ "id": {
34
+ "type": "Identifier",
35
+ "name": "QueryClientParams"
36
+ },
37
+ "typeAnnotation": {
38
+ "type": "TSTypeLiteral",
39
+ "members": [{
40
+ "type": "TSPropertySignature",
41
+ "key": {
42
+ "type": "Identifier",
43
+ "name": "contractAddress"
44
+ },
45
+ "computed": false,
46
+ "typeAnnotation": {
47
+ "type": "TSTypeAnnotation",
48
+ "typeAnnotation": {
49
+ "type": "TSStringKeyword"
50
+ }
51
+ }
52
+ }]
53
+ }
54
+ });
55
+ export const createRecoilQueryClient = (keyPrefix, QueryClient) => {
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'))])))]));
58
+ };
@@ -0,0 +1,22 @@
1
+ import generate from '@babel/generator';
2
+ import * as t from '@babel/types';
3
+ import query_msg from './__fixtures__/schema/query_msg.json';
4
+ import { createRecoilSelector, createRecoilSelectors, createRecoilQueryClient } from './recoil';
5
+
6
+ const expectCode = ast => {
7
+ expect(generate(ast).code).toMatchSnapshot();
8
+ };
9
+
10
+ const printCode = ast => {
11
+ console.log(generate(ast).code);
12
+ };
13
+
14
+ it('selector', () => {
15
+ expectCode(createRecoilSelector('SG721', 'SG721QueryClient', 'governanceModules'));
16
+ });
17
+ it('selectors', () => {
18
+ expectCode(t.program(createRecoilSelectors('SG721', 'SG721QueryClient', query_msg)));
19
+ });
20
+ it('client', () => {
21
+ printCode(createRecoilQueryClient('SG721', 'SG721QueryClient'));
22
+ });
@@ -0,0 +1,3 @@
1
+ ;
2
+ ;
3
+ export {};
package/module/utils.js CHANGED
@@ -1,5 +1,26 @@
1
1
  import * as t from '@babel/types';
2
2
  import { snake } from "case";
3
+ export const getMessageProperties = msg => {
4
+ if (msg.anyOf) return msg.anyOf;
5
+ if (msg.oneOf) return msg.oneOf;
6
+ if (msg.allOf) return msg.allOf;
7
+ return [];
8
+ };
9
+ export const tsPropertySignature = (key, typeAnnotation, optional) => {
10
+ const obj = t.tsPropertySignature(key, typeAnnotation);
11
+ obj.optional = optional;
12
+ return obj;
13
+ };
14
+ export const tsObjectPattern = (properties, typeAnnotation) => {
15
+ const obj = t.objectPattern(properties);
16
+ obj.typeAnnotation = typeAnnotation;
17
+ return obj;
18
+ };
19
+ export const callExpression = (callee, _arguments, typeParameters) => {
20
+ const callExpr = t.callExpression(callee, _arguments);
21
+ callExpr.typeParameters = typeParameters;
22
+ return callExpr;
23
+ };
3
24
  export const bindMethod = name => {
4
25
  return t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier(name)), t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier(name)), t.identifier('bind')), [t.thisExpression()])));
5
26
  };
package/module/wasm.js CHANGED
@@ -1,17 +1,11 @@
1
1
  import * as t from '@babel/types';
2
2
  import { camel, pascal } from 'case';
3
- import { bindMethod, typedIdentifier, promiseTypeAnnotation, classDeclaration, classProperty, arrowFunctionExpression } from './utils';
4
- export const getMessageProperties = msg => {
5
- if (msg.anyOf) return msg.anyOf;
6
- if (msg.oneOf) return msg.oneOf;
7
- if (msg.allOf) return msg.allOf;
8
- return [];
9
- };
3
+ import { bindMethod, typedIdentifier, promiseTypeAnnotation, classDeclaration, classProperty, arrowFunctionExpression, getMessageProperties } from './utils';
10
4
 
11
5
  const getTypeFromRef = $ref => {
12
6
  switch ($ref) {
13
7
  case '#/definitions/Binary':
14
- return t.tsTypeReference(t.identifier('BinaryType'));
8
+ return t.tsTypeReference(t.identifier('Binary'));
15
9
 
16
10
  case '#/definitions/Expiration':
17
11
  return t.tsTypeReference(t.identifier('Expiration'));
@@ -33,6 +27,13 @@ const getArrayTypeFromType = type => {
33
27
  return t.tsArrayType(getType(type));
34
28
  };
35
29
 
30
+ export const identifier = (name, typeAnnotation, optional = false) => {
31
+ const type = t.identifier(name);
32
+ type.typeAnnotation = typeAnnotation;
33
+ type.optional = optional;
34
+ return type;
35
+ };
36
+
36
37
  const getType = type => {
37
38
  switch (type) {
38
39
  case 'string':
@@ -49,7 +50,7 @@ const getType = type => {
49
50
  }
50
51
  };
51
52
 
52
- const getPropertyType = (schema, prop) => {
53
+ export const getPropertyType = (schema, prop) => {
53
54
  const props = schema.properties ?? {};
54
55
  let info = props[prop];
55
56
  let type = null;
@@ -120,7 +121,6 @@ const getPropertyType = (schema, prop) => {
120
121
  optional
121
122
  };
122
123
  };
123
-
124
124
  export const createWasmQueryMethod = jsonschema => {
125
125
  const underscoreName = Object.keys(jsonschema.properties)[0];
126
126
  const methodName = camel(underscoreName);
@@ -145,6 +145,13 @@ export const createQueryClass = (className, implementsClassName, queryMsg) => {
145
145
  t.classMethod('constructor', t.identifier('constructor'), [typedIdentifier('client', t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CosmWasmClient')))), typedIdentifier('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword()))], t.blockStatement([// client/contract set
146
146
  t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('client'))), t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.identifier('contractAddress'))), ...bindings])), ...methods], [t.tSExpressionWithTypeArguments(t.identifier(implementsClassName))]));
147
147
  };
148
+
149
+ const tsTypeOperator = (typeAnnotation, operator) => {
150
+ const obj = t.tsTypeOperator(typeAnnotation);
151
+ obj.operator = operator;
152
+ return obj;
153
+ };
154
+
148
155
  export const createWasmExecMethod = jsonschema => {
149
156
  const underscoreName = Object.keys(jsonschema.properties)[0];
150
157
  const methodName = camel(underscoreName);
@@ -154,7 +161,7 @@ export const createWasmExecMethod = jsonschema => {
154
161
  return t.objectProperty(t.identifier(prop), t.identifier(camel(prop)), false, prop === camel(prop));
155
162
  });
156
163
  return t.classProperty(t.identifier(methodName), arrowFunctionExpression(obj ? [// props
157
- obj] : [], 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([...args]))]), t.stringLiteral('auto')])))]), // return type
164
+ obj, t.assignmentPattern(identifier('fee', t.tsTypeAnnotation(t.tsUnionType([t.tSNumberKeyword(), t.tsTypeReference(t.identifier('StdFee'))])), false), t.stringLiteral('auto')), identifier('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), identifier('funds', t.tsTypeAnnotation(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)] : [], 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([...args]))]), t.identifier('fee'), t.identifier('memo'), t.identifier('funds')])))]), // return type
158
165
  t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true));
159
166
  };
160
167
  export const createExecuteClass = (className, implementsClassName, extendsClassName, execMsg) => {
@@ -182,12 +189,52 @@ export const createExecuteInterface = (className, extendsClassName, execMsg) =>
182
189
  const methods = getMessageProperties(execMsg).map(jsonschema => {
183
190
  const underscoreName = Object.keys(jsonschema.properties)[0];
184
191
  const methodName = camel(underscoreName);
185
- return createPropertyFunctionWithObjectParams(methodName, 'ExecuteResult', jsonschema.properties[underscoreName]);
192
+ return createPropertyFunctionWithObjectParamsForExec(methodName, 'ExecuteResult', jsonschema.properties[underscoreName]);
186
193
  });
187
194
  const extendsAst = extendsClassName ? [t.tSExpressionWithTypeArguments(t.identifier(extendsClassName))] : [];
188
195
  return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(className), null, extendsAst, t.tSInterfaceBody([// contract address
189
196
  t.tSPropertySignature(t.identifier('contractAddress'), t.tsTypeAnnotation(t.tsStringKeyword())), // contract address
190
- t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods])));
197
+ t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods // // fee
198
+ // t.tSPropertySignature(
199
+ // t.identifier('fee'),
200
+ // t.tsTypeAnnotation(
201
+ // t.tsUnionType(
202
+ // [
203
+ // t.tsNumberKeyword(),
204
+ // t.tsTypeReference(
205
+ // t.identifier('StdFee')
206
+ // ),
207
+ // t.tsLiteralType(
208
+ // t.stringLiteral('auto')
209
+ // )
210
+ // ]
211
+ // )
212
+ // )
213
+ // ),
214
+ // // memo
215
+ // propertySignature(
216
+ // 'memo',
217
+ // t.tsTypeAnnotation(
218
+ // t.tsStringKeyword()
219
+ // ),
220
+ // true
221
+ // ),
222
+ // // funds
223
+ // propertySignature(
224
+ // 'funds',
225
+ // t.tsTypeAnnotation(
226
+ // tsTypeOperator(
227
+ // t.tsArrayType(
228
+ // t.tsTypeReference(
229
+ // t.identifier('Coin')
230
+ // )
231
+ // ),
232
+ // 'readonly'
233
+ // )
234
+ // ),
235
+ // true
236
+ // )
237
+ ])));
191
238
  };
192
239
  export const propertySignature = (name, typeAnnotation, optional = false) => {
193
240
  // prop.leadingComments = [{
@@ -231,6 +278,16 @@ export const createPropertyFunctionWithObjectParams = (methodName, responseType,
231
278
  };
232
279
  return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(func));
233
280
  };
281
+ export const createPropertyFunctionWithObjectParamsForExec = (methodName, responseType, jsonschema) => {
282
+ const obj = createTypedObjectParams(jsonschema);
283
+ const fixedParams = [identifier('fee', t.tsTypeAnnotation(t.tsUnionType([t.tsNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), true), identifier('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), identifier('funds', t.tsTypeAnnotation(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
284
+ const func = {
285
+ type: 'TSFunctionType',
286
+ typeAnnotation: promiseTypeAnnotation(responseType),
287
+ parameters: obj ? [obj, ...fixedParams] : fixedParams
288
+ };
289
+ return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(func));
290
+ };
234
291
  export const createQueryInterface = (className, queryMsg) => {
235
292
  const methods = getMessageProperties(queryMsg).map(jsonschema => {
236
293
  const underscoreName = Object.keys(jsonschema.properties)[0];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.1.3",
3
+ "version": "0.2.1",
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",
@@ -85,5 +85,5 @@
85
85
  "ast-stringify": "0.1.0",
86
86
  "case": "1.6.3"
87
87
  },
88
- "gitHead": "6820d1185529bc0a4f67743d69a0cfe087c57eb7"
88
+ "gitHead": "1ba0a66724ba90b1107aaccb9d075e1c59401a69"
89
89
  }
package/types/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from './utils';
2
2
  export * from './wasm';
3
+ export * from './recoil';
4
+ export * from './react-query';
@@ -0,0 +1,20 @@
1
+ import * as t from '@babel/types';
2
+ import { QueryMsg } from './types';
3
+ interface ReactQueryHookQuery {
4
+ hookName: string;
5
+ hookParamsTypeName: string;
6
+ hookKeyName: string;
7
+ responseType: string;
8
+ methodName: string;
9
+ jsonschema: any;
10
+ }
11
+ export declare const createReactQueryHooks: (queryMsg: QueryMsg, contractName: string, QueryClient: string) => any;
12
+ export declare const createReactQueryHook: ({ hookName, hookParamsTypeName, responseType, hookKeyName, methodName, jsonschema }: ReactQueryHookQuery) => t.ExportNamedDeclaration;
13
+ interface ReactQueryHookQueryInterface {
14
+ QueryClient: string;
15
+ hookParamsTypeName: string;
16
+ responseType: string;
17
+ jsonschema: any;
18
+ }
19
+ export declare const createReactQueryHookInterface: ({ QueryClient, hookParamsTypeName, responseType, jsonschema }: ReactQueryHookQueryInterface) => t.ExportNamedDeclaration;
20
+ export {};
@@ -0,0 +1,30 @@
1
+ import * as t from '@babel/types';
2
+ import { QueryMsg } from './types';
3
+ export declare const createWasmRecoilMethod: (jsonschema: any) => void;
4
+ export declare const createRecoilSelector: (keyPrefix: string, QueryClient: string, methodName: string) => t.ExportNamedDeclaration;
5
+ export declare const createRecoilSelectors: (keyPrefix: string, QueryClient: string, queryMsg: QueryMsg) => any;
6
+ export declare const createRecoilQueryClientType: () => {
7
+ type: string;
8
+ id: {
9
+ type: string;
10
+ name: string;
11
+ };
12
+ typeAnnotation: {
13
+ type: string;
14
+ members: {
15
+ type: string;
16
+ key: {
17
+ type: string;
18
+ name: string;
19
+ };
20
+ computed: boolean;
21
+ typeAnnotation: {
22
+ type: string;
23
+ typeAnnotation: {
24
+ type: string;
25
+ };
26
+ };
27
+ }[];
28
+ };
29
+ };
30
+ export declare const createRecoilQueryClient: (keyPrefix: string, QueryClient: string) => t.ExportNamedDeclaration;
package/types/types.d.ts CHANGED
@@ -8,3 +8,17 @@ export interface Interface {
8
8
  name: string;
9
9
  fields: Field[];
10
10
  }
11
+ export interface QueryMsg {
12
+ $schema: string;
13
+ title: "QueryMsg";
14
+ oneOf?: any;
15
+ allOf?: any;
16
+ anyOf?: any;
17
+ }
18
+ export interface ExecuteMsg {
19
+ $schema: string;
20
+ title: "ExecuteMsg" | "ExecuteMsg_for_Empty";
21
+ oneOf?: any;
22
+ allOf?: any;
23
+ anyOf?: any;
24
+ }
package/types/utils.d.ts CHANGED
@@ -1,5 +1,10 @@
1
1
  import * as t from '@babel/types';
2
+ import { Field, QueryMsg } from './types';
2
3
  import { TSTypeAnnotation, TSExpressionWithTypeArguments } from '@babel/types';
4
+ export declare const getMessageProperties: (msg: QueryMsg | ExecuteMsg) => any;
5
+ export declare const tsPropertySignature: (key: t.Expression, typeAnnotation: t.TSTypeAnnotation, optional: boolean) => t.TSPropertySignature;
6
+ export declare const tsObjectPattern: (properties: (t.RestElement | t.ObjectProperty)[], typeAnnotation: t.TSTypeAnnotation) => t.ObjectPattern;
7
+ export declare const callExpression: (callee: t.Expression | t.V8IntrinsicIdentifier, _arguments: (t.Expression | t.SpreadElement | t.ArgumentPlaceholder)[], typeParameters: t.TSTypeParameterInstantiation) => t.CallExpression;
3
8
  export declare const bindMethod: (name: string) => t.ExpressionStatement;
4
9
  export declare const typedIdentifier: (name: string, typeAnnotation: TSTypeAnnotation, optional?: boolean) => t.Identifier;
5
10
  export declare const promiseTypeAnnotation: (name: any) => t.TSTypeAnnotation;
@@ -20,9 +25,9 @@ export declare const shorthandProperty: (prop: string) => t.ObjectProperty;
20
25
  export declare const importStmt: (names: string[], path: string) => t.ImportDeclaration;
21
26
  export declare const importAminoMsg: () => t.ImportDeclaration;
22
27
  export declare const getFieldDimensionality: (field: Field) => {
23
- typeName: any;
28
+ typeName: string;
24
29
  dimensions: number;
25
- isArray: any;
30
+ isArray: boolean;
26
31
  };
27
32
  export declare const memberExpressionOrIdentifier: (names: any) => any;
28
33
  export declare const memberExpressionOrIdentifierSnake: (names: any) => any;
package/types/wasm.d.ts CHANGED
@@ -1,19 +1,9 @@
1
1
  import * as t from '@babel/types';
2
- interface QueryMsg {
3
- $schema: string;
4
- title: "QueryMsg";
5
- oneOf?: any;
6
- allOf?: any;
7
- anyOf?: any;
8
- }
9
- interface ExecuteMsg {
10
- $schema: string;
11
- title: "ExecuteMsg" | "ExecuteMsg_for_Empty";
12
- oneOf?: any;
13
- allOf?: any;
14
- anyOf?: any;
15
- }
16
- export declare const getMessageProperties: (msg: QueryMsg | ExecuteMsg) => any;
2
+ import { QueryMsg, ExecuteMsg } from './types';
3
+ export declare const getPropertyType: (schema: any, prop: any) => {
4
+ type: any;
5
+ optional: any;
6
+ };
17
7
  export declare const createWasmQueryMethod: (jsonschema: any) => t.ClassProperty;
18
8
  export declare const createQueryClass: (className: string, implementsClassName: string, queryMsg: QueryMsg) => t.ExportNamedDeclaration;
19
9
  export declare const createWasmExecMethod: (jsonschema: any) => t.ClassProperty;
@@ -30,4 +20,3 @@ export declare const createPropertyFunctionWithObjectParams: (methodName: string
30
20
  export declare const createQueryInterface: (className: string, queryMsg: QueryMsg) => t.ExportNamedDeclaration;
31
21
  export declare const createTypeOrInterface: (Type: string, jsonschema: any) => t.ExportNamedDeclaration;
32
22
  export declare const createTypeInterface: (jsonschema: any) => t.ExportNamedDeclaration;
33
- export {};