wasm-ast-types 0.23.0 → 0.24.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. package/main/context/context.js +49 -5
  2. package/main/context/imports.js +42 -7
  3. package/main/index.js +13 -0
  4. package/main/provider/index.js +18 -0
  5. package/main/provider/provider.js +98 -0
  6. package/main/provider/provider.spec.js +63 -0
  7. package/main/utils/constants.js +9 -2
  8. package/main/utils/index.js +8 -1
  9. package/module/context/context.js +38 -4
  10. package/module/context/imports.js +35 -5
  11. package/module/index.js +2 -1
  12. package/module/provider/index.js +1 -0
  13. package/module/provider/provider.js +53 -0
  14. package/module/provider/provider.spec.js +58 -0
  15. package/module/utils/constants.js +7 -1
  16. package/module/utils/index.js +2 -1
  17. package/package.json +2 -2
  18. package/src/context/context.ts +59 -4
  19. package/src/context/imports.ts +84 -49
  20. package/src/index.ts +1 -0
  21. package/src/provider/__snapshots__/provider.spec.ts.snap +49 -0
  22. package/src/provider/index.ts +1 -0
  23. package/src/provider/provider.spec.ts +81 -0
  24. package/src/provider/provider.ts +237 -0
  25. package/src/react-query/react-query.ts +3 -3
  26. package/src/utils/constants.ts +7 -0
  27. package/src/utils/index.ts +1 -0
  28. package/types/client/client.d.ts +1 -3
  29. package/types/context/context.d.ts +34 -4
  30. package/types/context/imports.d.ts +3 -1
  31. package/types/index.d.ts +1 -0
  32. package/types/msg-builder/msg-builder.d.ts +3 -3
  33. package/types/provider/index.d.ts +1 -0
  34. package/types/provider/provider.d.ts +18 -0
  35. package/types/utils/babel.d.ts +1 -6
  36. package/types/utils/constants.d.ts +11 -0
  37. package/types/utils/index.d.ts +3 -0
  38. package/types/utils/types.d.ts +2 -7
