wasm-ast-types 0.1.4 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/main/index.js +13 -0
- package/main/react-query.js +113 -0
- package/main/react-query.spec.js +39 -0
- package/main/recoil.js +4 -10
- package/main/types.js +4 -0
- package/main/utils.js +34 -1
- package/main/wasm.js +42 -19
- package/module/index.js +2 -1
- package/module/react-query.js +82 -0
- package/module/react-query.spec.js +26 -0
- package/module/recoil.js +1 -8
- package/module/types.js +3 -0
- package/module/utils.js +21 -0
- package/module/wasm.js +71 -13
- package/package.json +2 -2
- package/types/index.d.ts +1 -0
- package/types/react-query.d.ts +20 -0
- package/types/recoil.d.ts +1 -8
- package/types/types.d.ts +14 -0
- package/types/utils.d.ts +7 -2
- package/types/wasm.d.ts +5 -16
package/main/index.js
CHANGED
@@ -41,4 +41,17 @@ Object.keys(_recoil).forEach(function (key) {
|
|
41
41
|
return _recoil[key];
|
42
42
|
}
|
43
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
|
+
});
|
44
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
CHANGED
@@ -11,7 +11,7 @@ var t = _interopRequireWildcard(require("@babel/types"));
|
|
11
11
|
|
12
12
|
var _case = require("case");
|
13
13
|
|
14
|
-
var
|
14
|
+
var _utils = require("./utils");
|
15
15
|
|
16
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
17
|
|
@@ -30,12 +30,6 @@ var createWasmRecoilMethod = function createWasmRecoilMethod(jsonschema) {
|
|
30
30
|
|
31
31
|
exports.createWasmRecoilMethod = createWasmRecoilMethod;
|
32
32
|
|
33
|
-
var callExpression = function callExpression(callee, _arguments, typeParameters) {
|
34
|
-
var callExpr = t.callExpression(callee, _arguments);
|
35
|
-
callExpr.typeParameters = typeParameters;
|
36
|
-
return callExpr;
|
37
|
-
};
|
38
|
-
|
39
33
|
var createRecoilSelector = function createRecoilSelector(keyPrefix, QueryClient, methodName) {
|
40
34
|
// const propertyNames = getMessageProperties(queryMsg)
|
41
35
|
// .map(method => Object.keys(method.properties)?.[0])
|
@@ -45,14 +39,14 @@ var createRecoilSelector = function createRecoilSelector(keyPrefix, QueryClient,
|
|
45
39
|
var selectorName = (0, _case.camel)("".concat(methodName, "Selector"));
|
46
40
|
var responseType = (0, _case.pascal)("".concat(methodName, "Response"));
|
47
41
|
var getterKey = (0, _case.camel)("".concat(keyPrefix).concat((0, _case.pascal)(methodName)));
|
48
|
-
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')
|
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')
|
49
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)))]))))])])])))]));
|
50
44
|
};
|
51
45
|
|
52
46
|
exports.createRecoilSelector = createRecoilSelector;
|
53
47
|
|
54
48
|
var createRecoilSelectors = function createRecoilSelectors(keyPrefix, QueryClient, queryMsg) {
|
55
|
-
return (0,
|
49
|
+
return (0, _utils.getMessageProperties)(queryMsg).map(function (schema) {
|
56
50
|
var underscoreName = Object.keys(schema.properties)[0];
|
57
51
|
var methodName = (0, _case.camel)(underscoreName);
|
58
52
|
return createRecoilSelector(keyPrefix, QueryClient, methodName);
|
@@ -92,7 +86,7 @@ exports.createRecoilQueryClientType = createRecoilQueryClientType;
|
|
92
86
|
|
93
87
|
var createRecoilQueryClient = function createRecoilQueryClient(keyPrefix, QueryClient) {
|
94
88
|
var getterKey = (0, _case.camel)("".concat(keyPrefix, 'QueryClient'));
|
95
|
-
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'))])))]));
|
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'))])))]));
|
96
90
|
};
|
97
91
|
|
98
92
|
exports.createRecoilQueryClient = createRecoilQueryClient;
|
package/main/types.js
ADDED
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.
|
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('
|
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
|
|
@@ -197,21 +206,22 @@ var createWasmExecMethod = function createWasmExecMethod(jsonschema) {
|
|
197
206
|
var args = Object.keys(properties).map(function (prop) {
|
198
207
|
return t.objectProperty(t.identifier(prop), t.identifier((0, _case.camel)(prop)), false, prop === (0, _case.camel)(prop));
|
199
208
|
});
|
209
|
+
var constantParams = [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)];
|
200
210
|
return t.classProperty(t.identifier(methodName), (0, _utils.arrowFunctionExpression)(obj ? [// props
|
201
|
-
obj] :
|
211
|
+
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
|
202
212
|
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true));
|
203
213
|
};
|
204
214
|
|
205
215
|
exports.createWasmExecMethod = createWasmExecMethod;
|
206
216
|
|
207
217
|
var createExecuteClass = function createExecuteClass(className, implementsClassName, extendsClassName, execMsg) {
|
208
|
-
var propertyNames = getMessageProperties(execMsg).map(function (method) {
|
218
|
+
var propertyNames = (0, _utils.getMessageProperties)(execMsg).map(function (method) {
|
209
219
|
var _Object$keys2;
|
210
220
|
|
211
221
|
return (_Object$keys2 = Object.keys(method.properties)) === null || _Object$keys2 === void 0 ? void 0 : _Object$keys2[0];
|
212
222
|
}).filter(Boolean);
|
213
223
|
var bindings = propertyNames.map(_case.camel).map(_utils.bindMethod);
|
214
|
-
var methods = getMessageProperties(execMsg).map(function (schema) {
|
224
|
+
var methods = (0, _utils.getMessageProperties)(execMsg).map(function (schema) {
|
215
225
|
return createWasmExecMethod(schema);
|
216
226
|
});
|
217
227
|
var blockStmt = [];
|
@@ -233,10 +243,10 @@ var createExecuteClass = function createExecuteClass(className, implementsClassN
|
|
233
243
|
exports.createExecuteClass = createExecuteClass;
|
234
244
|
|
235
245
|
var createExecuteInterface = function createExecuteInterface(className, extendsClassName, execMsg) {
|
236
|
-
var methods = getMessageProperties(execMsg).map(function (jsonschema) {
|
246
|
+
var methods = (0, _utils.getMessageProperties)(execMsg).map(function (jsonschema) {
|
237
247
|
var underscoreName = Object.keys(jsonschema.properties)[0];
|
238
248
|
var methodName = (0, _case.camel)(underscoreName);
|
239
|
-
return
|
249
|
+
return createPropertyFunctionWithObjectParamsForExec(methodName, 'ExecuteResult', jsonschema.properties[underscoreName]);
|
240
250
|
});
|
241
251
|
var extendsAst = extendsClassName ? [t.tSExpressionWithTypeArguments(t.identifier(extendsClassName))] : [];
|
242
252
|
return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(className), null, extendsAst, t.tSInterfaceBody([// contract address
|
@@ -301,8 +311,21 @@ var createPropertyFunctionWithObjectParams = function createPropertyFunctionWith
|
|
301
311
|
|
302
312
|
exports.createPropertyFunctionWithObjectParams = createPropertyFunctionWithObjectParams;
|
303
313
|
|
314
|
+
var createPropertyFunctionWithObjectParamsForExec = function createPropertyFunctionWithObjectParamsForExec(methodName, responseType, jsonschema) {
|
315
|
+
var obj = createTypedObjectParams(jsonschema);
|
316
|
+
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)];
|
317
|
+
var func = {
|
318
|
+
type: 'TSFunctionType',
|
319
|
+
typeAnnotation: (0, _utils.promiseTypeAnnotation)(responseType),
|
320
|
+
parameters: obj ? [obj].concat(fixedParams) : fixedParams
|
321
|
+
};
|
322
|
+
return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(func));
|
323
|
+
};
|
324
|
+
|
325
|
+
exports.createPropertyFunctionWithObjectParamsForExec = createPropertyFunctionWithObjectParamsForExec;
|
326
|
+
|
304
327
|
var createQueryInterface = function createQueryInterface(className, queryMsg) {
|
305
|
-
var methods = getMessageProperties(queryMsg).map(function (jsonschema) {
|
328
|
+
var methods = (0, _utils.getMessageProperties)(queryMsg).map(function (jsonschema) {
|
306
329
|
var underscoreName = Object.keys(jsonschema.properties)[0];
|
307
330
|
var methodName = (0, _case.camel)(underscoreName);
|
308
331
|
var responseType = (0, _case.pascal)("".concat(methodName, "Response"));
|
package/module/index.js
CHANGED
@@ -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
|
+
});
|
package/module/recoil.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import * as t from '@babel/types';
|
2
2
|
import { camel, pascal } from 'case';
|
3
|
-
import { getMessageProperties } from './
|
3
|
+
import { callExpression, getMessageProperties } from './utils';
|
4
4
|
export const createWasmRecoilMethod = jsonschema => {
|
5
5
|
const underscoreName = Object.keys(jsonschema.properties)[0];
|
6
6
|
const methodName = camel(underscoreName);
|
@@ -9,13 +9,6 @@ export const createWasmRecoilMethod = jsonschema => {
|
|
9
9
|
|
10
10
|
if (Object.keys(properties).length > 0) {} else {}
|
11
11
|
};
|
12
|
-
|
13
|
-
const callExpression = (callee, _arguments, typeParameters) => {
|
14
|
-
const callExpr = t.callExpression(callee, _arguments);
|
15
|
-
callExpr.typeParameters = typeParameters;
|
16
|
-
return callExpr;
|
17
|
-
};
|
18
|
-
|
19
12
|
export const createRecoilSelector = (keyPrefix, QueryClient, methodName) => {
|
20
13
|
// const propertyNames = getMessageProperties(queryMsg)
|
21
14
|
// .map(method => Object.keys(method.properties)?.[0])
|
package/module/types.js
ADDED
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('
|
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);
|
@@ -153,8 +160,9 @@ export const createWasmExecMethod = jsonschema => {
|
|
153
160
|
const args = Object.keys(properties).map(prop => {
|
154
161
|
return t.objectProperty(t.identifier(prop), t.identifier(camel(prop)), false, prop === camel(prop));
|
155
162
|
});
|
163
|
+
const constantParams = [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)];
|
156
164
|
return t.classProperty(t.identifier(methodName), arrowFunctionExpression(obj ? [// props
|
157
|
-
obj] :
|
165
|
+
obj, ...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([...args]))]), t.identifier('fee'), t.identifier('memo'), t.identifier('funds')])))]), // return type
|
158
166
|
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true));
|
159
167
|
};
|
160
168
|
export const createExecuteClass = (className, implementsClassName, extendsClassName, execMsg) => {
|
@@ -182,12 +190,52 @@ export const createExecuteInterface = (className, extendsClassName, execMsg) =>
|
|
182
190
|
const methods = getMessageProperties(execMsg).map(jsonschema => {
|
183
191
|
const underscoreName = Object.keys(jsonschema.properties)[0];
|
184
192
|
const methodName = camel(underscoreName);
|
185
|
-
return
|
193
|
+
return createPropertyFunctionWithObjectParamsForExec(methodName, 'ExecuteResult', jsonschema.properties[underscoreName]);
|
186
194
|
});
|
187
195
|
const extendsAst = extendsClassName ? [t.tSExpressionWithTypeArguments(t.identifier(extendsClassName))] : [];
|
188
196
|
return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(className), null, extendsAst, t.tSInterfaceBody([// contract address
|
189
197
|
t.tSPropertySignature(t.identifier('contractAddress'), t.tsTypeAnnotation(t.tsStringKeyword())), // contract address
|
190
|
-
t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods
|
198
|
+
t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods // // fee
|
199
|
+
// t.tSPropertySignature(
|
200
|
+
// t.identifier('fee'),
|
201
|
+
// t.tsTypeAnnotation(
|
202
|
+
// t.tsUnionType(
|
203
|
+
// [
|
204
|
+
// t.tsNumberKeyword(),
|
205
|
+
// t.tsTypeReference(
|
206
|
+
// t.identifier('StdFee')
|
207
|
+
// ),
|
208
|
+
// t.tsLiteralType(
|
209
|
+
// t.stringLiteral('auto')
|
210
|
+
// )
|
211
|
+
// ]
|
212
|
+
// )
|
213
|
+
// )
|
214
|
+
// ),
|
215
|
+
// // memo
|
216
|
+
// propertySignature(
|
217
|
+
// 'memo',
|
218
|
+
// t.tsTypeAnnotation(
|
219
|
+
// t.tsStringKeyword()
|
220
|
+
// ),
|
221
|
+
// true
|
222
|
+
// ),
|
223
|
+
// // funds
|
224
|
+
// propertySignature(
|
225
|
+
// 'funds',
|
226
|
+
// t.tsTypeAnnotation(
|
227
|
+
// tsTypeOperator(
|
228
|
+
// t.tsArrayType(
|
229
|
+
// t.tsTypeReference(
|
230
|
+
// t.identifier('Coin')
|
231
|
+
// )
|
232
|
+
// ),
|
233
|
+
// 'readonly'
|
234
|
+
// )
|
235
|
+
// ),
|
236
|
+
// true
|
237
|
+
// )
|
238
|
+
])));
|
191
239
|
};
|
192
240
|
export const propertySignature = (name, typeAnnotation, optional = false) => {
|
193
241
|
// prop.leadingComments = [{
|
@@ -231,6 +279,16 @@ export const createPropertyFunctionWithObjectParams = (methodName, responseType,
|
|
231
279
|
};
|
232
280
|
return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(func));
|
233
281
|
};
|
282
|
+
export const createPropertyFunctionWithObjectParamsForExec = (methodName, responseType, jsonschema) => {
|
283
|
+
const obj = createTypedObjectParams(jsonschema);
|
284
|
+
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)];
|
285
|
+
const func = {
|
286
|
+
type: 'TSFunctionType',
|
287
|
+
typeAnnotation: promiseTypeAnnotation(responseType),
|
288
|
+
parameters: obj ? [obj, ...fixedParams] : fixedParams
|
289
|
+
};
|
290
|
+
return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(func));
|
291
|
+
};
|
234
292
|
export const createQueryInterface = (className, queryMsg) => {
|
235
293
|
const methods = getMessageProperties(queryMsg).map(jsonschema => {
|
236
294
|
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.
|
3
|
+
"version": "0.2.2",
|
4
4
|
"description": "CosmWasm TypeScript AST generation",
|
5
5
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
6
6
|
"homepage": "https://github.com/pyramation/cosmwasm-typescript-gen/tree/master/packages/wasm-ast-types#readme",
|
@@ -85,5 +85,5 @@
|
|
85
85
|
"ast-stringify": "0.1.0",
|
86
86
|
"case": "1.6.3"
|
87
87
|
},
|
88
|
-
"gitHead": "
|
88
|
+
"gitHead": "c2a825ebda3fd8c1e2080ac9092148403460cc45"
|
89
89
|
}
|
package/types/index.d.ts
CHANGED
@@ -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 {};
|
package/types/recoil.d.ts
CHANGED
@@ -1,11 +1,5 @@
|
|
1
1
|
import * as t from '@babel/types';
|
2
|
-
|
3
|
-
$schema: string;
|
4
|
-
title: "QueryMsg";
|
5
|
-
oneOf?: any;
|
6
|
-
allOf?: any;
|
7
|
-
anyOf?: any;
|
8
|
-
}
|
2
|
+
import { QueryMsg } from './types';
|
9
3
|
export declare const createWasmRecoilMethod: (jsonschema: any) => void;
|
10
4
|
export declare const createRecoilSelector: (keyPrefix: string, QueryClient: string, methodName: string) => t.ExportNamedDeclaration;
|
11
5
|
export declare const createRecoilSelectors: (keyPrefix: string, QueryClient: string, queryMsg: QueryMsg) => any;
|
@@ -34,4 +28,3 @@ export declare const createRecoilQueryClientType: () => {
|
|
34
28
|
};
|
35
29
|
};
|
36
30
|
export declare const createRecoilQueryClient: (keyPrefix: string, QueryClient: string) => t.ExportNamedDeclaration;
|
37
|
-
export {};
|
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:
|
28
|
+
typeName: string;
|
24
29
|
dimensions: number;
|
25
|
-
isArray:
|
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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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 {};
|