wasm-ast-types 0.2.5 → 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/messages.js +10 -160
- package/main/messages.spec.js +1 -1
- 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 -164
- package/main/wasm.spec.js +18 -13
- package/module/messages.js +4 -146
- package/module/messages.spec.js +1 -1
- 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 -140
- package/module/wasm.spec.js +17 -13
- package/package.json +2 -2
package/main/wasm.js
CHANGED
@@ -7,149 +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
|
-
default:
|
32
|
-
if ($ref.startsWith('#/definitions/')) {
|
33
|
-
return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', '')));
|
34
|
-
}
|
35
|
-
|
36
|
-
throw new Error('what is $ref: ' + $ref);
|
37
|
-
}
|
38
|
-
};
|
39
|
-
|
40
|
-
var getArrayTypeFromRef = function getArrayTypeFromRef($ref) {
|
41
|
-
return t.tsArrayType(getTypeFromRef($ref));
|
42
|
-
};
|
43
|
-
|
44
|
-
var getArrayTypeFromType = function getArrayTypeFromType(type) {
|
45
|
-
return t.tsArrayType(getType(type));
|
46
|
-
};
|
47
|
-
|
48
|
-
var identifier = function identifier(name, typeAnnotation) {
|
49
|
-
var optional = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
50
|
-
var type = t.identifier(name);
|
51
|
-
type.typeAnnotation = typeAnnotation;
|
52
|
-
type.optional = optional;
|
53
|
-
return type;
|
54
|
-
};
|
55
|
-
|
56
|
-
exports.identifier = identifier;
|
57
|
-
|
58
|
-
var getType = function getType(type) {
|
59
|
-
switch (type) {
|
60
|
-
case 'string':
|
61
|
-
return t.tsStringKeyword();
|
62
|
-
|
63
|
-
case 'boolean':
|
64
|
-
return t.tSBooleanKeyword();
|
65
|
-
|
66
|
-
case 'integer':
|
67
|
-
return t.tsNumberKeyword();
|
68
|
-
|
69
|
-
default:
|
70
|
-
throw new Error('what is type: ' + type);
|
71
|
-
}
|
72
|
-
};
|
73
|
-
|
74
|
-
var getPropertyType = function getPropertyType(schema, prop) {
|
75
|
-
var _schema$properties, _schema$required, _schema$required2;
|
76
|
-
|
77
|
-
var props = (_schema$properties = schema.properties) !== null && _schema$properties !== void 0 ? _schema$properties : {};
|
78
|
-
var info = props[prop];
|
79
|
-
var type = null;
|
80
|
-
var optional = (_schema$required = schema.required) === null || _schema$required === void 0 ? void 0 : _schema$required.includes(prop);
|
81
|
-
|
82
|
-
if (info.allOf && info.allOf.length === 1) {
|
83
|
-
info = info.allOf[0];
|
84
|
-
}
|
85
|
-
|
86
|
-
if (typeof info.$ref === 'string') {
|
87
|
-
type = getTypeFromRef(info.$ref);
|
88
|
-
}
|
89
|
-
|
90
|
-
if (Array.isArray(info.anyOf)) {
|
91
|
-
// assuming 2nd is null, but let's check to ensure
|
92
|
-
if (info.anyOf.length !== 2) {
|
93
|
-
throw new Error('case not handled by transpiler. contact maintainers.');
|
94
|
-
}
|
95
|
-
|
96
|
-
var _info$anyOf = (0, _slicedToArray2["default"])(info.anyOf, 2),
|
97
|
-
nullableType = _info$anyOf[0],
|
98
|
-
nullType = _info$anyOf[1];
|
99
|
-
|
100
|
-
if ((nullType === null || nullType === void 0 ? void 0 : nullType.type) !== 'null') {
|
101
|
-
throw new Error('case not handled by transpiler. contact maintainers.');
|
102
|
-
}
|
103
|
-
|
104
|
-
type = getTypeFromRef(nullableType === null || nullableType === void 0 ? void 0 : nullableType.$ref);
|
105
|
-
optional = true;
|
106
|
-
}
|
107
|
-
|
108
|
-
if (typeof info.type === 'string') {
|
109
|
-
if (info.type === 'array') {
|
110
|
-
if (info.items.$ref) {
|
111
|
-
type = getArrayTypeFromRef(info.items.$ref);
|
112
|
-
} else {
|
113
|
-
type = getArrayTypeFromType(info.items.type);
|
114
|
-
}
|
115
|
-
} else {
|
116
|
-
type = getType(info.type);
|
117
|
-
}
|
118
|
-
}
|
119
|
-
|
120
|
-
if (Array.isArray(info.type)) {
|
121
|
-
// assuming 2nd is null, but let's check to ensure
|
122
|
-
if (info.type.length !== 2) {
|
123
|
-
throw new Error('case not handled by transpiler. contact maintainers.');
|
124
|
-
}
|
125
|
-
|
126
|
-
var _info$type = (0, _slicedToArray2["default"])(info.type, 2),
|
127
|
-
_nullableType = _info$type[0],
|
128
|
-
_nullType = _info$type[1];
|
129
|
-
|
130
|
-
if (_nullType !== 'null') {
|
131
|
-
throw new Error('case not handled by transpiler. contact maintainers.');
|
132
|
-
}
|
20
|
+
var _types2 = require("./utils/types");
|
133
21
|
|
134
|
-
|
135
|
-
optional = true;
|
136
|
-
}
|
137
|
-
|
138
|
-
if (!type) {
|
139
|
-
throw new Error('cannot find type for ' + JSON.stringify(info));
|
140
|
-
}
|
22
|
+
var _babel = require("./utils/babel");
|
141
23
|
|
142
|
-
|
143
|
-
optional = false;
|
144
|
-
}
|
145
|
-
|
146
|
-
return {
|
147
|
-
type: type,
|
148
|
-
optional: optional
|
149
|
-
};
|
150
|
-
};
|
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); }
|
151
25
|
|
152
|
-
|
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; }
|
153
27
|
|
154
28
|
var createWasmQueryMethod = function createWasmQueryMethod(jsonschema) {
|
155
29
|
var _jsonschema$propertie;
|
@@ -187,12 +61,6 @@ var createQueryClass = function createQueryClass(className, implementsClassName,
|
|
187
61
|
|
188
62
|
exports.createQueryClass = createQueryClass;
|
189
63
|
|
190
|
-
var tsTypeOperator = function tsTypeOperator(typeAnnotation, operator) {
|
191
|
-
var obj = t.tsTypeOperator(typeAnnotation);
|
192
|
-
obj.operator = operator;
|
193
|
-
return obj;
|
194
|
-
};
|
195
|
-
|
196
64
|
var createWasmExecMethod = function createWasmExecMethod(jsonschema) {
|
197
65
|
var _jsonschema$propertie2;
|
198
66
|
|
@@ -203,7 +71,7 @@ var createWasmExecMethod = function createWasmExecMethod(jsonschema) {
|
|
203
71
|
var args = Object.keys(properties).map(function (prop) {
|
204
72
|
return t.objectProperty(t.identifier(prop), t.identifier((0, _case.camel)(prop)), false, prop === (0, _case.camel)(prop));
|
205
73
|
});
|
206
|
-
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)];
|
207
75
|
return t.classProperty(t.identifier(methodName), (0, _utils.arrowFunctionExpression)(obj ? [// props
|
208
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
|
209
77
|
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true));
|
@@ -253,26 +121,6 @@ var createExecuteInterface = function createExecuteInterface(className, extendsC
|
|
253
121
|
|
254
122
|
exports.createExecuteInterface = createExecuteInterface;
|
255
123
|
|
256
|
-
var propertySignature = function propertySignature(name, typeAnnotation) {
|
257
|
-
var optional = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
258
|
-
// prop.leadingComments = [{
|
259
|
-
// type: 'Comment',
|
260
|
-
// value: ' Data on the token itself'
|
261
|
-
// }];
|
262
|
-
// prop.leadingComments = [{
|
263
|
-
// type: 'CommentBlock',
|
264
|
-
// value: '* Data on the token itself'
|
265
|
-
// }];
|
266
|
-
return {
|
267
|
-
type: 'TSPropertySignature',
|
268
|
-
key: t.identifier(name),
|
269
|
-
typeAnnotation: typeAnnotation,
|
270
|
-
optional: optional
|
271
|
-
};
|
272
|
-
};
|
273
|
-
|
274
|
-
exports.propertySignature = propertySignature;
|
275
|
-
|
276
124
|
var createTypedObjectParams = function createTypedObjectParams(jsonschema) {
|
277
125
|
var _jsonschema$propertie3;
|
278
126
|
|
@@ -280,11 +128,11 @@ var createTypedObjectParams = function createTypedObjectParams(jsonschema) {
|
|
280
128
|
var keys = Object.keys((_jsonschema$propertie3 = jsonschema.properties) !== null && _jsonschema$propertie3 !== void 0 ? _jsonschema$propertie3 : {});
|
281
129
|
if (!keys.length) return;
|
282
130
|
var typedParams = keys.map(function (prop) {
|
283
|
-
var _getPropertyType = getPropertyType(jsonschema, prop),
|
131
|
+
var _getPropertyType = (0, _types2.getPropertyType)(jsonschema, prop),
|
284
132
|
type = _getPropertyType.type,
|
285
133
|
optional = _getPropertyType.optional;
|
286
134
|
|
287
|
-
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);
|
288
136
|
});
|
289
137
|
var params = keys.map(function (prop) {
|
290
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);
|
@@ -310,7 +158,7 @@ exports.createPropertyFunctionWithObjectParams = createPropertyFunctionWithObjec
|
|
310
158
|
|
311
159
|
var createPropertyFunctionWithObjectParamsForExec = function createPropertyFunctionWithObjectParamsForExec(methodName, responseType, jsonschema) {
|
312
160
|
var obj = createTypedObjectParams(jsonschema);
|
313
|
-
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)];
|
314
162
|
var func = {
|
315
163
|
type: 'TSFunctionType',
|
316
164
|
typeAnnotation: (0, _utils.promiseTypeAnnotation)(responseType),
|
@@ -337,15 +185,15 @@ var createTypeOrInterface = function createTypeOrInterface(Type, jsonschema) {
|
|
337
185
|
var _jsonschema$propertie4;
|
338
186
|
|
339
187
|
if (jsonschema.type !== 'object') {
|
340
|
-
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)));
|
341
189
|
}
|
342
190
|
|
343
191
|
var props = Object.keys((_jsonschema$propertie4 = jsonschema.properties) !== null && _jsonschema$propertie4 !== void 0 ? _jsonschema$propertie4 : {}).map(function (prop) {
|
344
|
-
var _getPropertyType2 = getPropertyType(jsonschema, prop),
|
192
|
+
var _getPropertyType2 = (0, _types2.getPropertyType)(jsonschema, prop),
|
345
193
|
type = _getPropertyType2.type,
|
346
194
|
optional = _getPropertyType2.optional;
|
347
195
|
|
348
|
-
return propertySignature((0, _case.camel)(prop), t.tsTypeAnnotation(type), optional);
|
196
|
+
return (0, _babel.propertySignature)((0, _case.camel)(prop), t.tsTypeAnnotation(type), optional);
|
349
197
|
});
|
350
198
|
return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(Type), null, [], t.tsInterfaceBody((0, _toConsumableArray2["default"])(props))));
|
351
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/messages.js
CHANGED
@@ -1,134 +1,10 @@
|
|
1
1
|
import * as t from '@babel/types';
|
2
2
|
import { camel } from 'case';
|
3
3
|
import { bindMethod, typedIdentifier, classDeclaration, classProperty, arrowFunctionExpression, getMessageProperties } from './utils';
|
4
|
+
import { getPropertyType } from './utils/types';
|
5
|
+
import { identifier, tsTypeOperator, propertySignature } from './utils/babel';
|
4
6
|
|
5
|
-
const
|
6
|
-
switch ($ref) {
|
7
|
-
case '#/definitions/Binary':
|
8
|
-
return t.tsTypeReference(t.identifier('Binary'));
|
9
|
-
|
10
|
-
default:
|
11
|
-
if ($ref.startsWith('#/definitions/')) {
|
12
|
-
return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', '')));
|
13
|
-
}
|
14
|
-
|
15
|
-
throw new Error('what is $ref: ' + $ref);
|
16
|
-
}
|
17
|
-
};
|
18
|
-
|
19
|
-
const getArrayTypeFromRef = $ref => {
|
20
|
-
return t.tsArrayType(getTypeFromRef($ref));
|
21
|
-
};
|
22
|
-
|
23
|
-
const getArrayTypeFromType = type => {
|
24
|
-
return t.tsArrayType(getType(type));
|
25
|
-
}; // MARKED AS NOT DRY
|
26
|
-
|
27
|
-
|
28
|
-
const identifier = (name, typeAnnotation, optional = false) => {
|
29
|
-
const type = t.identifier(name);
|
30
|
-
type.typeAnnotation = typeAnnotation;
|
31
|
-
type.optional = optional;
|
32
|
-
return type;
|
33
|
-
};
|
34
|
-
|
35
|
-
const getType = type => {
|
36
|
-
switch (type) {
|
37
|
-
case 'string':
|
38
|
-
return t.tsStringKeyword();
|
39
|
-
|
40
|
-
case 'boolean':
|
41
|
-
return t.tSBooleanKeyword();
|
42
|
-
|
43
|
-
case 'integer':
|
44
|
-
return t.tsNumberKeyword();
|
45
|
-
|
46
|
-
default:
|
47
|
-
throw new Error('what is type: ' + type);
|
48
|
-
}
|
49
|
-
}; // MARKED AS NOT DRY
|
50
|
-
|
51
|
-
|
52
|
-
const getPropertyType = (schema, prop) => {
|
53
|
-
const props = schema.properties ?? {};
|
54
|
-
let info = props[prop];
|
55
|
-
let type = null;
|
56
|
-
let optional = schema.required?.includes(prop);
|
57
|
-
|
58
|
-
if (info.allOf && info.allOf.length === 1) {
|
59
|
-
info = info.allOf[0];
|
60
|
-
}
|
61
|
-
|
62
|
-
if (typeof info.$ref === 'string') {
|
63
|
-
type = getTypeFromRef(info.$ref);
|
64
|
-
}
|
65
|
-
|
66
|
-
if (Array.isArray(info.anyOf)) {
|
67
|
-
// assuming 2nd is null, but let's check to ensure
|
68
|
-
if (info.anyOf.length !== 2) {
|
69
|
-
throw new Error('case not handled by transpiler. contact maintainers.');
|
70
|
-
}
|
71
|
-
|
72
|
-
const [nullableType, nullType] = info.anyOf;
|
73
|
-
|
74
|
-
if (nullType?.type !== 'null') {
|
75
|
-
throw new Error('case not handled by transpiler. contact maintainers.');
|
76
|
-
}
|
77
|
-
|
78
|
-
type = getTypeFromRef(nullableType?.$ref);
|
79
|
-
optional = true;
|
80
|
-
}
|
81
|
-
|
82
|
-
if (typeof info.type === 'string') {
|
83
|
-
if (info.type === 'array') {
|
84
|
-
if (info.items.$ref) {
|
85
|
-
type = getArrayTypeFromRef(info.items.$ref);
|
86
|
-
} else {
|
87
|
-
type = getArrayTypeFromType(info.items.type);
|
88
|
-
}
|
89
|
-
} else {
|
90
|
-
type = getType(info.type);
|
91
|
-
}
|
92
|
-
}
|
93
|
-
|
94
|
-
if (Array.isArray(info.type)) {
|
95
|
-
// assuming 2nd is null, but let's check to ensure
|
96
|
-
if (info.type.length !== 2) {
|
97
|
-
throw new Error('case not handled by transpiler. contact maintainers.');
|
98
|
-
}
|
99
|
-
|
100
|
-
const [nullableType, nullType] = info.type;
|
101
|
-
|
102
|
-
if (nullType !== 'null') {
|
103
|
-
throw new Error('case not handled by transpiler. contact maintainers.');
|
104
|
-
}
|
105
|
-
|
106
|
-
type = getType(nullableType);
|
107
|
-
optional = true;
|
108
|
-
}
|
109
|
-
|
110
|
-
if (!type) {
|
111
|
-
throw new Error('cannot find type for ' + JSON.stringify(info));
|
112
|
-
}
|
113
|
-
|
114
|
-
if (schema.required?.includes(prop)) {
|
115
|
-
optional = false;
|
116
|
-
}
|
117
|
-
|
118
|
-
return {
|
119
|
-
type,
|
120
|
-
optional
|
121
|
-
};
|
122
|
-
};
|
123
|
-
|
124
|
-
const tsTypeOperator = (typeAnnotation, operator) => {
|
125
|
-
const obj = t.tsTypeOperator(typeAnnotation);
|
126
|
-
obj.operator = operator;
|
127
|
-
return obj;
|
128
|
-
}; // MARKED AS NOT DRY
|
129
|
-
|
130
|
-
|
131
|
-
const createWasmExecMethod = jsonschema => {
|
7
|
+
const createWasmExecMethodPartial = jsonschema => {
|
132
8
|
const underscoreName = Object.keys(jsonschema.properties)[0];
|
133
9
|
const methodName = camel(underscoreName);
|
134
10
|
const properties = jsonschema.properties[underscoreName].properties ?? {};
|
@@ -169,7 +45,7 @@ export const createFromPartialClass = (className, implementsClassName, execMsg)
|
|
169
45
|
const propertyNames = getMessageProperties(execMsg).map(method => Object.keys(method.properties)?.[0]).filter(Boolean);
|
170
46
|
const bindings = propertyNames.map(camel).map(bindMethod);
|
171
47
|
const methods = getMessageProperties(execMsg).map(schema => {
|
172
|
-
return
|
48
|
+
return createWasmExecMethodPartial(schema);
|
173
49
|
});
|
174
50
|
const blockStmt = [];
|
175
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]);
|
@@ -190,24 +66,6 @@ export const createFromPartialInterface = (className, execMsg) => {
|
|
190
66
|
t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods])));
|
191
67
|
}; // MARKED AS NOT DRY
|
192
68
|
|
193
|
-
const propertySignature = (name, typeAnnotation, optional = false) => {
|
194
|
-
// prop.leadingComments = [{
|
195
|
-
// type: 'Comment',
|
196
|
-
// value: ' Data on the token itself'
|
197
|
-
// }];
|
198
|
-
// prop.leadingComments = [{
|
199
|
-
// type: 'CommentBlock',
|
200
|
-
// value: '* Data on the token itself'
|
201
|
-
// }];
|
202
|
-
return {
|
203
|
-
type: 'TSPropertySignature',
|
204
|
-
key: t.identifier(name),
|
205
|
-
typeAnnotation,
|
206
|
-
optional
|
207
|
-
};
|
208
|
-
}; // MARKED AS NOT DRY
|
209
|
-
|
210
|
-
|
211
69
|
const createTypedObjectParams = (jsonschema, camelize = true) => {
|
212
70
|
const keys = Object.keys(jsonschema.properties ?? {});
|
213
71
|
if (!keys.length) return;
|
package/module/messages.spec.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import generate from '@babel/generator';
|
2
|
-
import execute_msg from '
|
2
|
+
import execute_msg from './../../../__fixtures__/basic/execute_msg_for__empty.json';
|
3
3
|
import { createFromPartialClass, createFromPartialInterface } from './messages';
|
4
4
|
|
5
5
|
const expectCode = ast => {
|
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
|
+
};
|