wasm-ast-types 0.2.3 → 0.2.6
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/messages.js +148 -0
- package/main/messages.spec.js +24 -0
- package/main/react-query.js +5 -3
- package/main/react-query.spec.js +1 -1
- package/main/recoil.spec.js +1 -1
- package/main/{utils.js → utils/babel.js} +31 -1
- package/main/{utils.spec.js → utils/babel.spec.js} +20 -20
- package/main/utils/index.js +18 -0
- package/main/utils/types.js +143 -0
- package/main/wasm.js +12 -167
- package/main/wasm.spec.js +18 -13
- package/module/index.js +1 -0
- package/module/messages.js +112 -0
- package/module/messages.spec.js +18 -0
- package/module/react-query.js +2 -1
- package/module/react-query.spec.js +1 -1
- package/module/recoil.spec.js +1 -1
- package/module/{utils.js → utils/babel.js} +19 -0
- package/module/{utils.spec.js → utils/babel.spec.js} +2 -2
- package/module/utils/index.js +1 -0
- package/module/utils/types.js +115 -0
- package/module/wasm.js +2 -143
- package/module/wasm.spec.js +17 -13
- package/package.json +2 -2
- package/types/index.d.ts +1 -0
- package/types/messages.d.ts +3 -34
- package/types/utils.d.ts +1 -1
- package/types/wasm.d.ts +2 -0
package/main/wasm.js
CHANGED
@@ -7,152 +7,23 @@ var _typeof = require("@babel/runtime/helpers/typeof");
|
|
7
7
|
Object.defineProperty(exports, "__esModule", {
|
8
8
|
value: true
|
9
9
|
});
|
10
|
-
exports.
|
10
|
+
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
|
|
14
|
-
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
15
|
-
|
16
14
|
var t = _interopRequireWildcard(require("@babel/types"));
|
17
15
|
|
18
16
|
var _case = require("case");
|
19
17
|
|
20
18
|
var _utils = require("./utils");
|
21
19
|
|
22
|
-
|
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 getTypeFromRef = function getTypeFromRef($ref) {
|
27
|
-
switch ($ref) {
|
28
|
-
case '#/definitions/Binary':
|
29
|
-
return t.tsTypeReference(t.identifier('Binary'));
|
30
|
-
|
31
|
-
case '#/definitions/Expiration':
|
32
|
-
return t.tsTypeReference(t.identifier('Expiration'));
|
33
|
-
|
34
|
-
default:
|
35
|
-
if ($ref.startsWith('#/definitions/')) {
|
36
|
-
return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', '')));
|
37
|
-
}
|
38
|
-
|
39
|
-
throw new Error('what is $ref: ' + $ref);
|
40
|
-
}
|
41
|
-
};
|
42
|
-
|
43
|
-
var getArrayTypeFromRef = function getArrayTypeFromRef($ref) {
|
44
|
-
return t.tsArrayType(getTypeFromRef($ref));
|
45
|
-
};
|
46
|
-
|
47
|
-
var getArrayTypeFromType = function getArrayTypeFromType(type) {
|
48
|
-
return t.tsArrayType(getType(type));
|
49
|
-
};
|
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
|
-
|
61
|
-
var getType = function getType(type) {
|
62
|
-
switch (type) {
|
63
|
-
case 'string':
|
64
|
-
return t.tsStringKeyword();
|
65
|
-
|
66
|
-
case 'boolean':
|
67
|
-
return t.tSBooleanKeyword();
|
68
|
-
|
69
|
-
case 'integer':
|
70
|
-
return t.tsNumberKeyword();
|
71
|
-
|
72
|
-
default:
|
73
|
-
throw new Error('what is type: ' + type);
|
74
|
-
}
|
75
|
-
};
|
76
|
-
|
77
|
-
var getPropertyType = function getPropertyType(schema, prop) {
|
78
|
-
var _schema$properties, _schema$required, _schema$required2;
|
79
|
-
|
80
|
-
var props = (_schema$properties = schema.properties) !== null && _schema$properties !== void 0 ? _schema$properties : {};
|
81
|
-
var info = props[prop];
|
82
|
-
var type = null;
|
83
|
-
var optional = (_schema$required = schema.required) === null || _schema$required === void 0 ? void 0 : _schema$required.includes(prop);
|
20
|
+
var _types2 = require("./utils/types");
|
84
21
|
|
85
|
-
|
86
|
-
info = info.allOf[0];
|
87
|
-
}
|
88
|
-
|
89
|
-
if (typeof info.$ref === 'string') {
|
90
|
-
type = getTypeFromRef(info.$ref);
|
91
|
-
}
|
22
|
+
var _babel = require("./utils/babel");
|
92
23
|
|
93
|
-
|
94
|
-
// assuming 2nd is null, but let's check to ensure
|
95
|
-
if (info.anyOf.length !== 2) {
|
96
|
-
throw new Error('case not handled by transpiler. contact maintainers.');
|
97
|
-
}
|
98
|
-
|
99
|
-
var _info$anyOf = (0, _slicedToArray2["default"])(info.anyOf, 2),
|
100
|
-
nullableType = _info$anyOf[0],
|
101
|
-
nullType = _info$anyOf[1];
|
102
|
-
|
103
|
-
if ((nullType === null || nullType === void 0 ? void 0 : nullType.type) !== 'null') {
|
104
|
-
throw new Error('case not handled by transpiler. contact maintainers.');
|
105
|
-
}
|
106
|
-
|
107
|
-
type = getTypeFromRef(nullableType === null || nullableType === void 0 ? void 0 : nullableType.$ref);
|
108
|
-
optional = true;
|
109
|
-
}
|
110
|
-
|
111
|
-
if (typeof info.type === 'string') {
|
112
|
-
if (info.type === 'array') {
|
113
|
-
if (info.items.$ref) {
|
114
|
-
type = getArrayTypeFromRef(info.items.$ref);
|
115
|
-
} else {
|
116
|
-
type = getArrayTypeFromType(info.items.type);
|
117
|
-
}
|
118
|
-
} else {
|
119
|
-
type = getType(info.type);
|
120
|
-
}
|
121
|
-
}
|
122
|
-
|
123
|
-
if (Array.isArray(info.type)) {
|
124
|
-
// assuming 2nd is null, but let's check to ensure
|
125
|
-
if (info.type.length !== 2) {
|
126
|
-
throw new Error('case not handled by transpiler. contact maintainers.');
|
127
|
-
}
|
128
|
-
|
129
|
-
var _info$type = (0, _slicedToArray2["default"])(info.type, 2),
|
130
|
-
_nullableType = _info$type[0],
|
131
|
-
_nullType = _info$type[1];
|
132
|
-
|
133
|
-
if (_nullType !== 'null') {
|
134
|
-
throw new Error('case not handled by transpiler. contact maintainers.');
|
135
|
-
}
|
136
|
-
|
137
|
-
type = getType(_nullableType);
|
138
|
-
optional = true;
|
139
|
-
}
|
140
|
-
|
141
|
-
if (!type) {
|
142
|
-
throw new Error('cannot find type for ' + JSON.stringify(info));
|
143
|
-
}
|
144
|
-
|
145
|
-
if ((_schema$required2 = schema.required) !== null && _schema$required2 !== void 0 && _schema$required2.includes(prop)) {
|
146
|
-
optional = false;
|
147
|
-
}
|
148
|
-
|
149
|
-
return {
|
150
|
-
type: type,
|
151
|
-
optional: optional
|
152
|
-
};
|
153
|
-
};
|
24
|
+
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); }
|
154
25
|
|
155
|
-
|
26
|
+
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; }
|
156
27
|
|
157
28
|
var createWasmQueryMethod = function createWasmQueryMethod(jsonschema) {
|
158
29
|
var _jsonschema$propertie;
|
@@ -190,12 +61,6 @@ var createQueryClass = function createQueryClass(className, implementsClassName,
|
|
190
61
|
|
191
62
|
exports.createQueryClass = createQueryClass;
|
192
63
|
|
193
|
-
var tsTypeOperator = function tsTypeOperator(typeAnnotation, operator) {
|
194
|
-
var obj = t.tsTypeOperator(typeAnnotation);
|
195
|
-
obj.operator = operator;
|
196
|
-
return obj;
|
197
|
-
};
|
198
|
-
|
199
64
|
var createWasmExecMethod = function createWasmExecMethod(jsonschema) {
|
200
65
|
var _jsonschema$propertie2;
|
201
66
|
|
@@ -206,7 +71,7 @@ var createWasmExecMethod = function createWasmExecMethod(jsonschema) {
|
|
206
71
|
var args = Object.keys(properties).map(function (prop) {
|
207
72
|
return t.objectProperty(t.identifier(prop), t.identifier((0, _case.camel)(prop)), false, prop === (0, _case.camel)(prop));
|
208
73
|
});
|
209
|
-
var constantParams = [t.assignmentPattern(identifier('fee', t.tsTypeAnnotation(t.tsUnionType([t.tSNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), 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)];
|
74
|
+
var constantParams = [t.assignmentPattern((0, _babel.identifier)('fee', t.tsTypeAnnotation(t.tsUnionType([t.tSNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), false), t.stringLiteral('auto')), (0, _babel.identifier)('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), (0, _babel.identifier)('funds', t.tsTypeAnnotation((0, _babel.tsTypeOperator)(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
|
210
75
|
return t.classProperty(t.identifier(methodName), (0, _utils.arrowFunctionExpression)(obj ? [// props
|
211
76
|
obj].concat(constantParams) : constantParams, t.blockStatement([t.returnStatement(t.awaitExpression(t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('execute')), [t.memberExpression(t.thisExpression(), t.identifier('sender')), t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.objectExpression([t.objectProperty(t.identifier(underscoreName), t.objectExpression((0, _toConsumableArray2["default"])(args)))]), t.identifier('fee'), t.identifier('memo'), t.identifier('funds')])))]), // return type
|
212
77
|
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true));
|
@@ -256,26 +121,6 @@ var createExecuteInterface = function createExecuteInterface(className, extendsC
|
|
256
121
|
|
257
122
|
exports.createExecuteInterface = createExecuteInterface;
|
258
123
|
|
259
|
-
var propertySignature = function propertySignature(name, typeAnnotation) {
|
260
|
-
var optional = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
261
|
-
// prop.leadingComments = [{
|
262
|
-
// type: 'Comment',
|
263
|
-
// value: ' Data on the token itself'
|
264
|
-
// }];
|
265
|
-
// prop.leadingComments = [{
|
266
|
-
// type: 'CommentBlock',
|
267
|
-
// value: '* Data on the token itself'
|
268
|
-
// }];
|
269
|
-
return {
|
270
|
-
type: 'TSPropertySignature',
|
271
|
-
key: t.identifier(name),
|
272
|
-
typeAnnotation: typeAnnotation,
|
273
|
-
optional: optional
|
274
|
-
};
|
275
|
-
};
|
276
|
-
|
277
|
-
exports.propertySignature = propertySignature;
|
278
|
-
|
279
124
|
var createTypedObjectParams = function createTypedObjectParams(jsonschema) {
|
280
125
|
var _jsonschema$propertie3;
|
281
126
|
|
@@ -283,11 +128,11 @@ var createTypedObjectParams = function createTypedObjectParams(jsonschema) {
|
|
283
128
|
var keys = Object.keys((_jsonschema$propertie3 = jsonschema.properties) !== null && _jsonschema$propertie3 !== void 0 ? _jsonschema$propertie3 : {});
|
284
129
|
if (!keys.length) return;
|
285
130
|
var typedParams = keys.map(function (prop) {
|
286
|
-
var _getPropertyType = getPropertyType(jsonschema, prop),
|
131
|
+
var _getPropertyType = (0, _types2.getPropertyType)(jsonschema, prop),
|
287
132
|
type = _getPropertyType.type,
|
288
133
|
optional = _getPropertyType.optional;
|
289
134
|
|
290
|
-
return propertySignature(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional);
|
135
|
+
return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional);
|
291
136
|
});
|
292
137
|
var params = keys.map(function (prop) {
|
293
138
|
return t.objectProperty(camelize ? t.identifier((0, _case.camel)(prop)) : t.identifier(prop), camelize ? t.identifier((0, _case.camel)(prop)) : t.identifier(prop), false, true);
|
@@ -313,7 +158,7 @@ exports.createPropertyFunctionWithObjectParams = createPropertyFunctionWithObjec
|
|
313
158
|
|
314
159
|
var createPropertyFunctionWithObjectParamsForExec = function createPropertyFunctionWithObjectParamsForExec(methodName, responseType, jsonschema) {
|
315
160
|
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)];
|
161
|
+
var fixedParams = [(0, _babel.identifier)('fee', t.tsTypeAnnotation(t.tsUnionType([t.tsNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), true), (0, _babel.identifier)('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), (0, _babel.identifier)('funds', t.tsTypeAnnotation((0, _babel.tsTypeOperator)(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
|
317
162
|
var func = {
|
318
163
|
type: 'TSFunctionType',
|
319
164
|
typeAnnotation: (0, _utils.promiseTypeAnnotation)(responseType),
|
@@ -340,15 +185,15 @@ var createTypeOrInterface = function createTypeOrInterface(Type, jsonschema) {
|
|
340
185
|
var _jsonschema$propertie4;
|
341
186
|
|
342
187
|
if (jsonschema.type !== 'object') {
|
343
|
-
return t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(Type), null, getType(jsonschema.type)));
|
188
|
+
return t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(Type), null, (0, _types2.getType)(jsonschema.type)));
|
344
189
|
}
|
345
190
|
|
346
191
|
var props = Object.keys((_jsonschema$propertie4 = jsonschema.properties) !== null && _jsonschema$propertie4 !== void 0 ? _jsonschema$propertie4 : {}).map(function (prop) {
|
347
|
-
var _getPropertyType2 = getPropertyType(jsonschema, prop),
|
192
|
+
var _getPropertyType2 = (0, _types2.getPropertyType)(jsonschema, prop),
|
348
193
|
type = _getPropertyType2.type,
|
349
194
|
optional = _getPropertyType2.optional;
|
350
195
|
|
351
|
-
return propertySignature((0, _case.camel)(prop), t.tsTypeAnnotation(type), optional);
|
196
|
+
return (0, _babel.propertySignature)((0, _case.camel)(prop), t.tsTypeAnnotation(type), optional);
|
352
197
|
});
|
353
198
|
return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(Type), null, [], t.tsInterfaceBody((0, _toConsumableArray2["default"])(props))));
|
354
199
|
};
|
package/main/wasm.spec.js
CHANGED
@@ -4,31 +4,33 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
4
4
|
|
5
5
|
var _generator = _interopRequireDefault(require("@babel/generator"));
|
6
6
|
|
7
|
-
var
|
7
|
+
var _execute_msg = _interopRequireDefault(require("./../../../__fixtures__/cw-named-groups/execute_msg.json"));
|
8
8
|
|
9
|
-
var
|
9
|
+
var _query_msg = _interopRequireDefault(require("./../../../__fixtures__/basic/query_msg.json"));
|
10
10
|
|
11
|
-
var
|
11
|
+
var _execute_msg_for__empty = _interopRequireDefault(require("./../../../__fixtures__/basic/execute_msg_for__empty.json"));
|
12
12
|
|
13
|
-
var
|
13
|
+
var _approval_response = _interopRequireDefault(require("./../../../__fixtures__/basic/approval_response.json"));
|
14
14
|
|
15
|
-
var
|
15
|
+
var _all_nft_info_response = _interopRequireDefault(require("./../../../__fixtures__/basic/all_nft_info_response.json"));
|
16
16
|
|
17
|
-
var
|
17
|
+
var _approvals_response = _interopRequireDefault(require("./../../../__fixtures__/basic/approvals_response.json"));
|
18
18
|
|
19
|
-
var
|
19
|
+
var _collection_info_response = _interopRequireDefault(require("./../../../__fixtures__/basic/collection_info_response.json"));
|
20
20
|
|
21
|
-
var
|
21
|
+
var _contract_info_response = _interopRequireDefault(require("./../../../__fixtures__/basic/contract_info_response.json"));
|
22
22
|
|
23
|
-
var
|
23
|
+
var _instantiate_msg = _interopRequireDefault(require("./../../../__fixtures__/basic/instantiate_msg.json"));
|
24
24
|
|
25
|
-
var
|
25
|
+
var _nft_info_response = _interopRequireDefault(require("./../../../__fixtures__/basic/nft_info_response.json"));
|
26
26
|
|
27
|
-
var
|
27
|
+
var _num_tokens_response = _interopRequireDefault(require("./../../../__fixtures__/basic/num_tokens_response.json"));
|
28
28
|
|
29
|
-
var
|
29
|
+
var _operators_response = _interopRequireDefault(require("./../../../__fixtures__/basic/operators_response.json"));
|
30
30
|
|
31
|
-
var
|
31
|
+
var _owner_of_response = _interopRequireDefault(require("./../../../__fixtures__/basic/owner_of_response.json"));
|
32
|
+
|
33
|
+
var _tokens_response = _interopRequireDefault(require("./../../../__fixtures__/basic/tokens_response.json"));
|
32
34
|
|
33
35
|
var _wasm = require("./wasm");
|
34
36
|
|
@@ -82,6 +84,9 @@ it('execute classes', function () {
|
|
82
84
|
it('execute classes no extends', function () {
|
83
85
|
expectCode((0, _wasm.createExecuteClass)('SG721Client', 'SG721Instance', null, _execute_msg_for__empty["default"]));
|
84
86
|
});
|
87
|
+
it('execute classes array types', function () {
|
88
|
+
expectCode((0, _wasm.createExecuteClass)('SG721Client', 'SG721Instance', null, _execute_msg["default"]));
|
89
|
+
});
|
85
90
|
it('execute interfaces no extends', function () {
|
86
91
|
expectCode((0, _wasm.createExecuteInterface)('SG721Instance', null, _execute_msg_for__empty["default"]));
|
87
92
|
});
|
package/module/index.js
CHANGED
@@ -0,0 +1,112 @@
|
|
1
|
+
import * as t from '@babel/types';
|
2
|
+
import { camel } from 'case';
|
3
|
+
import { bindMethod, typedIdentifier, classDeclaration, classProperty, arrowFunctionExpression, getMessageProperties } from './utils';
|
4
|
+
import { getPropertyType } from './utils/types';
|
5
|
+
import { identifier, tsTypeOperator, propertySignature } from './utils/babel';
|
6
|
+
|
7
|
+
const createWasmExecMethodPartial = jsonschema => {
|
8
|
+
const underscoreName = Object.keys(jsonschema.properties)[0];
|
9
|
+
const methodName = camel(underscoreName);
|
10
|
+
const properties = jsonschema.properties[underscoreName].properties ?? {};
|
11
|
+
const obj = createTypedObjectParams(jsonschema.properties[underscoreName]);
|
12
|
+
const args = Object.keys(properties).map(prop => {
|
13
|
+
return t.objectProperty(t.identifier(prop), t.identifier(camel(prop)), false, prop === camel(prop));
|
14
|
+
});
|
15
|
+
const constantParams = [// t.assignmentPattern(
|
16
|
+
// identifier(
|
17
|
+
// 'fee',
|
18
|
+
// t.tsTypeAnnotation(
|
19
|
+
// t.tsUnionType(
|
20
|
+
// [
|
21
|
+
// t.tSNumberKeyword(),
|
22
|
+
// t.tsTypeReference(
|
23
|
+
// t.identifier('StdFee')
|
24
|
+
// ),
|
25
|
+
// t.tsLiteralType(
|
26
|
+
// t.stringLiteral('auto')
|
27
|
+
// )
|
28
|
+
// ]
|
29
|
+
// )
|
30
|
+
// ),
|
31
|
+
// false
|
32
|
+
// ),
|
33
|
+
// t.stringLiteral('auto')
|
34
|
+
// ),
|
35
|
+
// identifier('memo', t.tsTypeAnnotation(
|
36
|
+
// t.tsStringKeyword()
|
37
|
+
// ), true),
|
38
|
+
identifier('funds', t.tsTypeAnnotation(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
|
39
|
+
return t.classProperty(t.identifier(methodName), arrowFunctionExpression(obj ? [// props
|
40
|
+
obj, ...constantParams] : constantParams, t.blockStatement([t.returnStatement(t.objectExpression([t.objectProperty(t.identifier('typeUrl'), t.stringLiteral('/cosmwasm.wasm.v1.MsgExecuteContract')), t.objectProperty(t.identifier('value'), t.callExpression(t.memberExpression(t.identifier('MsgExecuteContract'), t.identifier('fromPartial')), [t.objectExpression([t.objectProperty(t.identifier('sender'), t.memberExpression(t.thisExpression(), t.identifier('sender'))), t.objectProperty(t.identifier('contract'), t.memberExpression(t.thisExpression(), t.identifier('contractAddress'))), t.objectProperty(t.identifier('msg'), t.callExpression(t.identifier('toUtf8'), [t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.objectExpression([t.objectProperty(t.identifier(underscoreName), t.objectExpression(args))])])])), t.objectProperty(t.identifier('funds'), t.identifier('funds'), false, true)])]))]))]), // return type
|
41
|
+
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('MsgExecuteContractEncodeObject'))), false));
|
42
|
+
};
|
43
|
+
|
44
|
+
export const createFromPartialClass = (className, implementsClassName, execMsg) => {
|
45
|
+
const propertyNames = getMessageProperties(execMsg).map(method => Object.keys(method.properties)?.[0]).filter(Boolean);
|
46
|
+
const bindings = propertyNames.map(camel).map(bindMethod);
|
47
|
+
const methods = getMessageProperties(execMsg).map(schema => {
|
48
|
+
return createWasmExecMethodPartial(schema);
|
49
|
+
});
|
50
|
+
const blockStmt = [];
|
51
|
+
[].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]);
|
52
|
+
return t.exportNamedDeclaration(classDeclaration(className, [// sender
|
53
|
+
classProperty('sender', t.tsTypeAnnotation(t.tsStringKeyword())), // contractAddress
|
54
|
+
classProperty('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword())), // constructor
|
55
|
+
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));
|
56
|
+
};
|
57
|
+
export const createFromPartialInterface = (className, execMsg) => {
|
58
|
+
const methods = getMessageProperties(execMsg).map(jsonschema => {
|
59
|
+
const underscoreName = Object.keys(jsonschema.properties)[0];
|
60
|
+
const methodName = camel(underscoreName);
|
61
|
+
return createPropertyFunctionWithObjectParamsForPartial(methodName, 'MsgExecuteContractEncodeObject', jsonschema.properties[underscoreName]);
|
62
|
+
});
|
63
|
+
const extendsAst = [];
|
64
|
+
return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(className), null, extendsAst, t.tSInterfaceBody([// contract address
|
65
|
+
t.tSPropertySignature(t.identifier('contractAddress'), t.tsTypeAnnotation(t.tsStringKeyword())), // contract address
|
66
|
+
t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods])));
|
67
|
+
}; // MARKED AS NOT DRY
|
68
|
+
|
69
|
+
const createTypedObjectParams = (jsonschema, camelize = true) => {
|
70
|
+
const keys = Object.keys(jsonschema.properties ?? {});
|
71
|
+
if (!keys.length) return;
|
72
|
+
const typedParams = keys.map(prop => {
|
73
|
+
const {
|
74
|
+
type,
|
75
|
+
optional
|
76
|
+
} = getPropertyType(jsonschema, prop);
|
77
|
+
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
|
78
|
+
});
|
79
|
+
const params = keys.map(prop => {
|
80
|
+
return t.objectProperty(camelize ? t.identifier(camel(prop)) : t.identifier(prop), camelize ? t.identifier(camel(prop)) : t.identifier(prop), false, true);
|
81
|
+
});
|
82
|
+
const obj = t.objectPattern([...params]);
|
83
|
+
obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral([...typedParams]));
|
84
|
+
return obj;
|
85
|
+
};
|
86
|
+
|
87
|
+
const createPropertyFunctionWithObjectParamsForPartial = (methodName, responseType, jsonschema) => {
|
88
|
+
const obj = createTypedObjectParams(jsonschema);
|
89
|
+
const fixedParams = [// identifier('fee', t.tsTypeAnnotation(
|
90
|
+
// t.tsUnionType(
|
91
|
+
// [
|
92
|
+
// t.tsNumberKeyword(),
|
93
|
+
// t.tsTypeReference(
|
94
|
+
// t.identifier('StdFee')
|
95
|
+
// ),
|
96
|
+
// t.tsLiteralType(
|
97
|
+
// t.stringLiteral('auto')
|
98
|
+
// )
|
99
|
+
// ]
|
100
|
+
// )
|
101
|
+
// ), true),
|
102
|
+
// identifier('memo', t.tsTypeAnnotation(
|
103
|
+
// t.tsStringKeyword()
|
104
|
+
// ), true),
|
105
|
+
identifier('funds', t.tsTypeAnnotation(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
|
106
|
+
const func = {
|
107
|
+
type: 'TSFunctionType',
|
108
|
+
typeAnnotation: t.tsTypeAnnotation(t.tsTypeReference(t.identifier(responseType))),
|
109
|
+
parameters: obj ? [obj, ...fixedParams] : fixedParams
|
110
|
+
};
|
111
|
+
return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(func));
|
112
|
+
};
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import generate from '@babel/generator';
|
2
|
+
import execute_msg from './../../../__fixtures__/basic/execute_msg_for__empty.json';
|
3
|
+
import { createFromPartialClass, createFromPartialInterface } from './messages';
|
4
|
+
|
5
|
+
const expectCode = ast => {
|
6
|
+
expect(generate(ast).code).toMatchSnapshot();
|
7
|
+
};
|
8
|
+
|
9
|
+
const printCode = ast => {
|
10
|
+
console.log(generate(ast).code);
|
11
|
+
};
|
12
|
+
|
13
|
+
it('execute classes', () => {
|
14
|
+
expectCode(createFromPartialClass('SG721MessageComposer', 'SG721Message', execute_msg));
|
15
|
+
});
|
16
|
+
it('createFromPartialInterface', () => {
|
17
|
+
expectCode(createFromPartialInterface('SG721Message', execute_msg));
|
18
|
+
});
|
package/module/react-query.js
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
import * as t from '@babel/types';
|
2
2
|
import { camel, pascal } from 'case';
|
3
3
|
import { tsPropertySignature, tsObjectPattern, callExpression, getMessageProperties } from './utils';
|
4
|
-
import { propertySignature
|
4
|
+
import { propertySignature } from './utils/babel';
|
5
|
+
import { getPropertyType } from './utils/types';
|
5
6
|
export const createReactQueryHooks = (queryMsg, contractName, QueryClient) => {
|
6
7
|
return getMessageProperties(queryMsg).reduce((m, schema) => {
|
7
8
|
const underscoreName = Object.keys(schema.properties)[0];
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import generate from '@babel/generator';
|
2
2
|
import * as t from '@babel/types';
|
3
|
-
import query_msg from '
|
3
|
+
import query_msg from './../../../__fixtures__/basic/query_msg.json';
|
4
4
|
import { createReactQueryHooks } from './react-query';
|
5
5
|
|
6
6
|
const expectCode = ast => {
|
package/module/recoil.spec.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import generate from '@babel/generator';
|
2
2
|
import * as t from '@babel/types';
|
3
|
-
import query_msg from '
|
3
|
+
import query_msg from './../../../__fixtures__/basic/query_msg.json';
|
4
4
|
import { createRecoilSelector, createRecoilSelectors, createRecoilQueryClient } from './recoil';
|
5
5
|
|
6
6
|
const expectCode = ast => {
|
@@ -1,5 +1,24 @@
|
|
1
1
|
import * as t from '@babel/types';
|
2
2
|
import { snake } from "case";
|
3
|
+
export const propertySignature = (name, typeAnnotation, optional = false) => {
|
4
|
+
return {
|
5
|
+
type: 'TSPropertySignature',
|
6
|
+
key: t.identifier(name),
|
7
|
+
typeAnnotation,
|
8
|
+
optional
|
9
|
+
};
|
10
|
+
};
|
11
|
+
export const identifier = (name, typeAnnotation, optional = false) => {
|
12
|
+
const type = t.identifier(name);
|
13
|
+
type.typeAnnotation = typeAnnotation;
|
14
|
+
type.optional = optional;
|
15
|
+
return type;
|
16
|
+
};
|
17
|
+
export const tsTypeOperator = (typeAnnotation, operator) => {
|
18
|
+
const obj = t.tsTypeOperator(typeAnnotation);
|
19
|
+
obj.operator = operator;
|
20
|
+
return obj;
|
21
|
+
};
|
3
22
|
export const getMessageProperties = msg => {
|
4
23
|
if (msg.anyOf) return msg.anyOf;
|
5
24
|
if (msg.oneOf) return msg.oneOf;
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import { importStmt } from './
|
1
|
+
import { importStmt } from './babel';
|
2
2
|
import generate from '@babel/generator';
|
3
3
|
import * as t from '@babel/types';
|
4
|
-
import { bindMethod, typedIdentifier, promiseTypeAnnotation, classDeclaration, classProperty, arrowFunctionExpression } from './
|
4
|
+
import { bindMethod, typedIdentifier, promiseTypeAnnotation, classDeclaration, classProperty, arrowFunctionExpression } from './babel';
|
5
5
|
|
6
6
|
const expectCode = ast => {
|
7
7
|
expect(generate(ast).code).toMatchSnapshot();
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './babel';
|
@@ -0,0 +1,115 @@
|
|
1
|
+
import * as t from '@babel/types';
|
2
|
+
|
3
|
+
const getTypeFromRef = $ref => {
|
4
|
+
switch ($ref) {
|
5
|
+
case '#/definitions/Binary':
|
6
|
+
return t.tsTypeReference(t.identifier('Binary'));
|
7
|
+
|
8
|
+
default:
|
9
|
+
if ($ref.startsWith('#/definitions/')) {
|
10
|
+
return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', '')));
|
11
|
+
}
|
12
|
+
|
13
|
+
throw new Error('what is $ref: ' + $ref);
|
14
|
+
}
|
15
|
+
};
|
16
|
+
|
17
|
+
const getArrayTypeFromRef = $ref => {
|
18
|
+
return t.tsArrayType(getTypeFromRef($ref));
|
19
|
+
};
|
20
|
+
|
21
|
+
const getArrayTypeFromType = type => {
|
22
|
+
return t.tsArrayType(getType(type));
|
23
|
+
};
|
24
|
+
|
25
|
+
export const getType = type => {
|
26
|
+
switch (type) {
|
27
|
+
case 'string':
|
28
|
+
return t.tsStringKeyword();
|
29
|
+
|
30
|
+
case 'boolean':
|
31
|
+
return t.tSBooleanKeyword();
|
32
|
+
|
33
|
+
case 'integer':
|
34
|
+
return t.tsNumberKeyword();
|
35
|
+
|
36
|
+
default:
|
37
|
+
throw new Error('contact maintainers [unknown type]: ' + type);
|
38
|
+
}
|
39
|
+
};
|
40
|
+
export const getPropertyType = (schema, prop) => {
|
41
|
+
const props = schema.properties ?? {};
|
42
|
+
let info = props[prop];
|
43
|
+
let type = null;
|
44
|
+
let optional = schema.required?.includes(prop);
|
45
|
+
|
46
|
+
if (info.allOf && info.allOf.length === 1) {
|
47
|
+
info = info.allOf[0];
|
48
|
+
}
|
49
|
+
|
50
|
+
if (typeof info.$ref === 'string') {
|
51
|
+
type = getTypeFromRef(info.$ref);
|
52
|
+
}
|
53
|
+
|
54
|
+
if (Array.isArray(info.anyOf)) {
|
55
|
+
// assuming 2nd is null, but let's check to ensure
|
56
|
+
if (info.anyOf.length !== 2) {
|
57
|
+
throw new Error('case not handled by transpiler. contact maintainers.');
|
58
|
+
}
|
59
|
+
|
60
|
+
const [nullableType, nullType] = info.anyOf;
|
61
|
+
|
62
|
+
if (nullType?.type !== 'null') {
|
63
|
+
throw new Error('case not handled by transpiler. contact maintainers.');
|
64
|
+
}
|
65
|
+
|
66
|
+
type = getTypeFromRef(nullableType?.$ref);
|
67
|
+
optional = true;
|
68
|
+
}
|
69
|
+
|
70
|
+
if (typeof info.type === 'string') {
|
71
|
+
if (info.type === 'array') {
|
72
|
+
if (info.items.$ref) {
|
73
|
+
type = getArrayTypeFromRef(info.items.$ref);
|
74
|
+
} else {
|
75
|
+
type = getArrayTypeFromType(info.items.type);
|
76
|
+
}
|
77
|
+
} else {
|
78
|
+
type = getType(info.type);
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
82
|
+
if (Array.isArray(info.type)) {
|
83
|
+
// assuming 2nd is null, but let's check to ensure
|
84
|
+
if (info.type.length !== 2) {
|
85
|
+
throw new Error('please report this to maintainers (field type): ' + JSON.stringify(info, null, 2));
|
86
|
+
}
|
87
|
+
|
88
|
+
const [nullableType, nullType] = info.type;
|
89
|
+
|
90
|
+
if (nullType !== 'null') {
|
91
|
+
throw new Error('please report this to maintainers (field type): ' + JSON.stringify(info, null, 2));
|
92
|
+
}
|
93
|
+
|
94
|
+
if (nullableType === 'array') {
|
95
|
+
type = t.tsArrayType(getType(info.items.type));
|
96
|
+
} else {
|
97
|
+
type = getType(nullableType);
|
98
|
+
}
|
99
|
+
|
100
|
+
optional = true;
|
101
|
+
}
|
102
|
+
|
103
|
+
if (!type) {
|
104
|
+
throw new Error('cannot find type for ' + JSON.stringify(info));
|
105
|
+
}
|
106
|
+
|
107
|
+
if (schema.required?.includes(prop)) {
|
108
|
+
optional = false;
|
109
|
+
}
|
110
|
+
|
111
|
+
return {
|
112
|
+
type,
|
113
|
+
optional
|
114
|
+
};
|
115
|
+
};
|