@cosmwasm/ts-codegen 0.35.6 → 1.0.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 (250) hide show
  1. package/LICENSE-Apache +201 -0
  2. package/LICENSE-MIT +21 -0
  3. package/README.md +15 -7
  4. package/{types/src/builder → builder}/builder.d.ts +2 -2
  5. package/builder/builder.js +172 -0
  6. package/builder/index.js +17 -0
  7. package/bundler/bundler.d.ts +8 -0
  8. package/bundler/bundler.js +79 -0
  9. package/bundler/index.js +17 -0
  10. package/cli.d.ts +2 -0
  11. package/cli.js +24 -0
  12. package/cmds.d.ts +2 -0
  13. package/cmds.js +14 -0
  14. package/commands/create-boilerplate.d.ts +3 -0
  15. package/commands/create-boilerplate.js +132 -0
  16. package/commands/generate.d.ts +3 -0
  17. package/commands/generate.js +175 -0
  18. package/commands/install.d.ts +3 -0
  19. package/commands/install.js +107 -0
  20. package/{src/builder/builder.ts → esm/builder/builder.js} +39 -139
  21. package/esm/builder/index.js +1 -0
  22. package/esm/bundler/bundler.js +47 -0
  23. package/esm/bundler/index.js +1 -0
  24. package/esm/cli.js +20 -0
  25. package/{src → esm}/cmds.js +0 -1
  26. package/{src/commands/create-boilerplate.ts → esm/commands/create-boilerplate.js} +35 -70
  27. package/{src/commands/generate.ts → esm/commands/generate.js} +19 -24
  28. package/{src/commands/install.ts → esm/commands/install.js} +22 -35
  29. package/esm/file.js +18 -0
  30. package/esm/generators/client.js +67 -0
  31. package/esm/generators/create-helpers.js +36 -0
  32. package/esm/generators/message-composer.js +55 -0
  33. package/esm/generators/msg-builder.js +58 -0
  34. package/esm/generators/react-query.js +76 -0
  35. package/esm/generators/recoil.js +58 -0
  36. package/esm/generators/types.js +58 -0
  37. package/{src/index.ts → esm/index.js} +3 -6
  38. package/esm/plugins/client.js +59 -0
  39. package/esm/plugins/message-builder.js +52 -0
  40. package/esm/plugins/message-composer.js +46 -0
  41. package/esm/plugins/plugin-base.js +47 -0
  42. package/esm/plugins/provider-bundle.js +58 -0
  43. package/esm/plugins/provider.js +73 -0
  44. package/esm/plugins/react-query.js +69 -0
  45. package/esm/plugins/recoil.js +51 -0
  46. package/esm/plugins/types.js +38 -0
  47. package/{src → esm}/ts-codegen.js +1 -2
  48. package/{src/utils/clean.ts → esm/utils/clean.js} +7 -7
  49. package/{src/utils/cleanse.ts → esm/utils/cleanse.js} +18 -22
  50. package/esm/utils/files.js +42 -0
  51. package/{src/utils/parse.ts → esm/utils/parse.js} +4 -13
  52. package/esm/utils/prompt.js +59 -0
  53. package/{src/utils/schemas.ts → esm/utils/schemas.js} +16 -44
  54. package/esm/utils/unused.js +47 -0
  55. package/file.js +20 -0
  56. package/generators/client.js +95 -0
  57. package/generators/create-helpers.js +43 -0
  58. package/generators/message-composer.js +83 -0
  59. package/generators/msg-builder.js +86 -0
  60. package/generators/react-query.js +104 -0
  61. package/generators/recoil.js +86 -0
  62. package/generators/types.js +86 -0
  63. package/{module/helpers → helpers}/contractContextBase.js +5 -2
  64. package/{module/helpers → helpers}/contractContextBaseShortHandCtor.js +5 -2
  65. package/{module/helpers → helpers}/contractsContextTSX.js +5 -2
  66. package/{module/helpers/index.js → helpers/index.d.ts} +1 -1
  67. package/helpers/index.js +19 -0
  68. package/index.js +39 -0
  69. package/package.json +27 -51
  70. package/plugins/client.js +86 -0
  71. package/plugins/index.d.ts +1 -0
  72. package/plugins/index.js +17 -0
  73. package/plugins/message-builder.js +79 -0
  74. package/plugins/message-composer.js +73 -0
  75. package/{types/src/plugins → plugins}/plugin-base.d.ts +1 -1
  76. package/plugins/plugin-base.js +77 -0
  77. package/plugins/provider-bundle.js +85 -0
  78. package/{types/src/plugins → plugins}/provider.d.ts +2 -2
  79. package/plugins/provider.js +102 -0
  80. package/plugins/react-query.js +96 -0
  81. package/plugins/recoil.js +78 -0
  82. package/plugins/types.js +65 -0
  83. package/ts-codegen.js +8 -0
  84. package/utils/clean.js +49 -0
  85. package/utils/cleanse.js +89 -0
  86. package/utils/files.js +73 -0
  87. package/utils/header.js +14 -0
  88. package/utils/index.d.ts +1 -0
  89. package/utils/index.js +17 -0
  90. package/utils/parse.d.ts +1 -0
  91. package/utils/parse.js +43 -0
  92. package/utils/prompt.d.ts +3 -0
  93. package/utils/prompt.js +65 -0
  94. package/utils/schemas.d.ts +11 -0
  95. package/utils/schemas.js +81 -0
  96. package/utils/unused.js +73 -0
  97. package/main/builder/builder.js +0 -308
  98. package/main/builder/index.js +0 -16
  99. package/main/bundler/bundler.js +0 -61
  100. package/main/bundler/index.js +0 -16
  101. package/main/cli.js +0 -51
  102. package/main/cmds.js +0 -15
  103. package/main/commands/create-boilerplate.js +0 -166
  104. package/main/commands/generate.js +0 -196
  105. package/main/commands/install.js +0 -138
  106. package/main/file.js +0 -35
  107. package/main/generators/client.js +0 -90
  108. package/main/generators/create-helpers.js +0 -38
  109. package/main/generators/message-composer.js +0 -77
  110. package/main/generators/msg-builder.js +0 -84
  111. package/main/generators/react-query.js +0 -102
  112. package/main/generators/recoil.js +0 -81
  113. package/main/generators/types.js +0 -82
  114. package/main/helpers/contractContextBase.js +0 -8
  115. package/main/helpers/contractContextBaseShortHandCtor.js +0 -8
  116. package/main/helpers/contractsContextTSX.js +0 -8
  117. package/main/helpers/index.js +0 -38
  118. package/main/index.js +0 -119
  119. package/main/plugins/client.js +0 -112
  120. package/main/plugins/index.js +0 -16
  121. package/main/plugins/message-builder.js +0 -102
  122. package/main/plugins/message-composer.js +0 -98
  123. package/main/plugins/plugin-base.js +0 -103
  124. package/main/plugins/provider-bundle.js +0 -108
  125. package/main/plugins/provider.js +0 -126
  126. package/main/plugins/react-query.js +0 -120
  127. package/main/plugins/recoil.js +0 -108
  128. package/main/plugins/types.js +0 -89
  129. package/main/ts-codegen.js +0 -20
  130. package/main/utils/clean.js +0 -53
  131. package/main/utils/cleanse.js +0 -87
  132. package/main/utils/files.js +0 -57
  133. package/main/utils/header.js +0 -11
  134. package/main/utils/index.js +0 -16
  135. package/main/utils/parse.js +0 -44
  136. package/main/utils/prompt.js +0 -98
  137. package/main/utils/schemas.js +0 -144
  138. package/main/utils/unused.js +0 -57
  139. package/module/builder/builder.js +0 -143
  140. package/module/builder/index.js +0 -1
  141. package/module/bundler/bundler.js +0 -36
  142. package/module/bundler/index.js +0 -1
  143. package/module/cli.js +0 -19
  144. package/module/cmds.js +0 -8
  145. package/module/commands/create-boilerplate.js +0 -102
  146. package/module/commands/generate.js +0 -163
  147. package/module/commands/install.js +0 -98
  148. package/module/file.js +0 -18
  149. package/module/generators/client.js +0 -65
  150. package/module/generators/create-helpers.js +0 -33
  151. package/module/generators/message-composer.js +0 -52
  152. package/module/generators/msg-builder.js +0 -58
  153. package/module/generators/react-query.js +0 -75
  154. package/module/generators/recoil.js +0 -53
  155. package/module/generators/types.js +0 -55
  156. package/module/index.js +0 -14
  157. package/module/plugins/client.js +0 -63
  158. package/module/plugins/index.js +0 -1
  159. package/module/plugins/message-builder.js +0 -55
  160. package/module/plugins/message-composer.js +0 -49
  161. package/module/plugins/plugin-base.js +0 -66
  162. package/module/plugins/provider-bundle.js +0 -56
  163. package/module/plugins/provider.js +0 -68
  164. package/module/plugins/react-query.js +0 -72
  165. package/module/plugins/recoil.js +0 -57
  166. package/module/plugins/types.js +0 -42
  167. package/module/ts-codegen.js +0 -6
  168. package/module/utils/clean.js +0 -44
  169. package/module/utils/cleanse.js +0 -78
  170. package/module/utils/files.js +0 -40
  171. package/module/utils/header.js +0 -8
  172. package/module/utils/index.js +0 -1
  173. package/module/utils/parse.js +0 -35
  174. package/module/utils/prompt.js +0 -63
  175. package/module/utils/schemas.js +0 -85
  176. package/module/utils/unused.js +0 -43
  177. package/src/builder/index.ts +0 -1
  178. package/src/bundler/bundler.ts +0 -67
  179. package/src/bundler/index.ts +0 -1
  180. package/src/cli.js +0 -21
  181. package/src/file.js +0 -20
  182. package/src/generators/client.ts +0 -109
  183. package/src/generators/create-helpers.ts +0 -71
  184. package/src/generators/message-composer.ts +0 -86
  185. package/src/generators/msg-builder.ts +0 -78
  186. package/src/generators/react-query.ts +0 -102
  187. package/src/generators/recoil.ts +0 -92
  188. package/src/generators/types.ts +0 -94
  189. package/src/helpers/index.ts +0 -3
  190. package/src/plugins/client.ts +0 -124
  191. package/src/plugins/index.ts +0 -1
  192. package/src/plugins/message-builder.ts +0 -85
  193. package/src/plugins/message-composer.ts +0 -89
  194. package/src/plugins/plugin-base.ts +0 -122
  195. package/src/plugins/provider-bundle.ts +0 -98
  196. package/src/plugins/provider.ts +0 -115
  197. package/src/plugins/react-query.ts +0 -115
  198. package/src/plugins/recoil.ts +0 -89
  199. package/src/plugins/types.ts +0 -74
  200. package/src/utils/files.ts +0 -73
  201. package/src/utils/index.ts +0 -1
  202. package/src/utils/prompt.js +0 -65
  203. package/src/utils/unused.ts +0 -52
  204. package/types/src/bundler/bundler.d.ts +0 -4
  205. package/types/src/cli.d.ts +0 -1
  206. package/types/src/cmds.d.ts +0 -1
  207. package/types/src/commands/create-boilerplate.d.ts +0 -2
  208. package/types/src/commands/generate.d.ts +0 -2
  209. package/types/src/commands/install.d.ts +0 -2
  210. package/types/src/generators/msg-builder.ts +0 -5
  211. package/types/src/plugins/msg-builder.d.ts +0 -12
  212. package/types/src/plugins/use-contracts.d.ts +0 -12
  213. package/types/src/types.d.ts +0 -16
  214. package/types/src/utils/parse.d.ts +0 -1
  215. package/types/src/utils/prompt.d.ts +0 -3
  216. package/types/src/utils/schemas.d.ts +0 -10
  217. /package/{types/src/builder → builder}/index.d.ts +0 -0
  218. /package/{types/src/bundler → bundler}/index.d.ts +0 -0
  219. /package/{src/helpers/contractContextBase.ts → esm/helpers/contractContextBase.js} +0 -0
  220. /package/{src/helpers/contractContextBaseShortHandCtor.ts → esm/helpers/contractContextBaseShortHandCtor.js} +0 -0
  221. /package/{src/helpers/contractsContextTSX.ts → esm/helpers/contractsContextTSX.js} +0 -0
  222. /package/{types/src/helpers/index.d.ts → esm/helpers/index.js} +0 -0
  223. /package/{types/src/plugins/index.d.ts → esm/plugins/index.js} +0 -0
  224. /package/{src/utils/header.ts → esm/utils/header.js} +0 -0
  225. /package/{types/src/utils/index.d.ts → esm/utils/index.js} +0 -0
  226. /package/{types/src/file.d.ts → file.d.ts} +0 -0
  227. /package/{types/src/generators → generators}/client.d.ts +0 -0
  228. /package/{types/src/generators → generators}/create-helpers.d.ts +0 -0
  229. /package/{types/src/generators → generators}/message-composer.d.ts +0 -0
  230. /package/{types/src/generators → generators}/msg-builder.d.ts +0 -0
  231. /package/{types/src/generators → generators}/react-query.d.ts +0 -0
  232. /package/{types/src/generators → generators}/recoil.d.ts +0 -0
  233. /package/{types/src/generators → generators}/types.d.ts +0 -0
  234. /package/{types/src/helpers → helpers}/contractContextBase.d.ts +0 -0
  235. /package/{types/src/helpers → helpers}/contractContextBaseShortHandCtor.d.ts +0 -0
  236. /package/{types/src/helpers → helpers}/contractsContextTSX.d.ts +0 -0
  237. /package/{types/src/index.d.ts → index.d.ts} +0 -0
  238. /package/{types/src/plugins → plugins}/client.d.ts +0 -0
  239. /package/{types/src/plugins → plugins}/message-builder.d.ts +0 -0
  240. /package/{types/src/plugins → plugins}/message-composer.d.ts +0 -0
  241. /package/{types/src/plugins → plugins}/provider-bundle.d.ts +0 -0
  242. /package/{types/src/plugins → plugins}/react-query.d.ts +0 -0
  243. /package/{types/src/plugins → plugins}/recoil.d.ts +0 -0
  244. /package/{types/src/plugins → plugins}/types.d.ts +0 -0
  245. /package/{types/src/ts-codegen.d.ts → ts-codegen.d.ts} +0 -0
  246. /package/{types/src/utils → utils}/clean.d.ts +0 -0
  247. /package/{types/src/utils → utils}/cleanse.d.ts +0 -0
  248. /package/{types/src/utils → utils}/files.d.ts +0 -0
  249. /package/{types/src/utils → utils}/header.d.ts +0 -0
  250. /package/{types/src/utils → utils}/unused.d.ts +0 -0
