@cosmwasm/ts-codegen 1.7.0 → 1.7.2

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 (56) hide show
  1. package/README.md +14 -29
  2. package/builder/builder.js +11 -16
  3. package/esm/builder/builder.js +11 -16
  4. package/esm/{generators → helpers}/create-helpers.js +1 -9
  5. package/esm/index.js +0 -6
  6. package/esm/plugins/client.js +5 -6
  7. package/esm/plugins/index.js +6 -0
  8. package/esm/plugins/message-builder.js +2 -2
  9. package/esm/plugins/message-composer.js +3 -4
  10. package/esm/plugins/plugin-base.js +23 -6
  11. package/esm/plugins/provider-bundle.js +4 -2
  12. package/esm/plugins/provider.js +2 -2
  13. package/esm/plugins/react-query.js +2 -2
  14. package/esm/plugins/recoil.js +2 -2
  15. package/esm/plugins/types.js +3 -3
  16. package/esm/utils/contracts.js +5 -0
  17. package/{generators → helpers}/create-helpers.js +2 -10
  18. package/index.d.ts +0 -6
  19. package/index.js +0 -16
  20. package/package.json +6 -5
  21. package/plugins/client.d.ts +0 -1
  22. package/plugins/client.js +6 -7
  23. package/plugins/index.d.ts +6 -0
  24. package/plugins/index.js +6 -0
  25. package/plugins/message-builder.js +2 -2
  26. package/plugins/message-composer.d.ts +0 -1
  27. package/plugins/message-composer.js +4 -5
  28. package/plugins/plugin-base.d.ts +18 -4
  29. package/plugins/plugin-base.js +23 -6
  30. package/plugins/provider-bundle.d.ts +1 -1
  31. package/plugins/provider-bundle.js +4 -2
  32. package/plugins/provider.js +2 -2
  33. package/plugins/react-query.js +2 -2
  34. package/plugins/recoil.js +2 -2
  35. package/plugins/types.js +3 -3
  36. package/utils/contracts.d.ts +2 -0
  37. package/utils/contracts.js +9 -0
  38. package/esm/generators/client.js +0 -67
  39. package/esm/generators/message-builder.js +0 -58
  40. package/esm/generators/message-composer.js +0 -55
  41. package/esm/generators/react-query.js +0 -76
  42. package/esm/generators/recoil.js +0 -58
  43. package/esm/generators/types.js +0 -58
  44. package/generators/client.d.ts +0 -5
  45. package/generators/client.js +0 -95
  46. package/generators/message-builder.d.ts +0 -5
  47. package/generators/message-builder.js +0 -86
  48. package/generators/message-composer.d.ts +0 -5
  49. package/generators/message-composer.js +0 -83
  50. package/generators/react-query.d.ts +0 -4
  51. package/generators/react-query.js +0 -104
  52. package/generators/recoil.d.ts +0 -4
  53. package/generators/recoil.js +0 -86
  54. package/generators/types.d.ts +0 -4
  55. package/generators/types.js +0 -86
  56. /package/{generators → helpers}/create-helpers.d.ts +0 -0
package/plugins/client.js CHANGED
@@ -23,19 +23,18 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.ClientPlugin = exports.TYPE = void 0;
26
+ exports.ClientPlugin = void 0;
27
27
  const case_1 = require("case");
28
28
  const w = __importStar(require("wasm-ast-types"));
29
29
  const utils_1 = require("../utils");
30
30
  const wasm_ast_types_1 = require("wasm-ast-types");
31
31
  const plugin_base_1 = require("./plugin-base");
