@cosmwasm/ts-codegen 0.7.1 → 0.7.4

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.
package/README.md CHANGED
@@ -21,36 +21,168 @@ npm install -g @cosmwasm/ts-codegen
21
21
 
22
22
  The quickest and easiest way to interact with CosmWasm Contracts. `@cosmwasm/ts-codegen` converts your CosmWasm smart contracts into dev-friendly TypeScript classes so you can focus on shipping code.
23
23
 
24
+ ## Table of contents
25
+
26
+ - [@cosmwasm/ts-codegen](#cosmwasmts-codegen)
27
+ - [Table of contents](#table-of-contents)
28
+ - [QuickStart](#quickstart)
29
+ - [Usage](#usage)
30
+ - [Generate TS Clients](#generate)
31
+ - [React Query](#react-query)
32
+ - [Recoil](#recoil)
33
+ - [Message Composer](#message-composer)
34
+ - [Example Output](#example-output)
35
+ - [JSON Schema](#json-schema)
36
+ - [JSON Schema Generation](#json-schema-generation)
37
+ - [Exporting Schemas](#exporting-schemas)
38
+ - [Developing](#developing)
39
+ - [Related](#related)
40
+ ### Quickstart
41
+
42
+ Clone your project and `cd` into your contracts folder
43
+
44
+ ```sh
45
+ git clone git@github.com:public-awesome/stargaze-contracts.git
46
+ cd stargaze-contracts/contracts/sg721/
47
+ ```
48
+
49
+ Run `cosmwasm-ts-codegen` to generate your code.
50
+
51
+ ```sh
52
+ cosmwasm-ts-codegen generate --schema ./schema --out ./ts --name SG721
53
+ ```
54
+
55
+ The output will be in the folder specified by `--out`, enjoy!
56
+
24
57
  ## usage
25
58
 
59
+ Get started quickly using our `cli` by globally installing via npm:
60
+
61
+ ```
62
+ npm install -g @cosmwasm/ts-codegen
26
63
  ```
27
- cosmwasm-ts-codegen generate \
64
+ ### generate
65
+
66
+ Generate a basic TS client for your contracts. The `generate` command will make types which will be essential for your bindings.
67
+
68
+ This command also generates a `QueryClient` for queries as well as a `Client` for queries and mutations.
69
+
70
+ [see example output code](https://gist.github.com/pyramation/ba67ec56e4e2a39cadea55430f9993e5)
71
+
72
+
73
+ ```sh
74
+ cosmwasm-ts-codegen generate \
28
75
  --schema ./schema \
29
- --out ./src \
76
+ --out ./ts \
30
77
  --name MyContractName
31
78
  ```
32
79
 
33
- ### example
80
+ for programmatic usage, you can use the `tsClient` function:
34
81
 
82
+ ```ts
83
+ import { tsClient } from '@cosmwasm/ts-codegen';
84
+ declare const tsClient = (name: string, schemas: any[], outPath: string, tsClientOptions: TSClientOptions) => Promise<void>;
35
85
  ```
36
- git clone git@github.com:public-awesome/stargaze-contracts.git
37
- cd stargaze-contracts/contracts/sg721/
38
- cosmwasm-ts-codegen generate --schema ./schema --out ./ts --name SG721
86
+ #### TS Client Options
87
+
88
+ | option | description |
89
+ | ----------------------------- | --------------------------------------------------- |
90
+ | `tsClient.aliasExecuteMsg` | generate a type alias based on the contract name |
91
+
92
+ ### react query
93
+
94
+ Generate [react-query v3](https://react-query-v3.tanstack.com/) or [react-query v4](https://tanstack.com/query/v4/) bindings for your contracts with the `react-query` command.
95
+
96
+ [see example output code](https://gist.github.com/pyramation/a3bf4aa7b60a31287d0720ca1bb5473b)
97
+
98
+
99
+ Example without optional client, using v3, without mutations:
100
+
101
+ ```sh
102
+ cosmwasm-ts-codegen react-query \
103
+ --schema ./schema \
104
+ --out ./ts \
105
+ --name MyContractName \
106
+ --no-optionalClient \
107
+ --no-v4 \
108
+ --no-mutations
39
109
  ```
40
110
 
41
- ### JSON Schema Generation
111
+ Example with optional client, using v4, with mutations:
112
+
113
+ ```sh
114
+ cosmwasm-ts-codegen react-query \
115
+ --schema ./schema \
116
+ --out ./ts \
117
+ --name MyContractName \
118
+ --optionalClient \
119
+ --v4 \
120
+ --mutations
121
+ ```
122
+
123
+ For programmatic usage, you can use the `reactQuery` function:
124
+
125
+ ```ts
126
+ import { reactQuery } from '@cosmwasm/ts-codegen';
127
+ declare const reactQuery = (
128
+ contractName: string,
129
+ schemas: any[],
130
+ outPath: string,
131
+ reactQueryOptions?: ReactQueryOptions
132
+ ) => Promise<void>;
133
+ ```
134
+
135
+ #### React Query Options
136
+
137
+ | option | description |
138
+ | ------------------------------ | ------------------------------------------------------------------- |
139
+ | `reactQuery.optionalClient` | allows contract client to be undefined as the component renders |
140
+ | `reactQuery.v4` | uses `@tanstack/react-query` and syntax instead of v3 `react-query` |
141
+ | `reactQuery.mutations` | also generate mutations |
142
+ | `reactQuery.camelize` | use camelCase style for property names |
143
+
144
+ ### recoil
145
+
146
+ Generate [recoil](https://recoiljs.org/) bindings for your contracts with the `recoil` command.
147
+
148
+ [see example output code](https://gist.github.com/pyramation/48b28a75def1a16b233b369297f05f0e)
42
149
 
43
- Currently you have to have the JSON Schema output. Here is an example to start:
44
150
 
45
151
  ```sh
46
- ## get the Rust contracts
47
- git clone git@github.com:public-awesome/stargaze-contracts.git
48
- cd stargaze-contracts
49
- cargo build
152
+ cosmwasm-ts-codegen recoil \
153
+ --schema ./schema \
154
+ --out ./ts \
155
+ --name MyContractName
156
+ ```
50
157
 
51
- ## now build the schema
52
- cd contracts/sg721/
53
- cargo schema
158
+ for programmatic usage, you can use the `recoil` function:
159
+
160
+ ```ts
161
+ import { recoil } from '@cosmwasm/ts-codegen';
162
+ declare const recoil = (
163
+ name: string,
164
+ schemas: any[],
165
+ outPath: string
166
+ ) => Promise<void>;
167
+ ```
168
+ ### Message Composer
169
+
170
+ Generate pure message objects with the proper `utf8` encoding and `typeUrl` configured that you can broadcast yourself via `cosmjs` with the `from-partial` command.
171
+
172
+ [see example output code](https://gist.github.com/pyramation/f50869d1ecdb6d6ced2bc0a44c6ff492)
173
+
174
+ ```sh
175
+ cosmwasm-ts-codegen from-partial \
176
+ --schema ./schema \
177
+ --out ./ts \
178
+ --name MyContractName
179
+ ```
180
+
181
+ for programmatic usage, you can use the `fromPartial` function:
182
+
183
+ ```ts
184
+ import { fromPartial } from '@cosmwasm/ts-codegen';
185
+ declare const fromPartial = (name: string, schemas: any[], outPath: string) => Promise<void>;
54
186
  ```
55
187
 
56
188
  ### Example Output
@@ -72,8 +204,28 @@ https://gist.github.com/pyramation/a3bf4aa7b60a31287d0720ca1bb5473b
72
204
  https://gist.github.com/pyramation/48b28a75def1a16b233b369297f05f0e
73
205
 
74
206
 
75
- ### Exporting Schemas
207
+ ### JSON Schema
208
+
209
+ We generate code from the [JSON Schema](https://json-schema.org/) exported from CosmWasm smart contracts.
210
+ ### JSON Schema Generation
211
+
212
+ Currently you have to have the JSON Schema output. Here is an example to start.
213
+
214
+ First, get the Rust contracts and run `cargo build`:
215
+
216
+ ```sh
217
+ git clone git@github.com:public-awesome/stargaze-contracts.git
218
+ cd stargaze-contracts
219
+ cargo build
220
+ ```
221
+
222
+ now build the schema with `cargo schema`
76
223
 
224
+ ```sh
225
+ cd contracts/sg721/
226
+ cargo schema
227
+ ```
228
+ ### Exporting Schemas
77
229
  #### `cosmwasm_std` Examples
78
230
 
79
231
  ```rs
@@ -87,4 +239,38 @@ export_schema_with_title(
87
239
  &out_dir,
88
240
  "CosmosMsg_for_Empty",
89
241
  );
90
- ```
242
+ ```
243
+
244
+ ## Developing
245
+
246
+ ### Initial setup
247
+
248
+ ```
249
+ yarn
250
+ yarn bootstrap
251
+ ```
252
+
253
+ ### Building
254
+
255
+ ```
256
+ yarn build
257
+ ```
258
+
259
+ ### Tests
260
+
261
+ Then `cd` into a package and run the tests
262
+
263
+ ```
264
+ cd ./packages/wasm-ast-types
265
+ yarn test:watch
266
+ ```
267
+
268
+ ### Working with ASTs
269
+
270
+ See the [docs](https://github.com/CosmWasm/ts-codegen/blob/main/packages/wasm-ast-types/README.md) in the `wasm-ast-types` package.
271
+
272
+ ## Related
273
+
274
+ Checkout these related projects:
275
+
276
+ * [@osmonauts/telescope](https://github.com/osmosis-labs/telescope) a "babel for the Cosmos", Telescope is a TypeScript Transpiler for Cosmos Protobufs.
@@ -40,19 +40,23 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
40
40
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
41
41
 
42
42
  var _default = /*#__PURE__*/function () {
43
- var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(name, schemas, outPath) {
44
- var Contract, QueryMsg, ExecuteMsg, typeHash, Client, Instance, QueryClient, ReadOnlyInstance, body, context, children, code;
43
+ var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(name, schemas, outPath, tsClientOptions) {
44
+ var context, options, Contract, QueryMsg, ExecuteMsg, typeHash, Client, Instance, QueryClient, ReadOnlyInstance, body, children, code;
45
45
  return _regenerator["default"].wrap(function _callee$(_context) {
46
46
  while (1) {
47
47
  switch (_context.prev = _context.next) {
48
48
  case 0:
49
+ context = new w.RenderContext((0, _utils.getDefinitionSchema)(schemas), {
50
+ tsClient: tsClientOptions !== null && tsClientOptions !== void 0 ? tsClientOptions : {}
51
+ });
52
+ options = context.options.reactQuery;
49
53
  Contract = (0, _case.pascal)("".concat(name, "Contract")) + '.ts';
50
54
  QueryMsg = (0, _utils.findQueryMsg)(schemas);
51
55
  ExecuteMsg = (0, _utils.findExecuteMsg)(schemas);
52
- _context.next = 5;
56
+ _context.next = 7;
53
57
  return (0, _utils.findAndParseTypes)(schemas);
54
58
 
55
- case 5:
59
+ case 7:
56
60
  typeHash = _context.sent;
57
61
  Client = null;
58
62
  Instance = null;
@@ -66,8 +70,10 @@ var _default = /*#__PURE__*/function () {
66
70
  body.push((0, _clean.clean)(type));
67
71
  }); // alias the ExecuteMsg
68
72
 
69
- ExecuteMsg && body.push(t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier("".concat(name, "ExecuteMsg")), null, t.tsTypeReference(t.identifier('ExecuteMsg')))));
70
- context = new w.RenderContext((0, _utils.getDefinitionSchema)(schemas)); // query messages
73
+ if (options.aliasExecuteMsg && ExecuteMsg) {
74
+ body.push(t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier("".concat(name, "ExecuteMsg")), null, t.tsTypeReference(t.identifier('ExecuteMsg')))));
75
+ } // query messages
76
+
71
77
 
72
78
  if (QueryMsg) {
73
79
  QueryClient = (0, _case.pascal)("".concat(name, "QueryClient"));
@@ -92,7 +98,7 @@ var _default = /*#__PURE__*/function () {
92
98
  (0, _mkdirp.sync)(outPath);
93
99
  (0, _fs.writeFileSync)((0, _path.join)(outPath, Contract), code);
94
100
 
95
- case 21:
101
+ case 22:
96
102
  case "end":
97
103
  return _context.stop();
98
104
  }
@@ -100,7 +106,7 @@ var _default = /*#__PURE__*/function () {
100
106
  }, _callee);
101
107
  }));
102
108
 
103
- return function (_x, _x2, _x3) {
109
+ return function (_x, _x2, _x3, _x4) {
104
110
  return _ref.apply(this, arguments);
105
111
  };
106
112
  }();
@@ -11,7 +11,11 @@ import { getMessageProperties } from "wasm-ast-types";
11
11
  import { findAndParseTypes, findExecuteMsg, findQueryMsg, getDefinitionSchema } from '../utils';
12
12
  import { cosmjsAminoImportStatements } from '../utils/imports';
13
13
  import { RenderContext } from "wasm-ast-types";
14
- export default (async (name, schemas, outPath) => {
14
+ export default (async (name, schemas, outPath, tsClientOptions) => {
15
+ const context = new RenderContext(getDefinitionSchema(schemas), {
16
+ tsClient: tsClientOptions ?? {}
17
+ });
18
+ const options = context.options.reactQuery;
15
19
  const Contract = pascal(`${name}Contract`) + '.ts';
16
20
  const QueryMsg = findQueryMsg(schemas);
17
21
  const ExecuteMsg = findExecuteMsg(schemas);
@@ -28,8 +32,10 @@ export default (async (name, schemas, outPath) => {
28
32
  body.push(clean(type));
29
33
  }); // alias the ExecuteMsg
30
34
 
31
- ExecuteMsg && body.push(t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(`${name}ExecuteMsg`), null, t.tsTypeReference(t.identifier('ExecuteMsg')))));
32
- const context = new RenderContext(getDefinitionSchema(schemas)); // query messages
35
+ if (options.aliasExecuteMsg && ExecuteMsg) {
36
+ body.push(t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(`${name}ExecuteMsg`), null, t.tsTypeReference(t.identifier('ExecuteMsg')))));
37
+ } // query messages
38
+
33
39
 
34
40
  if (QueryMsg) {
35
41
  QueryClient = pascal(`${name}QueryClient`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cosmwasm/ts-codegen",
3
- "version": "0.7.1",
3
+ "version": "0.7.4",
4
4
  "description": "@cosmwasm/ts-codegen converts your CosmWasm smart contracts into dev-friendly TypeScript classes so you can focus on shipping code.",
5
5
  "author": "Dan Lynch <pyramation@gmail.com>",
6
6
  "homepage": "https://github.com/cosmwasm/ts-codegen",
@@ -90,7 +90,7 @@
90
90
  "minimist": "1.2.6",
91
91
  "mkdirp": "1.0.4",
92
92
  "shelljs": "0.8.5",
93
- "wasm-ast-types": "^0.6.0"
93
+ "wasm-ast-types": "^0.6.2"
94
94
  },
95
- "gitHead": "103ec48e18ab5bd88329ec36b558e5caeb79f4b9"
95
+ "gitHead": "a737503b0e3a69a25cb8f6e9c1950c92de9b1c74"
96
96
  }
@@ -0,0 +1,2 @@
1
+ declare const _default: (name: string, schemas: any[], outPath: string) => Promise<void>;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare const _default: (contractName: string, schemas: any[], outPath: string, reactQueryOptions?: ReactQueryOptions) => Promise<void>;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare const _default: (name: string, schemas: any[], outPath: string) => Promise<void>;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare const _default: (name: string, schemas: any[], outPath: string, tsClientOptions: TsClientOptions) => Promise<void>;
2
+ export default _default;
package/types/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- export { default as generate } from './generate';
2
- export { default as fromPartial } from './from-partial';
3
- export { default as reactQuery } from './react-query';
4
- export { default as recoil } from './recoil';
1
+ export { default as tsClient } from './generators/ts-client';
2
+ export { default as fromPartial } from './generators/from-partial';
3
+ export { default as reactQuery } from './generators/react-query';
4
+ export { default as recoil } from './generators/recoil';
5
5
  export * from './utils';
6
- export * from './imports';
6
+ export * from './utils/imports';
@@ -0,0 +1 @@
1
+ export declare const clean: (obj: any) => any;
@@ -0,0 +1 @@
1
+ export declare const cleanse: (obj: any) => any;
@@ -0,0 +1 @@
1
+ export declare const header: string;
@@ -0,0 +1 @@
1
+ export declare const cosmjsAminoImportStatements: (typeHash: any) => any;
@@ -0,0 +1 @@
1
+ export * from './schemas';
@@ -0,0 +1 @@
1
+ export declare const parser: (codes: any) => {};
@@ -0,0 +1,3 @@
1
+ export function getFuzzySearch(list: any): (answers: any, input: any) => Promise<any>;
2
+ export function getFuzzySearchNames(nameValueItemList: any): (answers: any, input: any) => Promise<any>;
3
+ export function prompt(questions?: any[], argv?: {}): Promise<any>;
@@ -0,0 +1,10 @@
1
+ import { JSONSchema } from 'wasm-ast-types';
2
+ export declare const readSchemas: ({ schemaDir, argv, clean }: {
3
+ schemaDir: any;
4
+ argv: any;
5
+ clean?: boolean;
6
+ }) => Promise<any>;
7
+ export declare const findQueryMsg: (schemas: any) => any;
8
+ export declare const findExecuteMsg: (schemas: any) => any;
9
+ export declare const findAndParseTypes: (schemas: any) => Promise<{}>;
10
+ export declare const getDefinitionSchema: (schemas: JSONSchema[]) => JSONSchema;