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,58 @@
1
+ import { createGettingProviders, createIContractsContext, createProvider } from "./provider";
2
+ import { expectCode } from "../../test-utils";
3
+ import { PROVIDER_TYPES } from "../utils/constants";
4
+ it("execute class", () => {
5
+ let info = {};
6
+ info[PROVIDER_TYPES.SIGNING_CLIENT_TYPE] = {
7
+ classname: "WhitelistClient"
8
+ };
9
+ info[PROVIDER_TYPES.QUERY_CLIENT_TYPE] = {
10
+ classname: "WhitelistQueryClient"
11
+ };
12
+ info[PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE] = {
13
+ classname: "WhitelistMessageComposer"
14
+ };
15
+ expectCode(createProvider("Whitelist", info));
16
+ });
17
+ it("execute class without message composer", () => {
18
+ let info = {};
19
+ info[PROVIDER_TYPES.SIGNING_CLIENT_TYPE] = {
20
+ classname: "WhitelistClient"
21
+ };
22
+ info[PROVIDER_TYPES.QUERY_CLIENT_TYPE] = {
23
+ classname: "WhitelistQueryClient"
24
+ };
25
+ expectCode(createProvider("Whitelist", info));
26
+ });
27
+ it("create IContractsContext", () => {
28
+ let info = {
29
+ Whitelist: {},
30
+ Marketplace: {}
31
+ };
32
+ info["Whitelist"][PROVIDER_TYPES.SIGNING_CLIENT_TYPE] = {
33
+ classname: "WhitelistClient"
34
+ };
35
+ info["Whitelist"][PROVIDER_TYPES.QUERY_CLIENT_TYPE] = {
36
+ classname: "WhitelistQueryClient"
37
+ };
38
+ info["Marketplace"][PROVIDER_TYPES.SIGNING_CLIENT_TYPE] = {
39
+ classname: "MarketplaceClient"
40
+ };
41
+ expectCode(createIContractsContext(info));
42
+ });
43
+ it("create getProviders", () => {
44
+ let info = {
45
+ Whitelist: {},
46
+ Marketplace: {}
47
+ };
48
+ info["Whitelist"][PROVIDER_TYPES.SIGNING_CLIENT_TYPE] = {
49
+ classname: "WhitelistClient"
50
+ };
51
+ info["Whitelist"][PROVIDER_TYPES.QUERY_CLIENT_TYPE] = {
52
+ classname: "WhitelistQueryClient"
53
+ };
54
+ info["Marketplace"][PROVIDER_TYPES.SIGNING_CLIENT_TYPE] = {
55
+ classname: "MarketplaceClient"
56
+ };
57
+ expectCode(createGettingProviders(info));
58
+ });
@@ -3,4 +3,10 @@ import * as t from '@babel/types';
3
3
  export const OPTIONAL_FUNDS_PARAM = identifier('_funds', t.tsTypeAnnotation(t.tsArrayType(t.tsTypeReference(t.identifier('Coin')))), true);
