wasm-ast-types 0.2.4 → 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.
@@ -1,137 +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 { createTypedObjectParams } from './utils/types';
5
+ import { identifier, tsTypeOperator } from './utils/babel';
4
6
 
5
- const getTypeFromRef = $ref => {
6
- switch ($ref) {
7
- case '#/definitions/Binary':
8
- return t.tsTypeReference(t.identifier('Binary'));
9
-
10
- case '#/definitions/Expiration':
11
- return t.tsTypeReference(t.identifier('Expiration'));
12
-
13
- default:
14
- if ($ref.startsWith('#/definitions/')) {
15
- return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', '')));
16
- }
17
-
18
- throw new Error('what is $ref: ' + $ref);
19
- }
20
- };
21
-
22
- const getArrayTypeFromRef = $ref => {
23
- return t.tsArrayType(getTypeFromRef($ref));
24
- };
25
-
26
- const getArrayTypeFromType = type => {
27
- return t.tsArrayType(getType(type));
28
- }; // MARKED AS NOT DRY
29
-
30
-
31
- const identifier = (name, typeAnnotation, optional = false) => {
32
- const type = t.identifier(name);
33
- type.typeAnnotation = typeAnnotation;
34
- type.optional = optional;
35
- return type;
36
- };
37
-
38
- const getType = type => {
39
- switch (type) {
40
- case 'string':
41
- return t.tsStringKeyword();
42
-
43
- case 'boolean':
44
- return t.tSBooleanKeyword();
45
-
46
- case 'integer':
47
- return t.tsNumberKeyword();
48
-
49
- default:
50
- throw new Error('what is type: ' + type);
51
- }
52
- }; // MARKED AS NOT DRY
53
-
54
-
55
- const getPropertyType = (schema, prop) => {
56
- const props = schema.properties ?? {};
57
- let info = props[prop];
58
- let type = null;
59
- let optional = schema.required?.includes(prop);
60
-
61
- if (info.allOf && info.allOf.length === 1) {
62
- info = info.allOf[0];
63
- }
64
-
65
- if (typeof info.$ref === 'string') {
66
- type = getTypeFromRef(info.$ref);
67
- }
68
-
69
- if (Array.isArray(info.anyOf)) {
70
- // assuming 2nd is null, but let's check to ensure
71
- if (info.anyOf.length !== 2) {
72
- throw new Error('case not handled by transpiler. contact maintainers.');
73
- }
74
-
75
- const [nullableType, nullType] = info.anyOf;
76
-
77
- if (nullType?.type !== 'null') {
78
- throw new Error('case not handled by transpiler. contact maintainers.');
79
- }
80
-
81
- type = getTypeFromRef(nullableType?.$ref);
82
- optional = true;
83
- }
84
-
85
- if (typeof info.type === 'string') {
86
- if (info.type === 'array') {
87
- if (info.items.$ref) {
88
- type = getArrayTypeFromRef(info.items.$ref);
89
- } else {
90
- type = getArrayTypeFromType(info.items.type);
91
- }
92
- } else {
93
- type = getType(info.type);
94
- }
95
- }
96
-
97
- if (Array.isArray(info.type)) {
98
- // assuming 2nd is null, but let's check to ensure
99
- if (info.type.length !== 2) {
100
- throw new Error('case not handled by transpiler. contact maintainers.');
101
- }
102
-
103
- const [nullableType, nullType] = info.type;
104
-
105
- if (nullType !== 'null') {
106
- throw new Error('case not handled by transpiler. contact maintainers.');
107
- }
108
-
109
- type = getType(nullableType);
110
- optional = true;
111
- }
112
-
113
- if (!type) {
114
- throw new Error('cannot find type for ' + JSON.stringify(info));
115
- }
116
-
117
- if (schema.required?.includes(prop)) {
118
- optional = false;
119
- }
120
-
121
- return {
122
- type,
123
- optional
124
- };
125
- };
126
-
127
- const tsTypeOperator = (typeAnnotation, operator) => {
128
- const obj = t.tsTypeOperator(typeAnnotation);
129
- obj.operator = operator;
130
- return obj;
131
- }; // MARKED AS NOT DRY
132
-
133
-
134
- const createWasmExecMethod = jsonschema => {
7
+ const createWasmExecMethodPartial = jsonschema => {
135
8
  const underscoreName = Object.keys(jsonschema.properties)[0];
136
9
  const methodName = camel(underscoreName);
137
10
  const properties = jsonschema.properties[underscoreName].properties ?? {};
@@ -172,7 +45,7 @@ export const createFromPartialClass = (className, implementsClassName, execMsg)
172
45
  const propertyNames = getMessageProperties(execMsg).map(method => Object.keys(method.properties)?.[0]).filter(Boolean);
173
46
  const bindings = propertyNames.map(camel).map(bindMethod);
174
47
  const methods = getMessageProperties(execMsg).map(schema => {
175
- return createWasmExecMethod(schema);
48
+ return createWasmExecMethodPartial(schema);
176
49
  });
177
50
  const blockStmt = [];
178
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]);
@@ -193,42 +66,6 @@ export const createFromPartialInterface = (className, execMsg) => {
193
66
  t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods])));
