wasm-ast-types 0.2.6 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/main/messages.js CHANGED
@@ -31,7 +31,7 @@ var createWasmExecMethodPartial = function createWasmExecMethodPartial(jsonschem
31
31
  var underscoreName = Object.keys(jsonschema.properties)[0];
32
32
  var methodName = (0, _case.camel)(underscoreName);
33
33
  var properties = (_jsonschema$propertie = jsonschema.properties[underscoreName].properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {};
34
- var obj = createTypedObjectParams(jsonschema.properties[underscoreName]);
34
+ var obj = (0, _types2.createTypedObjectParams)(jsonschema.properties[underscoreName]);
35
35
  var args = Object.keys(properties).map(function (prop) {
36
36
  return t.objectProperty(t.identifier(prop), t.identifier((0, _case.camel)(prop)), false, prop === (0, _case.camel)(prop));
37
37
  });
@@ -99,29 +99,8 @@ var createFromPartialInterface = function createFromPartialInterface(className,
99
99
 
100
100
  exports.createFromPartialInterface = createFromPartialInterface;
101
101
 
102
- var createTypedObjectParams = function createTypedObjectParams(jsonschema) {
103
- var _jsonschema$propertie2;
104
-
105
- var camelize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
106
- var keys = Object.keys((_jsonschema$propertie2 = jsonschema.properties) !== null && _jsonschema$propertie2 !== void 0 ? _jsonschema$propertie2 : {});
107
- if (!keys.length) return;
108
- var typedParams = keys.map(function (prop) {
109
- var _getPropertyType = (0, _types2.getPropertyType)(jsonschema, prop),
110
- type = _getPropertyType.type,
111
- optional = _getPropertyType.optional;
112
-
113
- return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional);
114
- });
115
- var params = keys.map(function (prop) {
116
- 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);
117
- });
118
- var obj = t.objectPattern((0, _toConsumableArray2["default"])(params));
119
- obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral((0, _toConsumableArray2["default"])(typedParams)));
120
- return obj;
121
- };
122
-
123
102
  var createPropertyFunctionWithObjectParamsForPartial = function createPropertyFunctionWithObjectParamsForPartial(methodName, responseType, jsonschema) {
124
- var obj = createTypedObjectParams(jsonschema);
103
+ var obj = (0, _types2.createTypedObjectParams)(jsonschema);
125
104
  var fixedParams = [// identifier('fee', t.tsTypeAnnotation(
126
105
  // t.tsUnionType(
127
106
  // [
@@ -34,6 +34,6 @@ var printCode = function printCode(ast) {
34
34
 
35
35
 
36
36
  it('createReactQueryHooks', function () {
37
- printCode(t.program((0, _reactQuery.createReactQueryHooks)(_query_msg["default"], 'Sg721', 'Sg721QueryClient')));
37
+ expectCode(t.program((0, _reactQuery.createReactQueryHooks)(_query_msg["default"], 'Sg721', 'Sg721QueryClient')));
38
38
  expectCode(t.program((0, _reactQuery.createReactQueryHooks)(_query_msg["default"], 'Sg721', 'Sg721QueryClient')));
39
39
  });
@@ -7,12 +7,18 @@ var _typeof = require("@babel/runtime/helpers/typeof");
7
7
  Object.defineProperty(exports, "__esModule", {
8
8
  value: true
9
9
  });
10
- exports.getType = exports.getPropertyType = void 0;
10
+ exports.getType = exports.getPropertyType = exports.createTypedObjectParams = void 0;
11
+
12
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
11
13
 
12
14
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
13
15
 
14
16
  var t = _interopRequireWildcard(require("@babel/types"));
15
17
 
18
+ var _case = require("case");
19
+
20
+ var _babel = require("./babel");
21
+
16
22
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
17
23
 
18
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; }
@@ -23,7 +29,7 @@ var getTypeFromRef = function getTypeFromRef($ref) {
23
29
  return t.tsTypeReference(t.identifier('Binary'));
24
30
 
25
31
  default:
26
- if ($ref.startsWith('#/definitions/')) {
32
+ if ($ref !== null && $ref !== void 0 && $ref.startsWith('#/definitions/')) {
27
33
  return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', '')));
28
34
  }
29
35
 
@@ -49,6 +55,8 @@ var getType = function getType(type) {
49
55
 
50
56
  case 'integer':
51
57
  return t.tsNumberKeyword();
58
+ // case 'object':
59
+ // return t.tsObjectKeyword();
52
60
 
53
61
  default:
54
62
  throw new Error('contact maintainers [unknown type]: ' + type);
@@ -63,7 +71,7 @@ var getPropertyType = function getPropertyType(schema, prop) {
63
71
  var props = (_schema$properties = schema.properties) !== null && _schema$properties !== void 0 ? _schema$properties : {};
64
72
  var info = props[prop];
65
73
  var type = null;
66
- var optional = (_schema$required = schema.required) === null || _schema$required === void 0 ? void 0 : _schema$required.includes(prop);
74
+ var optional = !((_schema$required = schema.required) !== null && _schema$required !== void 0 && _schema$required.includes(prop));
67
75
 
68
76
  if (info.allOf && info.allOf.length === 1) {
69
77
  info = info.allOf[0];
@@ -84,10 +92,19 @@ var getPropertyType = function getPropertyType(schema, prop) {
84
92
  nullType = _info$anyOf[1];
85
93
 
86
94
  if ((nullType === null || nullType === void 0 ? void 0 : nullType.type) !== 'null') {
87
- throw new Error('case not handled by transpiler. contact maintainers.');
95
+ throw new Error('[nullableType.type]: case not handled by transpiler. contact maintainers.');
96
+ }
97
+
98
+ if (!(nullableType !== null && nullableType !== void 0 && nullableType.$ref)) {
99
+ if (nullableType.title) {
100
+ type = t.tsTypeReference(t.identifier(nullableType.title));
101
+ } else {
102
+ throw new Error('[nullableType.title] case not handled by transpiler. contact maintainers.');
103
+ }
104
+ } else {
105
+ type = getTypeFromRef(nullableType === null || nullableType === void 0 ? void 0 : nullableType.$ref);
88
106
  }
89
107
 
90
- type = getTypeFromRef(nullableType === null || nullableType === void 0 ? void 0 : nullableType.$ref);
91
108
  optional = true;
92
109
  }
93
110
 
@@ -95,8 +112,12 @@ var getPropertyType = function getPropertyType(schema, prop) {
95
112
  if (info.type === 'array') {
96
113
  if (info.items.$ref) {
97
114
  type = getArrayTypeFromRef(info.items.$ref);
98
- } else {
115
+ } else if (info.items.title) {
116
+ type = t.tsArrayType(t.tsTypeReference(t.identifier(info.items.title)));
117
+ } else if (info.items.type) {
99
118
  type = getArrayTypeFromType(info.items.type);
119
+ } else {
120
+ throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
100
121
  }
101
122
  } else {
102
123
  type = getType(info.type);
@@ -140,4 +161,80 @@ var getPropertyType = function getPropertyType(schema, prop) {
140
161
  };
141
162
  };
142
163
 
143
- exports.getPropertyType = getPropertyType;
164
+ exports.getPropertyType = getPropertyType;
165
+
166
+ var createTypedObjectParams = function createTypedObjectParams(jsonschema) {
167
+ var _jsonschema$propertie;
168
+
169
+ var camelize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
170
+ var keys = Object.keys((_jsonschema$propertie = jsonschema.properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {});
171
+ if (!keys.length) return;
172
+ var typedParams = keys.map(function (prop) {
173
+ if (jsonschema.properties[prop].type === 'object') {
174
+ if (jsonschema.properties[prop].title) {
175
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(jsonschema.properties[prop].title))));
176
+ } else {
177
+ throw new Error('createTypedObjectParams() contact maintainer');
178
+ }
179
+ }
180
+
181
+ if (Array.isArray(jsonschema.properties[prop].allOf)) {
182
+ var _jsonschema$required;
183
+
184
+ var isOptional = !((_jsonschema$required = jsonschema.required) !== null && _jsonschema$required !== void 0 && _jsonschema$required.includes(prop));
185
+ var unionTypes = jsonschema.properties[prop].allOf.map(function (el) {
186
+ if (el.title) return el.title;
187
+ return el.type;
188
+ });
189
+ var uniqUnionTypes = (0, _toConsumableArray2["default"])(new Set(unionTypes));
190
+
191
+ if (uniqUnionTypes.length === 1) {
192
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(uniqUnionTypes[0]))), isOptional);
193
+ } else {
194
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(function (typ) {
195
+ return t.tsTypeReference(t.identifier(typ));
196
+ }))), isOptional);
197
+ }
198
+ } else if (Array.isArray(jsonschema.properties[prop].oneOf)) {
199
+ var _jsonschema$required2;
200
+
201
+ var _isOptional = !((_jsonschema$required2 = jsonschema.required) !== null && _jsonschema$required2 !== void 0 && _jsonschema$required2.includes(prop));
202
+
203
+ var _unionTypes = jsonschema.properties[prop].oneOf.map(function (el) {
204
+ if (el.title) return el.title;
205
+ return el.type;
206
+ });
207
+
208
+ var _uniqUnionTypes = (0, _toConsumableArray2["default"])(new Set(_unionTypes));
209
+
210
+ if (_uniqUnionTypes.length === 1) {
211
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(_uniqUnionTypes[0]))), _isOptional);
212
+ } else {
213
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(_uniqUnionTypes.map(function (typ) {
214
+ return t.tsTypeReference(t.identifier(typ));
215
+ }))), _isOptional);
216
+ }
217
+ }
218
+
219
+ try {
220
+ getPropertyType(jsonschema, prop);
221
+ } catch (e) {
222
+ console.log(e);
223
+ console.log(jsonschema, prop);
224
+ }
225
+
226
+ var _getPropertyType = getPropertyType(jsonschema, prop),
227
+ type = _getPropertyType.type,
228
+ optional = _getPropertyType.optional;
229
+
230
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional);
231
+ });
232
+ var params = keys.map(function (prop) {
233
+ 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);
234
+ });
235
+ var obj = t.objectPattern((0, _toConsumableArray2["default"])(params));
236
+ obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral((0, _toConsumableArray2["default"])(typedParams)));
237
+ return obj;
238
+ };
239
+
240
+ exports.createTypedObjectParams = createTypedObjectParams;
package/main/wasm.js CHANGED
@@ -7,7 +7,7 @@ var _typeof = require("@babel/runtime/helpers/typeof");
7
7
  Object.defineProperty(exports, "__esModule", {
8
8
  value: true
9
9
  });
