wasm-ast-types 0.23.1 → 0.25.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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';
@@ -56,28 +56,53 @@ export interface RenderOptions {
56
56
  client?: TSClientOptions;
57
57
  reactQuery?: ReactQueryOptions;
58
58
  }
59
+ export interface ProviderInfo {
60
+ classname: string;
61
+ filename: string;
62
+ basename: string;
63
+ }
59
64
  export interface IContext {
60
65
  refLookup($ref: string): any;
61
66
  addUtil(util: string): any;
62
- getImports(registeredUtils?: UtilMapping): any;
67
+ getImports(registeredUtils?: UtilMapping, filepath?: string): any;
63
68
  }
64
69
  export interface IRenderContext<TOpt = RenderOptions> extends IContext {
65
70
  contract: ContractInfo;
66
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
+ };
67
78
  }
68
79
  export declare const defaultOptions: RenderOptions;
69
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
+ }
70
94
  /**
71
95
  * context object for generating code.
72
96
  * only mergeDefaultOpt needs to implementing for combine options and default options.
73
97
  * @param TOpt option type
74
98
  */
75
99
  export declare abstract class RenderContextBase<TOpt = RenderOptions> implements IRenderContext<TOpt> {
100
+ builderContext: BuilderContext;
76
101
  contract: ContractInfo;
77
102
  utils: string[];
78
103
  schema: JSONSchema;
79
104
  options: TOpt;
80
- constructor(contract: ContractInfo, options?: TOpt);
105
+ constructor(contract: ContractInfo, options?: TOpt, builderContext?: BuilderContext);
81
106
  /**
82
107
  * merge options and default options
83
108
  * @param options
@@ -85,7 +110,13 @@ export declare abstract class RenderContextBase<TOpt = RenderOptions> implements
85
110
  abstract mergeDefaultOpt(options: TOpt): TOpt;
86
111
  refLookup($ref: string): JSONSchema;
87
112
  addUtil(util: string): void;
88
- 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;
89
120
  }
90
121
  export declare class RenderContext extends RenderContextBase {
91
122
  mergeDefaultOpt(options: RenderOptions): RenderOptions;
@@ -5,8 +5,8 @@ export interface ImportObj {
5
5
  path: string;
6
6
  importAs?: string;
7
7
  }
8
- export type GetUtilFn = (<TContext = RenderContext>(...args: any[]) => (context: TContext) => ImportObj);
9
- export type UtilMapping = {
8
+ export declare type GetUtilFn = (<TContext = RenderContext>(...args: any[]) => (context: TContext) => ImportObj);
9
+ export declare type UtilMapping = {
10
10
  [key: string]: ImportObj | string | GetUtilFn;
11
11
  };
12
12
  export declare const UTILS: {
@@ -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';
@@ -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;
@@ -3,3 +3,9 @@ export declare const OPTIONAL_FUNDS_PARAM: t.Identifier;
3
3
  export declare const OPTIONAL_FEE_PARAM: t.Identifier;
4
4
  export declare const OPTIONAL_MEMO_PARAM: t.Identifier;
5
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
+ };
@@ -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';