194
67
  }; // MARKED AS NOT DRY
195
68
 
196
- const propertySignature = (name, typeAnnotation, optional = false) => {
197
- // prop.leadingComments = [{
198
- // type: 'Comment',
199
- // value: ' Data on the token itself'
200
- // }];
201
- // prop.leadingComments = [{
202
- // type: 'CommentBlock',
203
- // value: '* Data on the token itself'
204
- // }];
205
- return {
206
- type: 'TSPropertySignature',
207
- key: t.identifier(name),
208
- typeAnnotation,
209
- optional
210
- };
211
- }; // MARKED AS NOT DRY
212
-
213
-
214
- const createTypedObjectParams = (jsonschema, camelize = true) => {
215
- const keys = Object.keys(jsonschema.properties ?? {});
216
- if (!keys.length) return;
217
- const typedParams = keys.map(prop => {
218
- const {
219
- type,
220
- optional
221
- } = getPropertyType(jsonschema, prop);
222
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
223
- });
224
- const params = keys.map(prop => {
225
- return t.objectProperty(camelize ? t.identifier(camel(prop)) : t.identifier(prop), camelize ? t.identifier(camel(prop)) : t.identifier(prop), false, true);
226
- });
227
- const obj = t.objectPattern([...params]);
228
- obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral([...typedParams]));
229
- return obj;
230
- };
231
-
232
69
  const createPropertyFunctionWithObjectParamsForPartial = (methodName, responseType, jsonschema) => {
233
70
  const obj = createTypedObjectParams(jsonschema);
234
71
  const fixedParams = [// identifier('fee', t.tsTypeAnnotation(
@@ -1,5 +1,5 @@
1
1
  import generate from '@babel/generator';
2
- import execute_msg from './__fixtures__/schema/execute_msg_for__empty.json';
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 => {
@@ -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, getPropertyType } from './wasm';
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 './__fixtures__/schema/query_msg.json';
3
+ import query_msg from './../../../__fixtures__/basic/query_msg.json';
4
4
  import { createReactQueryHooks } from './react-query';
5
5
 
6
6
  const expectCode = ast => {
@@ -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,6 +1,6 @@
1
1
  import generate from '@babel/generator';
2
2
  import * as t from '@babel/types';
3
- import query_msg from './__fixtures__/schema/query_msg.json';
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 './utils';
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 './utils';
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,187 @@
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
+ };
11
+
12
+ const getTypeFromRef = $ref => {
13
+ switch ($ref) {
14
+ case '#/definitions/Binary':
15
+ return t.tsTypeReference(t.identifier('Binary'));
16
+
17
+ default:
18
+ if ($ref?.startsWith('#/definitions/')) {
19
+ return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', '')));
20
+ }
21
+
22
+ throw new Error('what is $ref: ' + $ref);
23
+ }
24
+ };
25
+
26
+ const getArrayTypeFromRef = $ref => {
27
+ return t.tsArrayType(getTypeFromRef($ref));
28
+ };
29
+
30
+ const getArrayTypeFromType = type => {
31
+ return t.tsArrayType(getType(type));
32
+ };
33
+
34
+ export const getType = type => {
35
+ switch (type) {
36
+ case 'string':
37
+ return t.tsStringKeyword();
38
+
39
+ case 'boolean':
40
+ return t.tSBooleanKeyword();
41
+
42
+ case 'integer':
43
+ return t.tsNumberKeyword();
44
+ // case 'object':
45
+ // return t.tsObjectKeyword();
46
+
47
+ default:
48
+ throw new Error('contact maintainers [unknown type]: ' + type);
49
+ }
50
+ };
51
+ export const getPropertyType = (schema, prop) => {
52
+ const props = schema.properties ?? {};
53
+ let info = props[prop];
54
+ let type = null;
55
+ let optional = !schema.required?.includes(prop);
56
+
57
+ if (info.allOf && info.allOf.length === 1) {
58
+ info = info.allOf[0];
59
+ }
60
+
61
+ if (typeof info.$ref === 'string') {
62
+ type = getTypeFromRef(info.$ref);
63
+ }
64
+
65
+ if (Array.isArray(info.anyOf)) {
66
+ // assuming 2nd is null, but let's check to ensure
67
+ if (info.anyOf.length !== 2) {
68
+ throw new Error('case not handled by transpiler. contact maintainers.');
69
+ }
70
+
71
+ const [nullableType, nullType] = info.anyOf;
72
+
73
+ if (nullType?.type !== 'null') {
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);
85
+ }
86
+
87
+ optional = true;
88
+ }
89
+
90
+ if (typeof info.type === 'string') {
91
+ if (info.type === 'array') {
92
+ if (info.items.$ref) {
93
+ type = getArrayTypeFromRef(info.items.$ref);
94
+ } else if (info.items.title) {
95
+ type = t.tsArrayType(t.tsTypeReference(t.identifier(info.items.title)));
96
+ } else if (info.items.type) {
97
+ type = getArrayTypeFromType(info.items.type);
98
+ } else {
99
+ throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
100
+ }
101
+ } else {
102
+ type = getType(info.type);
103
+ }
104
+ }
105
+
106
+ if (Array.isArray(info.type)) {
107
+ // assuming 2nd is null, but let's check to ensure
108
+ if (info.type.length !== 2) {
109
+ throw new Error('please report this to maintainers (field type): ' + JSON.stringify(info, null, 2));
110
+ }
111
+
112
+ const [nullableType, nullType] = info.type;
113
+
114
+ if (nullType !== 'null') {
115
+ throw new Error('please report this to maintainers (field type): ' + JSON.stringify(info, null, 2));
116
+ }
117
+
118
+ if (nullableType === 'array') {
119
+ type = t.tsArrayType(getType(info.items.type));
120
+ } else {
121
+ type = getType(nullableType);
122
+ }
123
+
124
+ optional = true;
125
+ }
126
+
127
+ if (!type) {
128
+ throw new Error('cannot find type for ' + JSON.stringify(info));
129
+ }
130
+
131
+ if (schema.required?.includes(prop)) {
132
+ optional = false;
133
+ }
134
+
135
+ return {
136
+ type,
137
+ optional
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;
187
+ };
package/module/wasm.js CHANGED
@@ -1,126 +1,8 @@
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
-
5
- const getTypeFromRef = $ref => {
6
- switch ($ref) {
7
- case '#/definitions/Binary':
8
- return t.tsTypeReference(t.identifier('Binary'));
9
-
10
- case '#/definitions/Expiration':
11
- return t.tsTypeReference(t.identifier('Expiration'));
12
-
13
- default:
14
- if ($ref.startsWith('#/definitions/')) {
15
- return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', '')));
16
- }
17
-
18
- throw new Error('what is $ref: ' + $ref);
19
- }
20
- };
21
-
22
- const getArrayTypeFromRef = $ref => {
23
- return t.tsArrayType(getTypeFromRef($ref));
24
- };
25
-
26
- const getArrayTypeFromType = type => {
27
- return t.tsArrayType(getType(type));
28
- };
29
-
30
- export const identifier = (name, typeAnnotation, optional = false) => {
31
- const type = t.identifier(name);
32
- type.typeAnnotation = typeAnnotation;
33
- type.optional = optional;
34
- return type;
35
- };
36
-
37
- const getType = type => {
38
- switch (type) {
39
- case 'string':
40
- return t.tsStringKeyword();
41
-
42
- case 'boolean':
43
- return t.tSBooleanKeyword();
44
-
45
- case 'integer':
46
- return t.tsNumberKeyword();
47
-
48
- default:
49
- throw new Error('what is type: ' + type);
50
- }
51
- };
52
-
53
- export const getPropertyType = (schema, prop) => {
54
- const props = schema.properties ?? {};
55
- let info = props[prop];
56
- let type = null;
57
- let optional = schema.required?.includes(prop);
58
-
59
- if (info.allOf && info.allOf.length === 1) {
60
- info = info.allOf[0];
61
- }
62
-
63
- if (typeof info.$ref === 'string') {
64
- type = getTypeFromRef(info.$ref);
65
- }
66
-
67
- if (Array.isArray(info.anyOf)) {
68
- // assuming 2nd is null, but let's check to ensure
69
- if (info.anyOf.length !== 2) {
70
- throw new Error('case not handled by transpiler. contact maintainers.');
71
- }
72
-
73
- const [nullableType, nullType] = info.anyOf;
74
-
75
- if (nullType?.type !== 'null') {
76
- throw new Error('case not handled by transpiler. contact maintainers.');
77
- }
78
-
79
- type = getTypeFromRef(nullableType?.$ref);
80
- optional = true;
81
- }
82
-
83
- if (typeof info.type === 'string') {
84
- if (info.type === 'array') {
85
- if (info.items.$ref) {
86
- type = getArrayTypeFromRef(info.items.$ref);
87
- } else {
88
- type = getArrayTypeFromType(info.items.type);
89
- }
90
- } else {
91
- type = getType(info.type);
92
- }
93
- }
94
-
95
- if (Array.isArray(info.type)) {
96
- // assuming 2nd is null, but let's check to ensure
97
- if (info.type.length !== 2) {
98
- throw new Error('case not handled by transpiler. contact maintainers.');
99
- }
100
-
101
- const [nullableType, nullType] = info.type;
102
-
103
- if (nullType !== 'null') {
104
- throw new Error('case not handled by transpiler. contact maintainers.');
105
- }
106
-
107
- type = getType(nullableType);
108
- optional = true;
109
- }
110
-
111
- if (!type) {
112
- throw new Error('cannot find type for ' + JSON.stringify(info));
113
- }
114
-
115
- if (schema.required?.includes(prop)) {
116
- optional = false;
117
- }
118
-
119
- return {
120
- type,
121
- optional
122
- };
123
- };
4
+ import { getPropertyType, getType, createTypedObjectParams, forEmptyNameFix } from './utils/types';
5
+ import { identifier, tsTypeOperator, propertySignature } from './utils/babel';
124
6
  export const createWasmQueryMethod = jsonschema => {
125
7
  const underscoreName = Object.keys(jsonschema.properties)[0];
126
8
  const methodName = camel(underscoreName);
@@ -145,13 +27,6 @@ export const createQueryClass = (className, implementsClassName, queryMsg) => {
145
27
  t.classMethod('constructor', t.identifier('constructor'), [typedIdentifier('client', t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CosmWasmClient')))), typedIdentifier('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword()))], t.blockStatement([// client/contract set
146
28
  t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('client'))), t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.identifier('contractAddress'))), ...bindings])), ...methods], [t.tSExpressionWithTypeArguments(t.identifier(implementsClassName))]));