10
- exports.createWasmQueryMethod = exports.createWasmExecMethod = exports.createTypedObjectParams = exports.createTypeOrInterface = exports.createTypeInterface = exports.createQueryInterface = exports.createQueryClass = exports.createPropertyFunctionWithObjectParamsForExec = exports.createPropertyFunctionWithObjectParams = exports.createExecuteInterface = exports.createExecuteClass = void 0;
10
+ exports.createWasmQueryMethod = exports.createWasmExecMethod = 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
 
@@ -32,7 +32,7 @@ var createWasmQueryMethod = function createWasmQueryMethod(jsonschema) {
32
32
  var methodName = (0, _case.camel)(underscoreName);
33
33
  var responseType = (0, _case.pascal)("".concat(methodName, "Response"));
34
34
  var properties = (_jsonschema$propertie = jsonschema.properties[underscoreName].properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {};
35
- var obj = createTypedObjectParams(jsonschema.properties[underscoreName]);
35
+ var obj = (0, _types2.createTypedObjectParams)(jsonschema.properties[underscoreName]);
36
36
  var args = Object.keys(properties).map(function (prop) {
37
37
  return t.objectProperty(t.identifier(prop), t.identifier((0, _case.camel)(prop)), false, true);
38
38
  });
@@ -67,7 +67,7 @@ var createWasmExecMethod = function createWasmExecMethod(jsonschema) {
67
67
  var underscoreName = Object.keys(jsonschema.properties)[0];
68
68
  var methodName = (0, _case.camel)(underscoreName);
69
69
  var properties = (_jsonschema$propertie2 = jsonschema.properties[underscoreName].properties) !== null && _jsonschema$propertie2 !== void 0 ? _jsonschema$propertie2 : {};
70
- var obj = createTypedObjectParams(jsonschema.properties[underscoreName]);
70
+ var obj = (0, _types2.createTypedObjectParams)(jsonschema.properties[underscoreName]);
71
71
  var args = Object.keys(properties).map(function (prop) {
72
72
  return t.objectProperty(t.identifier(prop), t.identifier((0, _case.camel)(prop)), false, prop === (0, _case.camel)(prop));
73
73
  });
@@ -121,31 +121,8 @@ var createExecuteInterface = function createExecuteInterface(className, extendsC
121
121
 
122
122
  exports.createExecuteInterface = createExecuteInterface;
123
123
 
124
- var createTypedObjectParams = function createTypedObjectParams(jsonschema) {
125
- var _jsonschema$propertie3;
126
-
127
- var camelize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
128
- var keys = Object.keys((_jsonschema$propertie3 = jsonschema.properties) !== null && _jsonschema$propertie3 !== void 0 ? _jsonschema$propertie3 : {});
129
- if (!keys.length) return;
130
- var typedParams = keys.map(function (prop) {
131
- var _getPropertyType = (0, _types2.getPropertyType)(jsonschema, prop),
132
- type = _getPropertyType.type,
133
- optional = _getPropertyType.optional;
134
-
135
- return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional);
136
- });
137
- var params = keys.map(function (prop) {
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);
139
- });
140
- var obj = t.objectPattern((0, _toConsumableArray2["default"])(params));
141
- obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral((0, _toConsumableArray2["default"])(typedParams)));
142
- return obj;
143
- };
144
-
145
- exports.createTypedObjectParams = createTypedObjectParams;
146
-
147
124
  var createPropertyFunctionWithObjectParams = function createPropertyFunctionWithObjectParams(methodName, responseType, jsonschema) {
148
- var obj = createTypedObjectParams(jsonschema);
125
+ var obj = (0, _types2.createTypedObjectParams)(jsonschema);
149
126
  var func = {
150
127
  type: 'TSFunctionType',
151
128
  typeAnnotation: (0, _utils.promiseTypeAnnotation)(responseType),
@@ -157,7 +134,7 @@ var createPropertyFunctionWithObjectParams = function createPropertyFunctionWith
157
134
  exports.createPropertyFunctionWithObjectParams = createPropertyFunctionWithObjectParams;
158
135
 
159
136
  var createPropertyFunctionWithObjectParamsForExec = function createPropertyFunctionWithObjectParamsForExec(methodName, responseType, jsonschema) {
160
- var obj = createTypedObjectParams(jsonschema);
137
+ var obj = (0, _types2.createTypedObjectParams)(jsonschema);
161
138
  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)];
162
139
  var func = {
163
140
  type: 'TSFunctionType',
@@ -182,16 +159,20 @@ var createQueryInterface = function createQueryInterface(className, queryMsg) {
182
159
  exports.createQueryInterface = createQueryInterface;
183
160
 
184
161
  var createTypeOrInterface = function createTypeOrInterface(Type, jsonschema) {
185
- var _jsonschema$propertie4;
162
+ var _jsonschema$propertie3;
186
163
 
187
164
  if (jsonschema.type !== 'object') {
165
+ if (!jsonschema.type) {
166
+ return t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(Type), null, t.tsTypeReference(t.identifier(jsonschema.title))));
167
+ }
168
+
188
169
  return t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(Type), null, (0, _types2.getType)(jsonschema.type)));
189
170
  }
190
171
 
191
- var props = Object.keys((_jsonschema$propertie4 = jsonschema.properties) !== null && _jsonschema$propertie4 !== void 0 ? _jsonschema$propertie4 : {}).map(function (prop) {
192
- var _getPropertyType2 = (0, _types2.getPropertyType)(jsonschema, prop),
193
- type = _getPropertyType2.type,
194
- optional = _getPropertyType2.optional;
172
+ var props = Object.keys((_jsonschema$propertie3 = jsonschema.properties) !== null && _jsonschema$propertie3 !== void 0 ? _jsonschema$propertie3 : {}).map(function (prop) {
173
+ var _getPropertyType = (0, _types2.getPropertyType)(jsonschema, prop),
174
+ type = _getPropertyType.type,
175
+ optional = _getPropertyType.optional;
195
176
 
196
177
  return (0, _babel.propertySignature)((0, _case.camel)(prop), t.tsTypeAnnotation(type), optional);
197
178
  });
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ var _generator = _interopRequireDefault(require("@babel/generator"));
6
+
7
+ var _execute_msg_for__empty = _interopRequireDefault(require("./../../../__fixtures__/sg721/execute_msg_for__empty.json"));
8
+
9
+ var _wasm = require("./wasm");
10
+
11
+ var expectCode = function expectCode(ast) {
12
+ expect((0, _generator["default"])(ast).code).toMatchSnapshot();
13
+ };
14
+
15
+ var printCode = function printCode(ast) {
16
+ console.log((0, _generator["default"])(ast).code);
17
+ };
18
+
19
+ it('execute_msg_for__empty', function () {
20
+ expectCode((0, _wasm.createTypeInterface)(_execute_msg_for__empty["default"]));
21
+ });
22
+ it('query classes', function () {
23
+ expectCode((0, _wasm.createQueryClass)('SG721QueryClient', 'SG721ReadOnlyInstance', _execute_msg_for__empty["default"]));
24
+ });
25
+ it('execute classes array types', function () {
26
+ expectCode((0, _wasm.createExecuteClass)('SG721Client', 'SG721Instance', null, _execute_msg_for__empty["default"]));
27
+ });
28
+ it('execute interfaces no extends', function () {
29
+ expectCode((0, _wasm.createExecuteInterface)('SG721Instance', null, _execute_msg_for__empty["default"]));
30
+ });
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ var _generator = _interopRequireDefault(require("@babel/generator"));
6
+
7
+ var _cosmos_msg_for__empty = _interopRequireDefault(require("./../../../__fixtures__/vectis/govec/cosmos_msg_for__empty.json"));
8
+
9
+ var _execute_msg_for__empty = _interopRequireDefault(require("./../../../__fixtures__/vectis/govec/execute_msg_for__empty.json"));
10
+
11
+ var _can_execute_relay_response = _interopRequireDefault(require("./../../../__fixtures__/vectis/govec/can_execute_relay_response.json"));
12
+
13
+ var _info_response = _interopRequireDefault(require("./../../../__fixtures__/vectis/govec/info_response.json"));
14
+
15
+ var _relay_transaction = _interopRequireDefault(require("./../../../__fixtures__/vectis/govec/relay_transaction.json"));
16
+
17
+ var _wasm = require("./wasm");
18
+
19
+ var expectCode = function expectCode(ast) {
20
+ expect((0, _generator["default"])(ast).code).toMatchSnapshot();
21
+ };
22
+
23
+ var printCode = function printCode(ast) {
24
+ console.log((0, _generator["default"])(ast).code);
25
+ };
26
+
27
+ it('cosmos_msg_for__empty', function () {
28
+ expectCode((0, _wasm.createTypeInterface)(_cosmos_msg_for__empty["default"]));
29
+ });
30
+ it('execute_msg_for__empty', function () {
31
+ expectCode((0, _wasm.createTypeInterface)(_execute_msg_for__empty["default"]));
32
+ });
33
+ it('can_execute_relay_response', function () {
34
+ expectCode((0, _wasm.createTypeInterface)(_can_execute_relay_response["default"]));
35
+ });
36
+ it('info_response', function () {
37
+ expectCode((0, _wasm.createTypeInterface)(_info_response["default"]));
38
+ });
39
+ it('relay_transaction', function () {
40
+ expectCode((0, _wasm.createTypeInterface)(_relay_transaction["default"]));
41
+ });
42
+ it('query classes', function () {
43
+ expectCode((0, _wasm.createQueryClass)('SG721QueryClient', 'SG721ReadOnlyInstance', _cosmos_msg_for__empty["default"]));
44
+ });
45
+ it('query classes', function () {
46
+ expectCode((0, _wasm.createQueryClass)('SG721QueryClient', 'SG721ReadOnlyInstance', _execute_msg_for__empty["default"]));
47
+ });
48
+ it('execute classes array types', function () {
49
+ expectCode((0, _wasm.createExecuteClass)('SG721Client', 'SG721Instance', null, _execute_msg_for__empty["default"]));
50
+ });
51
+ it('execute interfaces no extends', function () {
52
+ expectCode((0, _wasm.createExecuteInterface)('SG721Instance', null, _execute_msg_for__empty["default"]));
53
+ });
@@ -1,8 +1,8 @@
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
+ import { createTypedObjectParams } from './utils/types';
5
+ import { identifier, tsTypeOperator } from './utils/babel';
6
6
 
7
7
  const createWasmExecMethodPartial = jsonschema => {
8
8
  const underscoreName = Object.keys(jsonschema.properties)[0];
@@ -66,24 +66,6 @@ export const createFromPartialInterface = (className, execMsg) => {
66
66
  t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods])));
67
67
  }; // MARKED AS NOT DRY
