wasm-ast-types 0.2.5 → 0.3.1

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/module/wasm.js CHANGED
@@ -1,123 +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
- default:
11
- if ($ref.startsWith('#/definitions/')) {
12
- return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', '')));
13
- }
14
-
15
- throw new Error('what is $ref: ' + $ref);
16
- }
17
- };
18
-
19
- const getArrayTypeFromRef = $ref => {
20
- return t.tsArrayType(getTypeFromRef($ref));
21
- };
22
-
23
- const getArrayTypeFromType = type => {
24
- return t.tsArrayType(getType(type));
25
- };
26
-
27
- export const identifier = (name, typeAnnotation, optional = false) => {
28
- const type = t.identifier(name);
29
- type.typeAnnotation = typeAnnotation;
30
- type.optional = optional;
31
- return type;
32
- };
33
-
34
- 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
-
45
- default:
46
- throw new Error('what is type: ' + type);
47
- }
48
- };
49
-
50
- export const getPropertyType = (schema, prop) => {
51
- const props = schema.properties ?? {};
52
- let info = props[prop];
53
- let type = null;
54
- let optional = schema.required?.includes(prop);
55
-
56
- if (info.allOf && info.allOf.length === 1) {
57
- info = info.allOf[0];
58
- }
59
-
60
- if (typeof info.$ref === 'string') {
61
- type = getTypeFromRef(info.$ref);
62
- }
63
-
64
- if (Array.isArray(info.anyOf)) {
65
- // assuming 2nd is null, but let's check to ensure
66
- if (info.anyOf.length !== 2) {
67
- throw new Error('case not handled by transpiler. contact maintainers.');
68
- }
69
-
70
- const [nullableType, nullType] = info.anyOf;
71
-
72
- if (nullType?.type !== 'null') {
73
- throw new Error('case not handled by transpiler. contact maintainers.');
74
- }
75
-
76
- type = getTypeFromRef(nullableType?.$ref);
77
- optional = true;
78
- }
79
-
80
- if (typeof info.type === 'string') {
81
- if (info.type === 'array') {
82
- if (info.items.$ref) {
83
- type = getArrayTypeFromRef(info.items.$ref);
84
- } else {
85
- type = getArrayTypeFromType(info.items.type);
86
- }
87
- } else {
88
- type = getType(info.type);
89
- }
90
- }
91
-
92
- if (Array.isArray(info.type)) {
93
- // assuming 2nd is null, but let's check to ensure
94
- if (info.type.length !== 2) {
95
- throw new Error('case not handled by transpiler. contact maintainers.');
96
- }
97
-
98
- const [nullableType, nullType] = info.type;
99
-
100
- if (nullType !== 'null') {
101
- throw new Error('case not handled by transpiler. contact maintainers.');
102
- }
103
-
104
- type = getType(nullableType);
105
- optional = true;
106
- }
107
-
108
- if (!type) {
109
- throw new Error('cannot find type for ' + JSON.stringify(info));
110
- }
111
-
112
- if (schema.required?.includes(prop)) {
113
- optional = false;
114
- }
115
-
116
- return {
117
- type,
118
- optional
119
- };
120
- };
4
+ import { getPropertyType, getType, createTypedObjectParams, forEmptyNameFix } from './utils/types';
5
+ import { identifier, tsTypeOperator, propertySignature } from './utils/babel';
121
6
  export const createWasmQueryMethod = jsonschema => {
122
7
  const underscoreName = Object.keys(jsonschema.properties)[0];
123
8
  const methodName = camel(underscoreName);
@@ -142,13 +27,6 @@ export const createQueryClass = (className, implementsClassName, queryMsg) => {
142
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
143
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))]));
144
29
  };
145
-
146
- const tsTypeOperator = (typeAnnotation, operator) => {
147
- const obj = t.tsTypeOperator(typeAnnotation);
148
- obj.operator = operator;
149
- return obj;
150
- };
151
-
152
30
  export const createWasmExecMethod = jsonschema => {
153
31
  const underscoreName = Object.keys(jsonschema.properties)[0];
154
32
  const methodName = camel(underscoreName);
@@ -194,39 +72,6 @@ export const createExecuteInterface = (className, extendsClassName, execMsg) =>
194
72
  t.tSPropertySignature(t.identifier('contractAddress'), t.tsTypeAnnotation(t.tsStringKeyword())), // contract address
195
73
  t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods])));
196
74
  };