147
29
  };
148
-
149
- const tsTypeOperator = (typeAnnotation, operator) => {
150
- const obj = t.tsTypeOperator(typeAnnotation);
151
- obj.operator = operator;
152
- return obj;
153
- };
154
-
155
30
  export const createWasmExecMethod = jsonschema => {
156
31
  const underscoreName = Object.keys(jsonschema.properties)[0];
157
32
  const methodName = camel(underscoreName);
@@ -197,39 +72,6 @@ export const createExecuteInterface = (className, extendsClassName, execMsg) =>
197
72
  t.tSPropertySignature(t.identifier('contractAddress'), t.tsTypeAnnotation(t.tsStringKeyword())), // contract address
198
73
  t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods])));
199
74
  };
200
- export const propertySignature = (name, typeAnnotation, optional = false) => {
201
- // prop.leadingComments = [{
202
- // type: 'Comment',
203
- // value: ' Data on the token itself'
204
- // }];
205
- // prop.leadingComments = [{
206
- // type: 'CommentBlock',
207
- // value: '* Data on the token itself'
208
- // }];
209
- return {
210
- type: 'TSPropertySignature',
211
- key: t.identifier(name),
212
- typeAnnotation,
213
- optional
214
- };
215
- };
216
- export const createTypedObjectParams = (jsonschema, camelize = true) => {
217
- const keys = Object.keys(jsonschema.properties ?? {});
218
- if (!keys.length) return;
219
- const typedParams = keys.map(prop => {
220
- const {
221
- type,
222
- optional
223
- } = getPropertyType(jsonschema, prop);
224
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
225
- });
226
- const params = keys.map(prop => {
227
- return t.objectProperty(camelize ? t.identifier(camel(prop)) : t.identifier(prop), camelize ? t.identifier(camel(prop)) : t.identifier(prop), false, true);
228
- });
229
- const obj = t.objectPattern([...params]);
230
- obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral([...typedParams]));
231
- return obj;
232
- };
233
75
  export const createPropertyFunctionWithObjectParams = (methodName, responseType, jsonschema) => {
234
76
  const obj = createTypedObjectParams(jsonschema);
235
77
  const func = {
@@ -260,6 +102,10 @@ export const createQueryInterface = (className, queryMsg) => {
260
102
  };
261
103
  export const createTypeOrInterface = (Type, jsonschema) => {
262
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
+
263
109
  return t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(Type), null, getType(jsonschema.type)));
264
110
  }
265
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
+ });