68
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
69
  const createPropertyFunctionWithObjectParamsForPartial = (methodName, responseType, jsonschema) => {
88
70
  const obj = createTypedObjectParams(jsonschema);
89
71
  const fixedParams = [// identifier('fee', t.tsTypeAnnotation(
@@ -21,6 +21,6 @@ const printCode = ast => {
21
21
 
22
22
 
23
23
  it('createReactQueryHooks', () => {
24
- printCode(t.program(createReactQueryHooks(query_msg, 'Sg721', 'Sg721QueryClient')));
24
+ expectCode(t.program(createReactQueryHooks(query_msg, 'Sg721', 'Sg721QueryClient')));
25
25
  expectCode(t.program(createReactQueryHooks(query_msg, 'Sg721', 'Sg721QueryClient')));
26
26
  });
@@ -1,4 +1,6 @@
1
1
  import * as t from '@babel/types';
2
+ import { camel } from 'case';
3
+ import { propertySignature } from './babel';
2
4
 
3
5
  const getTypeFromRef = $ref => {
4
6
  switch ($ref) {
@@ -6,7 +8,7 @@ const getTypeFromRef = $ref => {
6
8
  return t.tsTypeReference(t.identifier('Binary'));
7
9
 
8
10
  default:
9
- if ($ref.startsWith('#/definitions/')) {
11
+ if ($ref?.startsWith('#/definitions/')) {
10
12
  return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', '')));
11
13
  }
12
14
 
@@ -32,6 +34,8 @@ export const getType = type => {
32
34
 
33
35
  case 'integer':
34
36
  return t.tsNumberKeyword();
37
+ // case 'object':
38
+ // return t.tsObjectKeyword();
35
39
 
36
40
  default:
37
41
  throw new Error('contact maintainers [unknown type]: ' + type);
@@ -41,7 +45,7 @@ export const getPropertyType = (schema, prop) => {
41
45
  const props = schema.properties ?? {};
42
46
  let info = props[prop];
43
47
  let type = null;
44
- let optional = schema.required?.includes(prop);
48
+ let optional = !schema.required?.includes(prop);
45
49
 
46
50
  if (info.allOf && info.allOf.length === 1) {
47
51
  info = info.allOf[0];
@@ -60,10 +64,19 @@ export const getPropertyType = (schema, prop) => {
60
64
  const [nullableType, nullType] = info.anyOf;
61
65
 
62
66
  if (nullType?.type !== 'null') {
63
- throw new Error('case not handled by transpiler. contact maintainers.');
67
+ throw new Error('[nullableType.type]: case not handled by transpiler. contact maintainers.');
68
+ }
69
+
70
+ if (!nullableType?.$ref) {
71
+ if (nullableType.title) {
72
+ type = t.tsTypeReference(t.identifier(nullableType.title));
73
+ } else {
74
+ throw new Error('[nullableType.title] case not handled by transpiler. contact maintainers.');
75
+ }
76
+ } else {
77
+ type = getTypeFromRef(nullableType?.$ref);
64
78
  }
65
79
 
66
- type = getTypeFromRef(nullableType?.$ref);
67
80
  optional = true;
68
81
  }
69
82
 
@@ -71,8 +84,12 @@ export const getPropertyType = (schema, prop) => {
71
84
  if (info.type === 'array') {
72
85
  if (info.items.$ref) {
73
86
  type = getArrayTypeFromRef(info.items.$ref);
74
- } else {
87
+ } else if (info.items.title) {
88
+ type = t.tsArrayType(t.tsTypeReference(t.identifier(info.items.title)));
89
+ } else if (info.items.type) {
75
90
  type = getArrayTypeFromType(info.items.type);
91
+ } else {
92
+ throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
76
93
  }
77
94
  } else {
78
95
  type = getType(info.type);
@@ -112,4 +129,64 @@ export const getPropertyType = (schema, prop) => {
112
129
  type,
113
130
  optional
114
131
  };
132
+ };
133
+ export const createTypedObjectParams = (jsonschema, camelize = true) => {
134
+ const keys = Object.keys(jsonschema.properties ?? {});
135
+ if (!keys.length) return;
136
+ const typedParams = keys.map(prop => {
137
+ if (jsonschema.properties[prop].type === 'object') {
138
+ if (jsonschema.properties[prop].title) {
139
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(jsonschema.properties[prop].title))));
140
+ } else {
141
+ throw new Error('createTypedObjectParams() contact maintainer');
142
+ }
143
+ }
144
+
145
+ if (Array.isArray(jsonschema.properties[prop].allOf)) {
146
+ const isOptional = !jsonschema.required?.includes(prop);
147
+ const unionTypes = jsonschema.properties[prop].allOf.map(el => {
148
+ if (el.title) return el.title;
149
+ return el.type;
150
+ });
151
+ const uniqUnionTypes = [...new Set(unionTypes)];
152
+
153
+ if (uniqUnionTypes.length === 1) {
154
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(uniqUnionTypes[0]))), isOptional);
155
+ } else {
156
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(typ))))), isOptional);
157
+ }
158
+ } else if (Array.isArray(jsonschema.properties[prop].oneOf)) {
159
+ const isOptional = !jsonschema.required?.includes(prop);
160
+ const unionTypes = jsonschema.properties[prop].oneOf.map(el => {
161
+ if (el.title) return el.title;
162
+ return el.type;
163
+ });
164
+ const uniqUnionTypes = [...new Set(unionTypes)];
165
+
166
+ if (uniqUnionTypes.length === 1) {
167
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(uniqUnionTypes[0]))), isOptional);
168
+ } else {
169
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(typ))))), isOptional);
170
+ }
171
+ }
172
+
173
+ try {
174
+ getPropertyType(jsonschema, prop);
175
+ } catch (e) {
176
+ console.log(e);
177
+ console.log(jsonschema, prop);
178
+ }
179
+
180
+ const {
181
+ type,
182
+ optional
183
+ } = getPropertyType(jsonschema, prop);
184
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
185
+ });
186
+ const params = keys.map(prop => {
187
+ return t.objectProperty(camelize ? t.identifier(camel(prop)) : t.identifier(prop), camelize ? t.identifier(camel(prop)) : t.identifier(prop), false, true);
188
+ });
189
+ const obj = t.objectPattern([...params]);
190
+ obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral([...typedParams]));
191
+ return obj;
115
192
  };
