wasm-ast-types 0.6.0 → 0.7.0
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/client/client.js +6 -0
- package/main/context/context.js +28 -1
- package/main/context/imports.js +138 -0
- package/main/context/index.js +13 -0
- package/main/index.js +4 -4
- package/main/{from-partial → message-composer}/index.js +4 -4
- package/main/{from-partial/from-partial.js → message-composer/message-composer.js} +13 -9
- package/main/{from-partial/from-partial.spec.js → message-composer/message-composer.spec.js} +4 -4
- package/main/react-query/react-query.js +10 -4
- package/main/react-query/react-query.spec.js +2 -2
- package/main/recoil/recoil.js +2 -0
- package/main/utils/babel.js +7 -1
- package/main/utils/types.js +1 -1
- package/module/client/client.js +6 -0
- package/module/context/context.js +26 -1
- package/module/context/imports.js +97 -0
- package/module/context/index.js +2 -1
- package/module/index.js +1 -1
- package/module/message-composer/index.js +1 -0
- package/module/{from-partial/from-partial.js → message-composer/message-composer.js} +10 -6
- package/module/message-composer/message-composer.spec.js +12 -0
- package/module/react-query/react-query.js +10 -4
- package/module/react-query/react-query.spec.js +2 -2
- package/module/recoil/recoil.js +2 -0
- package/module/utils/babel.js +3 -0
- package/module/utils/types.js +1 -1
- package/package.json +2 -2
- package/types/src/context/context.d.ts +23 -2
- package/types/src/context/imports.d.ts +39 -0
- package/types/src/context/index.d.ts +1 -0
- package/types/src/index.d.ts +1 -1
- package/types/src/message-composer/index.d.ts +1 -0
- package/types/src/message-composer/message-composer.d.ts +5 -0
- package/types/src/utils/babel.d.ts +1 -0
- package/module/from-partial/from-partial.spec.js +0 -12
- package/module/from-partial/index.js +0 -1
- package/types/src/from-partial/from-partial.d.ts +0 -4
- package/types/src/from-partial/index.d.ts +0 -1
package/main/client/client.js
CHANGED
@@ -43,6 +43,7 @@ var createWasmQueryMethod = function createWasmQueryMethod(context, jsonschema)
|
|
43
43
|
exports.createWasmQueryMethod = createWasmQueryMethod;
|
44
44
|
|
45
45
|
var createQueryClass = function createQueryClass(context, className, implementsClassName, queryMsg) {
|
46
|
+
context.addUtil('CosmWasmClient');
|
46
47
|
var propertyNames = (0, _utils.getMessageProperties)(queryMsg).map(function (method) {
|
47
48
|
var _Object$keys;
|
48
49
|
|
@@ -85,6 +86,9 @@ var getWasmMethodArgs = function getWasmMethodArgs(context, jsonschema) {
|
|
85
86
|
exports.getWasmMethodArgs = getWasmMethodArgs;
|
86
87
|
|
87
88
|
var createWasmExecMethod = function createWasmExecMethod(context, jsonschema) {
|
89
|
+
context.addUtil('ExecuteResult');
|
90
|
+
context.addUtil('StdFee');
|
91
|
+
context.addUtil('Coin');
|
88
92
|
var underscoreName = Object.keys(jsonschema.properties)[0];
|
89
93
|
var methodName = (0, _case.camel)(underscoreName);
|
90
94
|
var obj = (0, _types2.createTypedObjectParams)(context, jsonschema.properties[underscoreName]);
|
@@ -97,6 +101,7 @@ var createWasmExecMethod = function createWasmExecMethod(context, jsonschema) {
|
|
97
101
|
exports.createWasmExecMethod = createWasmExecMethod;
|
98
102
|
|
99
103
|
var createExecuteClass = function createExecuteClass(context, className, implementsClassName, extendsClassName, execMsg) {
|
104
|
+
context.addUtil('SigningCosmWasmClient');
|
100
105
|
var propertyNames = (0, _utils.getMessageProperties)(execMsg).map(function (method) {
|
101
106
|
var _Object$keys2;
|
102
107
|
|
@@ -151,6 +156,7 @@ var createPropertyFunctionWithObjectParams = function createPropertyFunctionWith
|
|
151
156
|
exports.createPropertyFunctionWithObjectParams = createPropertyFunctionWithObjectParams;
|
152
157
|
|
153
158
|
var createPropertyFunctionWithObjectParamsForExec = function createPropertyFunctionWithObjectParamsForExec(context, methodName, responseType, jsonschema) {
|
159
|
+
context.addUtil('Coin');
|
154
160
|
var obj = (0, _types2.createTypedObjectParams)(context, jsonschema);
|
155
161
|
var func = {
|
156
162
|
type: 'TSFunctionType',
|
package/main/context/context.js
CHANGED
@@ -13,12 +13,28 @@ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/creat
|
|
13
13
|
|
14
14
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
15
15
|
|
16
|
+
var _imports = require("./imports");
|
17
|
+
|
16
18
|
var _deepmerge = _interopRequireDefault(require("deepmerge"));
|
17
19
|
|
18
20
|
var defaultOptions = {
|
21
|
+
types: {
|
22
|
+
enabled: true,
|
23
|
+
aliasExecuteMsg: false
|
24
|
+
},
|
25
|
+
client: {
|
26
|
+
enabled: true
|
27
|
+
},
|
28
|
+
recoil: {
|
29
|
+
enabled: false
|
30
|
+
},
|
31
|
+
messageComposer: {
|
32
|
+
enabled: false
|
33
|
+
},
|
19
34
|
reactQuery: {
|
35
|
+
enabled: false,
|
20
36
|
optionalClient: false,
|
21
|
-
|
37
|
+
version: 'v3',
|
22
38
|
mutations: false,
|
23
39
|
camelize: true
|
24
40
|
}
|
@@ -29,6 +45,7 @@ var RenderContext = /*#__PURE__*/function () {
|
|
29
45
|
function RenderContext(schema, options) {
|
30
46
|
(0, _classCallCheck2["default"])(this, RenderContext);
|
31
47
|
(0, _defineProperty2["default"])(this, "schema", void 0);
|
48
|
+
(0, _defineProperty2["default"])(this, "utils", []);
|
32
49
|
this.schema = schema;
|
33
50
|
this.options = (0, _deepmerge["default"])(defaultOptions, options !== null && options !== void 0 ? options : {});
|
34
51
|
}
|
@@ -41,6 +58,16 @@ var RenderContext = /*#__PURE__*/function () {
|
|
41
58
|
var refName = $ref.replace('#/definitions/', '');
|
42
59
|
return (_this$schema$definiti = this.schema.definitions) === null || _this$schema$definiti === void 0 ? void 0 : _this$schema$definiti[refName];
|
43
60
|
}
|
61
|
+
}, {
|
62
|
+
key: "addUtil",
|
63
|
+
value: function addUtil(util) {
|
64
|
+
this.utils[util] = true;
|
65
|
+
}
|
66
|
+
}, {
|
67
|
+
key: "getImports",
|
68
|
+
value: function getImports() {
|
69
|
+
return (0, _imports.getImportStatements)((0, _imports.convertUtilsToImportList)(this, Object.keys(this.utils)));
|
70
|
+
}
|
44
71
|
}]);
|
45
72
|
return RenderContext;
|
46
73
|
}();
|
@@ -0,0 +1,138 @@
|
|
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.getImportStatements = exports.convertUtilsToImportList = exports.UTILS = void 0;
|
11
|
+
|
12
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
13
|
+
|
14
|
+
var t = _interopRequireWildcard(require("@babel/types"));
|
15
|
+
|
16
|
+
var _utils = require("../utils");
|
17
|
+
|
18
|
+
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); }
|
19
|
+
|
20
|
+
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; }
|
21
|
+
|
22
|
+
var makeReactQuerySwitch = function makeReactQuerySwitch(varName) {
|
23
|
+
return function (context) {
|
24
|
+
switch (context.options.reactQuery.version) {
|
25
|
+
case 'v4':
|
26
|
+
return {
|
27
|
+
type: 'import',
|
28
|
+
path: '@tanstack/react-query',
|
29
|
+
name: varName
|
30
|
+
};
|
31
|
+
|
32
|
+
case 'v3':
|
33
|
+
default:
|
34
|
+
return {
|
35
|
+
type: 'import',
|
36
|
+
path: 'react-query',
|
37
|
+
name: varName
|
38
|
+
};
|
39
|
+
}
|
40
|
+
};
|
41
|
+
};
|
42
|
+
|
43
|
+
var UTILS = {
|
44
|
+
MsgExecuteContract: 'cosmjs-types/cosmwasm/wasm/v1/tx',
|
45
|
+
MsgExecuteContractEncodeObject: 'cosmwasm',
|
46
|
+
Coin: '@cosmjs/amino',
|
47
|
+
toUtf8: '@cosmjs/encoding',
|
48
|
+
selectorFamily: 'recoil',
|
49
|
+
StdFee: '@cosmjs/amino',
|
50
|
+
CosmWasmClient: '@cosmjs/cosmwasm-stargate',
|
51
|
+
ExecuteResult: '@cosmjs/cosmwasm-stargate',
|
52
|
+
SigningCosmWasmClient: '@cosmjs/cosmwasm-stargate',
|
53
|
+
// react-query
|
54
|
+
useQuery: makeReactQuerySwitch('useQuery'),
|
55
|
+
UseQueryOptions: makeReactQuerySwitch('UseQueryOptions'),
|
56
|
+
useMutation: makeReactQuerySwitch('useMutation'),
|
57
|
+
UseMutationOptions: makeReactQuerySwitch('UseMutationOptions')
|
58
|
+
};
|
59
|
+
exports.UTILS = UTILS;
|
60
|
+
|
61
|
+
var convertUtilsToImportList = function convertUtilsToImportList(context, utils) {
|
62
|
+
return utils.map(function (util) {
|
63
|
+
if (!UTILS.hasOwnProperty(util)) throw new Error("missing Util! ::[".concat(util, "]"));
|
64
|
+
|
65
|
+
if (typeof UTILS[util] === 'string') {
|
66
|
+
return {
|
67
|
+
type: 'import',
|
68
|
+
path: UTILS[util],
|
69
|
+
name: util
|
70
|
+
};
|
71
|
+
} else if (typeof UTILS[util] === 'function') {
|
72
|
+
return UTILS[util](context);
|
73
|
+
} else {
|
74
|
+
UTILS[util];
|
75
|
+
}
|
76
|
+
});
|
77
|
+
};
|
78
|
+
|
79
|
+
exports.convertUtilsToImportList = convertUtilsToImportList;
|
80
|
+
|
81
|
+
var getImportStatements = function getImportStatements(list) {
|
82
|
+
var imports = list.reduce(function (m, obj) {
|
83
|
+
m[obj.path] = m[obj.path] || [];
|
84
|
+
var exists = m[obj.path].find(function (el) {
|
85
|
+
return el.type === obj.type && el.path === obj.path && el.name === obj.name;
|
86
|
+
}); // TODO some have google.protobuf.Any shows up... figure out the better way to handle this
|
87
|
+
|
88
|
+
if (/\./.test(obj.name)) {
|
89
|
+
obj.name = obj.name.split('.')[obj.name.split('.').length - 1];
|
90
|
+
}
|
91
|
+
|
92
|
+
if (!exists) m[obj.path].push(obj);
|
93
|
+
return m;
|
94
|
+
}, {});
|
95
|
+
return Object.entries(imports).reduce(function (m, _ref) {
|
96
|
+
var _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
|
97
|
+
importPath = _ref2[0],
|
98
|
+
imports = _ref2[1];
|
99
|
+
|
100
|
+
var defaultImports = imports.filter(function (a) {
|
101
|
+
return a.type === 'default';
|
102
|
+
});
|
103
|
+
|
104
|
+
if (defaultImports.length) {
|
105
|
+
if (defaultImports.length > 1) throw new Error('more than one default name NOT allowed.');
|
106
|
+
m.push(t.importDeclaration([t.importDefaultSpecifier(t.identifier(defaultImports[0].name))], t.stringLiteral(defaultImports[0].path)));
|
107
|
+
}
|
108
|
+
|
109
|
+
var namedImports = imports.filter(function (a) {
|
110
|
+
return a.type === 'import' && (!a.importAs || a.name === a.importAs);
|
111
|
+
});
|
112
|
+
|
113
|
+
if (namedImports.length) {
|
114
|
+
m.push((0, _utils.importStmt)(namedImports.map(function (i) {
|
115
|
+
return i.name;
|
116
|
+
}), namedImports[0].path));
|
117
|
+
}
|
118
|
+
|
119
|
+
var aliasNamedImports = imports.filter(function (a) {
|
120
|
+
return a.type === 'import' && a.importAs && a.name !== a.importAs;
|
121
|
+
});
|
122
|
+
aliasNamedImports.forEach(function (imp) {
|
123
|
+
m.push((0, _utils.importAs)(imp.name, imp.importAs, imp.path));
|
124
|
+
});
|
125
|
+
var namespaced = imports.filter(function (a) {
|
126
|
+
return a.type === 'namespace';
|
127
|
+
});
|
128
|
+
|
129
|
+
if (namespaced.length) {
|
130
|
+
if (namespaced.length > 1) throw new Error('more than one namespaced name NOT allowed.');
|
131
|
+
m.push(t.importDeclaration([t.importNamespaceSpecifier(t.identifier(namespaced[0].name))], t.stringLiteral(namespaced[0].path)));
|
132
|
+
}
|
133
|
+
|
134
|
+
return m;
|
135
|
+
}, []);
|
136
|
+
};
|
137
|
+
|
138
|
+
exports.getImportStatements = getImportStatements;
|
package/main/context/index.js
CHANGED
@@ -15,4 +15,17 @@ Object.keys(_context).forEach(function (key) {
|
|
15
15
|
return _context[key];
|
16
16
|
}
|
17
17
|
});
|
18
|
+
});
|
19
|
+
|
20
|
+
var _imports = require("./imports");
|
21
|
+
|
22
|
+
Object.keys(_imports).forEach(function (key) {
|
23
|
+
if (key === "default" || key === "__esModule") return;
|
24
|
+
if (key in exports && exports[key] === _imports[key]) return;
|
25
|
+
Object.defineProperty(exports, key, {
|
26
|
+
enumerable: true,
|
27
|
+
get: function get() {
|
28
|
+
return _imports[key];
|
29
|
+
}
|
30
|
+
});
|
18
31
|
});
|
package/main/index.js
CHANGED
@@ -56,15 +56,15 @@ Object.keys(_recoil).forEach(function (key) {
|
|
56
56
|
});
|
57
57
|
});
|
58
58
|
|
59
|
-
var
|
59
|
+
var _messageComposer = require("./message-composer");
|
60
60
|
|
61
|
-
Object.keys(
|
61
|
+
Object.keys(_messageComposer).forEach(function (key) {
|
62
62
|
if (key === "default" || key === "__esModule") return;
|
63
|
-
if (key in exports && exports[key] ===
|
63
|
+
if (key in exports && exports[key] === _messageComposer[key]) return;
|
64
64
|
Object.defineProperty(exports, key, {
|
65
65
|
enumerable: true,
|
66
66
|
get: function get() {
|
67
|
-
return
|
67
|
+
return _messageComposer[key];
|
68
68
|
}
|
69
69
|
});
|
70
70
|
});
|
@@ -4,15 +4,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
|
7
|
-
var
|
7
|
+
var _messageComposer = require("./message-composer");
|
8
8
|
|
9
|
-
Object.keys(
|
9
|
+
Object.keys(_messageComposer).forEach(function (key) {
|
10
10
|
if (key === "default" || key === "__esModule") return;
|
11
|
-
if (key in exports && exports[key] ===
|
11
|
+
if (key in exports && exports[key] === _messageComposer[key]) return;
|
12
12
|
Object.defineProperty(exports, key, {
|
13
13
|
enumerable: true,
|
14
14
|
get: function get() {
|
15
|
-
return
|
15
|
+
return _messageComposer[key];
|
16
16
|
}
|
17
17
|
});
|
18
18
|
});
|
@@ -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.
|
10
|
+
exports.createMessageComposerInterface = exports.createMessageComposerClass = void 0;
|
11
11
|
|
12
12
|
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
13
13
|
|
@@ -27,7 +27,11 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
|
|
27
27
|
|
28
28
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
29
29
|
|
30
|
-
var
|
30
|
+
var createWasmExecMethodMessageComposer = function createWasmExecMethodMessageComposer(context, jsonschema) {
|
31
|
+
context.addUtil('Coin');
|
32
|
+
context.addUtil('MsgExecuteContractEncodeObject');
|
33
|
+
context.addUtil('MsgExecuteContract');
|
34
|
+
context.addUtil('toUtf8');
|
31
35
|
var underscoreName = Object.keys(jsonschema.properties)[0];
|
32
36
|
var methodName = (0, _case.camel)(underscoreName);
|
33
37
|
var obj = (0, _types2.createTypedObjectParams)(context, jsonschema.properties[underscoreName]);
|
@@ -38,7 +42,7 @@ var createWasmExecMethodPartial = function createWasmExecMethodPartial(context,
|
|
38
42
|
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('MsgExecuteContractEncodeObject'))), false));
|
39
43
|
};
|
40
44
|
|
41
|
-
var
|
45
|
+
var createMessageComposerClass = function createMessageComposerClass(context, className, implementsClassName, execMsg) {
|
42
46
|
var propertyNames = (0, _utils.getMessageProperties)(execMsg).map(function (method) {
|
43
47
|
var _Object$keys;
|
44
48
|
|
@@ -46,7 +50,7 @@ var createFromPartialClass = function createFromPartialClass(context, className,
|
|
46
50
|
}).filter(Boolean);
|
47
51
|
var bindings = propertyNames.map(_case.camel).map(_utils.bindMethod);
|
48
52
|
var methods = (0, _utils.getMessageProperties)(execMsg).map(function (schema) {
|
49
|
-
return
|
53
|
+
return createWasmExecMethodMessageComposer(context, schema);
|
50
54
|
});
|
51
55
|
var blockStmt = [];
|
52
56
|
[].push.apply(blockStmt, [t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('sender')), t.identifier('sender'))), t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.identifier('contractAddress')))].concat((0, _toConsumableArray2["default"])(bindings)));
|
@@ -56,13 +60,13 @@ var createFromPartialClass = function createFromPartialClass(context, className,
|
|
56
60
|
t.classMethod('constructor', t.identifier('constructor'), [(0, _utils.typedIdentifier)('sender', t.tsTypeAnnotation(t.tsStringKeyword())), (0, _utils.typedIdentifier)('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword()))], t.blockStatement(blockStmt))].concat((0, _toConsumableArray2["default"])(methods)), [t.tSExpressionWithTypeArguments(t.identifier(implementsClassName))], null));
|
57
61
|
};
|
58
62
|
|
59
|
-
exports.
|
63
|
+
exports.createMessageComposerClass = createMessageComposerClass;
|
60
64
|
|
61
|
-
var
|
65
|
+
var createMessageComposerInterface = function createMessageComposerInterface(context, className, execMsg) {
|
62
66
|
var methods = (0, _utils.getMessageProperties)(execMsg).map(function (jsonschema) {
|
63
67
|
var underscoreName = Object.keys(jsonschema.properties)[0];
|
64
68
|
var methodName = (0, _case.camel)(underscoreName);
|
65
|
-
return
|
69
|
+
return createPropertyFunctionWithObjectParamsForMessageComposer(context, methodName, 'MsgExecuteContractEncodeObject', jsonschema.properties[underscoreName]);
|
66
70
|
});
|
67
71
|
var extendsAst = [];
|
68
72
|
return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(className), null, extendsAst, t.tSInterfaceBody([// contract address
|
@@ -70,9 +74,9 @@ var createFromPartialInterface = function createFromPartialInterface(context, cl
|
|
70
74
|
t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword()))].concat((0, _toConsumableArray2["default"])(methods)))));
|
71
75
|
};
|
72
76
|
|
73
|
-
exports.
|
77
|
+
exports.createMessageComposerInterface = createMessageComposerInterface;
|
74
78
|
|
75
|
-
var
|
79
|
+
var createPropertyFunctionWithObjectParamsForMessageComposer = function createPropertyFunctionWithObjectParamsForMessageComposer(context, methodName, responseType, jsonschema) {
|
76
80
|
var obj = (0, _types2.createTypedObjectParams)(context, jsonschema);
|
77
81
|
var fixedParams = [(0, _babel.identifier)('funds', t.tsTypeAnnotation(t.tsArrayType(t.tsTypeReference(t.identifier('Coin')))), true)];
|
78
82
|
var func = {
|
package/main/{from-partial/from-partial.spec.js → message-composer/message-composer.spec.js}
RENAMED
@@ -4,7 +4,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
4
4
|
|
5
5
|
var _execute_msg_for__empty = _interopRequireDefault(require("../../../../__fixtures__/basic/execute_msg_for__empty.json"));
|
6
6
|
|
7
|
-
var
|
7
|
+
var _messageComposer = require("./message-composer");
|
8
8
|
|
9
9
|
var _context = require("../context");
|
10
10
|
|
@@ -12,9 +12,9 @@ var _testUtils = require("../../test-utils");
|
|
12
12
|
|
13
13
|
it('execute classes', function () {
|
14
14
|
var ctx = new _context.RenderContext(_execute_msg_for__empty["default"]);
|
15
|
-
(0, _testUtils.expectCode)((0,
|
15
|
+
(0, _testUtils.expectCode)((0, _messageComposer.createMessageComposerClass)(ctx, 'SG721MessageComposer', 'SG721Message', _execute_msg_for__empty["default"]));
|
16
16
|
});
|
17
|
-
it('
|
17
|
+
it('createMessageComposerInterface', function () {
|
18
18
|
var ctx = new _context.RenderContext(_execute_msg_for__empty["default"]);
|
19
|
-
(0, _testUtils.expectCode)((0,
|
19
|
+
(0, _testUtils.expectCode)((0, _messageComposer.createMessageComposerInterface)(ctx, 'SG721Message', _execute_msg_for__empty["default"]));
|
20
20
|
});
|
@@ -72,6 +72,8 @@ var createReactQueryHook = function createReactQueryHook(_ref2) {
|
|
72
72
|
hookKeyName = _ref2.hookKeyName,
|
73
73
|
methodName = _ref2.methodName,
|
74
74
|
jsonschema = _ref2.jsonschema;
|
75
|
+
context.addUtil('useQuery');
|
76
|
+
context.addUtil('UseQueryOptions');
|
75
77
|
var options = context.options.reactQuery;
|
76
78
|
var keys = Object.keys((_jsonschema$propertie = jsonschema.properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {});
|
77
79
|
var args = [];
|
@@ -159,7 +161,7 @@ var createReactQueryMutationHooks = function createReactQueryMutationHooks(_ref4
|
|
159
161
|
|
160
162
|
var hasMsg = !!((_Object$keys = Object.keys(properties)) !== null && _Object$keys !== void 0 && _Object$keys.length || jsonschema !== null && jsonschema !== void 0 && jsonschema.$ref); // <ExecuteResult, Error, Cw4UpdateMembersMutation>
|
161
163
|
|
162
|
-
var useMutationTypeParameter = generateMutationTypeParameter(
|
164
|
+
var useMutationTypeParameter = generateMutationTypeParameter(context, mutationHookParamsTypeName);
|
163
165
|
return [createReactQueryMutationArgsInterface({
|
164
166
|
context: context,
|
165
167
|
mutationHookParamsTypeName: mutationHookParamsTypeName,
|
@@ -183,12 +185,13 @@ var createReactQueryMutationHooks = function createReactQueryMutationHooks(_ref4
|
|
183
185
|
|
184
186
|
exports.createReactQueryMutationHooks = createReactQueryMutationHooks;
|
185
187
|
|
186
|
-
function generateMutationTypeParameter(
|
188
|
+
var generateMutationTypeParameter = function generateMutationTypeParameter(context, mutationHookParamsTypeName) {
|
189
|
+
context.addUtil('ExecuteResult');
|
187
190
|
return t.tsTypeParameterInstantiation([// Data
|
188
191
|
t.tSTypeReference(t.identifier('ExecuteResult')), // Error
|
189
192
|
t.tsTypeReference(t.identifier('Error')), // Variables
|
190
193
|
t.tsTypeReference(t.identifier(mutationHookParamsTypeName))]);
|
191
|
-
}
|
194
|
+
};
|
192
195
|
|
193
196
|
/**
|
194
197
|
*
|
@@ -208,6 +211,8 @@ var createReactQueryMutationHook = function createReactQueryMutationHook(_ref5)
|
|
208
211
|
execMethodName = _ref5.execMethodName,
|
209
212
|
useMutationTypeParameter = _ref5.useMutationTypeParameter,
|
210
213
|
hasMsg = _ref5.hasMsg;
|
214
|
+
context.addUtil('useMutation');
|
215
|
+
context.addUtil('UseMutationOptions');
|
211
216
|
var useMutationFunctionArgs = [(0, _babel.shorthandProperty)('client')];
|
212
217
|
if (hasMsg) useMutationFunctionArgs.push((0, _babel.shorthandProperty)('msg'));
|
213
218
|
useMutationFunctionArgs.push(t.objectProperty(t.identifier('args'), t.assignmentPattern(t.objectPattern(_client.FIXED_EXECUTE_PARAMS.map(function (param) {
|
@@ -229,8 +234,9 @@ var createReactQueryHookInterface = function createReactQueryHookInterface(_ref6
|
|
229
234
|
jsonschema = _ref6.jsonschema;
|
230
235
|
// merge the user options with the defaults
|
231
236
|
var options = context.options.reactQuery;
|
237
|
+
context.addUtil('UseQueryOptions');
|
232
238
|
var typedUseQueryOptions = t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([(0, _babel.typeRefOrOptionalUnion)(t.identifier(responseType), options.optionalClient), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(responseType)), t.tsArrayType(t.tsParenthesizedType(t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])))]));
|
233
|
-
var body = [(0, _utils.tsPropertySignature)(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(QueryClient))), options.optionalClient), (0, _utils.tsPropertySignature)(t.identifier('options'), t.tsTypeAnnotation(options.v4 ? t.tSIntersectionType([(0, _babel.omitTypeReference)(typedUseQueryOptions, "'queryKey' | 'queryFn' | 'initialData'"), t.tSTypeLiteral([t.tsPropertySignature(t.identifier('initialData?'), t.tsTypeAnnotation(t.tsUndefinedKeyword()))])]) : typedUseQueryOptions), true)];
|
239
|
+
var body = [(0, _utils.tsPropertySignature)(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(QueryClient))), options.optionalClient), (0, _utils.tsPropertySignature)(t.identifier('options'), t.tsTypeAnnotation(options.version === 'v4' ? t.tSIntersectionType([(0, _babel.omitTypeReference)(typedUseQueryOptions, "'queryKey' | 'queryFn' | 'initialData'"), t.tSTypeLiteral([t.tsPropertySignature(t.identifier('initialData?'), t.tsTypeAnnotation(t.tsUndefinedKeyword()))])]) : typedUseQueryOptions), true)];
|
234
240
|
var props = getProps(context, jsonschema);
|
235
241
|
|
236
242
|
if (props.length) {
|
@@ -42,7 +42,7 @@ it('createReactQueryHooks', function () {
|
|
42
42
|
(0, _testUtils.expectCode)(t.program((0, _reactQuery.createReactQueryHooks)({
|
43
43
|
context: new _context.RenderContext(_query_msg["default"], {
|
44
44
|
reactQuery: {
|
45
|
-
|
45
|
+
version: 'v4'
|
46
46
|
}
|
47
47
|
}),
|
48
48
|
queryMsg: _query_msg["default"],
|
@@ -53,7 +53,7 @@ it('createReactQueryHooks', function () {
|
|
53
53
|
context: new _context.RenderContext(_query_msg["default"], {
|
54
54
|
reactQuery: {
|
55
55
|
optionalClient: true,
|
56
|
-
|
56
|
+
version: 'v4'
|
57
57
|
}
|
58
58
|
}),
|
59
59
|
queryMsg: _query_msg["default"],
|
package/main/recoil/recoil.js
CHANGED
@@ -18,6 +18,7 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
|
|
18
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
19
|
|
20
20
|
var createRecoilSelector = function createRecoilSelector(context, keyPrefix, QueryClient, methodName) {
|
21
|
+
context.addUtil('selectorFamily');
|
21
22
|
var selectorName = (0, _case.camel)("".concat(methodName, "Selector"));
|
22
23
|
var responseType = (0, _case.pascal)("".concat(methodName, "Response"));
|
23
24
|
var getterKey = (0, _case.camel)("".concat(keyPrefix).concat((0, _case.pascal)(methodName)));
|
@@ -66,6 +67,7 @@ var createRecoilQueryClientType = function createRecoilQueryClientType() {
|
|
66
67
|
exports.createRecoilQueryClientType = createRecoilQueryClientType;
|
67
68
|
|
68
69
|
var createRecoilQueryClient = function createRecoilQueryClient(context, keyPrefix, QueryClient) {
|
70
|
+
context.addUtil('selectorFamily');
|
69
71
|
var getterKey = (0, _case.camel)("".concat(keyPrefix, 'QueryClient'));
|
70
72
|
return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier('queryClient'), (0, _utils.callExpression)(t.identifier('selectorFamily'), [t.objectExpression([t.objectProperty(t.identifier('key'), t.stringLiteral(getterKey)), t.objectProperty(t.identifier('get'), t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier('contractAddress'), t.identifier('contractAddress'), false, true)])], t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier('get'), t.identifier('get'), false, true)])], t.blockStatement([t.variableDeclaration('const', [t.variableDeclarator(t.identifier('client'), t.callExpression(t.identifier('get'), [t.identifier('cosmWasmClient')]))]), t.returnStatement(t.newExpression(t.identifier(QueryClient), [t.identifier('client'), t.identifier('contractAddress')]))]), false)))])], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(QueryClient)), t.tsTypeReference(t.identifier('QueryClientParams'))])))]));
|
71
73
|
};
|
package/main/utils/babel.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.typeRefOrOptionalUnion = exports.tsTypeOperator = exports.tsPropertySignature = exports.tsObjectPattern = exports.shorthandProperty = exports.recursiveNamespace = exports.propertySignature = exports.promiseTypeAnnotation = exports.pickTypeReference = exports.parameterizedTypeReference = exports.optionalConditionalExpression = exports.omitTypeReference = exports.memberExpressionOrIdentifierSnake = exports.memberExpressionOrIdentifier = exports.importStmt = exports.importAminoMsg = exports.identifier = exports.getMessageProperties = exports.getFieldDimensionality = exports.classProperty = exports.classDeclaration = exports.callExpression = exports.bindMethod = exports.arrowFunctionExpression = exports.arrayTypeNDimensions = exports.FieldTypeAsts = void 0;
|
10
|
+
exports.typedIdentifier = exports.typeRefOrOptionalUnion = exports.tsTypeOperator = exports.tsPropertySignature = exports.tsObjectPattern = exports.shorthandProperty = exports.recursiveNamespace = exports.propertySignature = exports.promiseTypeAnnotation = exports.pickTypeReference = exports.parameterizedTypeReference = exports.optionalConditionalExpression = exports.omitTypeReference = exports.memberExpressionOrIdentifierSnake = exports.memberExpressionOrIdentifier = exports.importStmt = exports.importAs = exports.importAminoMsg = exports.identifier = exports.getMessageProperties = exports.getFieldDimensionality = exports.classProperty = exports.classDeclaration = exports.callExpression = exports.bindMethod = exports.arrowFunctionExpression = exports.arrayTypeNDimensions = exports.FieldTypeAsts = void 0;
|
11
11
|
|
12
12
|
var _toArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toArray"));
|
13
13
|
|
@@ -194,6 +194,12 @@ var importStmt = function importStmt(names, path) {
|
|
194
194
|
|
195
195
|
exports.importStmt = importStmt;
|
196
196
|
|
197
|
+
var importAs = function importAs(name, _importAs, importPath) {
|
198
|
+
return t.importDeclaration([t.importSpecifier(t.identifier(_importAs), t.identifier(name))], t.stringLiteral(importPath));
|
199
|
+
};
|
200
|
+
|
201
|
+
exports.importAs = importAs;
|
202
|
+
|
197
203
|
var importAminoMsg = function importAminoMsg() {
|
198
204
|
return importStmt(['AminoMsg'], '@cosmjs/amino');
|
199
205
|
};
|
package/main/utils/types.js
CHANGED
@@ -176,7 +176,7 @@ function getPropertySignatureFromProp(context, jsonschema, prop, camelize) {
|
|
176
176
|
if (jsonschema.properties[prop].title) {
|
177
177
|
return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(jsonschema.properties[prop].title))));
|
178
178
|
} else {
|
179
|
-
throw new Error('
|
179
|
+
throw new Error('getPropertySignatureFromProp() contact maintainer');
|
180
180
|
}
|
181
181
|
}
|
182
182
|
|
package/module/client/client.js
CHANGED
@@ -15,6 +15,7 @@ export const createWasmQueryMethod = (context, jsonschema) => {
|
|
15
15
|
return t.classProperty(t.identifier(methodName), arrowFunctionExpression(obj ? [obj] : [], t.blockStatement([t.returnStatement(t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('queryContractSmart')), [t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.objectExpression([actionArg])]))]), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier(responseType))]))), true));
|
16
16
|
};
|
17
17
|
export const createQueryClass = (context, className, implementsClassName, queryMsg) => {
|
18
|
+
context.addUtil('CosmWasmClient');
|
18
19
|
const propertyNames = getMessageProperties(queryMsg).map(method => Object.keys(method.properties)?.[0]).filter(Boolean);
|
19
20
|
const bindings = propertyNames.map(camel).map(bindMethod);
|
20
21
|
const methods = getMessageProperties(queryMsg).map(schema => {
|
@@ -43,6 +44,9 @@ export const getWasmMethodArgs = (context, jsonschema) => {
|
|
43
44
|
return args;
|
44
45
|
};
|
45
46
|
export const createWasmExecMethod = (context, jsonschema) => {
|
47
|
+
context.addUtil('ExecuteResult');
|
48
|
+
context.addUtil('StdFee');
|
49
|
+
context.addUtil('Coin');
|
46
50
|
const underscoreName = Object.keys(jsonschema.properties)[0];
|
47
51
|
const methodName = camel(underscoreName);
|
48
52
|
const obj = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
|
@@ -52,6 +56,7 @@ export const createWasmExecMethod = (context, jsonschema) => {
|
|
52
56
|
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true));
|
53
57
|
};
|
54
58
|
export const createExecuteClass = (context, className, implementsClassName, extendsClassName, execMsg) => {
|
59
|
+
context.addUtil('SigningCosmWasmClient');
|
55
60
|
const propertyNames = getMessageProperties(execMsg).map(method => Object.keys(method.properties)?.[0]).filter(Boolean);
|
56
61
|
const bindings = propertyNames.map(camel).map(bindMethod);
|
57
62
|
const methods = getMessageProperties(execMsg).map(schema => {
|
@@ -93,6 +98,7 @@ export const createPropertyFunctionWithObjectParams = (context, methodName, resp
|
|
93
98
|
return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(func));
|
94
99
|
};
|
95
100
|
export const createPropertyFunctionWithObjectParamsForExec = (context, methodName, responseType, jsonschema) => {
|
101
|
+
context.addUtil('Coin');
|
96
102
|
const obj = createTypedObjectParams(context, jsonschema);
|
97
103
|
const func = {
|
98
104
|
type: 'TSFunctionType',
|
@@ -1,10 +1,25 @@
|
|
1
1
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
2
|
+
import { convertUtilsToImportList, getImportStatements } from "./imports";
|
2
3
|
import deepmerge from "deepmerge"; /// Plugin Types
|
3
4
|
|
4
5
|
export const defaultOptions = {
|
6
|
+
types: {
|
7
|
+
enabled: true,
|
8
|
+
aliasExecuteMsg: false
|
9
|
+
},
|
10
|
+
client: {
|
11
|
+
enabled: true
|
12
|
+
},
|
13
|
+
recoil: {
|
14
|
+
enabled: false
|
15
|
+
},
|
16
|
+
messageComposer: {
|
17
|
+
enabled: false
|
18
|
+
},
|
5
19
|
reactQuery: {
|
20
|
+
enabled: false,
|
6
21
|
optionalClient: false,
|
7
|
-
|
22
|
+
version: 'v3',
|
8
23
|
mutations: false,
|
9
24
|
camelize: true
|
10
25
|
}
|
@@ -13,6 +28,8 @@ export class RenderContext {
|
|
13
28
|
constructor(schema, options) {
|
14
29
|
_defineProperty(this, "schema", void 0);
|
15
30
|
|
31
|
+
_defineProperty(this, "utils", []);
|
32
|
+
|
16
33
|
this.schema = schema;
|
17
34
|
this.options = deepmerge(defaultOptions, options ?? {});
|
18
35
|
}
|
@@ -22,4 +39,12 @@ export class RenderContext {
|
|
22
39
|
return this.schema.definitions?.[refName];
|
23
40
|
}
|
24
41
|
|
42
|
+
addUtil(util) {
|
43
|
+
this.utils[util] = true;
|
44
|
+
}
|
45
|
+
|
46
|
+
getImports() {
|
47
|
+
return getImportStatements(convertUtilsToImportList(this, Object.keys(this.utils)));
|
48
|
+
}
|
49
|
+
|
25
50
|
}
|
@@ -0,0 +1,97 @@
|
|
1
|
+
import * as t from '@babel/types';
|
2
|
+
import { importAs, importStmt } from "../utils";
|
3
|
+
|
4
|
+
const makeReactQuerySwitch = varName => {
|
5
|
+
return context => {
|
6
|
+
switch (context.options.reactQuery.version) {
|
7
|
+
case 'v4':
|
8
|
+
return {
|
9
|
+
type: 'import',
|
10
|
+
path: '@tanstack/react-query',
|
11
|
+
name: varName
|
12
|
+
};
|
13
|
+
|
14
|
+
case 'v3':
|
15
|
+
default:
|
16
|
+
return {
|
17
|
+
type: 'import',
|
18
|
+
path: 'react-query',
|
19
|
+
name: varName
|
20
|
+
};
|
21
|
+
}
|
22
|
+
};
|
23
|
+
};
|
24
|
+
|
25
|
+
export const UTILS = {
|
26
|
+
MsgExecuteContract: 'cosmjs-types/cosmwasm/wasm/v1/tx',
|
27
|
+
MsgExecuteContractEncodeObject: 'cosmwasm',
|
28
|
+
Coin: '@cosmjs/amino',
|
29
|
+
toUtf8: '@cosmjs/encoding',
|
30
|
+
selectorFamily: 'recoil',
|
31
|
+
StdFee: '@cosmjs/amino',
|
32
|
+
CosmWasmClient: '@cosmjs/cosmwasm-stargate',
|
33
|
+
ExecuteResult: '@cosmjs/cosmwasm-stargate',
|
34
|
+
SigningCosmWasmClient: '@cosmjs/cosmwasm-stargate',
|
35
|
+
// react-query
|
36
|
+
useQuery: makeReactQuerySwitch('useQuery'),
|
37
|
+
UseQueryOptions: makeReactQuerySwitch('UseQueryOptions'),
|
38
|
+
useMutation: makeReactQuerySwitch('useMutation'),
|
39
|
+
UseMutationOptions: makeReactQuerySwitch('UseMutationOptions')
|
40
|
+
};
|
41
|
+
export const convertUtilsToImportList = (context, utils) => {
|
42
|
+
return utils.map(util => {
|
43
|
+
if (!UTILS.hasOwnProperty(util)) throw new Error(`missing Util! ::[${util}]`);
|
44
|
+
|
45
|
+
if (typeof UTILS[util] === 'string') {
|
46
|
+
return {
|
47
|
+
type: 'import',
|
48
|
+
path: UTILS[util],
|
49
|
+
name: util
|
50
|
+
};
|
51
|
+
} else if (typeof UTILS[util] === 'function') {
|
52
|
+
return UTILS[util](context);
|
53
|
+
} else {
|
54
|
+
UTILS[util];
|
55
|
+
}
|
56
|
+
});
|
57
|
+
};
|
58
|
+
export const getImportStatements = list => {
|
59
|
+
const imports = list.reduce((m, obj) => {
|
60
|
+
m[obj.path] = m[obj.path] || [];
|
61
|
+
const exists = m[obj.path].find(el => el.type === obj.type && el.path === obj.path && el.name === obj.name); // TODO some have google.protobuf.Any shows up... figure out the better way to handle this
|
62
|
+
|
63
|
+
if (/\./.test(obj.name)) {
|
64
|
+
obj.name = obj.name.split('.')[obj.name.split('.').length - 1];
|
65
|
+
}
|
66
|
+
|
67
|
+
if (!exists) m[obj.path].push(obj);
|
68
|
+
return m;
|
69
|
+
}, {});
|
70
|
+
return Object.entries(imports).reduce((m, [importPath, imports]) => {
|
71
|
+
const defaultImports = imports.filter(a => a.type === 'default');
|
72
|
+
|
73
|
+
if (defaultImports.length) {
|
74
|
+
if (defaultImports.length > 1) throw new Error('more than one default name NOT allowed.');
|
75
|
+
m.push(t.importDeclaration([t.importDefaultSpecifier(t.identifier(defaultImports[0].name))], t.stringLiteral(defaultImports[0].path)));
|
76
|
+
}
|
77
|
+
|
78
|
+
const namedImports = imports.filter(a => a.type === 'import' && (!a.importAs || a.name === a.importAs));
|
79
|
+
|
80
|
+
if (namedImports.length) {
|
81
|
+
m.push(importStmt(namedImports.map(i => i.name), namedImports[0].path));
|
82
|
+
}
|
83
|
+
|
84
|
+
const aliasNamedImports = imports.filter(a => a.type === 'import' && a.importAs && a.name !== a.importAs);
|
85
|
+
aliasNamedImports.forEach(imp => {
|
86
|
+
m.push(importAs(imp.name, imp.importAs, imp.path));
|
87
|
+
});
|
88
|
+
const namespaced = imports.filter(a => a.type === 'namespace');
|
89
|
+
|
90
|
+
if (namespaced.length) {
|
91
|
+
if (namespaced.length > 1) throw new Error('more than one namespaced name NOT allowed.');
|
92
|
+
m.push(t.importDeclaration([t.importNamespaceSpecifier(t.identifier(namespaced[0].name))], t.stringLiteral(namespaced[0].path)));
|
93
|
+
}
|
94
|
+
|
95
|
+
return m;
|
96
|
+
}, []);
|
97
|
+
};
|
package/module/context/index.js
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
export * from './context';
|
1
|
+
export * from './context';
|
2
|
+
export * from './imports';
|
package/module/index.js
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './message-composer';
|
@@ -5,7 +5,11 @@ import { createTypedObjectParams } from '../utils/types';
|
|
5
5
|
import { identifier } from '../utils/babel';
|
6
6
|
import { getWasmMethodArgs } from '../client/client';
|
7
7
|
|
8
|
-
const
|
8
|
+
const createWasmExecMethodMessageComposer = (context, jsonschema) => {
|
9
|
+
context.addUtil('Coin');
|
10
|
+
context.addUtil('MsgExecuteContractEncodeObject');
|
11
|
+
context.addUtil('MsgExecuteContract');
|
12
|
+
context.addUtil('toUtf8');
|
9
13
|
const underscoreName = Object.keys(jsonschema.properties)[0];
|
10
14
|
const methodName = camel(underscoreName);
|
11
15
|
const obj = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
|
@@ -16,11 +20,11 @@ const createWasmExecMethodPartial = (context, jsonschema) => {
|
|
16
20
|
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('MsgExecuteContractEncodeObject'))), false));
|
17
21
|
};
|
18
22
|
|
19
|
-
export const
|
23
|
+
export const createMessageComposerClass = (context, className, implementsClassName, execMsg) => {
|
20
24
|
const propertyNames = getMessageProperties(execMsg).map(method => Object.keys(method.properties)?.[0]).filter(Boolean);
|
21
25
|
const bindings = propertyNames.map(camel).map(bindMethod);
|
22
26
|
const methods = getMessageProperties(execMsg).map(schema => {
|
23
|
-
return
|
27
|
+
return createWasmExecMethodMessageComposer(context, schema);
|
24
28
|
});
|
25
29
|
const blockStmt = [];
|
26
30
|
[].push.apply(blockStmt, [t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('sender')), t.identifier('sender'))), t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.identifier('contractAddress'))), ...bindings]);
|
@@ -29,11 +33,11 @@ export const createFromPartialClass = (context, className, implementsClassName,
|
|
29
33
|
classProperty('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword())), // constructor
|
30
34
|
t.classMethod('constructor', t.identifier('constructor'), [typedIdentifier('sender', t.tsTypeAnnotation(t.tsStringKeyword())), typedIdentifier('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword()))], t.blockStatement(blockStmt)), ...methods], [t.tSExpressionWithTypeArguments(t.identifier(implementsClassName))], null));
|
31
35
|
};
|
32
|
-
export const
|
36
|
+
export const createMessageComposerInterface = (context, className, execMsg) => {
|
33
37
|
const methods = getMessageProperties(execMsg).map(jsonschema => {
|
34
38
|
const underscoreName = Object.keys(jsonschema.properties)[0];
|
35
39
|
const methodName = camel(underscoreName);
|
36
|
-
return
|
40
|
+
return createPropertyFunctionWithObjectParamsForMessageComposer(context, methodName, 'MsgExecuteContractEncodeObject', jsonschema.properties[underscoreName]);
|
37
41
|
});
|
38
42
|
const extendsAst = [];
|
39
43
|
return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(className), null, extendsAst, t.tSInterfaceBody([// contract address
|
@@ -41,7 +45,7 @@ export const createFromPartialInterface = (context, className, execMsg) => {
|
|
41
45
|
t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods])));
|
42
46
|
};
|
43
47
|
|
44
|
-
const
|
48
|
+
const createPropertyFunctionWithObjectParamsForMessageComposer = (context, methodName, responseType, jsonschema) => {
|
45
49
|
const obj = createTypedObjectParams(context, jsonschema);
|
46
50
|
const fixedParams = [identifier('funds', t.tsTypeAnnotation(t.tsArrayType(t.tsTypeReference(t.identifier('Coin')))), true)];
|
47
51
|
const func = {
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import execute_msg from '../../../../__fixtures__/basic/execute_msg_for__empty.json';
|
2
|
+
import { createMessageComposerClass, createMessageComposerInterface } from './message-composer';
|
3
|
+
import { RenderContext } from '../context';
|
4
|
+
import { expectCode } from '../../test-utils';
|
5
|
+
it('execute classes', () => {
|
6
|
+
const ctx = new RenderContext(execute_msg);
|
7
|
+
expectCode(createMessageComposerClass(ctx, 'SG721MessageComposer', 'SG721Message', execute_msg));
|
8
|
+
});
|
9
|
+
it('createMessageComposerInterface', () => {
|
10
|
+
const ctx = new RenderContext(execute_msg);
|
11
|
+
expectCode(createMessageComposerInterface(ctx, 'SG721Message', execute_msg));
|
12
|
+
});
|
@@ -46,6 +46,8 @@ export const createReactQueryHook = ({
|
|
46
46
|
methodName,
|
47
47
|
jsonschema
|
48
48
|
}) => {
|
49
|
+
context.addUtil('useQuery');
|
50
|
+
context.addUtil('UseQueryOptions');
|
49
51
|
const options = context.options.reactQuery;
|
50
52
|
const keys = Object.keys(jsonschema.properties ?? {});
|
51
53
|
let args = [];
|
@@ -126,7 +128,7 @@ export const createReactQueryMutationHooks = ({
|
|
126
128
|
|
127
129
|
const hasMsg = !!(Object.keys(properties)?.length || jsonschema?.$ref); // <ExecuteResult, Error, Cw4UpdateMembersMutation>
|
128
130
|
|
129
|
-
const useMutationTypeParameter = generateMutationTypeParameter(
|
131
|
+
const useMutationTypeParameter = generateMutationTypeParameter(context, mutationHookParamsTypeName);
|
130
132
|
return [createReactQueryMutationArgsInterface({
|
131
133
|
context,
|
132
134
|
mutationHookParamsTypeName,
|
@@ -147,12 +149,13 @@ export const createReactQueryMutationHooks = ({
|
|
147
149
|
* Generates the mutation type parameter. If args exist, we use a pick. If not, we just return the params type.
|
148
150
|
*/
|
149
151
|
|
150
|
-
|
152
|
+
const generateMutationTypeParameter = (context, mutationHookParamsTypeName) => {
|
153
|
+
context.addUtil('ExecuteResult');
|
151
154
|
return t.tsTypeParameterInstantiation([// Data
|
152
155
|
t.tSTypeReference(t.identifier('ExecuteResult')), // Error
|
153
156
|
t.tsTypeReference(t.identifier('Error')), // Variables
|
154
157
|
t.tsTypeReference(t.identifier(mutationHookParamsTypeName))]);
|
155
|
-
}
|
158
|
+
};
|
156
159
|
|
157
160
|
/**
|
158
161
|
*
|
@@ -173,6 +176,8 @@ export const createReactQueryMutationHook = ({
|
|
173
176
|
useMutationTypeParameter,
|
174
177
|
hasMsg
|
175
178
|
}) => {
|
179
|
+
context.addUtil('useMutation');
|
180
|
+
context.addUtil('UseMutationOptions');
|
176
181
|
const useMutationFunctionArgs = [shorthandProperty('client')];
|
177
182
|
if (hasMsg) useMutationFunctionArgs.push(shorthandProperty('msg'));
|
178
183
|
useMutationFunctionArgs.push(t.objectProperty(t.identifier('args'), t.assignmentPattern(t.objectPattern(FIXED_EXECUTE_PARAMS.map(param => shorthandProperty(param.name))), t.objectExpression([]))));
|
@@ -188,8 +193,9 @@ export const createReactQueryHookInterface = ({
|
|
188
193
|
}) => {
|
189
194
|
// merge the user options with the defaults
|
190
195
|
const options = context.options.reactQuery;
|
196
|
+
context.addUtil('UseQueryOptions');
|
191
197
|
const typedUseQueryOptions = t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([typeRefOrOptionalUnion(t.identifier(responseType), options.optionalClient), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(responseType)), t.tsArrayType(t.tsParenthesizedType(t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])))]));
|
192
|
-
const body = [tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(QueryClient))), options.optionalClient), tsPropertySignature(t.identifier('options'), t.tsTypeAnnotation(options.v4 ? t.tSIntersectionType([omitTypeReference(typedUseQueryOptions, "'queryKey' | 'queryFn' | 'initialData'"), t.tSTypeLiteral([t.tsPropertySignature(t.identifier('initialData?'), t.tsTypeAnnotation(t.tsUndefinedKeyword()))])]) : typedUseQueryOptions), true)];
|
198
|
+
const body = [tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(QueryClient))), options.optionalClient), tsPropertySignature(t.identifier('options'), t.tsTypeAnnotation(options.version === 'v4' ? t.tSIntersectionType([omitTypeReference(typedUseQueryOptions, "'queryKey' | 'queryFn' | 'initialData'"), t.tSTypeLiteral([t.tsPropertySignature(t.identifier('initialData?'), t.tsTypeAnnotation(t.tsUndefinedKeyword()))])]) : typedUseQueryOptions), true)];
|
193
199
|
const props = getProps(context, jsonschema);
|
194
200
|
|
195
201
|
if (props.length) {
|
@@ -26,7 +26,7 @@ it('createReactQueryHooks', () => {
|
|
26
26
|
expectCode(t.program(createReactQueryHooks({
|
27
27
|
context: new RenderContext(query_msg, {
|
28
28
|
reactQuery: {
|
29
|
-
|
29
|
+
version: 'v4'
|
30
30
|
}
|
31
31
|
}),
|
32
32
|
queryMsg: query_msg,
|
@@ -37,7 +37,7 @@ it('createReactQueryHooks', () => {
|
|
37
37
|
context: new RenderContext(query_msg, {
|
38
38
|
reactQuery: {
|
39
39
|
optionalClient: true,
|
40
|
-
|
40
|
+
version: 'v4'
|
41
41
|
}
|
42
42
|
}),
|
43
43
|
queryMsg: query_msg,
|
package/module/recoil/recoil.js
CHANGED
@@ -2,6 +2,7 @@ import * as t from '@babel/types';
|
|
2
2
|
import { camel, pascal } from 'case';
|
3
3
|
import { callExpression, getMessageProperties } from '../utils';
|
4
4
|
export const createRecoilSelector = (context, keyPrefix, QueryClient, methodName) => {
|
5
|
+
context.addUtil('selectorFamily');
|
5
6
|
const selectorName = camel(`${methodName}Selector`);
|
6
7
|
const responseType = pascal(`${methodName}Response`);
|
7
8
|
const getterKey = camel(`${keyPrefix}${pascal(methodName)}`);
|
@@ -39,6 +40,7 @@ export const createRecoilQueryClientType = () => ({
|
|
39
40
|
}
|
40
41
|
});
|
41
42
|
export const createRecoilQueryClient = (context, keyPrefix, QueryClient) => {
|
43
|
+
context.addUtil('selectorFamily');
|
42
44
|
const getterKey = camel(`${keyPrefix}${'QueryClient'}`);
|
43
45
|
return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier('queryClient'), callExpression(t.identifier('selectorFamily'), [t.objectExpression([t.objectProperty(t.identifier('key'), t.stringLiteral(getterKey)), t.objectProperty(t.identifier('get'), t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier('contractAddress'), t.identifier('contractAddress'), false, true)])], t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier('get'), t.identifier('get'), false, true)])], t.blockStatement([t.variableDeclaration('const', [t.variableDeclarator(t.identifier('client'), t.callExpression(t.identifier('get'), [t.identifier('cosmWasmClient')]))]), t.returnStatement(t.newExpression(t.identifier(QueryClient), [t.identifier('client'), t.identifier('contractAddress')]))]), false)))])], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(QueryClient)), t.tsTypeReference(t.identifier('QueryClientParams'))])))]));
|
44
46
|
};
|
package/module/utils/babel.js
CHANGED
@@ -110,6 +110,9 @@ export const shorthandProperty = prop => {
|
|
110
110
|
export const importStmt = (names, path) => {
|
111
111
|
return t.importDeclaration(names.map(name => t.importSpecifier(t.identifier(name), t.identifier(name))), t.stringLiteral(path));
|
112
112
|
};
|
113
|
+
export const importAs = (name, importAs, importPath) => {
|
114
|
+
return t.importDeclaration([t.importSpecifier(t.identifier(importAs), t.identifier(name))], t.stringLiteral(importPath));
|
115
|
+
};
|
113
116
|
export const importAminoMsg = () => {
|
114
117
|
return importStmt(['AminoMsg'], '@cosmjs/amino');
|
115
118
|
};
|
package/module/utils/types.js
CHANGED
@@ -139,7 +139,7 @@ export function getPropertySignatureFromProp(context, jsonschema, prop, camelize
|
|
139
139
|
if (jsonschema.properties[prop].title) {
|
140
140
|
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(jsonschema.properties[prop].title))));
|
141
141
|
} else {
|
142
|
-
throw new Error('
|
142
|
+
throw new Error('getPropertySignatureFromProp() contact maintainer');
|
143
143
|
}
|
144
144
|
}
|
145
145
|
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "wasm-ast-types",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.7.0",
|
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",
|
@@ -86,5 +86,5 @@
|
|
86
86
|
"case": "1.6.3",
|
87
87
|
"deepmerge": "4.2.2"
|
88
88
|
},
|
89
|
-
"gitHead": "
|
89
|
+
"gitHead": "09236055739cc020418f89a246d8a954353c7ec3"
|
90
90
|
}
|
@@ -1,12 +1,30 @@
|
|
1
1
|
import { JSONSchema } from "../types";
|
2
2
|
export interface ReactQueryOptions {
|
3
|
+
enabled?: boolean;
|
3
4
|
optionalClient?: boolean;
|
4
|
-
|
5
|
+
version?: 'v3' | 'v4';
|
5
6
|
mutations?: boolean;
|
6
7
|
camelize?: boolean;
|
7
8
|
}
|
9
|
+
export interface TSClientOptions {
|
10
|
+
enabled?: boolean;
|
11
|
+
}
|
12
|
+
export interface MessageComposerOptions {
|
13
|
+
enabled?: boolean;
|
14
|
+
}
|
15
|
+
export interface RecoilOptions {
|
16
|
+
enabled?: boolean;
|
17
|
+
}
|
18
|
+
export interface TSTypesOptions {
|
19
|
+
enabled?: boolean;
|
20
|
+
aliasExecuteMsg?: boolean;
|
21
|
+
}
|
8
22
|
export interface RenderOptions {
|
9
|
-
|
23
|
+
types?: TSTypesOptions;
|
24
|
+
recoil?: RecoilOptions;
|
25
|
+
messageComposer?: MessageComposerOptions;
|
26
|
+
client?: TSClientOptions;
|
27
|
+
reactQuery?: ReactQueryOptions;
|
10
28
|
}
|
11
29
|
export interface RenderContext {
|
12
30
|
schema: JSONSchema;
|
@@ -15,6 +33,9 @@ export interface RenderContext {
|
|
15
33
|
export declare const defaultOptions: RenderOptions;
|
16
34
|
export declare class RenderContext implements RenderContext {
|
17
35
|
schema: JSONSchema;
|
36
|
+
utils: string[];
|
18
37
|
constructor(schema: JSONSchema, options?: RenderOptions);
|
19
38
|
refLookup($ref: string): JSONSchema;
|
39
|
+
addUtil(util: string): void;
|
40
|
+
getImports(): any[];
|
20
41
|
}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
import { RenderContext } from './context';
|
2
|
+
export interface ImportObj {
|
3
|
+
type: 'import' | 'default' | 'namespace';
|
4
|
+
name: string;
|
5
|
+
path: string;
|
6
|
+
importAs?: string;
|
7
|
+
}
|
8
|
+
export declare const UTILS: {
|
9
|
+
MsgExecuteContract: string;
|
10
|
+
MsgExecuteContractEncodeObject: string;
|
11
|
+
Coin: string;
|
12
|
+
toUtf8: string;
|
13
|
+
StdFee: string;
|
14
|
+
CosmWasmClient: string;
|
15
|
+
ExecuteResult: string;
|
16
|
+
SigningCosmWasmClient: string;
|
17
|
+
useQuery: (context: RenderContext) => {
|
18
|
+
type: string;
|
19
|
+
path: string;
|
20
|
+
name: any;
|
21
|
+
};
|
22
|
+
UseQueryOptions: (context: RenderContext) => {
|
23
|
+
type: string;
|
24
|
+
path: string;
|
25
|
+
name: any;
|
26
|
+
};
|
27
|
+
useMutation: (context: RenderContext) => {
|
28
|
+
type: string;
|
29
|
+
path: string;
|
30
|
+
name: any;
|
31
|
+
};
|
32
|
+
UseMutationOptions: (context: RenderContext) => {
|
33
|
+
type: string;
|
34
|
+
path: string;
|
35
|
+
name: any;
|
36
|
+
};
|
37
|
+
};
|
38
|
+
export declare const convertUtilsToImportList: (context: RenderContext, utils: string[]) => ImportObj[];
|
39
|
+
export declare const getImportStatements: (list: ImportObj[]) => any[];
|
package/types/src/index.d.ts
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './message-composer';
|
@@ -0,0 +1,5 @@
|
|
1
|
+
import * as t from '@babel/types';
|
2
|
+
import { ExecuteMsg } from '../types';
|
3
|
+
import { RenderContext } from '../context';
|
4
|
+
export declare const createMessageComposerClass: (context: RenderContext, className: string, implementsClassName: string, execMsg: ExecuteMsg) => t.ExportNamedDeclaration;
|
5
|
+
export declare const createMessageComposerInterface: (context: RenderContext, className: string, execMsg: ExecuteMsg) => t.ExportNamedDeclaration;
|
@@ -31,6 +31,7 @@ export declare const FieldTypeAsts: {
|
|
31
31
|
};
|
32
32
|
export declare const shorthandProperty: (prop: string) => t.ObjectProperty;
|
33
33
|
export declare const importStmt: (names: string[], path: string) => t.ImportDeclaration;
|
34
|
+
export declare const importAs: (name: string, importAs: string, importPath: string) => t.ImportDeclaration;
|
34
35
|
export declare const importAminoMsg: () => t.ImportDeclaration;
|
35
36
|
export declare const getFieldDimensionality: (field: Field) => {
|
36
37
|
typeName: string;
|
@@ -1,12 +0,0 @@
|
|
1
|
-
import execute_msg from '../../../../__fixtures__/basic/execute_msg_for__empty.json';
|
2
|
-
import { createFromPartialClass, createFromPartialInterface } from './from-partial';
|
3
|
-
import { RenderContext } from '../context';
|
4
|
-
import { expectCode } from '../../test-utils';
|
5
|
-
it('execute classes', () => {
|
6
|
-
const ctx = new RenderContext(execute_msg);
|
7
|
-
expectCode(createFromPartialClass(ctx, 'SG721MessageComposer', 'SG721Message', execute_msg));
|
8
|
-
});
|
9
|
-
it('createFromPartialInterface', () => {
|
10
|
-
const ctx = new RenderContext(execute_msg);
|
11
|
-
expectCode(createFromPartialInterface(ctx, 'SG721Message', execute_msg));
|
12
|
-
});
|
@@ -1 +0,0 @@
|
|
1
|
-
export * from './from-partial';
|
@@ -1,4 +0,0 @@
|
|
1
|
-
import * as t from '@babel/types';
|
2
|
-
import { ExecuteMsg } from '../types';
|
3
|
-
export declare const createFromPartialClass: (context: RenderContext, className: string, implementsClassName: string, execMsg: ExecuteMsg) => t.ExportNamedDeclaration;
|
4
|
-
export declare const createFromPartialInterface: (context: RenderContext, className: string, execMsg: ExecuteMsg) => t.ExportNamedDeclaration;
|
@@ -1 +0,0 @@
|
|
1
|
-
export * from './from-partial';
|