4
4
  export const OPTIONAL_FEE_PARAM = identifier('fee', t.tsTypeAnnotation(t.tsUnionType([t.tsNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), true);
5
5
  export const OPTIONAL_MEMO_PARAM = identifier('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true);
6
- export const FIXED_EXECUTE_PARAMS = [OPTIONAL_FEE_PARAM, OPTIONAL_MEMO_PARAM, OPTIONAL_FUNDS_PARAM];
6
+ export const FIXED_EXECUTE_PARAMS = [OPTIONAL_FEE_PARAM, OPTIONAL_MEMO_PARAM, OPTIONAL_FUNDS_PARAM];
7
+ export const PROVIDER_TYPES = {
8
+ SIGNING_CLIENT_TYPE: "client",
9
+ QUERY_CLIENT_TYPE: "queryClient",
10
+ MESSAGE_COMPOSER_TYPE: 'message-composer',
11
+ PROVIDER_TYPE: 'provider'
12
+ };
@@ -2,4 +2,5 @@ export * from './babel';
2
2
  export * from './types';
3
3
  export * from './ref';
4
4
  export { OPTIONAL_FUNDS_PARAM } from './constants';
5
- export { FIXED_EXECUTE_PARAMS } from './constants';
5
+ export { FIXED_EXECUTE_PARAMS } from './constants';
6
+ export { PROVIDER_TYPES } from './constants';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.23.0",
3
+ "version": "0.24.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",
@@ -88,5 +88,5 @@
88
88
  "case": "1.6.3",
89
89
  "deepmerge": "4.2.2"
90
90
  },
91
- "gitHead": "9979ae4b7209f920fe1b822ede2ba80ee0f2eade"
91
+ "gitHead": "4553eb80cd89969583df6164d1c764ab5b289c63"
92
92
  }
@@ -2,6 +2,7 @@ import { JSONSchema } from "../types";
2
2
  import { refLookup } from "../utils";
3
3
  import { convertUtilsToImportList, getImportStatements, UtilMapping } from "./imports";
4
4
  import deepmerge from "deepmerge";
5
+ import { basename, extname } from 'path'
5
6
 
6
7
  /// Plugin Types
7
8
  export interface ReactQueryOptions {
@@ -67,16 +68,28 @@ export interface RenderOptions {
67
68
  reactQuery?: ReactQueryOptions;
68
69
  }
69
70
 
71
+ export interface ProviderInfo{
72
+ classname: string,
73
+ filename: string,
74
+ basename: string,
75
+ }
70
76
 
71
77
  export interface IContext {
72
78
  refLookup($ref: string);
73
79
  addUtil(util: string);
74
- getImports(registeredUtils?: UtilMapping);
80
+ getImports(registeredUtils?: UtilMapping, filepath?: string);
75
81
  }
76
82
 
77
83
  export interface IRenderContext<TOpt = RenderOptions> extends IContext {
78
84
  contract: ContractInfo;
79
85
  options: TOpt;
86
+
87
+ addProviderInfo(contractName:string, type: string, classname: string, filename: string): void;
88
+ getProviderInfos(): {
89
+ [key: string]: {
90
+ [key: string]: ProviderInfo;
91
+ };
92
+ };
80
93
  }
81
94
 
82
95
  export const defaultOptions: RenderOptions = {
@@ -127,23 +140,54 @@ export const getDefinitionSchema = (schemas: JSONSchema[]): JSONSchema => {
127
140
  return aggregateSchema;
128
141
  };
129
142
 
143
+ export class BuilderContext{
144
+ providers:{
145
+ [key: string]: {
146
+ [key: string]: ProviderInfo;
147
+ };
148
+ } = {};
149
+
150
+ addProviderInfo(contractName:string, type: string, classname: string, filename: string): void {
151
+ if(!this.providers[contractName]){
152
+ this.providers[contractName] = {}
153
+ }
154
+
155
+ this.providers[contractName][type] = {
156
+ classname,
157
+ filename,
158
+ basename: basename(filename, extname(filename))
159
+ };
160
+ }
161
+ getProviderInfos(): {
162
+ [key: string]: {
163
+ [key: string]: ProviderInfo;
164
+ };
165
+ }{
166
+ return this.providers;
167
+ }
168
+ }
169
+
130
170
  /**
131
171
  * context object for generating code.
132
172
  * only mergeDefaultOpt needs to implementing for combine options and default options.
133
173
  * @param TOpt option type
134
174
  */
135
175
  export abstract class RenderContextBase<TOpt = RenderOptions> implements IRenderContext<TOpt> {
176
+ builderContext: BuilderContext;
136
177
  contract: ContractInfo;
137
178
  utils: string[] = [];
138
179
  schema: JSONSchema;
139
180
  options: TOpt;
181
+
140
182
  constructor(
141
183
  contract: ContractInfo,
142
- options?: TOpt
184
+ options?: TOpt,
185
+ builderContext?: BuilderContext
143
186
  ) {
144
187
  this.contract = contract;
145
188
  this.schema = getDefinitionSchema(contract.schemas);
146
189
  this.options = this.mergeDefaultOpt(options);
190
+ this.builderContext = builderContext;
147
191
  }
148
192
  /**
149
193
  * merge options and default options
@@ -156,13 +200,24 @@ export abstract class RenderContextBase<TOpt = RenderOptions> implements IRender
156
200
  addUtil(util: string) {
157
201
  this.utils[util] = true;
158
202
  }
159
- getImports(registeredUtils?: UtilMapping) {
203
+ addProviderInfo(contractName:string, type: string, classname: string, filename: string): void {
204
+ this.builderContext.addProviderInfo(contractName, type, classname, filename);
205
+ }
206
+ getProviderInfos(): {
207
+ [key: string]: {
208
+ [key: string]: ProviderInfo;
209
+ };
210
+ } {
211
+ return this.builderContext.providers;
212
+ }
213
+ getImports(registeredUtils?: UtilMapping, filepath?: string) {
160
214
  return getImportStatements(
161
215
  convertUtilsToImportList(
162
216
  this,
163
217
  Object.keys(this.utils),
164
218
  registeredUtils,
165
- )
219
+ ),
220
+ filepath
166
221
  );
167
222
  }
168
223
  }
@@ -1,6 +1,7 @@
1
1
  import * as t from '@babel/types';
2
2
  import { importAs, importStmt } from "../utils";
3
3
  import { RenderContext } from './context';
4
+ import { relative, dirname, extname } from 'path';
4
5
 
5
6
 
6
7
  export interface ImportObj {
@@ -57,6 +58,10 @@ export const UTILS = {
57
58
 
58
59
  };
59
60
 
61
+ export const UTIL_HELPERS = [
62
+ '__contractContextBase__',
63
+ ];
64
+
60
65
  export const convertUtilsToImportList = (
61
66
  context: RenderContext,
62
67
  utils: string[],
@@ -102,59 +107,89 @@ export const convertUtil = (
102
107
  }
103
108
  };
104
109
 
105
- export const getImportStatements = (list: ImportObj[]) => {
106
- const imports = list.reduce((m, obj) => {
107
- m[obj.path] = m[obj.path] || [];
108
- const exists = m[obj.path].find(el => el.type === obj.type && el.path === obj.path && el.name === obj.name);
109
110
 
110
- // TODO some have google.protobuf.Any shows up... figure out the better way to handle this
111
- if (/\./.test(obj.name)) {
112
- obj.name = obj.name.split('.')[obj.name.split('.').length - 1];
113
- }
111
+ // __helpers__
112
+ export const getImportStatements = (
113
+ list: ImportObj[],
114
+ filepath?: string
115
+ ) => {
116
+
117
+ // swap helpers with helpers file...
118
+ const modifiedImports = list.map(imp => {
119
+ if (filepath && UTIL_HELPERS.includes(imp.path)) {
120
+ const name = imp.path.replace(/__/g, '');
121
+ return {
122
+ ...imp,
123
+ path: getRelativePath(filepath, `./${name}`)
124
+ }
125
+ }
126
+ return imp;
127
+ });
114
128
 
115
- if (!exists) m[obj.path].push(obj);
116
- return m;
129
+ const imports = modifiedImports.reduce((m, obj) => {
130
+ m[obj.path] = m[obj.path] || [];
131
+ const exists = m[obj.path].find(el =>
132
+ el.type === obj.type && el.path === obj.path && el.name === obj.name);
133
+
134
+ // MARKED AS NOT DRY [google.protobuf names]
135
+ // TODO some have google.protobuf.Any shows up... figure out the better way to handle this
136
+ if (/\./.test(obj.name)) {
137
+ obj.name = obj.name.split('.')[obj.name.split('.').length - 1];
138
+ }
139
+
140
+ if (!exists) {
141
+ m[obj.path].push(obj);
142
+ }
143
+ return m;
117
144
  }, {})
118
145
 
146
+
119
147
  return Object.entries(imports)
120
- .reduce((m, [importPath, imports]: [string, ImportObj[]]) => {
121
- const defaultImports = imports.filter(a => a.type === 'default');
122
- if (defaultImports.length) {
123
- if (defaultImports.length > 1) throw new Error('more than one default name NOT allowed.')
124
- m.push(
125
- t.importDeclaration(
126
- [
127
- t.importDefaultSpecifier(
128
- t.identifier(defaultImports[0].name)
148
+ .reduce((m, [importPath, imports]: [string, ImportObj[]]) => {
149
+ const defaultImports = imports.filter(a => a.type === 'default');
150
+ if (defaultImports.length) {
151
+ if (defaultImports.length > 1) throw new Error('more than one default name NOT allowed.')
152
+ m.push(
153
+ t.importDeclaration(
154
+ [
155
+ t.importDefaultSpecifier(
156
+ t.identifier(defaultImports[0].name)
157
+ )
158
+ ],
159
+ t.stringLiteral(defaultImports[0].path)
160
+ )
129
161
  )
130
- ],
131
- t.stringLiteral(defaultImports[0].path)
132
- )
133
- )
134
- }
135
- const namedImports = imports.filter(a => a.type === 'import' && (!a.importAs || (a.name === a.importAs)));
136
- if (namedImports.length) {
137
- m.push(importStmt(namedImports.map(i => i.name), namedImports[0].path));
138
- }
139
- const aliasNamedImports = imports.filter(a => a.type === 'import' && (a.importAs && (a.name !== a.importAs)));
140
- aliasNamedImports.forEach(imp => {
141
- m.push(importAs(imp.name, imp.importAs, imp.path));
142
- });
143
-
144
- const namespaced = imports.filter(a => a.type === 'namespace');
145
- if (namespaced.length) {
146
- if (namespaced.length > 1) throw new Error('more than one namespaced name NOT allowed.')
147
- m.push(
148
- t.importDeclaration(
149
- [
150
- t.importNamespaceSpecifier(
151
- t.identifier(namespaced[0].name)
162
+ }
163
+ const namedImports = imports.filter(a => a.type === 'import' && (!a.importAs || (a.name === a.importAs)));
164
+ if (namedImports.length) {
165
+ m.push(importStmt(namedImports.map(i => i.name), namedImports[0].path));
166
+ }
167
+ const aliasNamedImports = imports.filter(a => a.type === 'import' && (a.importAs && (a.name !== a.importAs)));
168
+ aliasNamedImports.forEach(imp => {
169
+ m.push(importAs(imp.name, imp.importAs, imp.path));
170
+ });
171
+
172
+ const namespaced = imports.filter(a => a.type === 'namespace');
173
+ if (namespaced.length) {
174
+ if (namespaced.length > 1) throw new Error('more than one namespaced name NOT allowed.')
175
+ m.push(
176
+ t.importDeclaration(
177
+ [
178
+ t.importNamespaceSpecifier(
179
+ t.identifier(namespaced[0].name)
180
+ )
181
+ ],
182
+ t.stringLiteral(namespaced[0].path)
183
+ )
152
184
  )
153
- ],
154
- t.stringLiteral(namespaced[0].path)
155
- )
156
- )
157
- }
158
- return m;
159
- }, [])
160
- };
185
+ }
186
+ return m;
187
+ }, [])
188
+ };
189
+
190
+ export const getRelativePath = (f1: string, f2: string) => {
191
+ const rel = relative(dirname(f1), f2);
192
+ let importPath = rel.replace(extname(rel), '');
193
+ if (!/^\./.test(importPath)) importPath = `./${importPath}`;
194
+ return importPath;
195
+ }
package/src/index.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,49 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`create IContractsContext 1`] = `
4
+ "export interface IContractsContext {
5
+ whitelist: ISigningClientProvider<WhitelistClient> & IQueryClientProvider<WhitelistQueryClient>;
6
+ marketplace: ISigningClientProvider<MarketplaceClient>;
7
+ }"
8
+ `;
9
+
10
+ exports[`create getProviders 1`] = `
11
+ "export const getProviders = (address?: string, cosmWasmClient?: CosmWasmClient, signingCosmWasmClient?: SigningCosmWasmClient) => ({
12
+ whitelist: new Whitelist({
13
+ address,
14
+ cosmWasmClient,
15
+ signingCosmWasmClient
16
+ }),
17
+ marketplace: new Marketplace({
18
+ address,
19
+ cosmWasmClient,
20
+ signingCosmWasmClient
21
+ })
22
+ });"
23
+ `;
24
+
25
+ exports[`execute class 1`] = `
26
+ "export class Whitelist extends ContractBase<WhitelistClient, WhitelistQueryClient, WhitelistMessageComposer> {
27
+ constructor({
28
+ address,
29
+ cosmWasmClient,
30
+ signingCosmWasmClient
31
+ }: IContractConstructor) {
32
+ super(address, cosmWasmClient, signingCosmWasmClient, WhitelistClient, WhitelistQueryClient, WhitelistMessageComposer);
33
+ }
34
+
35
+ }"
36
+ `;
37
+
38
+ exports[`execute class without message composer 1`] = `
39
+ "export class Whitelist extends ContractBase<WhitelistClient, WhitelistQueryClient, IEmptyClient> {
40
+ constructor({
41
+ address,
42
+ cosmWasmClient,
43
+ signingCosmWasmClient
44
+ }: IContractConstructor) {
45
+ super(address, cosmWasmClient, signingCosmWasmClient, WhitelistClient, WhitelistQueryClient, undefined);
46
+ }
47
+
48
+ }"
49
+ `;
@@ -0,0 +1 @@
1
+ export * from './provider';
@@ -0,0 +1,81 @@
1
+ import execute_msg from "../../../../__fixtures__/basic/execute_msg_for__empty.json";
2
+ import query_msg from "../../../../__fixtures__/basic/query_msg.json";
3
+ import ownership from "../../../../__fixtures__/basic/ownership.json";
4
+ import { createGettingProviders, createIContractsContext, createProvider } from "./provider";
5
+
6
+ import { expectCode, makeContext } from "../../test-utils";
7
+ import { PROVIDER_TYPES } from "../utils/constants";
8
+
9
+ it("execute class", () => {
10
+ let info = {};
11
+
12
+ info[PROVIDER_TYPES.SIGNING_CLIENT_TYPE] = {
13
+ classname: "WhitelistClient",
14
+ };
15
+
16
+ info[PROVIDER_TYPES.QUERY_CLIENT_TYPE] = {
17
+ classname: "WhitelistQueryClient",
18
+ };
19
+
20
+ info[PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE] = {
21
+ classname: "WhitelistMessageComposer",
22
+ };
23
+
24
+ expectCode(createProvider("Whitelist", info));
25
+ });
26
+
27
+ it("execute class without message composer", () => {
28
+ let info = {};
29
+
30
+ info[PROVIDER_TYPES.SIGNING_CLIENT_TYPE] = {
31
+ classname: "WhitelistClient",
32
+ };
33
+
34
+ info[PROVIDER_TYPES.QUERY_CLIENT_TYPE] = {
35
+ classname: "WhitelistQueryClient",
36
+ };
37
+
38
+ expectCode(createProvider("Whitelist", info));
39
+ });
40
+
41
+ it("create IContractsContext", () => {
42
+ let info = {
43
+ Whitelist: {},
44
+ Marketplace: {},
45
+ };
46
+
47
+ info["Whitelist"][PROVIDER_TYPES.SIGNING_CLIENT_TYPE] = {
48
+ classname: "WhitelistClient",
49
+ };
50
+
51
+ info["Whitelist"][PROVIDER_TYPES.QUERY_CLIENT_TYPE] = {
52
+ classname: "WhitelistQueryClient",
53
+ };
54
+
55
+ info["Marketplace"][PROVIDER_TYPES.SIGNING_CLIENT_TYPE] = {
56
+ classname: "MarketplaceClient",
57
+ };
58
+
59
+ expectCode(createIContractsContext(info));
60
+ });
61
+
62
+ it("create getProviders", () => {
63
+ let info = {
64
+ Whitelist: {},
65
+ Marketplace: {},
66
+ };
67
+
68
+ info["Whitelist"][PROVIDER_TYPES.SIGNING_CLIENT_TYPE] = {
69
+ classname: "WhitelistClient",
70
+ };
71
+
72
+ info["Whitelist"][PROVIDER_TYPES.QUERY_CLIENT_TYPE] = {
73
+ classname: "WhitelistQueryClient",
74
+ };
75
+
76
+ info["Marketplace"][PROVIDER_TYPES.SIGNING_CLIENT_TYPE] = {
77
+ classname: "MarketplaceClient",
78
+ };
79
+
80
+ expectCode(createGettingProviders(info));
81
+ });