@cosmwasm/ts-codegen 0.7.4 → 0.8.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 (71) hide show
  1. package/README.md +238 -64
  2. package/main/builder/builder.js +456 -0
  3. package/main/builder/index.js +18 -0
  4. package/main/bundler/bundler.js +74 -0
  5. package/main/bundler/index.js +18 -0
  6. package/main/cmds.js +3 -9
  7. package/main/commands/create-boilerplate.js +195 -0
  8. package/main/commands/generate.js +91 -16
  9. package/main/generators/{ts-client.js → client.js} +24 -24
  10. package/main/generators/{from-partial.js → message-composer.js} +29 -21
  11. package/main/generators/react-query.js +27 -21
  12. package/main/generators/recoil.js +31 -15
  13. package/main/generators/types.js +95 -0
  14. package/main/index.js +71 -17
  15. package/main/types.js +1 -0
  16. package/main/utils/schemas.js +5 -3
  17. package/module/builder/builder.js +178 -0
  18. package/module/builder/index.js +1 -0
  19. package/module/bundler/bundler.js +36 -0
  20. package/module/bundler/index.js +1 -0
  21. package/module/cmds.js +2 -6
  22. package/module/commands/create-boilerplate.js +116 -0
  23. package/module/commands/generate.js +82 -8
  24. package/module/generators/{ts-client.js → client.js} +17 -17
  25. package/module/generators/{from-partial.js → message-composer.js} +23 -17
  26. package/module/generators/react-query.js +20 -15
  27. package/module/generators/recoil.js +23 -9
  28. package/module/generators/types.js +40 -0
  29. package/module/index.js +12 -5
  30. package/module/types.js +1 -0
  31. package/module/utils/schemas.js +3 -2
  32. package/package.json +4 -3
  33. package/types/builder/builder.d.ts +39 -0
  34. package/types/builder/index.d.ts +1 -0
  35. package/types/bundler/bundler.d.ts +4 -0
  36. package/types/bundler/index.d.ts +1 -0
  37. package/types/commands/{from-partial.d.ts → client.d.ts} +0 -0
  38. package/types/commands/message-composer.d.ts +2 -0
  39. package/types/commands/types.d.ts +2 -0
  40. package/types/generators/client.d.ts +3 -0
  41. package/types/generators/message-composer.d.ts +3 -0
  42. package/types/generators/react-query.d.ts +2 -1
  43. package/types/generators/recoil.d.ts +2 -1
  44. package/types/generators/types.d.ts +3 -0
  45. package/types/index.d.ts +10 -5
  46. package/types/types.d.ts +1 -0
  47. package/types/utils/schemas.d.ts +8 -4
  48. package/main/commands/from-partial.js +0 -78
  49. package/main/commands/react-query.js +0 -98
  50. package/main/commands/recoil.js +0 -78
  51. package/main/utils/imports.js +0 -26
  52. package/module/commands/from-partial.js +0 -33
  53. package/module/commands/react-query.js +0 -54
  54. package/module/commands/recoil.js +0 -33
  55. package/module/utils/imports.js +0 -10
  56. package/types/clean.d.ts +0 -1
  57. package/types/cleanse.d.ts +0 -1
  58. package/types/commands/boilerplate.d.ts +0 -2
  59. package/types/cosmwasm-typescript-gen.d.ts +0 -2
  60. package/types/from-partial.d.ts +0 -2
  61. package/types/generate.d.ts +0 -2
  62. package/types/generators/from-partial.d.ts +0 -2
  63. package/types/generators/ts-client.d.ts +0 -2
  64. package/types/header.d.ts +0 -1
  65. package/types/imports.d.ts +0 -1
  66. package/types/parse.d.ts +0 -1
  67. package/types/prompt.d.ts +0 -3
  68. package/types/react-query.d.ts +0 -2
  69. package/types/recoil.d.ts +0 -2
  70. package/types/utils/imports.d.ts +0 -1
  71. package/types/utils.d.ts +0 -10