32
- exports.TYPE = "client";
33
32
  class ClientPlugin extends plugin_base_1.BuilderPluginBase {
34
33
  initContext(contract, options) {
35
- return new wasm_ast_types_1.RenderContext(contract, options, this.builder.builderContext);
34
+ return new wasm_ast_types_1.RenderContext(contract, options, this.builder?.builderContext);
36
35
  }
37
36
  async doRender(name, context) {
38
- const { enabled } = this.option.client;
37
+ const { enabled } = this.options.client;
39
38
  if (!enabled) {
40
39
  return;
41
40
  }
@@ -65,8 +64,8 @@ class ClientPlugin extends plugin_base_1.BuilderPluginBase {
65
64
  if (children.length > 0) {
66
65
  Client = (0, case_1.pascal)(`${name}Client`);
67
66
  Instance = (0, case_1.pascal)(`${name}Interface`);
68
- body.push(w.createExecuteInterface(context, Instance, this.option.client.execExtendsQuery ? ReadOnlyInstance : null, ExecuteMsg));
69
- body.push(w.createExecuteClass(context, Client, Instance, this.option.client.execExtendsQuery ? QueryClient : null, ExecuteMsg));
67
+ body.push(w.createExecuteInterface(context, Instance, this.options.client.execExtendsQuery ? ReadOnlyInstance : null, ExecuteMsg));
68
+ body.push(w.createExecuteClass(context, Client, Instance, this.options.client.execExtendsQuery ? QueryClient : null, ExecuteMsg));
70
69
  context.addProviderInfo(name, w.PROVIDER_TYPES.SIGNING_CLIENT_TYPE, Client, localname);
71
70
  }
72
71
  }
@@ -76,7 +75,7 @@ class ClientPlugin extends plugin_base_1.BuilderPluginBase {
76
75
  }
77
76
  return [
78
77
  {
79
- type: exports.TYPE,
78
+ type: "client",
80
79
  localname,
81
80
  body,
82
81
  },
@@ -1 +1,7 @@
1
1
  export * from "./plugin-base";
2
+ export * from "./client";
3
+ export * from "./react-query";
4
+ export * from "./recoil";
5
+ export * from "./message-builder";
6
+ export * from "./message-composer";
7
+ export * from "./types";
package/plugins/index.js CHANGED
@@ -15,3 +15,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./plugin-base"), exports);
18
+ __exportStar(require("./client"), exports);
19
+ __exportStar(require("./react-query"), exports);
20
+ __exportStar(require("./recoil"), exports);
21
+ __exportStar(require("./message-builder"), exports);
22
+ __exportStar(require("./message-composer"), exports);
23
+ __exportStar(require("./types"), exports);
@@ -31,10 +31,10 @@ const wasm_ast_types_1 = require("wasm-ast-types");
31
31
  const plugin_base_1 = require("./plugin-base");
32
32
  class MessageBuilderPlugin extends plugin_base_1.BuilderPluginBase {
33
33
  initContext(contract, options) {
34
- return new wasm_ast_types_1.RenderContext(contract, options, this.builder.builderContext);
34
+ return new wasm_ast_types_1.RenderContext(contract, options, this.builder?.builderContext);
35
35
  }
36
36
  async doRender(name, context) {
37
- const { enabled } = this.option.messageBuilder;
37
+ const { enabled } = this.options.messageBuilder;
38
38
  if (!enabled) {
39
39
  return;
40
40
  }
@@ -1,7 +1,6 @@
1
1
  import { ContractInfo, RenderContextBase, RenderContext, RenderOptions } from "wasm-ast-types";
2
2
  import { BuilderFileType } from "../builder";
3
3
  import { BuilderPluginBase } from "./plugin-base";
4
- export declare const TYPE = "message-composer";
5
4
  export declare class MessageComposerPlugin extends BuilderPluginBase<RenderOptions> {
6
5
  initContext(contract: ContractInfo, options?: RenderOptions): RenderContextBase<RenderOptions>;
7
6
  doRender(name: string, context: RenderContext): Promise<{
@@ -23,19 +23,18 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.MessageComposerPlugin = exports.TYPE = void 0;
26
+ exports.MessageComposerPlugin = void 0;
27
27
  const case_1 = require("case");
28
28
  const w = __importStar(require("wasm-ast-types"));
29
29
  const utils_1 = require("../utils");
30
30
  const wasm_ast_types_1 = require("wasm-ast-types");
31
31
  const plugin_base_1 = require("./plugin-base");
32
- exports.TYPE = "message-composer";
33
32
  class MessageComposerPlugin extends plugin_base_1.BuilderPluginBase {
34
33
  initContext(contract, options) {
35
- return new wasm_ast_types_1.RenderContext(contract, options, this.builder.builderContext);
34
+ return new wasm_ast_types_1.RenderContext(contract, options, this.builder?.builderContext);
36
35
  }
37
36
  async doRender(name, context) {
38
- const { enabled } = this.option.messageComposer;
37
+ const { enabled } = this.options.messageComposer;
39
38
  if (!enabled) {
40
39
  return;
41
40
  }
@@ -63,7 +62,7 @@ class MessageComposerPlugin extends plugin_base_1.BuilderPluginBase {
63
62
  }
64
63
  return [
65
64
  {
66
- type: exports.TYPE,
65
+ type: "message-composer",
67
66
  localname,
68
67
  body,
69
68
  },
@@ -10,6 +10,11 @@ export interface IBuilderPlugin {
10
10
  utils: UtilMapping;
11
11
  builder?: TSBuilder;
12
12
  setBuilder(builder: TSBuilder): void;
13
+ /**
14
+ * prop to indicate to execute the render function in the lifecycle of main process or after
15
+ */
16
+ readonly lifecycle: "main" | "after";
17
+ defaultContractName?: string;
13
18
  /**
14
19
  * render generated cdoe.
15
20
  * @param name the name of contract
@@ -17,7 +22,7 @@ export interface IBuilderPlugin {
17
22
  * @param outPath the path of generated code.
18
23
  * @returns info of generated files.
19
24
  */
20
- render(name: string, contractInfo: ContractInfo, outPath: string): Promise<BuilderFile[]>;
25
+ render(outPath: string, name?: string, contractInfo?: ContractInfo): Promise<BuilderFile[]>;
21
26
  }
22
27
  /**
23
28
  * BuilderPluginBase enable ts-codegen users implement their own plugins by only implement a few functions.
@@ -26,11 +31,16 @@ export declare abstract class BuilderPluginBase<TOpt extends {
26
31
  enabled?: boolean;
27
32
  }> implements IBuilderPlugin {
28
33
  builder?: TSBuilder;
29
- option: TOpt;
34
+ options: TOpt;
30
35
  utils: UtilMapping;
31
- constructor(opt: TOpt, builder?: TSBuilder);
36
+ /**
37
+ * prop to indicate to execute the render function in the lifecycle of main process or after
38
+ */
39
+ lifecycle: "main" | "after";
40
+ defaultContractName?: string;
41
+ constructor(opts?: TOpt, builder?: TSBuilder);
32
42
  setBuilder(builder: TSBuilder): void;
33
- render(name: string, contractInfo: ContractInfo, outPath: string): Promise<BuilderFile[]>;
43
+ render(outPath: string, name?: string, contractInfo?: ContractInfo): Promise<BuilderFile[]>;
34
44
  /**
35
45
  * init context here
36
46
  * @param contract
@@ -48,4 +58,8 @@ export declare abstract class BuilderPluginBase<TOpt extends {
48
58
  localname: string;
49
59
  body: any[];
50
60
  }[]>;
61
+ /**
62
+ * get default options
63
+ */
64
+ getDefaultOptions(opts: TOpt): any;
51
65
  }
@@ -31,28 +31,39 @@ const mkdirp_1 = require("mkdirp");
31
31
  const path_1 = require("path");
32
32
  const fs_1 = require("fs");
33
33
  const header_1 = require("../utils/header");
34
+ const wasm_ast_types_1 = require("wasm-ast-types");
34
35
  const generator_1 = __importDefault(require("@babel/generator"));
35
36
  const t = __importStar(require("@babel/types"));
37
+ const deepmerge_1 = __importDefault(require("deepmerge"));
36
38
  /**
37
39
  * BuilderPluginBase enable ts-codegen users implement their own plugins by only implement a few functions.
38
40
  */
39
41
  class BuilderPluginBase {
40
42
  builder;
41
- option;
43
+ options;
42
44
  utils;
43
- constructor(opt, builder) {
44
- this.option = opt;
45
+ /**
46
+ * prop to indicate to execute the render function in the lifecycle of main process or after
47
+ */
48
+ lifecycle;
49
+ defaultContractName;
50
+ constructor(opts, builder) {
51
+ this.options = opts;
45
52
  this.builder = builder;
53
+ this.lifecycle = "main";
46
54
  }
47
55
  setBuilder(builder) {
48
56
  this.builder = builder;
49
57
  }
50
- async render(name, contractInfo, outPath) {
51
- const { enabled } = this.option;
58
+ async render(outPath, name, contractInfo) {
59
+ if (!this.options) {
60
+ this.options = this.getDefaultOptions(this.options);
61
+ }
62
+ const { enabled } = this.options;
52
63
  if (!enabled) {
53
64
  return;
54
65
  }
55
- const context = this.initContext(contractInfo, this.option);
66
+ const context = this.initContext(contractInfo, this.options);
56
67
  const results = await this.doRender(name, context);
57
68
  if (!results || !results.length) {
58
69
  return [];
@@ -73,5 +84,11 @@ class BuilderPluginBase {
73
84
  };
74
85
  });
75
86
  }
87
+ /**
88
+ * get default options
89
+ */
90
+ getDefaultOptions(opts) {
91
+ return (0, deepmerge_1.default)(wasm_ast_types_1.defaultOptions, opts ?? {});
92
+ }
76
93
  }
77
94
  exports.BuilderPluginBase = BuilderPluginBase;
@@ -2,7 +2,7 @@ import { ContractInfo, RenderContextBase, RenderContext } from "wasm-ast-types";
2
2
  import { BuilderFileType, TSBuilderOptions } from "../builder";
3
3
  import { BuilderPluginBase } from "./plugin-base";
4
4
  export declare class ContractsProviderBundlePlugin extends BuilderPluginBase<TSBuilderOptions> {
5
- constructor(opt: TSBuilderOptions);
5
+ constructor(opt?: TSBuilderOptions);
6
6
  initContext(contract: ContractInfo, options?: TSBuilderOptions): RenderContextBase<TSBuilderOptions>;
7
7
  doRender(name: string, context: RenderContext): Promise<{
8
8
  type: BuilderFileType;
@@ -32,6 +32,8 @@ const provider_1 = require("./provider");
32
32
  class ContractsProviderBundlePlugin extends plugin_base_1.BuilderPluginBase {
33
33
  constructor(opt) {
34
34
  super(opt);
35
+ this.lifecycle = "after";
36
+ this.defaultContractName = "contractContextProviders";
35
37
  this.utils = {
36
38
  CosmWasmClient: "@cosmjs/cosmwasm-stargate",
37
39
  SigningCosmWasmClient: "@cosmjs/cosmwasm-stargate",
@@ -41,10 +43,10 @@ class ContractsProviderBundlePlugin extends plugin_base_1.BuilderPluginBase {
41
43
  };
42
44
  }
43
45
  initContext(contract, options) {
44
- return new wasm_ast_types_1.RenderContext(contract, options, this.builder.builderContext);
46
+ return new wasm_ast_types_1.RenderContext(contract, options, this.builder?.builderContext);
45
47
  }
46
48
  async doRender(name, context) {
47
- if (!this.option?.useContractsHook?.enabled) {
49
+ if (!this.options?.useContractsHook?.enabled) {
48
50
  return;
49
51
  }
50
52
  const providerInfos = context.getProviderInfos();
@@ -42,10 +42,10 @@ class ContractsContextProviderPlugin extends plugin_base_1.BuilderPluginBase {
42
42
  };
43
43
  }
44
44
  initContext(contract, options) {
45
- return new wasm_ast_types_1.RenderContext(contract, options, this.builder.builderContext);
45
+ return new wasm_ast_types_1.RenderContext(contract, options, this.builder?.builderContext);
46
46
  }
47
47
  async doRender(name, context) {
48
- if (!this.option?.useContractsHook?.enabled) {
48
+ if (!this.options?.useContractsHook?.enabled) {
49
49
  return;
50
50
  }
51
51
  const providerInfo = context.getProviderInfos()[name];
@@ -31,10 +31,10 @@ const wasm_ast_types_1 = require("wasm-ast-types");
31
31
  const plugin_base_1 = require("./plugin-base");
32
32
  class ReactQueryPlugin extends plugin_base_1.BuilderPluginBase {
33
33
  initContext(contract, options) {
34
- return new wasm_ast_types_1.RenderContext(contract, options, this.builder.builderContext);
34
+ return new wasm_ast_types_1.RenderContext(contract, options, this.builder?.builderContext);
35
35
  }
36
36
  async doRender(name, context) {
37
- const options = this.option.reactQuery;
37
+ const options = this.options.reactQuery;
38
38
  const { enabled } = options;
39
39
  if (!enabled) {
40
40
  return;
package/plugins/recoil.js CHANGED
@@ -34,10 +34,10 @@ class RecoilPlugin extends plugin_base_1.BuilderPluginBase {
34
34
  selectorFamily: 'recoil',
35
35
  };
36
36
  initContext(contract, options) {
37
- return new wasm_ast_types_1.RenderContext(contract, options, this.builder.builderContext);
37
+ return new wasm_ast_types_1.RenderContext(contract, options, this.builder?.builderContext);
38
38
  }
39
39
  async doRender(name, context) {
40
- const { enabled } = this.option.recoil;
40
+ const { enabled } = this.options.recoil;
41
41
  if (!enabled) {
42
42
  return;
43
43
  }
package/plugins/types.js CHANGED
@@ -32,15 +32,15 @@ const wasm_ast_types_1 = require("wasm-ast-types");
32
32
  const plugin_base_1 = require("./plugin-base");
33
33
  class TypesPlugin extends plugin_base_1.BuilderPluginBase {
34
34
  initContext(contract, options) {
35
- return new wasm_ast_types_1.RenderContext(contract, options, this.builder.builderContext);
35
+ return new wasm_ast_types_1.RenderContext(contract, options, this.builder?.builderContext);
36
36
  }
37
37
  async doRender(name, context) {
38
- const { enabled } = this.option.types;
38
+ const { enabled } = this.options.types;
39
39
  if (!enabled) {
40
40
  return;
41
41
  }
42
42
  const { schemas } = context.contract;
43
- const options = this.option.types;
43
+ const options = this.options.types;
44
44
  const localname = (0, case_1.pascal)(name) + '.types.ts';
45
45
  const ExecuteMsg = (0, utils_1.findExecuteMsg)(schemas);
46
46
  const typeHash = await (0, utils_1.findAndParseTypes)(schemas);
@@ -0,0 +1,2 @@
1
+ import { ContractInfo } from "wasm-ast-types";
2
+ export declare const createDefaultContractInfo: () => ContractInfo;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createDefaultContractInfo = void 0;
4
+ const createDefaultContractInfo = () => {
5
+ return {
6
+ schemas: [],
7
+ };
8
+ };
9
+ exports.createDefaultContractInfo = createDefaultContractInfo;
@@ -1,67 +0,0 @@
1
- import { pascal } from "case";
2
- import { header } from '../utils/header';
3
- import { join } from "path";
4
- import { sync as mkdirp } from "mkdirp";
5
- import * as w from 'wasm-ast-types';
6
- import * as t from '@babel/types';
7
- import { writeFileSync } from 'fs';
8
- import generate from "@babel/generator";
9
- import { getMessageProperties } from "wasm-ast-types";
10
- import { findAndParseTypes, findExecuteMsg, findQueryMsg } from '../utils';
11
- import { RenderContext } from "wasm-ast-types";
12
- export default async (name, contractInfo, outPath, tsClientOptions) => {
13
- const { schemas } = contractInfo;
14
- const context = new RenderContext(contractInfo, {
15
- client: tsClientOptions ?? {}
16
- });
17
- // const options = context.options.client;
18
- const localname = pascal(name) + '.client.ts';
19
- const TypesFile = pascal(name) + '.types';
20
- const QueryMsg = findQueryMsg(schemas);
21
- const ExecuteMsg = findExecuteMsg(schemas);
22
- const typeHash = await findAndParseTypes(schemas);
23
- let Client = null;
24
- let Instance = null;
25
- let QueryClient = null;
26
- let ReadOnlyInstance = null;
27
- const body = [];
28
- body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
29
- // query messages
30
- if (QueryMsg) {
31
- QueryClient = pascal(`${name}QueryClient`);
32
- ReadOnlyInstance = pascal(`${name}ReadOnlyInterface`);
33
- body.push(w.createQueryInterface(context, ReadOnlyInstance, QueryMsg));
34
- body.push(w.createQueryClass(context, QueryClient, ReadOnlyInstance, QueryMsg));
35
- }
36
- // execute messages
37
- if (ExecuteMsg) {
38
- const children = getMessageProperties(ExecuteMsg);
39
- if (children.length > 0) {
40
- Client = pascal(`${name}Client`);
41
- Instance = pascal(`${name}Interface`);
42
- body.push(w.createExecuteInterface(context, Instance, context.options.client.execExtendsQuery ? ReadOnlyInstance : null, ExecuteMsg));
43
- body.push(w.createExecuteClass(context, Client, Instance, context.options.client.execExtendsQuery ? QueryClient : null, ExecuteMsg));
44
- }
45
- }
46
- if (typeHash.hasOwnProperty('Coin')) {
47
- // @ts-ignore
48
- delete context.utils.Coin;
49
- }
50
- const imports = context.getImports();
51
- const code = header + generate(
52
- // @ts-ignore
53
- t.program([
54
- ...imports,
55
- ...body
56
- ])).code;
57
- mkdirp(outPath);
58
- writeFileSync(join(outPath, localname), code);
59
- return [
60
- {
61
- type: 'client',
62
- contract: name,
63
- localname,
64
- filename: join(outPath, localname),
65
- }
66
- ];
67
- };
@@ -1,58 +0,0 @@
1
- import { pascal } from "case";
2
- import { header } from "../utils/header";
3
- import { join } from "path";
4
- import { sync as mkdirp } from "mkdirp";
5
- import * as w from "wasm-ast-types";
6
- import * as t from "@babel/types";
7
- import { writeFileSync } from "fs";
8
- import generate from "@babel/generator";
9
- import { getMessageProperties } from "wasm-ast-types";
10
- import { findAndParseTypes, findExecuteMsg, findQueryMsg } from '../utils';
11
- import { RenderContext } from 'wasm-ast-types';
12
- export default async (name, contractInfo, outPath, messageBuilderOptions) => {
13
- const { schemas } = contractInfo;
14
- const context = new RenderContext(contractInfo, {
15
- messageBuilder: messageBuilderOptions ?? {},
16
- });
17
- const localname = pascal(name) + ".message-builder.ts";
18
- const TypesFile = pascal(name) + ".types";
19
- const ExecuteMsg = findExecuteMsg(schemas);
20
- const typeHash = await findAndParseTypes(schemas);
21
- const body = [];
22
- body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
23
- body.push(w.importStmt(["CamelCasedProperties"], "type-fest"));
24
- // execute messages
25
- if (ExecuteMsg) {
26
- const children = getMessageProperties(ExecuteMsg);
27
- if (children.length > 0) {
28
- const className = pascal(`${name}ExecuteMsgBuilder`);
29
- body.push(w.createMessageBuilderClass(context, className, ExecuteMsg));
30
- }
31
- }
32
- const QueryMsg = findQueryMsg(schemas);
33
- // query messages
34
- if (QueryMsg) {
35
- const children = getMessageProperties(QueryMsg);
36
- if (children.length > 0) {
37
- const className = pascal(`${name}QueryMsgBuilder`);
38
- body.push(w.createMessageBuilderClass(context, className, QueryMsg));
39
- }
40
- }
41
- if (typeHash.hasOwnProperty("Coin")) {
42
- // @ts-ignore
43
- delete context.utils.Coin;
44
- }
45
- const imports = context.getImports();
46
- // @ts-ignore
47
- const code = header + generate(t.program([...imports, ...body])).code;
48
- mkdirp(outPath);
49
- writeFileSync(join(outPath, localname), code);
50
- return [
51
- {
52
- type: "message-builder",
53
- contract: name,
54
- localname,
55
- filename: join(outPath, localname),
56
- },
57
- ];
58
- };
@@ -1,55 +0,0 @@
1
- import { pascal } from "case";
2
- import { header } from '../utils/header';
3
- import { join } from "path";
4
- import { sync as mkdirp } from "mkdirp";
5
- import * as w from 'wasm-ast-types';
6
- import * as t from '@babel/types';
7
- import { writeFileSync } from 'fs';
8
- import generate from "@babel/generator";
9
- import { getMessageProperties } from "wasm-ast-types";
10
- import { findAndParseTypes, findExecuteMsg } from "../utils";
11
- import { RenderContext } from "wasm-ast-types";
12
- export default async (name, contractInfo, outPath, messageComposerOptions) => {
13
- const { schemas } = contractInfo;
14
- const context = new RenderContext(contractInfo, {
15
- messageComposer: messageComposerOptions ?? {}
16
- });
17
- // const options = context.options.messageComposer;
18
- const localname = pascal(name) + '.message-composer.ts';
19
- const TypesFile = pascal(name) + '.types';
20
- const ExecuteMsg = findExecuteMsg(schemas);
21
- const typeHash = await findAndParseTypes(schemas);
22
- const body = [];
23
- body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
24
- // execute messages
25
- if (ExecuteMsg) {
26
- const children = getMessageProperties(ExecuteMsg);
27
- if (children.length > 0) {
28
- const TheClass = pascal(`${name}MessageComposer`);
29
- const Interface = pascal(`${name}Message`);
30
- body.push(w.createMessageComposerInterface(context, Interface, ExecuteMsg));
31
- body.push(w.createMessageComposerClass(context, TheClass, Interface, ExecuteMsg));
32
- }
33
- }
34
- if (typeHash.hasOwnProperty('Coin')) {
35
- // @ts-ignore
36
- delete context.utils.Coin;
37
- }
38
- const imports = context.getImports();
39
- const code = header + generate(
40
- // @ts-ignore
41
- t.program([
42
- ...imports,
43
- ...body
44
- ])).code;
45
- mkdirp(outPath);
46
- writeFileSync(join(outPath, localname), code);
47
- return [
48
- {
49
- type: 'message-composer',
50
- contract: name,
51
- localname,
52
- filename: join(outPath, localname),
53
- }
54
- ];
55
- };
@@ -1,76 +0,0 @@
1
- import { pascal } from "case";
2
- import { header } from '../utils/header';
3
- import { join } from "path";
4
- import { sync as mkdirp } from "mkdirp";
5
- import * as w from 'wasm-ast-types';
6
- import { RenderContext } from 'wasm-ast-types';
7
- import * as t from '@babel/types';
8
- import { writeFileSync } from 'fs';
9
- import generate from "@babel/generator";
10
- import { findAndParseTypes, findExecuteMsg, findQueryMsg } from '../utils';
11
- import { getMessageProperties } from "wasm-ast-types";
12
- export default async (contractName, contractInfo, outPath, reactQueryOptions) => {
13
- const { schemas } = contractInfo;
14
- const context = new RenderContext(contractInfo, {
15
- reactQuery: reactQueryOptions ?? {}
16
- });
17
- const options = context.options.reactQuery;
18
- const localname = pascal(`${contractName}`) + '.react-query.ts';
19
- const ContractFile = pascal(`${contractName}`) + '.client';
20
- const TypesFile = pascal(`${contractName}`) + '.types';
21
- const QueryMsg = findQueryMsg(schemas);
22
- const ExecuteMsg = findExecuteMsg(schemas);
23
- const typeHash = await findAndParseTypes(schemas);
24
- const ExecuteClient = pascal(`${contractName}Client`);
25
- const QueryClient = pascal(`${contractName}QueryClient`);
26
- const body = [];
27
- const clientImports = [];
28
- QueryMsg && clientImports.push(QueryClient);
29
- // check that there are commands within the exec msg
30
- const shouldGenerateMutationHooks = ExecuteMsg && options?.version === 'v4' && options?.mutations && getMessageProperties(ExecuteMsg).length > 0;
31
- if (shouldGenerateMutationHooks) {
32
- clientImports.push(ExecuteClient);
33
- }
34
- // general contract imports
35
- body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
36
- // client imports
37
- body.push(w.importStmt(clientImports, `./${ContractFile}`));
38
- // query messages
39
- if (QueryMsg) {
40
- [].push.apply(body, w.createReactQueryHooks({
41
- context,
42
- queryMsg: QueryMsg,
43
- contractName: contractName,
44
- QueryClient
45
- }));
46
- }
47
- if (shouldGenerateMutationHooks) {
48
- [].push.apply(body, w.createReactQueryMutationHooks({
49
- context,
50
- execMsg: ExecuteMsg,
51
- contractName: contractName,
52
- ExecuteClient
53
- }));
54
- }
55
- if (typeHash.hasOwnProperty('Coin')) {
56
- // @ts-ignore
57
- delete context.utils.Coin;
58
- }
59
- const imports = context.getImports();
60
- const code = header + generate(
61
- // @ts-ignore
62
- t.program([
63
- ...imports,
64
- ...body
65
- ])).code;
66
- mkdirp(outPath);
67
- writeFileSync(join(outPath, localname), code);
68
- return [
69
- {
70
- type: 'react-query',
71
- contract: contractName,
72
- localname,
73
- filename: join(outPath, localname),
74
- }
75
- ];
76
- };
@@ -1,58 +0,0 @@
1
- import { pascal } from "case";
2
- import { header } from '../utils/header';
3
- import { join } from "path";
4
- import { sync as mkdirp } from "mkdirp";
5
- import * as w from 'wasm-ast-types';
6
- import * as t from '@babel/types';
7
- import { writeFileSync } from 'fs';
8
- import generate from "@babel/generator";
9
- import { findAndParseTypes, findQueryMsg } from "../utils";
10
- import { RenderContext } from "wasm-ast-types";
11
- export default async (name, contractInfo, outPath, recoilOptions) => {
12
- const { schemas } = contractInfo;
13
- const context = new RenderContext(contractInfo, {
14
- recoil: recoilOptions ?? {}
15
- });
16
- const options = context.options.recoil;
17
- const localname = pascal(name) + '.recoil.ts';
18
- const ContractFile = pascal(name) + '.client';
19
- const TypesFile = pascal(name) + '.types';
20
- const QueryMsg = findQueryMsg(schemas);
21
- const typeHash = await findAndParseTypes(schemas);
22
- let QueryClient = null;
23
- let ReadOnlyInstance = null;
24
- const body = [];
25
- body.push(w.importStmt(['cosmWasmClient'], './chain'));
26
- body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
27
- // query messages
28
- if (QueryMsg) {
29
- QueryClient = pascal(`${name}QueryClient`);
30
- ReadOnlyInstance = pascal(`${name}ReadOnlyInterface`);
31
- body.push(w.importStmt([QueryClient], `./${ContractFile}`));
32
- body.push(w.createRecoilQueryClientType());
33
- body.push(w.createRecoilQueryClient(context, name, QueryClient));
34
- const selectors = w.createRecoilSelectors(context, name, QueryClient, QueryMsg);
35
- body.push(...selectors);
36
- }
37
- if (typeHash.hasOwnProperty('Coin')) {
38
- // @ts-ignore
39
- delete context.utils.Coin;
40
- }
41
- const imports = context.getImports();
42
- const code = header + generate(
43
- // @ts-ignore
44
- t.program([
45
- ...imports,
46
- ...body
47
- ])).code;
48
- mkdirp(outPath);
49
- writeFileSync(join(outPath, localname), code);
50
- return [
51
- {
52
- type: 'recoil',
53
- contract: name,
54
- localname,
55
- filename: join(outPath, localname),
56
- }
57
- ];
58
- };