wasm-ast-types 0.9.0 → 0.11.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.
Files changed (39) hide show
  1. package/main/client/client.js +2 -2
  2. package/main/client/test/ts-client.arrays-ref.spec.js +1 -3
  3. package/main/client/test/ts-client.arrays.spec.js +7 -3
  4. package/main/client/test/ts-client.cw-named-groups.test.js +1 -3
  5. package/main/client/test/ts-client.cw-proposal-single.test.js +4 -6
  6. package/main/client/test/ts-client.empty-enums.spec.js +1 -3
  7. package/main/client/test/ts-client.issues.test.js +4 -6
  8. package/main/client/test/ts-client.sg721.spec.js +1 -3
  9. package/main/client/test/ts-client.spec.js +17 -19
  10. package/main/client/test/ts-client.vectis.spec.js +9 -11
  11. package/main/context/context.js +25 -4
  12. package/main/message-composer/message-composer.spec.js +2 -4
  13. package/main/react-query/react-query.js +1 -1
  14. package/main/react-query/react-query.spec.js +5 -7
  15. package/main/recoil/recoil.js +3 -3
  16. package/main/recoil/recoil.spec.js +2 -4
  17. package/main/utils/types.js +110 -32
  18. package/module/client/client.js +4 -4
  19. package/module/client/test/ts-client.arrays-ref.spec.js +2 -3
  20. package/module/client/test/ts-client.arrays.spec.js +7 -3
  21. package/module/client/test/ts-client.cw-named-groups.test.js +2 -3
  22. package/module/client/test/ts-client.cw-proposal-single.test.js +5 -6
  23. package/module/client/test/ts-client.empty-enums.spec.js +2 -3
  24. package/module/client/test/ts-client.issues.test.js +5 -6
  25. package/module/client/test/ts-client.sg721.spec.js +2 -3
  26. package/module/client/test/ts-client.spec.js +18 -19
  27. package/module/client/test/ts-client.vectis.spec.js +10 -11
  28. package/module/context/context.js +23 -3
  29. package/module/message-composer/message-composer.spec.js +3 -4
  30. package/module/react-query/react-query.js +2 -2
  31. package/module/react-query/react-query.spec.js +6 -7
  32. package/module/recoil/recoil.js +4 -4
  33. package/module/recoil/recoil.spec.js +3 -4
  34. package/module/utils/types.js +91 -27
  35. package/package.json +2 -2
  36. package/types/context/context.d.ts +25 -3
  37. package/types/react-query/react-query.d.ts +3 -2
  38. package/types/recoil/recoil.d.ts +1 -1
  39. package/types/utils/types.d.ts +10 -1
@@ -1,11 +1,10 @@
1
1
  import * as t from '@babel/types';
2
2
  import query_msg from '../../../../__fixtures__/basic/query_msg.json';
3
3
  import { createRecoilSelector, createRecoilSelectors, createRecoilQueryClient } from './recoil';
4
- import { RenderContext } from '../context';
5
- import { expectCode } from '../../test-utils';
6
- const ctx = new RenderContext(query_msg);
4
+ import { expectCode, makeContext } from '../../test-utils';
5
+ const ctx = makeContext(query_msg);
7
6
  it('selector', () => {
8
- expectCode(createRecoilSelector(ctx, 'SG721', 'SG721QueryClient', 'governanceModules'));
7
+ expectCode(createRecoilSelector(ctx, 'SG721', 'SG721QueryClient', 'governanceModules', 'GovernanceModulesResponse'));
9
8
  });