package/README.md CHANGED
@@ -27,17 +27,22 @@ The quickest and easiest way to interact with CosmWasm Contracts. `@cosmwasm/ts-
27
27
  - [Table of contents](#table-of-contents)
28
28
  - [QuickStart](#quickstart)
29
29
  - [Usage](#usage)
30
- - [Generate TS Clients](#generate)
30
+ - [Programmatic Usage](#programmatic-usage)
31
+ - [Types](#types)
32
+ - [TS Clients](#client)
31
33
  - [React Query](#react-query)
32
34
  - [Recoil](#recoil)
33
35
  - [Message Composer](#message-composer)
36
+ - [Bundles](#bundles)
37
+ - [CLI Usage and Examples](#cli-usage-and-examples)
38
+ - [Advanced Usage](#advanced-usage)
34
39
  - [Example Output](#example-output)
35
40
  - [JSON Schema](#json-schema)
36
41
  - [JSON Schema Generation](#json-schema-generation)
37
42
  - [Exporting Schemas](#exporting-schemas)
38
43
  - [Developing](#developing)
39
44
  - [Related](#related)
40
- ### Quickstart
45
+ ## Quickstart
41
46
 
42
47
  Clone your project and `cd` into your contracts folder
43
48
 
@@ -49,157 +54,326 @@ cd stargaze-contracts/contracts/sg721/
49
54
  Run `cosmwasm-ts-codegen` to generate your code.
50
55
 
51
56
  ```sh
52
- cosmwasm-ts-codegen generate --schema ./schema --out ./ts --name SG721
57
+ cosmwasm-ts-codegen generate \
58
+ --plugin client \
59
+ --schema ./schema \
60
+ --out ./ts \
61
+ --name SG721
53
62
  ```
54
63
 
55
64
  The output will be in the folder specified by `--out`, enjoy!
56
65
 
57
- ## usage
66
+ ## Usage
58
67
 
59
- Get started quickly using our `cli` by globally installing via npm:
68
+ You can get started quickly using our `cli` by globally installing via npm:
60
69
 
61
70
  ```
62
71
  npm install -g @cosmwasm/ts-codegen