@@ -0,0 +1,237 @@
1
+ import * as t from "@babel/types";
2
+ import { camel, pascal } from "case";
3
+ import { ProviderInfo, RenderContext } from "../context";
4
+ import { PROVIDER_TYPES } from "../utils/constants";
5
+ import { identifier, tsObjectPattern } from "../utils";
6
+
7
+ export const createProvider = (
8
+ name: string,
9
+ providerInfos: {
10
+ [key: string]: ProviderInfo;
11
+ }
12
+ ) => {
13
+ const classDeclaration = t.classDeclaration(
14
+ t.identifier(name),
15
+ t.identifier("ContractBase"),
16
+ t.classBody([
17
+ t.classMethod(
18
+ "constructor",
19
+ t.identifier("constructor"),
20
+ [
21
+ tsObjectPattern(
22
+ [
23
+ t.objectProperty(
24
+ t.identifier("address"),
25
+ t.identifier("address"),
26
+ false,
27
+ true
28
+ ),
29
+ t.objectProperty(
30
+ t.identifier("cosmWasmClient"),
31
+ t.identifier("cosmWasmClient"),
32
+ false,
33
+ true
34
+ ),
35
+ t.objectProperty(
36
+ t.identifier("signingCosmWasmClient"),
37
+ t.identifier("signingCosmWasmClient"),
38
+ false,
39
+ true
40
+ ),
41
+ ],
42
+ t.tsTypeAnnotation(
43
+ t.tsTypeReference(t.identifier("IContractConstructor"))
44
+ )
45
+ ),
46
+ ],
47
+ t.blockStatement([
48
+ t.expressionStatement(
49
+ t.callExpression(t.super(), [
50
+ t.identifier("address"),
51
+ t.identifier("cosmWasmClient"),
52
+ t.identifier("signingCosmWasmClient"),
53
+ t.identifier(
54
+ providerInfos[PROVIDER_TYPES.SIGNING_CLIENT_TYPE]
55
+ ? providerInfos[PROVIDER_TYPES.SIGNING_CLIENT_TYPE].classname
56
+ : "undefined"
57
+ ),
58
+ t.identifier(
59
+ providerInfos[PROVIDER_TYPES.QUERY_CLIENT_TYPE]
60
+ ? providerInfos[PROVIDER_TYPES.QUERY_CLIENT_TYPE].classname
61
+ : "undefined"
62
+ ),
63
+ t.identifier(
64
+ providerInfos[PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE]
65
+ ? providerInfos[PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE]
66
+ .classname
67
+ : "undefined"
68
+ ),
69
+ ])
70
+ ),
71
+ ])
72
+ ),
73
+ ])
74
+ );
75
+
76
+ classDeclaration.superTypeParameters = t.tsTypeParameterInstantiation([
77
+ t.tsTypeReference(
78
+ t.identifier(
79
+ providerInfos[PROVIDER_TYPES.SIGNING_CLIENT_TYPE]
80
+ ? providerInfos[PROVIDER_TYPES.SIGNING_CLIENT_TYPE].classname
81
+ : "IEmptyClient"
82
+ )
83
+ ),
84
+ t.tsTypeReference(
85
+ t.identifier(
86
+ providerInfos[PROVIDER_TYPES.QUERY_CLIENT_TYPE]
87
+ ? providerInfos[PROVIDER_TYPES.QUERY_CLIENT_TYPE].classname
88
+ : "IEmptyClient"
89
+ )
90
+ ),
91
+ t.tsTypeReference(
92
+ t.identifier(
93
+ providerInfos[PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE]
94
+ ? providerInfos[PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE].classname
95
+ : "IEmptyClient"
96
+ )
97
+ ),
98
+ ]);
99
+
100
+ return t.exportNamedDeclaration(classDeclaration);
101
+ };
102
+
103
+ export const createIContractsContext = (providerInfos: {
104
+ [key: string]: {
105
+ [key: string]: ProviderInfo;
106
+ };
107
+ }) => {
108
+ const properties: t.TSTypeElement[] = [];
109
+
110
+ for (const key in providerInfos) {
111
+ if (Object.prototype.hasOwnProperty.call(providerInfos, key)) {
112
+ const contractProviderInfo = providerInfos[key];
113
+
114
+ properties.push(createProperty(key, contractProviderInfo));
115
+ }
116
+ }
117
+
118
+ return t.exportNamedDeclaration(
119
+ t.tsInterfaceDeclaration(
120
+ t.identifier("IContractsContext"),
121
+ null,
122
+ null,
123
+ t.tsInterfaceBody(properties)
124
+ )
125
+ );
126
+ };
127
+
128
+ let PROVIDER_MAPPING = {};
129
+
130
+ PROVIDER_MAPPING[PROVIDER_TYPES.SIGNING_CLIENT_TYPE] = "ISigningClientProvider";
131
+ PROVIDER_MAPPING[PROVIDER_TYPES.QUERY_CLIENT_TYPE] = "IQueryClientProvider";
132
+ PROVIDER_MAPPING[PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE] =
133
+ "IMessageComposerProvider";
134
+
135
+ export const createProperty = (
136
+ name: string,
137
+ providerInfos: {
138
+ [key: string]: ProviderInfo;
139
+ }
140
+ ): t.TSTypeElement => {
141
+ let typeAnnotation: t.TSTypeAnnotation = null;
142
+
143
+ const keys = Object.keys(providerInfos);
144
+
145
+ if (keys?.length == 1) {
146
+ const key = keys[0];
147
+
148
+ typeAnnotation = t.tsTypeAnnotation(
149
+ t.tsTypeReference(
150
+ t.identifier(PROVIDER_MAPPING[key]),
151
+ t.tsTypeParameterInstantiation([
152
+ t.tsTypeReference(t.identifier(providerInfos[key].classname)),
153
+ ])
154
+ )
155
+ );
156
+ } else {
157
+ const typeRefs: t.TSTypeReference[] = [];
158
+
159
+ for (const key of keys) {
160
+ typeRefs.push(
161
+ t.tsTypeReference(
162
+ t.identifier(PROVIDER_MAPPING[key]),
163
+ t.tsTypeParameterInstantiation([
164
+ t.tsTypeReference(t.identifier(providerInfos[key].classname)),
165
+ ])
166
+ )
167
+ );
168
+ }
169
+
170
+ typeAnnotation = t.tsTypeAnnotation(t.tsIntersectionType(typeRefs));
171
+ }
172
+
173
+ return t.tsPropertySignature(t.identifier(camel(name)), typeAnnotation);
174
+ };
175
+
176
+ export const createGettingProviders = (providerInfos: {
177
+ [key: string]: {
178
+ [key: string]: ProviderInfo;
179
+ };
180
+ }) => {
181
+ const properties = [];
182
+
183
+ for (const key of Object.keys(providerInfos)) {
184
+ properties.push(
185
+ t.objectProperty(
186
+ t.identifier(camel(key)),
187
+ t.newExpression(t.identifier(pascal(key)), [
188
+ t.objectExpression([
189
+ t.objectProperty(
190
+ t.identifier("address"),
191
+ t.identifier("address"),
192
+ false,
193
+ true
194
+ ),
195
+ t.objectProperty(
196
+ t.identifier("cosmWasmClient"),
197
+ t.identifier("cosmWasmClient"),
198
+ false,
199
+ true
200
+ ),
201
+ t.objectProperty(
202
+ t.identifier("signingCosmWasmClient"),
203
+ t.identifier("signingCosmWasmClient"),
204
+ false,
205
+ true
206
+ ),
207
+ ]),
208
+ ])
209
+ )
210
+ );
211
+ }
212
+
213
+ return t.exportNamedDeclaration(
214
+ t.variableDeclaration("const", [
215
+ t.variableDeclarator(
216
+ t.identifier("getProviders"),
217
+ t.arrowFunctionExpression(
218
+ [
219
+ identifier(
220
+ "address?",
221
+ t.tsTypeAnnotation(t.tsTypeReference(t.identifier("string")))
222
+ ),
223
+ identifier(
224
+ "cosmWasmClient?",
225
+ t.tsTypeAnnotation(t.tsTypeReference(t.identifier("CosmWasmClient")))
226
+ ),
227
+ identifier(
228
+ "signingCosmWasmClient?",
229
+ t.tsTypeAnnotation(t.tsTypeReference(t.identifier("SigningCosmWasmClient")))
230
+ ),
231
+ ],
232
+ t.objectExpression(properties)
233
+ )
234
+ ),
235
+ ])
236
+ );
237
+ };
@@ -429,9 +429,9 @@ export const createReactQueryMutationArgsInterface = ({
429
429
  t.tsTypeAnnotation(
430
430
  // @ts-ignore:next-line
431
431
  t.tsTypeLiteral([
432
- propertySignature('fee', OPTIONAL_FEE_PARAM.typeAnnotation, true),
433
- propertySignature('memo', OPTIONAL_MEMO_PARAM.typeAnnotation, true),
434
- propertySignature('funds', OPTIONAL_FUNDS_PARAM.typeAnnotation, true)
432
+ propertySignature('fee', OPTIONAL_FEE_PARAM.typeAnnotation as t.TSTypeAnnotation, true),
433
+ propertySignature('memo', OPTIONAL_MEMO_PARAM.typeAnnotation as t.TSTypeAnnotation, true),
434
+ propertySignature('funds', OPTIONAL_FUNDS_PARAM.typeAnnotation as t.TSTypeAnnotation, true)
435
435
  ])
436
436
  )
437
437
  );
@@ -28,3 +28,10 @@ export const FIXED_EXECUTE_PARAMS = [
28
28
  OPTIONAL_MEMO_PARAM,
29
29
  OPTIONAL_FUNDS_PARAM
30
30
  ];
31
+
32
+ export const PROVIDER_TYPES = {
33
+ SIGNING_CLIENT_TYPE: "client",
34
+ QUERY_CLIENT_TYPE: "queryClient",
35
+ MESSAGE_COMPOSER_TYPE : 'message-composer',
36
+ PROVIDER_TYPE : 'provider'
37
+ };
@@ -3,3 +3,4 @@ export * from './types';
3
3
  export * from './ref';
4
4
  export { OPTIONAL_FUNDS_PARAM } from './constants';
5
5
  export { FIXED_EXECUTE_PARAMS } from './constants';
6
+ export { PROVIDER_TYPES } from './constants';
@@ -1,9 +1,7 @@
1
1
  import * as t from '@babel/types';
2
- import { QueryMsg, ExecuteMsg } from '../types';
2
+ import { ExecuteMsg, JSONSchema, QueryMsg } from '../types';
3
3
  import { RenderContext } from '../context';
4
- import { JSONSchema } from '../types';
5
4
  export declare const CONSTANT_EXEC_PARAMS: (t.AssignmentPattern | t.Identifier)[];
6
- export declare const FIXED_EXECUTE_PARAMS: t.Identifier[];
7
5
  export declare const createWasmQueryMethod: (context: RenderContext, jsonschema: any) => t.ClassProperty;
8
6
  export declare const createQueryClass: (context: RenderContext, className: string, implementsClassName: string, queryMsg: QueryMsg) => t.ExportNamedDeclaration;
9
7
  export declare const getWasmMethodArgs: (context: RenderContext, jsonschema: JSONSchema) => t.ObjectProperty[];
@@ -25,7 +25,6 @@ export interface RecoilOptions {
25
25
  }
26
26
  export interface TSTypesOptions {
27
27
  enabled?: boolean;
28
- // deprecated
29
28
  aliasExecuteMsg?: boolean;
30
29
  aliasEntryPoints?: boolean;
31
30
  }
@@ -57,28 +56,53 @@ export interface RenderOptions {
57
56
  client?: TSClientOptions;
58
57
  reactQuery?: ReactQueryOptions;
59
58
  }
59
+ export interface ProviderInfo {
60
+ classname: string;
61
+ filename: string;
62
+ basename: string;
63
+ }
60
64
  export interface IContext {
61
65
  refLookup($ref: string): any;
62
66
  addUtil(util: string): any;
63
- getImports(registeredUtils?: UtilMapping): any;
67
+ getImports(registeredUtils?: UtilMapping, filepath?: string): any;
64
68
  }
65
69
  export interface IRenderContext<TOpt = RenderOptions> extends IContext {
66
70
  contract: ContractInfo;
67
71
  options: TOpt;
72
+ addProviderInfo(contractName: string, type: string, classname: string, filename: string): void;
73
+ getProviderInfos(): {
74
+ [key: string]: {
75
+ [key: string]: ProviderInfo;
76
+ };
77
+ };
68
78
  }
69
79
  export declare const defaultOptions: RenderOptions;
70
80
  export declare const getDefinitionSchema: (schemas: JSONSchema[]) => JSONSchema;
81
+ export declare class BuilderContext {
82
+ providers: {
83
+ [key: string]: {
84
+ [key: string]: ProviderInfo;
85
+ };
86
+ };
87
+ addProviderInfo(contractName: string, type: string, classname: string, filename: string): void;
88
+ getProviderInfos(): {
89
+ [key: string]: {
90
+ [key: string]: ProviderInfo;
91
+ };
92
+ };
93
+ }
71
94
  /**
72
95
  * context object for generating code.
73
96
  * only mergeDefaultOpt needs to implementing for combine options and default options.
74
97
  * @param TOpt option type
75
98
  */
76
99
  export declare abstract class RenderContextBase<TOpt = RenderOptions> implements IRenderContext<TOpt> {
100
+ builderContext: BuilderContext;
77
101
  contract: ContractInfo;
78
102
  utils: string[];
79
103
  schema: JSONSchema;
80
104
  options: TOpt;
81
- constructor(contract: ContractInfo, options?: TOpt);
105
+ constructor(contract: ContractInfo, options?: TOpt, builderContext?: BuilderContext);
82
106
  /**
83
107
  * merge options and default options
84
108
  * @param options
@@ -86,7 +110,13 @@ export declare abstract class RenderContextBase<TOpt = RenderOptions> implements
86
110
  abstract mergeDefaultOpt(options: TOpt): TOpt;
87
111
  refLookup($ref: string): JSONSchema;
88
112
  addUtil(util: string): void;
89
- getImports(registeredUtils?: UtilMapping): any;
113
+ addProviderInfo(contractName: string, type: string, classname: string, filename: string): void;
114
+ getProviderInfos(): {
115
+ [key: string]: {
116
+ [key: string]: ProviderInfo;
117
+ };
118
+ };
119
+ getImports(registeredUtils?: UtilMapping, filepath?: string): any;
90
120
  }
91
121
  export declare class RenderContext extends RenderContextBase {
92
122
  mergeDefaultOpt(options: RenderOptions): RenderOptions;
@@ -40,6 +40,8 @@ export declare const UTILS: {
40
40
  name: any;
41
41
  };
42
42
  };
43
+ export declare const UTIL_HELPERS: string[];
43
44
  export declare const convertUtilsToImportList: (context: RenderContext, utils: string[], registeredUtils?: UtilMapping) => ImportObj[];
44
45
  export declare const convertUtil: (context: RenderContext, util: string, registeredUtils: object) => ImportObj;
45
- export declare const getImportStatements: (list: ImportObj[]) => any[];
46
+ export declare const getImportStatements: (list: ImportObj[], filepath?: string) => any[];
47
+ export declare const getRelativePath: (f1: string, f2: string) => string;
package/types/index.d.ts CHANGED
@@ -6,3 +6,4 @@ export * from './message-composer';
6
6
  export * from './react-query';
7
7
  export * from './types';
8
8
  export * from './msg-builder';
9
+ export * from './provider';
@@ -1,4 +1,4 @@
1
- import * as t from "@babel/types";
2
- import { ExecuteMsg, QueryMsg } from "../types";
3
- import { RenderContext } from "../context";
1
+ import * as t from '@babel/types';
2
+ import { ExecuteMsg, QueryMsg } from '../types';
3
+ import { RenderContext } from '../context';
4
4
  export declare const createMsgBuilderClass: (context: RenderContext, className: string, msg: ExecuteMsg | QueryMsg) => t.ExportNamedDeclaration;
@@ -0,0 +1 @@
1
+ export * from './provider';
@@ -0,0 +1,18 @@
1
+ import * as t from "@babel/types";
2
+ import { ProviderInfo } from "../context";
3
+ export declare const createProvider: (name: string, providerInfos: {
4
+ [key: string]: ProviderInfo;
5
+ }) => t.ExportNamedDeclaration;
6
+ export declare const createIContractsContext: (providerInfos: {
7
+ [key: string]: {
8
+ [key: string]: ProviderInfo;
9
+ };
10
+ }) => t.ExportNamedDeclaration;
11
+ export declare const createProperty: (name: string, providerInfos: {
12
+ [key: string]: ProviderInfo;
13
+ }) => t.TSTypeElement;
14
+ export declare const createGettingProviders: (providerInfos: {
15
+ [key: string]: {
16
+ [key: string]: ProviderInfo;
17
+ };
18
+ }) => t.ExportNamedDeclaration;
@@ -1,12 +1,7 @@
1
1
  import * as t from '@babel/types';
2
2
  import { Field } from '../types';
3
3
  import { TSTypeAnnotation, TSExpressionWithTypeArguments } from '@babel/types';
4
- export declare const propertySignature: (name: string, typeAnnotation: t.TSTypeAnnotation, optional?: boolean) => {
5
- type: string;
6
- key: t.Identifier;
7
- typeAnnotation: t.TSTypeAnnotation;
8
- optional: boolean;
9
- };
4
+ export declare const propertySignature: (name: string, typeAnnotation: TSTypeAnnotation, optional?: boolean) => t.TSPropertySignature;
10
5
  export declare const identifier: (name: string, typeAnnotation: t.TSTypeAnnotation, optional?: boolean) => t.Identifier;
11
6
  export declare const tsTypeOperator: (typeAnnotation: t.TSType, operator: string) => t.TSTypeOperator;
12
7
  export declare const getMessageProperties: (msg: any) => any[];
@@ -0,0 +1,11 @@
1
+ import * as t from '@babel/types';
2
+ export declare const OPTIONAL_FUNDS_PARAM: t.Identifier;
3
+ export declare const OPTIONAL_FEE_PARAM: t.Identifier;
4
+ export declare const OPTIONAL_MEMO_PARAM: t.Identifier;
5
+ export declare const FIXED_EXECUTE_PARAMS: t.Identifier[];
6
+ export declare const PROVIDER_TYPES: {
7
+ SIGNING_CLIENT_TYPE: string;
8
+ QUERY_CLIENT_TYPE: string;
9
+ MESSAGE_COMPOSER_TYPE: string;
10
+ PROVIDER_TYPE: string;
11
+ };
@@ -1,3 +1,6 @@
1
1
  export * from './babel';
2
2
  export * from './types';
3
3
  export * from './ref';
4
+ export { OPTIONAL_FUNDS_PARAM } from './constants';
5
+ export { FIXED_EXECUTE_PARAMS } from './constants';
6
+ export { PROVIDER_TYPES } from './constants';
@@ -17,11 +17,6 @@ export declare const getPropertyType: (context: RenderContext, schema: JSONSchem
17
17
  type: any;
18
18
  optional: boolean;
19
19
  };
20
- export declare function getPropertySignatureFromProp(context: RenderContext, jsonschema: JSONSchema, prop: string, camelize: boolean): {
21
- type: string;
22
- key: t.Identifier;
23
- typeAnnotation: t.TSTypeAnnotation;
24
- optional: boolean;
25
- };
20
+ export declare function getPropertySignatureFromProp(context: RenderContext, jsonschema: JSONSchema, prop: string, camelize: boolean): t.TSPropertySignature;
26
21
  export declare const getParamsTypeAnnotation: (context: RenderContext, jsonschema: any, camelize?: boolean) => t.TSTypeAnnotation;
27
- export declare const createTypedObjectParams: (context: RenderContext, jsonschema: JSONSchema, camelize?: boolean) => t.ObjectPattern;
22
+ export declare const createTypedObjectParams: (context: RenderContext, jsonschema: JSONSchema, camelize?: boolean) => (t.Identifier | t.Pattern | t.RestElement);