wasm-ast-types 0.2.3 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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 } 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,22 +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
75
  export const createTypedObjectParams = (jsonschema, camelize = true) => {
217
76
  const keys = Object.keys(jsonschema.properties ?? {});
218
77
  if (!keys.length) return;
@@ -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
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.2.3",
3
+ "version": "0.2.6",
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": "e7cd5b9e8bc60161c28db9a74f3c372ddd0fc6b1"
88
+ "gitHead": "9a94ff9a623b4f4d9f8c1f742b15b43eb525295f"
89
89
  }
package/types/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './utils';
2
2
  export * from './wasm';
3
3
  export * from './recoil';
4
+ export * from './messages';
4
5
  export * from './react-query';
@@ -1,35 +1,4 @@
1
1
  import * as t from '@babel/types';
2
- import { Mutation } from './types';
3
- export declare const addMsgMethod: ({ methodName, typeUrl, TypeName, methodToCall }: {
4
- methodName: any;
5
- typeUrl: any;
6
- TypeName: any;
7
- methodToCall: any;
8
- }) => t.ObjectMethod;
9
- export declare const addFromJSONMethod: ({ methodName, typeUrl, TypeName }: {
10
- methodName: any;
11
- typeUrl: any;
12
- TypeName: any;
13
- }) => t.ObjectMethod;
14
- export declare const addFromPartialMethod: ({ methodName, typeUrl, TypeName }: {
15
- methodName: any;
16
- typeUrl: any;
17
- TypeName: any;
18
- }) => t.ObjectMethod;
19
- export declare const addToJSONMethod: ({ methodName, typeUrl, TypeName }: {
20
- methodName: any;
21
- typeUrl: any;
22
- TypeName: any;
23
- }) => t.ObjectMethod;
24
- export declare const addJsonMethod: ({ methodName, typeUrl, TypeName }: {
25
- methodName: any;
26
- typeUrl: any;
27
- TypeName: any;
28
- }) => t.ObjectMethod;
29
- export declare const addEncodedMethod: ({ methodName, typeUrl, TypeName }: {
30
- methodName: any;
31
- typeUrl: any;
32
- TypeName: any;
33
- }) => t.ObjectMethod;
34
- export declare const messages: (mutations: Mutation[]) => t.ExportNamedDeclaration;
35
- export declare const encoded: (mutations: Mutation[]) => t.ExportNamedDeclaration;
2
+ import { ExecuteMsg } from './types';
3
+ export declare const createFromPartialClass: (className: string, implementsClassName: string, execMsg: ExecuteMsg) => t.ExportNamedDeclaration;
4
+ export declare const createFromPartialInterface: (className: string, execMsg: ExecuteMsg) => t.ExportNamedDeclaration;
package/types/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as t from '@babel/types';
2
- import { Field, QueryMsg } from './types';
2
+ import { Field, QueryMsg, ExecuteMsg } from './types';
3
3
  import { TSTypeAnnotation, TSExpressionWithTypeArguments } from '@babel/types';
4
4
  export declare const getMessageProperties: (msg: QueryMsg | ExecuteMsg) => any;
5
5
  export declare const tsPropertySignature: (key: t.Expression, typeAnnotation: t.TSTypeAnnotation, optional: boolean) => t.TSPropertySignature;
package/types/wasm.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as t from '@babel/types';
2
2
  import { QueryMsg, ExecuteMsg } from './types';
3
+ export declare const identifier: (name: string, typeAnnotation: t.TSTypeAnnotation, optional?: boolean) => t.Identifier;
3
4
  export declare const getPropertyType: (schema: any, prop: any) => {
4
5
  type: any;
5
6
  optional: any;
@@ -17,6 +18,7 @@ export declare const propertySignature: (name: string, typeAnnotation: t.TSTypeA
17
18
  };
18
19
  export declare const createTypedObjectParams: (jsonschema: any, camelize?: boolean) => t.ObjectPattern;
19
20
  export declare const createPropertyFunctionWithObjectParams: (methodName: string, responseType: string, jsonschema: any) => t.TSPropertySignature;
21
+ export declare const createPropertyFunctionWithObjectParamsForExec: (methodName: string, responseType: string, jsonschema: any) => t.TSPropertySignature;
20
22
  export declare const createQueryInterface: (className: string, queryMsg: QueryMsg) => t.ExportNamedDeclaration;
21
23
  export declare const createTypeOrInterface: (Type: string, jsonschema: any) => t.ExportNamedDeclaration;
22
24
  export declare const createTypeInterface: (jsonschema: any) => t.ExportNamedDeclaration;