package/module/wasm.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as t from '@babel/types';
2
2
  import { camel, pascal } from 'case';
3
3
  import { bindMethod, typedIdentifier, promiseTypeAnnotation, classDeclaration, classProperty, arrowFunctionExpression, getMessageProperties } from './utils';
4
- import { getPropertyType, getType } from './utils/types';
4
+ import { getPropertyType, getType, createTypedObjectParams } from './utils/types';
5
5
  import { identifier, tsTypeOperator, propertySignature } from './utils/babel';
6
6
  export const createWasmQueryMethod = jsonschema => {
7
7
  const underscoreName = Object.keys(jsonschema.properties)[0];
@@ -72,23 +72,6 @@ export const createExecuteInterface = (className, extendsClassName, execMsg) =>
72
72
  t.tSPropertySignature(t.identifier('contractAddress'), t.tsTypeAnnotation(t.tsStringKeyword())), // contract address
73
73
  t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods])));
74
74
  };
75
- export const createTypedObjectParams = (jsonschema, camelize = true) => {
76
- const keys = Object.keys(jsonschema.properties ?? {});
77
- if (!keys.length) return;
78
- const typedParams = keys.map(prop => {
79
- const {
80
- type,
81
- optional
82
- } = getPropertyType(jsonschema, prop);
83
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
84
- });
85
- const params = keys.map(prop => {
86
- return t.objectProperty(camelize ? t.identifier(camel(prop)) : t.identifier(prop), camelize ? t.identifier(camel(prop)) : t.identifier(prop), false, true);
87
- });
88
- const obj = t.objectPattern([...params]);
89
- obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral([...typedParams]));
90
- return obj;
91
- };
92
75
  export const createPropertyFunctionWithObjectParams = (methodName, responseType, jsonschema) => {
93
76
  const obj = createTypedObjectParams(jsonschema);
94
77
  const func = {
@@ -119,6 +102,10 @@ export const createQueryInterface = (className, queryMsg) => {
119
102
  };
120
103
  export const createTypeOrInterface = (Type, jsonschema) => {
121
104
  if (jsonschema.type !== 'object') {
105
+ if (!jsonschema.type) {
106
+ return t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(Type), null, t.tsTypeReference(t.identifier(jsonschema.title))));
107
+ }
108
+
122
109
  return t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(Type), null, getType(jsonschema.type)));