197
- export const propertySignature = (name, typeAnnotation, optional = false) => {
198
- // prop.leadingComments = [{
199
- // type: 'Comment',
200
- // value: ' Data on the token itself'
201
- // }];
202
- // prop.leadingComments = [{
203
- // type: 'CommentBlock',
204
- // value: '* Data on the token itself'
205
- // }];
206
- return {
207
- type: 'TSPropertySignature',
208
- key: t.identifier(name),
209
- typeAnnotation,
210
- optional
211
- };
212
- };
213
- export const createTypedObjectParams = (jsonschema, camelize = true) => {
214
- const keys = Object.keys(jsonschema.properties ?? {});
215
- if (!keys.length) return;
216
- const typedParams = keys.map(prop => {
217
- const {
218
- type,
219
- optional
220
- } = getPropertyType(jsonschema, prop);
221
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
222
- });
223
- const params = keys.map(prop => {
224
- return t.objectProperty(camelize ? t.identifier(camel(prop)) : t.identifier(prop), camelize ? t.identifier(camel(prop)) : t.identifier(prop), false, true);
225
- });
226
- const obj = t.objectPattern([...params]);
227
- obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral([...typedParams]));
228
- return obj;
229
- };
230
75
  export const createPropertyFunctionWithObjectParams = (methodName, responseType, jsonschema) => {
231
76
  const obj = createTypedObjectParams(jsonschema);
232
77
  const func = {
@@ -257,6 +102,10 @@ export const createQueryInterface = (className, queryMsg) => {
257
102
  };
258
103
  export const createTypeOrInterface = (Type, jsonschema) => {
259
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
+
260
109
  return t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(Type), null, getType(jsonschema.type)));
261
110
  }
262
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
+ });
@@ -1,17 +1,18 @@
1
1
  import generate from '@babel/generator';
2
- import query_msg from './__fixtures__/schema/query_msg.json';
3
- import execute_msg from './__fixtures__/schema/execute_msg_for__empty.json';
4
- import approval_response from './__fixtures__/schema/approval_response.json';
5
- import all_nft_info_response from './__fixtures__/schema/all_nft_info_response.json';
6
- import approvals_response from './__fixtures__/schema/approvals_response.json';
7
- import collection_info_response from './__fixtures__/schema/collection_info_response.json';
8
- import contract_info_response from './__fixtures__/schema/contract_info_response.json';
9
- import instantiate_msg from './__fixtures__/schema/instantiate_msg.json';
10
- import nft_info_response from './__fixtures__/schema/nft_info_response.json';
11
- import num_tokens_response from './__fixtures__/schema/num_tokens_response.json';
12
- import operators_response from './__fixtures__/schema/operators_response.json';
13
- import owner_of_response from './__fixtures__/schema/owner_of_response.json';
14
- import tokens_response from './__fixtures__/schema/tokens_response.json';
2
+ import execute_msg_named_groups from './../../../__fixtures__/cw-named-groups/execute_msg.json';
3
+ import query_msg from './../../../__fixtures__/basic/query_msg.json';
4
+ import execute_msg from './../../../__fixtures__/basic/execute_msg_for__empty.json';
5
+ import approval_response from './../../../__fixtures__/basic/approval_response.json';
6
+ import all_nft_info_response from './../../../__fixtures__/basic/all_nft_info_response.json';
7
+ import approvals_response from './../../../__fixtures__/basic/approvals_response.json';
8
+ import collection_info_response from './../../../__fixtures__/basic/collection_info_response.json';
9
+ import contract_info_response from './../../../__fixtures__/basic/contract_info_response.json';
10
+ import instantiate_msg from './../../../__fixtures__/basic/instantiate_msg.json';
11
+ import nft_info_response from './../../../__fixtures__/basic/nft_info_response.json';
12
+ import num_tokens_response from './../../../__fixtures__/basic/num_tokens_response.json';
13
+ import operators_response from './../../../__fixtures__/basic/operators_response.json';
14
+ import owner_of_response from './../../../__fixtures__/basic/owner_of_response.json';
15
+ import tokens_response from './../../../__fixtures__/basic/tokens_response.json';
15
16
  import { createQueryClass, createQueryInterface, createExecuteClass, createExecuteInterface, createTypeInterface } from './wasm';
16
17
 
17
18
  const expectCode = ast => {
@@ -64,6 +65,9 @@ it('execute classes', () => {
64
65
  it('execute classes no extends', () => {
65
66
  expectCode(createExecuteClass('SG721Client', 'SG721Instance', null, execute_msg));
66
67
  });
68
+ it('execute classes array types', () => {
69
+ expectCode(createExecuteClass('SG721Client', 'SG721Instance', null, execute_msg_named_groups));
70
+ });
67
71
  it('execute interfaces no extends', () => {
68
72
  expectCode(createExecuteInterface('SG721Instance', null, execute_msg));
69
73
  });
@@ -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.5",
3
+ "version": "0.3.1",
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": "4e067214d530188822eca31ddbeb6eb9ec3ba462"
88
+ "gitHead": "aade6ffc08df51adc9922d4cd813fe8ef5bebfa1"
89
89
  }