63
72
  ```
64
- ### generate
73
+ ### Programmatic Usage
74
+
75
+ For production usage, we recommend setting up a build script that uses the main entry point:
65
76
 
66
- Generate a basic TS client for your contracts. The `generate` command will make types which will be essential for your bindings.
77
+ ```ts
78
+ import codegen from '@cosmwasm/ts-codegen';
79
+
80
+ export const main = async () => {
81
+ await codegen({
82
+ contracts: [
83
+ {
84
+ name: 'SG721',
85
+ dir: './path/to/sg721/schema'
86
+ },
87
+ {
88
+ name: 'Minter',
89
+ dir: './path/to/Minter/schema'
90
+ }
91
+ ],
92
+ outPath: './path/to/code/src/',
93
+ options: {
94
+ bundle: {
95
+ bundleFile: 'index.ts',
96
+ scope: 'contracts'
97
+ },
98
+ types: {
99
+ enabled: true
100
+ },
101
+ client: {
102
+ enabled: true
103
+ },
104
+ reactQuery: {
105
+ enabled: true,
106
+ optionalClient: true,
107
+ version: 'v4',
108
+ mutations: true
109
+ },
110
+ recoil: {
111
+ enabled: false
112
+ },
113
+ messageComposer: {
114
+ enabled: false
115
+ }
116
+ }
117
+ });
118
+ };
119
+
120
+ main().then(() => {
121
+ console.log('✨ all done!);
122
+ });
123
+ ```
124
+ #### Types
67
125
 
68
- This command also generates a `QueryClient` for queries as well as a `Client` for queries and mutations.
126
+ Typescript types and interfaces are generated in separate files so they can be imported into various generated plugins.
69
127
 
70
128
  [see example output code](https://gist.github.com/pyramation/ba67ec56e4e2a39cadea55430f9993e5)
71
129
 
130
+ #### Types Options
131
+
132
+ | option | description |
133
+ | ----------------------------- | --------------------------------------------------- |
134
+ | `types.enabled` | enable type generation |
135
+ | `types.aliasExecuteMsg` | generate a type alias based on the contract name |
136
+
137
+ ### Client
138
+
139
+ The `client` plugin will generate TS client classes for your contracts. This option generates a `QueryClient` for queries as well as a `Client` for queries and mutations.
140
+
141
+ [see example output code](https://gist.github.com/pyramation/ba67ec56e4e2a39cadea55430f9993e5)
142
+
143
+ #### Client Options
144
+
145
+ | option | description |
146
+ | ----------------------------- | --------------------------------------------------- |
147
+ | `client.enabled` | generate TS client classes for your contracts |
148
+
149
+ #### Client via CLI
72
150
 
73
151
  ```sh
74
152
  cosmwasm-ts-codegen generate \
153
+ --plugin client
75
154
  --schema ./schema \
76
155
  --out ./ts \
77
156
  --name MyContractName
78
157
  ```
158
+ ### React Query
79
159
 
80
- for programmatic usage, you can use the `tsClient` function:
81
-
82
- ```ts
83
- import { tsClient } from '@cosmwasm/ts-codegen';
84
- declare const tsClient = (name: string, schemas: any[], outPath: string, tsClientOptions: TSClientOptions) => Promise<void>;
85
- ```
86
- #### TS Client Options
160
+ 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.
87
161
 
88
- | option | description |
89
- | ----------------------------- | --------------------------------------------------- |
90
- | `tsClient.aliasExecuteMsg` | generate a type alias based on the contract name |
162
+ [see example output code](https://gist.github.com/pyramation/a3bf4aa7b60a31287d0720ca1bb5473b)
91
163
 
92
- ### react query
164
+ #### React Query Options
93
165
 
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.
166
+ | option | description |
167
+ | ------------------------------ | ------------------------------------------------------------------- |
168
+ | `reactQuery.enabled` | enable the react-query plugin |
169
+ | `reactQuery.optionalClient` | allows contract client to be undefined as the component renders |
170
+ | `reactQuery.version` | `v4` uses `@tanstack/react-query` and `v3` uses `react-query` |
171
+ | `reactQuery.mutations` | also generate mutations |
172
+ | `reactQuery.camelize` | use camelCase style for property names |
95
173
 
96
- [see example output code](https://gist.github.com/pyramation/a3bf4aa7b60a31287d0720ca1bb5473b)
97
174
 
175
+ #### React Query via CLI
98
176
 
99
- Example without optional client, using v3, without mutations:
177
+ Here is an example without optional client, using v3 for `react-query`, without mutations:
100
178
 
101
179
  ```sh
102
- cosmwasm-ts-codegen react-query \
180
+ cosmwasm-ts-codegen generate \
181
+ --plugin client \
182
+ --plugin react-query \
103
183
  --schema ./schema \
104
184
  --out ./ts \
105
185
  --name MyContractName \
186
+ --version v3 \
106
187
  --no-optionalClient \
107
- --no-v4 \
108
188
  --no-mutations
109
189
  ```
110
190
 
111
191
  Example with optional client, using v4, with mutations:
112
192
 
113
193
  ```sh
114
- cosmwasm-ts-codegen react-query \
194
+ cosmwasm-ts-codegen generate \
195
+ --plugin react-query \
115
196
  --schema ./schema \
116
197
  --out ./ts \
117
198
  --name MyContractName \
118
199
  --optionalClient \
119
- --v4 \
200
+ --version v4 \
120
201
  --mutations
121
202
  ```
122
203
 
123
- For programmatic usage, you can use the `reactQuery` function:
204
+ ### Recoil
124
205
 
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>;
206
+ Generate [recoil](https://recoiljs.org/) bindings for your contracts with the `recoil` command.
207
+
208
+ [see example output code](https://gist.github.com/pyramation/48b28a75def1a16b233b369297f05f0e)
209
+
210
+ #### Recoil via CLI
211
+
212
+ ```sh
213
+ cosmwasm-ts-codegen generate \
214
+ --plugin recoil \
215
+ --schema ./schema \
216
+ --out ./ts \
217
+ --name MyContractName
133
218
  ```
134
219
 
135
- #### React Query Options
220
+ #### Recoil Options
136
221
 
137
222
  | option | description |
138
223
  | ------------------------------ | ------------------------------------------------------------------- |
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 |
224
+ | `recoil.enabled` | enable the recoil plugin |
143
225
 
144
- ### recoil
226
+ ### Message Composer
145
227
 
146
- Generate [recoil](https://recoiljs.org/) bindings for your contracts with the `recoil` command.
228
+ Generate pure message objects with the proper `utf8` encoding and `typeUrl` configured that you can broadcast yourself via `cosmjs` with the `message-composer` command.
147
229
 
148
- [see example output code](https://gist.github.com/pyramation/48b28a75def1a16b233b369297f05f0e)
230
+ [see example output code](https://gist.github.com/pyramation/f50869d1ecdb6d6ced2bc0a44c6ff492)
149
231
 
232
+ #### Message Composer via CLI
150
233
 
151
234
  ```sh
152
- cosmwasm-ts-codegen recoil \
235
+ cosmwasm-ts-codegen generate \
236
+ --plugin message-composer \
153
237
  --schema ./schema \
154
238
  --out ./ts \
155
239
  --name MyContractName
156
240
  ```
241
+ #### Message Composer Options
242
+
243
+ | option | description |
244
+ | ------------------------------ | ------------------------------------------------------------------- |
245
+ | `messageComposer.enabled` | enable the messageComposer plugin |
157
246
 
158
- for programmatic usage, you can use the `recoil` function:
247
+ ### Bundles
248
+
249
+ The bundler will make a nice package of all your contracts. For example:
159
250
 
160
251
  ```ts
161
- import { recoil } from '@cosmwasm/ts-codegen';
162
- declare const recoil = (
163
- name: string,
164
- schemas: any[],
165
- outPath: string
166
- ) => Promise<void>;
252
+ const {
253
+ MinterQueryClient,
254
+ useMinterConfigQuery
255
+ } = contracts.Minter;
256
+
257
+ const { CwAdminFactoryClient } = contracts.CwAdminFactory;
167
258
  ```
168
- ### Message Composer
259
+ #### Bundler Options
169
260
 
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.
261
+ | option | description |
262
+ | --------------------- | -------------------------------------------------------------------------------- |
263
+ | `bundle.enabled` | enable the bundler plugin |
264
+ | `bundle.scope` | name of the scope, defaults to `contracts` (you can use `.` to make more scopes) |
265
+ | `bundle.bundleFile` | name of the bundle file |
171
266
 
172
- [see example output code](https://gist.github.com/pyramation/f50869d1ecdb6d6ced2bc0a44c6ff492)
267
+ ### CLI Usage and Examples
268
+
269
+ #### Interactive prompt
270
+
271
+ The CLI is interactive, and if you don't specify an option, it will interactively prompt you.
272
+
273
+ ```sh
274
+ cosmwasm-ts-codegen generate
275
+ ? [plugin] which plugins? (Press <space> to select, <a> to toggle all, <i> to invert selection)
276
+ ❯◯ client
277
+ ◯ recoil
278
+ ◯ react-query
279
+ ◯ message-composer
280
+ ```
281
+
282
+ In this example, you can press space bar to select a number of plugins you wish you enable.
283
+ #### Specifying Plugins
284
+
285
+ Additionally, it will also show you the name of the field (in this case `plugin`) so you can specify the parameter (for example when using CI/CD) on the comand line. Here is an exampl with `--plugin` set to `client` via CLI:
173
286
 
174
287
  ```sh
175
- cosmwasm-ts-codegen from-partial \
288
+ cosmwasm-ts-codegen generate \
289
+ --plugin client
176
290
  --schema ./schema \
177
291
  --out ./ts \
178
- --name MyContractName
292
+ --name MyContractName
293
+ ```
294
+
295
+ You can specify multiple `--plugin` options using the `generate` command:
296
+
297
+ ```sh
298
+ cosmwasm-ts-codegen generate \
299
+ --plugin client \
300
+ --plugin recoil \
301
+ --schema ./schema \
302
+ --out ./ts \
303
+ --name SG721
179
304
  ```
180
305
 
181
- for programmatic usage, you can use the `fromPartial` function:
306
+ #### Bypassing the Prompt
182
307
 
183
- ```ts
184
- import { fromPartial } from '@cosmwasm/ts-codegen';
185
- declare const fromPartial = (name: string, schemas: any[], outPath: string) => Promise<void>;
308
+ All options can be provided so you can bypass the prompt.
309
+
310
+ For confirm options, you can pass `--no-<name>` to set the value to false. Here is an example without optional client, using v3 for `react-query`, without mutations:
311
+
312
+ ```sh
313
+ cosmwasm-ts-codegen generate \
314
+ --plugin client \
315
+ --plugin react-query \
316
+ --schema ./schema \
317
+ --out ./ts \
318
+ --name MyContractName \
319
+ --version v3 \
320
+ --no-optionalClient \
321
+ --no-mutations
322
+ ```
323
+
324
+ Example with optional client, using v4, with mutations:
325
+
326
+ ```sh
327
+ cosmwasm-ts-codegen generate \
328
+ --plugin react-query \
329
+ --schema ./schema \
330
+ --out ./ts \
331
+ --name MyContractName \
332
+ --optionalClient \
333
+ --version v4 \
334
+ --mutations
186
335
  ```
187
336
 
337
+ #### Types Only Option
338
+
339
+ If needed, you can generate only the types with the `typesOnly` option;
340
+
341
+ ```sh
342
+ cosmwasm-ts-codegen generate \
343
+ --typesOnly \
344
+ --schema ./schema \
345
+ --out ./ts \
346
+ --name SG721
347
+ ```
348
+
349
+ ### Advanced Usage
350
+
351
+ for lower-level access, you can import the various plugins directly:
352
+
353
+ ```ts
354
+ import {
355
+ generateTypes,
356
+ generateClient,
357
+ generateReactQuery,
358
+ generateRecoil,
359
+ generateMessageComposer,
360
+ } from '@cosmwasm/ts-codegen';
361
+ ```
188
362
  ### Example Output
189
363
 
190
- - `cosmwasm-ts-codegen generate`
364
+ - `cosmwasm-ts-codegen generate --plugin client`
191
365
 
192
366
  https://gist.github.com/pyramation/ba67ec56e4e2a39cadea55430f9993e5
193
367
 
194
- - `cosmwasm-ts-codegen from-partial`
368
+ - `cosmwasm-ts-codegen generate --plugin message-composer`
195
369
 
196
370
  https://gist.github.com/pyramation/f50869d1ecdb6d6ced2bc0a44c6ff492
197
371
 
198
- - `cosmwasm-ts-codegen react-query`
372
+ - `cosmwasm-ts-codegen generate --plugin react-query`
199
373
 
200
374
  https://gist.github.com/pyramation/a3bf4aa7b60a31287d0720ca1bb5473b
201
375
 
202
- - `cosmwasm-ts-codegen recoil`
376
+ - `cosmwasm-ts-codegen generate --plugin recoil`
203
377
 
204
378
  https://gist.github.com/pyramation/48b28a75def1a16b233b369297f05f0e
205
379