123
110
  }
124
111
 
@@ -0,0 +1,24 @@
1
+ import generate from '@babel/generator';
2
+ import execute_msg_for__empty from './../../../__fixtures__/sg721/execute_msg_for__empty.json';
3
+ import { createQueryClass, createExecuteClass, createExecuteInterface, createTypeInterface } from './wasm';
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_msg_for__empty', () => {
14
+ expectCode(createTypeInterface(execute_msg_for__empty));
15
+ });
16
+ it('query classes', () => {
17
+ expectCode(createQueryClass('SG721QueryClient', 'SG721ReadOnlyInstance', execute_msg_for__empty));
18
+ });
19
+ it('execute classes array types', () => {
20
+ expectCode(createExecuteClass('SG721Client', 'SG721Instance', null, execute_msg_for__empty));
21
+ });
22
+ it('execute interfaces no extends', () => {
23
+ expectCode(createExecuteInterface('SG721Instance', null, execute_msg_for__empty));
24
+ });
@@ -0,0 +1,43 @@
1
+ import generate from '@babel/generator';
2
+ import cosmos_msg_for__empty from './../../../__fixtures__/vectis/govec/cosmos_msg_for__empty.json';
3
+ import execute_msg_for__empty from './../../../__fixtures__/vectis/govec/execute_msg_for__empty.json';
4
+ import can_execute_relay_response from './../../../__fixtures__/vectis/govec/can_execute_relay_response.json';
5
+ import info_response from './../../../__fixtures__/vectis/govec/info_response.json';
6
+ import relay_transaction from './../../../__fixtures__/vectis/govec/relay_transaction.json';
7
+ import { createQueryClass, createExecuteClass, createExecuteInterface, createTypeInterface } from './wasm';
8
+
9
+ const expectCode = ast => {
10
+ expect(generate(ast).code).toMatchSnapshot();
11
+ };
12
+
13
+ const printCode = ast => {
14
+ console.log(generate(ast).code);
15
+ };
16
+
17
+ it('cosmos_msg_for__empty', () => {
18
+ expectCode(createTypeInterface(cosmos_msg_for__empty));
19
+ });
20
+ it('execute_msg_for__empty', () => {
21
+ expectCode(createTypeInterface(execute_msg_for__empty));
22
+ });
23
+ it('can_execute_relay_response', () => {
24
+ expectCode(createTypeInterface(can_execute_relay_response));
25
+ });
26
+ it('info_response', () => {
27
+ expectCode(createTypeInterface(info_response));
28
+ });
29
+ it('relay_transaction', () => {
30
+ expectCode(createTypeInterface(relay_transaction));
31
+ });
32
+ it('query classes', () => {
33
+ expectCode(createQueryClass('SG721QueryClient', 'SG721ReadOnlyInstance', cosmos_msg_for__empty));
34
+ });
35
+ it('query classes', () => {
36
+ expectCode(createQueryClass('SG721QueryClient', 'SG721ReadOnlyInstance', execute_msg_for__empty));
37
+ });
38
+ it('execute classes array types', () => {
39
+ expectCode(createExecuteClass('SG721Client', 'SG721Instance', null, execute_msg_for__empty));
40
+ });
41
+ it('execute interfaces no extends', () => {
42
+ expectCode(createExecuteInterface('SG721Instance', null, execute_msg_for__empty));
43
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.2.6",
3
+ "version": "0.3.2",
4
4
  "description": "CosmWasm TypeScript AST generation",
5
5
  "author": "Dan Lynch <pyramation@gmail.com>",
6
6
  "homepage": "https://github.com/pyramation/cosmwasm-typescript-gen/tree/master/packages/wasm-ast-types#readme",
@@ -85,5 +85,5 @@
85
85
  "ast-stringify": "0.1.0",
86
86
  "case": "1.6.3"
87
87
  },
88
- "gitHead": "9a94ff9a623b4f4d9f8c1f742b15b43eb525295f"
88
+ "gitHead": "bf8f96d8f70a2bdfff2f210dc07c26c14575478b"
89
89
  }