wasm-ast-types 0.2.6 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/main/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,23 +7,39 @@ 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.forEmptyNameFix = 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; }
19
25
 
26
+ var forEmptyNameFix = function forEmptyNameFix(name) {
27
+ if (name.endsWith('For_Empty')) {
28
+ return name.replace(/For_Empty$/, '_for_Empty');
29
+ }
30
+
31
+ return name;
32
+ };
33
+
34
+ exports.forEmptyNameFix = forEmptyNameFix;
35
+
20
36
  var getTypeFromRef = function getTypeFromRef($ref) {
21
37
  switch ($ref) {
22
38
  case '#/definitions/Binary':
23
39
  return t.tsTypeReference(t.identifier('Binary'));
24
40
 
25
41
  default:
26
- if ($ref.startsWith('#/definitions/')) {
42
+ if ($ref !== null && $ref !== void 0 && $ref.startsWith('#/definitions/')) {
27
43
  return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', '')));
28
44
  }
29
45
 
@@ -49,6 +65,8 @@ var getType = function getType(type) {
49
65
 
50
66
  case 'integer':
51
67
  return t.tsNumberKeyword();
68
+ // case 'object':
69
+ // return t.tsObjectKeyword();
52
70
 
53
71
  default:
54
72
  throw new Error('contact maintainers [unknown type]: ' + type);
@@ -63,7 +81,7 @@ var getPropertyType = function getPropertyType(schema, prop) {
63
81
  var props = (_schema$properties = schema.properties) !== null && _schema$properties !== void 0 ? _schema$properties : {};
64
82
  var info = props[prop];
65
83
  var type = null;
66
- var optional = (_schema$required = schema.required) === null || _schema$required === void 0 ? void 0 : _schema$required.includes(prop);
84
+ var optional = !((_schema$required = schema.required) !== null && _schema$required !== void 0 && _schema$required.includes(prop));
67
85
 
68
86
  if (info.allOf && info.allOf.length === 1) {
69
87
  info = info.allOf[0];
@@ -84,10 +102,19 @@ var getPropertyType = function getPropertyType(schema, prop) {
84
102
  nullType = _info$anyOf[1];
85
103
 
86
104
  if ((nullType === null || nullType === void 0 ? void 0 : nullType.type) !== 'null') {
87
- throw new Error('case not handled by transpiler. contact maintainers.');
105
+ throw new Error('[nullableType.type]: case not handled by transpiler. contact maintainers.');
106
+ }
107
+
108
+ if (!(nullableType !== null && nullableType !== void 0 && nullableType.$ref)) {
109
+ if (nullableType.title) {
110
+ type = t.tsTypeReference(t.identifier(nullableType.title));
111
+ } else {
112
+ throw new Error('[nullableType.title] case not handled by transpiler. contact maintainers.');
113
+ }
114
+ } else {
115
+ type = getTypeFromRef(nullableType === null || nullableType === void 0 ? void 0 : nullableType.$ref);
88
116
  }
89
117
 
90
- type = getTypeFromRef(nullableType === null || nullableType === void 0 ? void 0 : nullableType.$ref);
91
118
  optional = true;
92
119
  }
93
120
 
@@ -95,8 +122,12 @@ var getPropertyType = function getPropertyType(schema, prop) {
95
122
  if (info.type === 'array') {
96
123
  if (info.items.$ref) {
97
124
  type = getArrayTypeFromRef(info.items.$ref);
98
- } else {
125
+ } else if (info.items.title) {
126
+ type = t.tsArrayType(t.tsTypeReference(t.identifier(info.items.title)));
127
+ } else if (info.items.type) {
99
128
  type = getArrayTypeFromType(info.items.type);
129
+ } else {
130
+ throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
100
131
  }
101
132
  } else {
102
133
  type = getType(info.type);
@@ -140,4 +171,62 @@ var getPropertyType = function getPropertyType(schema, prop) {
140
171
  };
141
172
  };
142
173
 
143
- exports.getPropertyType = getPropertyType;
174
+ exports.getPropertyType = getPropertyType;
175
+
176
+ var createTypedObjectParams = function createTypedObjectParams(jsonschema) {
177
+ var _jsonschema$propertie;
178
+
179
+ var camelize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
180
+ var keys = Object.keys((_jsonschema$propertie = jsonschema.properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {});
181
+ if (!keys.length) return;
182
+ var typedParams = keys.map(function (prop) {
183
+ if (jsonschema.properties[prop].type === 'object') {
184
+ if (jsonschema.properties[prop].title) {
185
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(forEmptyNameFix(jsonschema.properties[prop].title)))));
186
+ } else {
187
+ throw new Error('createTypedObjectParams() contact maintainer');
188
+ }
189
+ }
190
+
191
+ if (Array.isArray(jsonschema.properties[prop].allOf)) {
192
+ var _jsonschema$required;
193
+
194
+ var allOf = JSON.stringify(jsonschema.properties[prop].allOf, null, 2);
195
+ var isOptional = !((_jsonschema$required = jsonschema.required) !== null && _jsonschema$required !== void 0 && _jsonschema$required.includes(prop));
196
+ var unionTypes = jsonschema.properties[prop].allOf.map(function (el) {
197
+ if (el.title) return el.title;
198
+ return el.type;
199
+ });
200
+ var uniqUnionTypes = (0, _toConsumableArray2["default"])(new Set(unionTypes));
201
+
202
+ if (uniqUnionTypes.length === 1) {
203
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(forEmptyNameFix(uniqUnionTypes[0])))), isOptional);
204
+ } else {
205
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(function (typ) {
206
+ return t.tsTypeReference(t.identifier(forEmptyNameFix(typ)));
207
+ }))), isOptional);
208
+ }
209
+ }
210
+
211
+ try {
212
+ getPropertyType(jsonschema, prop);
213
+ } catch (e) {
214
+ console.log(e);
215
+ console.log(jsonschema, prop);
216
+ }
217
+
218
+ var _getPropertyType = getPropertyType(jsonschema, prop),
219
+ type = _getPropertyType.type,
220
+ optional = _getPropertyType.optional;
221
+
222
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional);
223
+ });
224
+ var params = keys.map(function (prop) {
225
+ 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);
226
+ });
227
+ var obj = t.objectPattern((0, _toConsumableArray2["default"])(params));
228
+ obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral((0, _toConsumableArray2["default"])(typedParams)));
229
+ return obj;
230
+ };
231
+
232
+ 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((0, _types2.forEmptyNameFix)(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,38 @@
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/cosmos_msg_for__empty.json"));
8
+
9
+ var _execute_msg_for__empty = _interopRequireDefault(require("./../../../__fixtures__/vectis/execute_msg_for__empty.json"));
10
+
11
+ var _wasm = require("./wasm");
12
+
13
+ var expectCode = function expectCode(ast) {
14
+ expect((0, _generator["default"])(ast).code).toMatchSnapshot();
15
+ };
16
+
17
+ var printCode = function printCode(ast) {
18
+ console.log((0, _generator["default"])(ast).code);
19
+ };
20
+
21
+ it('cosmos_msg_for__empty', function () {
22
+ expectCode((0, _wasm.createTypeInterface)(_cosmos_msg_for__empty["default"]));
23
+ });
24
+ it('execute_msg_for__empty', function () {
25
+ expectCode((0, _wasm.createTypeInterface)(_execute_msg_for__empty["default"]));
26
+ });
27
+ it('query classes', function () {
28
+ expectCode((0, _wasm.createQueryClass)('SG721QueryClient', 'SG721ReadOnlyInstance', _cosmos_msg_for__empty["default"]));
29
+ });
30
+ it('query classes', function () {
31
+ expectCode((0, _wasm.createQueryClass)('SG721QueryClient', 'SG721ReadOnlyInstance', _execute_msg_for__empty["default"]));
32
+ });
33
+ it('execute classes array types', function () {
34
+ expectCode((0, _wasm.createExecuteClass)('SG721Client', 'SG721Instance', null, _execute_msg_for__empty["default"]));
35
+ });
36
+ it('execute interfaces no extends', function () {
37
+ expectCode((0, _wasm.createExecuteInterface)('SG721Instance', null, _execute_msg_for__empty["default"]));
38
+ });
@@ -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,13 @@
1
1
  import * as t from '@babel/types';
2
+ import { camel } from 'case';
3
+ import { propertySignature } from './babel';
4
+ export const forEmptyNameFix = name => {
5
+ if (name.endsWith('For_Empty')) {
6
+ return name.replace(/For_Empty$/, '_for_Empty');
7
+ }
8
+
9
+ return name;
10
+ };
2
11
 
3
12
  const getTypeFromRef = $ref => {
4
13
  switch ($ref) {
@@ -6,7 +15,7 @@ const getTypeFromRef = $ref => {
6
15
  return t.tsTypeReference(t.identifier('Binary'));
7
16
 
8
17
  default:
9
- if ($ref.startsWith('#/definitions/')) {
18
+ if ($ref?.startsWith('#/definitions/')) {
10
19
  return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', '')));
11
20
  }
12
21
 
@@ -32,6 +41,8 @@ export const getType = type => {
32
41
 
33
42
  case 'integer':
34
43
  return t.tsNumberKeyword();
44
+ // case 'object':
45
+ // return t.tsObjectKeyword();
35
46
 
36
47
  default:
37
48
  throw new Error('contact maintainers [unknown type]: ' + type);
@@ -41,7 +52,7 @@ export const getPropertyType = (schema, prop) => {
41
52
  const props = schema.properties ?? {};
42
53
  let info = props[prop];
43
54
  let type = null;
44
- let optional = schema.required?.includes(prop);
55
+ let optional = !schema.required?.includes(prop);
45
56
 
46
57
  if (info.allOf && info.allOf.length === 1) {
47
58
  info = info.allOf[0];
@@ -60,10 +71,19 @@ export const getPropertyType = (schema, prop) => {
60
71
  const [nullableType, nullType] = info.anyOf;
61
72
 
62
73
  if (nullType?.type !== 'null') {
63
- throw new Error('case not handled by transpiler. contact maintainers.');
74
+ throw new Error('[nullableType.type]: case not handled by transpiler. contact maintainers.');
75
+ }
76
+
77
+ if (!nullableType?.$ref) {
78
+ if (nullableType.title) {
79
+ type = t.tsTypeReference(t.identifier(nullableType.title));
80
+ } else {
81
+ throw new Error('[nullableType.title] case not handled by transpiler. contact maintainers.');
82
+ }
83
+ } else {
84
+ type = getTypeFromRef(nullableType?.$ref);
64
85
  }
65
86
 
66
- type = getTypeFromRef(nullableType?.$ref);
67
87
  optional = true;
68
88
  }
69
89
 
@@ -71,8 +91,12 @@ export const getPropertyType = (schema, prop) => {
71
91
  if (info.type === 'array') {
72
92
  if (info.items.$ref) {
73
93
  type = getArrayTypeFromRef(info.items.$ref);
74
- } else {
94
+ } else if (info.items.title) {
95
+ type = t.tsArrayType(t.tsTypeReference(t.identifier(info.items.title)));
96
+ } else if (info.items.type) {
75
97
  type = getArrayTypeFromType(info.items.type);
98
+ } else {
99
+ throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
76
100
  }
77
101
  } else {
78
102
  type = getType(info.type);
@@ -112,4 +136,52 @@ export const getPropertyType = (schema, prop) => {
112
136
  type,
113
137
  optional
114
138
  };
139
+ };
140
+ export const createTypedObjectParams = (jsonschema, camelize = true) => {
141
+ const keys = Object.keys(jsonschema.properties ?? {});
142
+ if (!keys.length) return;
143
+ const typedParams = keys.map(prop => {
144
+ if (jsonschema.properties[prop].type === 'object') {
145
+ if (jsonschema.properties[prop].title) {
146
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(forEmptyNameFix(jsonschema.properties[prop].title)))));
147
+ } else {
148
+ throw new Error('createTypedObjectParams() contact maintainer');
149
+ }
150
+ }
151
+
152
+ if (Array.isArray(jsonschema.properties[prop].allOf)) {
153
+ const allOf = JSON.stringify(jsonschema.properties[prop].allOf, null, 2);
154
+ const isOptional = !jsonschema.required?.includes(prop);
155
+ const unionTypes = jsonschema.properties[prop].allOf.map(el => {
156
+ if (el.title) return el.title;
157
+ return el.type;
158
+ });
159
+ const uniqUnionTypes = [...new Set(unionTypes)];
160
+
161
+ if (uniqUnionTypes.length === 1) {
162
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(forEmptyNameFix(uniqUnionTypes[0])))), isOptional);
163
+ } else {
164
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(forEmptyNameFix(typ)))))), isOptional);
165
+ }
166
+ }
167
+
168
+ try {
169
+ getPropertyType(jsonschema, prop);
170
+ } catch (e) {
171
+ console.log(e);
172
+ console.log(jsonschema, prop);
173
+ }
174
+
175
+ const {
176
+ type,
177
+ optional
178
+ } = getPropertyType(jsonschema, prop);
179
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
180
+ });
181
+ const params = keys.map(prop => {
182
+ return t.objectProperty(camelize ? t.identifier(camel(prop)) : t.identifier(prop), camelize ? t.identifier(camel(prop)) : t.identifier(prop), false, true);
183
+ });
184
+ const obj = t.objectPattern([...params]);
185
+ obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral([...typedParams]));
186
+ return obj;
115
187
  };
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, forEmptyNameFix } 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(forEmptyNameFix(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,31 @@
1
+ import generate from '@babel/generator';
2
+ import cosmos_msg_for__empty from './../../../__fixtures__/vectis/cosmos_msg_for__empty.json';
3
+ import execute_msg_for__empty from './../../../__fixtures__/vectis/execute_msg_for__empty.json';
4
+ import { createQueryClass, createExecuteClass, createExecuteInterface, createTypeInterface } from './wasm';
5
+
6
+ const expectCode = ast => {
7
+ expect(generate(ast).code).toMatchSnapshot();
8
+ };
9
+
10
+ const printCode = ast => {
11
+ console.log(generate(ast).code);
12
+ };
13
+
14
+ it('cosmos_msg_for__empty', () => {
15
+ expectCode(createTypeInterface(cosmos_msg_for__empty));
16
+ });
17
+ it('execute_msg_for__empty', () => {
18
+ expectCode(createTypeInterface(execute_msg_for__empty));
19
+ });
20
+ it('query classes', () => {
21
+ expectCode(createQueryClass('SG721QueryClient', 'SG721ReadOnlyInstance', cosmos_msg_for__empty));
22
+ });
23
+ it('query classes', () => {
24
+ expectCode(createQueryClass('SG721QueryClient', 'SG721ReadOnlyInstance', execute_msg_for__empty));
25
+ });
26
+ it('execute classes array types', () => {
27
+ expectCode(createExecuteClass('SG721Client', 'SG721Instance', null, execute_msg_for__empty));
28
+ });
29
+ it('execute interfaces no extends', () => {
30
+ expectCode(createExecuteInterface('SG721Instance', null, execute_msg_for__empty));
31
+ });
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.0",
4
4
  "description": "CosmWasm TypeScript AST generation",
5
5
  "author": "Dan Lynch <pyramation@gmail.com>",
6
6
  "homepage": "https://github.com/pyramation/cosmwasm-typescript-gen/tree/master/packages/wasm-ast-types#readme",
@@ -85,5 +85,5 @@
85
85
  "ast-stringify": "0.1.0",
86
86
  "case": "1.6.3"
87
87
  },
88
- "gitHead": "9a94ff9a623b4f4d9f8c1f742b15b43eb525295f"
88
+ "gitHead": "0e1c21c1c495002883f9511bb1dd693c468af0a6"
89
89
  }