10
9
  it('selectors', () => {
11
10
  expectCode(t.program(createRecoilSelectors(ctx, 'SG721', 'SG721QueryClient', query_msg)));
@@ -1,6 +1,12 @@
1
1
  import * as t from '@babel/types';
2
- import { camel } from 'case';
2
+ import { camel, pascal } from 'case';
3
3
  import { propertySignature } from './babel';
4
+ export function getResponseType(context, underscoreName) {
5
+ const methodName = camel(underscoreName);
6
+ return pascal(context.contract?.responses?.[underscoreName]?.title ?? // after v1.1 is adopted, we can deprecate this and require the above response
7
+ `${methodName}Response`);
8
+ }
9
+ ;
4
10
 
5
11
  const getTypeStrFromRef = $ref => {
6
12
  if ($ref?.startsWith('#/definitions/')) {
@@ -31,7 +37,9 @@ const getTypeOrRef = obj => {
31
37
  };
32
38
 
33
39
  const getArrayTypeFromItems = items => {
34
- if (items.type === 'array') {
40
+ const detect = detectType(items.type);
41
+
42
+ if (detect.type === 'array') {
35
43
  if (Array.isArray(items.items)) {
36
44
  return t.tsArrayType(t.tsArrayType(getTypeOrRef(items.items[0])));
37
45
  } else {
@@ -39,37 +47,38 @@ const getArrayTypeFromItems = items => {
39
47
  }
40
48
  }
41
49
 
42
- return t.tsArrayType(getType(items.type));
50
+ return t.tsArrayType(getType(detect.type));
43
51
  };
44
52
 
45
- export const getType = type => {
46
- switch (type) {
47
- case 'string':
48
- return t.tsStringKeyword();
53
+ export const detectType = type => {
54
+ let optional = false;
55
+ let theType = '';
49
56
 
50
- case 'boolean':
51
- return t.tSBooleanKeyword();
57
+ if (Array.isArray(type)) {
58
+ if (type.length !== 2) {
59
+ throw new Error('[getType(array length)] case not handled by transpiler. contact maintainers.');
60
+ }
52
61
 
53
- case 'integer':
54
- return t.tsNumberKeyword();
62
+ const [nullableType, nullType] = type;
55
63
 
56
- default:
57
- throw new Error('contact maintainers [unknown type]: ' + type);
58
- }
59
- };
60
- export const getPropertyType = (context, schema, prop) => {
61
- const props = schema.properties ?? {};
62
- let info = props[prop];
63
- let type = null;
64
- let optional = !schema.required?.includes(prop);
64
+ if (nullType !== 'null') {
65
+ throw new Error('[getType(null)] case not handled by transpiler. contact maintainers.');
66
+ }
65
67
 
66
- if (info.allOf && info.allOf.length === 1) {
67
- info = info.allOf[0];
68
+ theType = nullableType;
69
+ optional = true;
70
+ } else {
71
+ theType = type;
68
72
  }
69
73
 
70
- if (typeof info.$ref === 'string') {
71
- type = getTypeFromRef(info.$ref);
72
- }
74
+ return {
75
+ type: theType,
76
+ optional
77
+ };
78
+ };
79
+ export const getTypeInfo = info => {
80
+ let type = undefined;
81
+ let optional = undefined;
73
82
 
74
83
  if (Array.isArray(info.anyOf)) {
75
84
  // assuming 2nd is null, but let's check to ensure
@@ -112,7 +121,9 @@ export const getPropertyType = (context, schema, prop) => {
112
121
  throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
113
122
  }
114
123
  } else {
115
- type = getType(info.type);
124
+ const detect = detectType(info.type);
125
+ type = getType(detect.type);
126
+ optional = detect.optional;
116
127
  }
117
128
  }
118
129
 
@@ -129,7 +140,16 @@ export const getPropertyType = (context, schema, prop) => {
129
140
  }
130
141
 
131
142
  if (nullableType === 'array' && typeof info.items === 'object' && !Array.isArray(info.items)) {
132
- type = t.tsArrayType(getType(info.items.type));
143
+ const detect = detectType(info.items.type);
144
+
145
+ if (detect.type === 'array') {
146
+ // wen recursion?
147
+ type = t.tsArrayType(getArrayTypeFromItems(info.items));
148
+ } else {
149
+ type = t.tsArrayType(getType(detect.type));
150
+ }
151
+
152
+ optional = detect.optional;
133
153
  } else {
134
154
  type = getType(nullableType);
135
155
  }
@@ -137,6 +157,50 @@ export const getPropertyType = (context, schema, prop) => {
137
157
  optional = true;
138
158
  }
139
159
 
160
+ return {
161
+ type,
162
+ optional
163
+ };
164
+ };
165
+ export const getType = type => {
166
+ switch (type) {
167
+ case 'string':
168
+ return t.tsStringKeyword();
169
+
170
+ case 'boolean':
171
+ return t.tSBooleanKeyword();
172
+
173
+ case 'integer':
174
+ return t.tsNumberKeyword();
175
+
176
+ default:
177
+ throw new Error('contact maintainers [unknown type]: ' + type);
178
+ }
179
+ };
180
+ export const getPropertyType = (context, schema, prop) => {
181
+ const props = schema.properties ?? {};
182
+ let info = props[prop];
183
+ let type = null;
184
+ let optional = !schema.required?.includes(prop);
185
+
186
+ if (info.allOf && info.allOf.length === 1) {
187
+ info = info.allOf[0];
188
+ }
189
+
190
+ if (typeof info.$ref === 'string') {
191
+ type = getTypeFromRef(info.$ref);
192
+ }
193
+
194
+ const typeInfo = getTypeInfo(info);
195
+
196
+ if (typeof typeInfo.optional !== 'undefined') {
197
+ optional = typeInfo.optional;
198
+ }
199
+
200
+ if (typeof typeInfo.type !== 'undefined') {
201
+ type = typeInfo.type;
202
+ }
203
+
140
204
  if (!type) {
141
205
  throw new Error('cannot find type for ' + JSON.stringify(info));
142
206
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.9.0",
3
+ "version": "0.11.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",
@@ -86,5 +86,5 @@
86
86
  "case": "1.6.3",
87
87
  "deepmerge": "4.2.2"
88
88
  },
89
- "gitHead": "3b7ca02c9283c346a365d8426d3034af355afcac"
89
+ "gitHead": "d7f8c715f76edac8c5c2762e58fe8c73351d2af5"
90
90
  }
@@ -20,6 +20,25 @@ export interface TSTypesOptions {
20
20
  enabled?: boolean;
21
21
  aliasExecuteMsg?: boolean;
22
22
  }
23
+ interface KeyedSchema {
24
+ [key: string]: JSONSchema;
25
+ }
26
+ export interface IDLObject {
27
+ contract_name: string;
28
+ contract_version: string;
29
+ idl_version: string;
30
+ instantiate: JSONSchema;
31
+ execute: JSONSchema;
32
+ query: JSONSchema;
33
+ migrate: JSONSchema;
34
+ sudo: JSONSchema;
35
+ responses: KeyedSchema;
36
+ }
37
+ export interface ContractInfo {
38
+ schemas: JSONSchema[];
39
+ responses?: Record<string, JSONSchema>;
40
+ idlObject?: IDLObject;
41
+ }
23
42
  export interface RenderOptions {
24
43
  types?: TSTypesOptions;
25
44
  recoil?: RecoilOptions;
@@ -28,15 +47,18 @@ export interface RenderOptions {
28
47
  reactQuery?: ReactQueryOptions;
29
48
  }
30
49
  export interface RenderContext {
31
- schema: JSONSchema;
50
+ contract: ContractInfo;
32
51
  options: RenderOptions;
33
52
  }
34
53
  export declare const defaultOptions: RenderOptions;
54
+ export declare const getDefinitionSchema: (schemas: JSONSchema[]) => JSONSchema;
35
55
  export declare class RenderContext implements RenderContext {
36
- schema: JSONSchema;
56
+ contract: ContractInfo;
37
57
  utils: string[];
38
- constructor(schema: JSONSchema, options?: RenderOptions);
58
+ schema: JSONSchema;
59
+ constructor(contract: ContractInfo, options?: RenderOptions);
39
60
  refLookup($ref: string): JSONSchema;
40
61
  addUtil(util: string): void;
41
62
  getImports(): any[];
42
63
  }
64
+ export {};
@@ -7,6 +7,7 @@ interface ReactQueryHookQuery {
7
7
  hookName: string;
8
8
  hookParamsTypeName: string;
9
9
  hookKeyName: string;
10
+ queryKeysName: string;
10
11
  responseType: string;
11
12
  methodName: string;
12
13
  jsonschema: any;
@@ -17,8 +18,8 @@ interface ReactQueryHooks {
17
18
  contractName: string;
18
19
  QueryClient: string;
19
20
  }
20
- export declare const createReactQueryHooks: ({ context, queryMsg, contractName, QueryClient }: ReactQueryHooks) => t.ExportNamedDeclaration[];
21
- export declare const createReactQueryHook: ({ context, hookName, hookParamsTypeName, responseType, hookKeyName, methodName, jsonschema }: ReactQueryHookQuery) => t.ExportNamedDeclaration;
21
+ export declare const createReactQueryHooks: ({ context, queryMsg, contractName, QueryClient }: ReactQueryHooks) => any[];
22
+ export declare const createReactQueryHook: ({ context, hookName, hookParamsTypeName, responseType, hookKeyName, queryKeysName, methodName, jsonschema }: ReactQueryHookQuery) => t.ExportNamedDeclaration;
22
23
  interface ReactQueryMutationHookInterface {
23
24
  context: RenderContext;
24
25
  ExecuteClient: string;
@@ -1,7 +1,7 @@
1
1
  import * as t from '@babel/types';
2
2
  import { QueryMsg } from '../types';
3
3
  import { RenderContext } from '../context';
4
- export declare const createRecoilSelector: (context: RenderContext, keyPrefix: string, QueryClient: string, methodName: string) => t.ExportNamedDeclaration;
4
+ export declare const createRecoilSelector: (context: RenderContext, keyPrefix: string, QueryClient: string, methodName: string, responseType: string) => t.ExportNamedDeclaration;
5
5
  export declare const createRecoilSelectors: (context: RenderContext, keyPrefix: string, QueryClient: string, queryMsg: QueryMsg) => any;
6
6
  export declare const createRecoilQueryClientType: () => {
7
7
  type: string;
@@ -2,8 +2,17 @@ import * as t from '@babel/types';
2
2
  import { TSTypeAnnotation } from '@babel/types';
3
3
  import { RenderContext } from '../context';
4
4
  import { JSONSchema } from '../types';
5
+ export declare function getResponseType(context: RenderContext, underscoreName: string): string;
5
6
  export declare const getTypeFromRef: ($ref: any) => t.TSTypeReference;
6
- export declare const getType: (type: any) => t.TSBooleanKeyword | t.TSNumberKeyword | t.TSStringKeyword;
7
+ export declare const detectType: (type: string | string[]) => {
8
+ type: string;
9
+ optional: boolean;
10
+ };
11
+ export declare const getTypeInfo: (info: JSONSchema) => {
12
+ type: any;
13
+ optional: any;
14
+ };
15
+ export declare const getType: (type: string) => t.TSBooleanKeyword | t.TSNumberKeyword | t.TSStringKeyword;
7
16
  export declare const getPropertyType: (context: RenderContext, schema: JSONSchema, prop: string) => {
8
17
  type: any;
9
18
  optional: boolean;