@@ -9,7 +9,7 @@ export interface IBuilderPlugin {
9
9
  */
10
10
  utils: UtilMapping;
11
11
  builder?: TSBuilder;
12
- setBuilder(builder: TSBuilder): any;
12
+ setBuilder(builder: TSBuilder): void;
13
13
  /**
14
14
  * render generated cdoe.
15
15
  * @param name the name of contract
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.BuilderPluginBase = void 0;
30
+ const mkdirp_1 = require("mkdirp");
31
+ const path_1 = require("path");
32
+ const fs_1 = require("fs");
33
+ const header_1 = require("../utils/header");
34
+ const generator_1 = __importDefault(require("@babel/generator"));
35
+ const t = __importStar(require("@babel/types"));
36
+ /**
37
+ * BuilderPluginBase enable ts-codegen users implement their own plugins by only implement a few functions.
38
+ */
39
+ class BuilderPluginBase {
40
+ builder;
41
+ option;
42
+ utils;
43
+ constructor(opt, builder) {
44
+ this.option = opt;
45
+ this.builder = builder;
46
+ }
47
+ setBuilder(builder) {
48
+ this.builder = builder;
49
+ }
50
+ async render(name, contractInfo, outPath) {
51
+ const { enabled } = this.option;
52
+ if (!enabled) {
53
+ return;
54
+ }
55
+ const context = this.initContext(contractInfo, this.option);
56
+ const results = await this.doRender(name, context);
57
+ if (!results || !results.length) {
58
+ return [];
59
+ }
60
+ return results.map((result) => {
61
+ const imports = context.getImports(this.utils, result.localname);
62
+ // @ts-ignore
63
+ const code = header_1.header + (0, generator_1.default)(t.program([...imports, ...result.body])).code;
64
+ (0, mkdirp_1.sync)(outPath);
65
+ const filename = (0, path_1.join)(outPath, result.localname);
66
+ (0, fs_1.writeFileSync)(filename, code);
67
+ return {
68
+ type: result.type,
69
+ pluginType: result.pluginType,
70
+ contract: name,
71
+ localname: result.localname,
72
+ filename,
73
+ };
74
+ });
75
+ }
76
+ }
77
+ exports.BuilderPluginBase = BuilderPluginBase;
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.ContractsProviderBundlePlugin = void 0;
27
+ const case_1 = require("case");
28
+ const w = __importStar(require("wasm-ast-types"));
29
+ const wasm_ast_types_1 = require("wasm-ast-types");
30
+ const plugin_base_1 = require("./plugin-base");
31
+ const provider_1 = require("./provider");
32
+ class ContractsProviderBundlePlugin extends plugin_base_1.BuilderPluginBase {
33
+ constructor(opt) {
34
+ super(opt);
35
+ this.utils = {
36
+ CosmWasmClient: "@cosmjs/cosmwasm-stargate",
37
+ SigningCosmWasmClient: "@cosmjs/cosmwasm-stargate",
38
+ IQueryClientProvider: "__contractContextBase__",
39
+ ISigningClientProvider: "__contractContextBase__",
40
+ IMessageComposerProvider: "__contractContextBase__",
41
+ };
42
+ }
43
+ initContext(contract, options) {
44
+ return new wasm_ast_types_1.RenderContext(contract, options, this.builder.builderContext);
45
+ }
46
+ async doRender(name, context) {
47
+ if (!this.option?.useContractsHooks?.enabled) {
48
+ return;
49
+ }
50
+ const providerInfos = context.getProviderInfos();
51
+ if (!Object.keys(providerInfos || {})?.length) {
52
+ return;
53
+ }
54
+ const localname = "contractContextProviders.ts";
55
+ const body = [];
56
+ context.addUtil("CosmWasmClient");
57
+ context.addUtil("SigningCosmWasmClient");
58
+ context.addUtil("IQueryClientProvider");
59
+ context.addUtil("ISigningClientProvider");
60
+ context.addUtil("IMessageComposerProvider");
61
+ for (const name in providerInfos) {
62
+ if (Object.prototype.hasOwnProperty.call(providerInfos, name)) {
63
+ const providerInfo = providerInfos[name];
64
+ for (const key in providerInfo) {
65
+ if (Object.prototype.hasOwnProperty.call(providerInfo, key)) {
66
+ const info = providerInfo[key];
67
+ body.push(w.importStmt([info.classname], `./${info.basename}`));
68
+ }
69
+ }
70
+ body.push(w.importStmt([(0, case_1.pascal)(name)], `./${(0, provider_1.GetLocalBaseNameByContractName)(name)}`));
71
+ }
72
+ }
73
+ body.push(w.createIContractsContext(providerInfos));
74
+ body.push(w.createGettingProviders(providerInfos));
75
+ return [
76
+ {
77
+ type: "plugin",
78
+ pluginType: "contractContextProviders",
79
+ localname,
80
+ body,
81
+ },
82
+ ];
83
+ }
84
+ }
85
+ exports.ContractsProviderBundlePlugin = ContractsProviderBundlePlugin;
@@ -1,8 +1,8 @@
1
1
  import { ContractInfo, RenderContextBase, RenderContext } from "wasm-ast-types";
2
2
  import { BuilderFileType, TSBuilderOptions } from "../builder";
3
3
  import { BuilderPluginBase } from "./plugin-base";
4
- export declare const GetLocalNameByContractName: (name: any) => string;
5
- export declare const GetLocalBaseNameByContractName: (name: any) => string;
4
+ export declare const GetLocalNameByContractName: (name: string) => string;
5
+ export declare const GetLocalBaseNameByContractName: (name: string) => string;
6
6
  export declare class ContractsContextProviderPlugin extends BuilderPluginBase<TSBuilderOptions> {
7
7
  constructor(opt: TSBuilderOptions);
8
8
  initContext(contract: ContractInfo, options?: TSBuilderOptions): RenderContextBase<TSBuilderOptions>;
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.ContractsContextProviderPlugin = exports.GetLocalBaseNameByContractName = exports.GetLocalNameByContractName = void 0;
27
+ const case_1 = require("case");
28
+ const w = __importStar(require("wasm-ast-types"));
29
+ const wasm_ast_types_1 = require("wasm-ast-types");
30
+ const plugin_base_1 = require("./plugin-base");
31
+ const GetLocalNameByContractName = (name) => `${(0, case_1.pascal)(name)}.provider.ts`;
32
+ exports.GetLocalNameByContractName = GetLocalNameByContractName;
33
+ const GetLocalBaseNameByContractName = (name) => `${(0, case_1.pascal)(name)}.provider`;
34
+ exports.GetLocalBaseNameByContractName = GetLocalBaseNameByContractName;
35
+ class ContractsContextProviderPlugin extends plugin_base_1.BuilderPluginBase {
36
+ constructor(opt) {
37
+ super(opt);
38
+ this.utils = {
39
+ ContractBase: "__contractContextBase__",
40
+ IContractConstructor: "__contractContextBase__",
41
+ IEmptyClient: "__contractContextBase__",
42
+ };
43
+ }
44
+ initContext(contract, options) {
45
+ return new wasm_ast_types_1.RenderContext(contract, options, this.builder.builderContext);
46
+ }
47
+ async doRender(name, context) {
48
+ if (!this.option?.useContractsHooks?.enabled) {
49
+ return;
50
+ }
51
+ const providerInfo = context.getProviderInfos()[name];
52
+ if (!Object.keys(providerInfo || {})?.length) {
53
+ return;
54
+ }
55
+ context.addUtil("ContractBase");
56
+ context.addUtil("IContractConstructor");
57
+ const localname = (0, exports.GetLocalNameByContractName)(name);
58
+ let needEmptyClientType = false;
59
+ let clientFile = null;
60
+ let clientClasses = [];
61
+ const body = [];
62
+ const signClientProviderInfo = providerInfo[w.PROVIDER_TYPES.SIGNING_CLIENT_TYPE];
63
+ if (signClientProviderInfo) {
64
+ clientFile = `./${signClientProviderInfo.basename}`;
65
+ clientClasses.push(signClientProviderInfo.classname);
66
+ }
67
+ else {
68
+ needEmptyClientType = true;
69
+ }
70
+ const queryClientProviderInfo = providerInfo[w.PROVIDER_TYPES.QUERY_CLIENT_TYPE];
71
+ if (queryClientProviderInfo) {
72
+ clientFile = `./${queryClientProviderInfo.basename}`;
73
+ clientClasses.push(queryClientProviderInfo.classname);
74
+ }
75
+ else {
76
+ needEmptyClientType = true;
77
+ }
78
+ if (clientFile) {
79
+ body.push(w.importStmt(clientClasses, clientFile));
80
+ }
81
+ const messageComposerProviderInfo = providerInfo[w.PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE];
82
+ if (messageComposerProviderInfo) {
83
+ body.push(w.importStmt([messageComposerProviderInfo.classname], `./${messageComposerProviderInfo.basename}`));
84
+ }
85
+ else {
86
+ needEmptyClientType = true;
87
+ }
88
+ if (needEmptyClientType) {
89
+ context.addUtil("IEmptyClient");
90
+ }
91
+ body.push(w.createProvider(name, providerInfo));
92
+ return [
93
+ {
94
+ type: "plugin",
95
+ pluginType: "provider",
96
+ localname,
97
+ body,
98
+ },
99
+ ];
100
+ }
101
+ }
102
+ exports.ContractsContextProviderPlugin = ContractsContextProviderPlugin;
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.ReactQueryPlugin = void 0;
27
+ const case_1 = require("case");
28
+ const w = __importStar(require("wasm-ast-types"));
29
+ const utils_1 = require("../utils");
30
+ const wasm_ast_types_1 = require("wasm-ast-types");
31
+ const plugin_base_1 = require("./plugin-base");
32
+ class ReactQueryPlugin extends plugin_base_1.BuilderPluginBase {
33
+ initContext(contract, options) {
34
+ return new wasm_ast_types_1.RenderContext(contract, options, this.builder.builderContext);
35
+ }
36
+ async doRender(name, context) {
37
+ const options = this.option.reactQuery;
38
+ const { enabled } = options;
39
+ if (!enabled) {
40
+ return;
41
+ }
42
+ const { schemas } = context.contract;
43
+ const localname = (0, case_1.pascal)(`${name}`) + '.react-query.ts';
44
+ const ContractFile = (0, case_1.pascal)(`${name}`) + '.client';
45
+ const TypesFile = (0, case_1.pascal)(`${name}`) + '.types';
46
+ const QueryMsg = (0, utils_1.findQueryMsg)(schemas);
47
+ const ExecuteMsg = (0, utils_1.findExecuteMsg)(schemas);
48
+ const typeHash = await (0, utils_1.findAndParseTypes)(schemas);
49
+ const ExecuteClient = (0, case_1.pascal)(`${name}Client`);
50
+ const QueryClient = (0, case_1.pascal)(`${name}QueryClient`);
51
+ const body = [];
52
+ const clientImports = [];
53
+ QueryMsg && clientImports.push(QueryClient);
54
+ // check that there are commands within the exec msg
55
+ const shouldGenerateMutationHooks = ExecuteMsg &&
56
+ options?.version === 'v4' &&
57
+ options?.mutations &&
58
+ (0, wasm_ast_types_1.getMessageProperties)(ExecuteMsg).length > 0;
59
+ if (shouldGenerateMutationHooks) {
60
+ clientImports.push(ExecuteClient);
61
+ }
62
+ // general contract imports
63
+ body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
64
+ // client imports
65
+ body.push(w.importStmt(clientImports, `./${ContractFile}`));
66
+ // query messages
67
+ if (QueryMsg) {
68
+ [].push.apply(body, w.createReactQueryHooks({
69
+ context,
70
+ queryMsg: QueryMsg,
71
+ contractName: name,
72
+ QueryClient
73
+ }));
74
+ }
75
+ if (shouldGenerateMutationHooks) {
76
+ [].push.apply(body, w.createReactQueryMutationHooks({
77
+ context,
78
+ execMsg: ExecuteMsg,
79
+ contractName: name,
80
+ ExecuteClient
81
+ }));
82
+ }
83
+ if (typeHash.hasOwnProperty('Coin')) {
84
+ // @ts-ignore
85
+ delete context.utils.Coin;
86
+ }
87
+ return [
88
+ {
89
+ type: 'react-query',
90
+ localname,
91
+ body
92
+ }
93
+ ];
94
+ }
95
+ }
96
+ exports.ReactQueryPlugin = ReactQueryPlugin;
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.RecoilPlugin = void 0;
27
+ const case_1 = require("case");
28
+ const w = __importStar(require("wasm-ast-types"));
29
+ const utils_1 = require("../utils");
30
+ const wasm_ast_types_1 = require("wasm-ast-types");
31
+ const plugin_base_1 = require("./plugin-base");
32
+ class RecoilPlugin extends plugin_base_1.BuilderPluginBase {
33
+ utils = {
34
+ selectorFamily: 'recoil',
35
+ };
36
+ initContext(contract, options) {
37
+ return new wasm_ast_types_1.RenderContext(contract, options, this.builder.builderContext);
38
+ }
39
+ async doRender(name, context) {
40
+ const { enabled } = this.option.recoil;
41
+ if (!enabled) {
42
+ return;
43
+ }
44
+ const { schemas } = context.contract;
45
+ const localname = (0, case_1.pascal)(name) + '.recoil.ts';
46
+ const ContractFile = (0, case_1.pascal)(name) + '.client';
47
+ const TypesFile = (0, case_1.pascal)(name) + '.types';
48
+ const QueryMsg = (0, utils_1.findQueryMsg)(schemas);
49
+ const typeHash = await (0, utils_1.findAndParseTypes)(schemas);
50
+ let QueryClient = null;
51
+ let ReadOnlyInstance = null;
52
+ const body = [];
53
+ body.push(w.importStmt(['cosmWasmClient'], './chain'));
54
+ body.push(w.importStmt(Object.keys(typeHash), `./${TypesFile}`));
55
+ // query messages
56
+ if (QueryMsg) {
57
+ QueryClient = (0, case_1.pascal)(`${name}QueryClient`);
58
+ ReadOnlyInstance = (0, case_1.pascal)(`${name}ReadOnlyInterface`);
59
+ body.push(w.importStmt([QueryClient], `./${ContractFile}`));
60
+ body.push(w.createRecoilQueryClientType());
61
+ body.push(w.createRecoilQueryClient(context, name, QueryClient));
62
+ const selectors = w.createRecoilSelectors(context, name, QueryClient, QueryMsg);
63
+ body.push(...selectors);
64
+ }
65
+ if (typeHash.hasOwnProperty('Coin')) {
66
+ // @ts-ignore
67
+ delete context.utils.Coin;
68
+ }
69
+ return [
70
+ {
71
+ type: 'recoil',
72
+ localname,
73
+ body
74
+ }
75
+ ];
76
+ }
77
+ }
78
+ exports.RecoilPlugin = RecoilPlugin;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.TypesPlugin = void 0;
27
+ const t = __importStar(require("@babel/types"));
28
+ const clean_1 = require("../utils/clean");
29
+ const case_1 = require("case");
30
+ const utils_1 = require("../utils");
31
+ const wasm_ast_types_1 = require("wasm-ast-types");
32
+ const plugin_base_1 = require("./plugin-base");
33
+ class TypesPlugin extends plugin_base_1.BuilderPluginBase {
34
+ initContext(contract, options) {
35
+ return new wasm_ast_types_1.RenderContext(contract, options, this.builder.builderContext);
36
+ }
37
+ async doRender(name, context) {
38
+ const { enabled } = this.option.types;
39
+ if (!enabled) {
40
+ return;
41
+ }
42
+ const { schemas } = context.contract;
43
+ const options = this.option.types;
44
+ const localname = (0, case_1.pascal)(name) + '.types.ts';
45
+ const ExecuteMsg = (0, utils_1.findExecuteMsg)(schemas);
46
+ const typeHash = await (0, utils_1.findAndParseTypes)(schemas);
47
+ const body = [];
48
+ // TYPES
49
+ Object.values(typeHash).forEach((type) => {
50
+ body.push((0, clean_1.clean)(type));
51
+ });
52
+ // alias the ExecuteMsg
53
+ if (options.aliasExecuteMsg && ExecuteMsg) {
54
+ body.push(t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(`${name}ExecuteMsg`), null, t.tsTypeReference(t.identifier('ExecuteMsg')))));
55
+ }
56
+ return [
57
+ {
58
+ type: 'type',
59
+ localname,
60
+ body
61
+ }
62
+ ];
63
+ }
64
+ }
65
+ exports.TypesPlugin = TypesPlugin;
package/ts-codegen.js ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ const cli_1 = require("./cli");
5
+ var argv = require('minimist')(process.argv.slice(2));
6
+ (async () => {
7
+ await (0, cli_1.cli)(argv);
8
+ })();
package/utils/clean.js ADDED
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.clean = void 0;
4
+ const clean = (obj) => {
5
+ var copy;
6
+ // Handle the 3 simple types, and null or undefined
7
+ if (null == obj || 'object' != typeof obj)
8
+ return obj;
9
+ // Handle Date
10
+ if (obj instanceof Date) {
11
+ copy = new Date();
12
+ copy.setTime(obj.getTime());
13
+ return copy;
14
+ }
15
+ // Handle Array
16
+ if (obj instanceof Array) {
17
+ copy = [];
18
+ for (var i = 0, len = obj.length; i < len; i++) {
19
+ copy[i] = (0, exports.clean)(obj[i]);
20
+ }
21
+ return copy;
22
+ }
23
+ // Handle Object
24
+ if (obj instanceof Object || typeof obj === 'object') {
25
+ copy = {};
26
+ for (var attr in obj) {
27
+ if (obj.hasOwnProperty(attr)) {
28
+ switch (attr) {
29
+ case 'leadingComments':
30
+ case 'trailingComments':
31
+ case 'loc':
32
+ case 'start':
33
+ case 'end':
34
+ break;
35
+ default:
36
+ // @ts-ignore
37
+ copy[attr] = (0, exports.clean)(obj[attr]);
38
+ }
39
+ }
40
+ else {
41
+ // @ts-ignore
42
+ copy[attr] = (0, exports.clean)(obj[attr]);
43
+ }
44
+ }
45
+ return copy;
46
+ }
47
+ throw new Error("Unable to copy obj! Its type isn't supported.");
48
+ };
49
+ exports.clean = clean;
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cleanse = void 0;
4
+ const case_1 = require("case");
5
+ const cleanFor = (str) => {
6
+ /*
7
+ 1. look at first char after _for_
8
+ 2. ONLY if you find capitals after, modify it
9
+ */
10
+ while (/_[a-z]+_[A-Z]/.test(str)) {
11
+ const m = str.match(/(_[a-z]+_)[A-Z]/);
12
+ str = str.replace(m[1], (0, case_1.pascal)(m[1]));
13
+ }
14
+ return str;
15
+ };
16
+ const cleanNullable = (str) => {
17
+ if (/^Nullable_/.test(str)) {
18
+ str = str.replace(/^Nullable_/, 'Nullable');
19
+ }
20
+ return str;
21
+ };
22
+ const cleanse = (obj) => {
23
+ var copy;
24
+ // Handle the 3 simple types, and null or undefined
25
+ if (null == obj || 'object' != typeof obj)
26
+ return obj;
27
+ // Handle Date
28
+ if (obj instanceof Date) {
29
+ copy = new Date();
30
+ copy.setTime(obj.getTime());
31
+ return copy;
32
+ }
33
+ // Handle Array
34
+ if (obj instanceof Array) {
35
+ copy = [];
36
+ for (var i = 0, len = obj.length; i < len; i++) {
37
+ copy[i] = (0, exports.cleanse)(obj[i]);
38
+ }
39
+ return copy;
40
+ }
41
+ // Handle Object
42
+ if (obj instanceof Object || typeof obj === 'object') {
43
+ copy = {};
44
+ // https://github.com/CosmWasm/cosmwasm-typescript-gen/issues/27
45
+ if (Array.isArray(obj.enum) && obj.enum.length === 0) {
46
+ delete obj.enum;
47
+ if (!obj.type) {
48
+ obj.type = 'string';
49
+ }
50
+ }
51
+ for (var attr in obj) {
52
+ if (obj.hasOwnProperty(attr)) {
53
+ if (/_for_/.test(attr)) {
54
+ // @ts-ignore
55
+ copy[cleanFor(attr)] = (0, exports.cleanse)(obj[attr]);
56
+ }
57
+ else if (/^Nullable_/.test(attr)) {
58
+ // @ts-ignore
59
+ copy[cleanNullable(attr)] = (0, exports.cleanse)(obj[attr]);
60
+ }
61
+ else {
62
+ switch (attr) {
63
+ case 'title':
64
+ case '$ref':
65
+ if (typeof obj[attr] === 'string') {
66
+ // @ts-ignore
67
+ copy[attr] = (0, exports.cleanse)(cleanNullable(cleanFor(obj[attr])));
68
+ }
69
+ else {
70
+ // @ts-ignore
71
+ copy[attr] = (0, exports.cleanse)(obj[attr]);
72
+ }
73
+ break;
74
+ default:
75
+ // @ts-ignore
76
+ copy[attr] = (0, exports.cleanse)(obj[attr]);
77
+ }
78
+ }
79
+ }
80
+ else {
81
+ // @ts-ignore
82
+ copy[attr] = (0, exports.cleanse)(obj[attr]);
83
+ }
84
+ }
85
+ return copy;
86
+ }
87
+ throw new Error("Unable to copy obj! Its type isn't supported.");
88
+ };
89
+ exports.cleanse = cleanse;