@cosmwasm/ts-codegen 0.7.3 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +238 -59
- package/main/builder/builder.js +456 -0
- package/main/builder/index.js +18 -0
- package/main/bundler/bundler.js +74 -0
- package/main/bundler/index.js +18 -0
- package/main/cmds.js +3 -9
- package/main/commands/create-boilerplate.js +195 -0
- package/main/commands/generate.js +91 -16
- package/main/generators/{ts-client.js → client.js} +29 -23
- package/main/generators/{from-partial.js → message-composer.js} +29 -21
- package/main/generators/react-query.js +27 -21
- package/main/generators/recoil.js +31 -15
- package/main/generators/types.js +95 -0
- package/main/index.js +71 -17
- package/main/types.js +1 -0
- package/main/utils/schemas.js +5 -3
- package/module/builder/builder.js +178 -0
- package/module/builder/index.js +1 -0
- package/module/bundler/bundler.js +36 -0
- package/module/bundler/index.js +1 -0
- package/module/cmds.js +2 -6
- package/module/commands/create-boilerplate.js +116 -0
- package/module/commands/generate.js +82 -8
- package/module/generators/{ts-client.js → client.js} +21 -15
- package/module/generators/{from-partial.js → message-composer.js} +23 -17
- package/module/generators/react-query.js +20 -15
- package/module/generators/recoil.js +23 -9
- package/module/generators/types.js +40 -0
- package/module/index.js +12 -5
- package/module/types.js +1 -0
- package/module/utils/schemas.js +3 -2
- package/package.json +5 -3
- package/types/builder/builder.d.ts +39 -0
- package/types/builder/index.d.ts +1 -0
- package/types/bundler/bundler.d.ts +4 -0
- package/types/bundler/index.d.ts +1 -0
- package/types/commands/{from-partial.d.ts → client.d.ts} +0 -0
- package/types/commands/message-composer.d.ts +2 -0
- package/types/commands/types.d.ts +2 -0
- package/types/generators/client.d.ts +3 -0
- package/types/generators/message-composer.d.ts +3 -0
- package/types/generators/react-query.d.ts +2 -1
- package/types/generators/recoil.d.ts +2 -1
- package/types/generators/types.d.ts +3 -0
- package/types/index.d.ts +10 -5
- package/types/types.d.ts +1 -0
- package/types/utils/schemas.d.ts +8 -4
- package/main/commands/from-partial.js +0 -78
- package/main/commands/react-query.js +0 -98
- package/main/commands/recoil.js +0 -78
- package/main/utils/imports.js +0 -26
- package/module/commands/from-partial.js +0 -33
- package/module/commands/react-query.js +0 -54
- package/module/commands/recoil.js +0 -33
- package/module/utils/imports.js +0 -10
- package/types/clean.d.ts +0 -1
- package/types/cleanse.d.ts +0 -1
- package/types/commands/boilerplate.d.ts +0 -2
- package/types/cosmwasm-typescript-gen.d.ts +0 -2
- package/types/from-partial.d.ts +0 -2
- package/types/generate.d.ts +0 -2
- package/types/generators/from-partial.d.ts +0 -2
- package/types/generators/ts-client.d.ts +0 -2
- package/types/header.d.ts +0 -1
- package/types/imports.d.ts +0 -1
- package/types/parse.d.ts +0 -1
- package/types/prompt.d.ts +0 -3
- package/types/react-query.d.ts +0 -2
- package/types/recoil.d.ts +0 -2
- package/types/utils/imports.d.ts +0 -1
- 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
|
-
- [
|
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
|
-
|
45
|
+
## Quickstart
|
41
46
|
|
42
47
|
Clone your project and `cd` into your contracts folder
|
43
48
|
|
@@ -49,152 +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
|
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
|
-
##
|
66
|
+
## Usage
|
58
67
|
|
59
|
-
|
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
|
-
###
|
73
|
+
### Programmatic Usage
|
74
|
+
|
75
|
+
For production usage, we recommend setting up a build script that uses the main entry point:
|
65
76
|
|
66
|
-
|
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
|
-
|
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
|
-
|
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.
|
81
161
|
|
82
|
-
|
83
|
-
import { tsClient } from '@cosmwasm/ts-codegen';
|
84
|
-
declare const tsClient = (name: string, schemas: any[], outPath: string) => Promise<void>;
|
85
|
-
```
|
162
|
+
[see example output code](https://gist.github.com/pyramation/a3bf4aa7b60a31287d0720ca1bb5473b)
|
86
163
|
|
87
|
-
|
164
|
+
#### React Query Options
|
88
165
|
|
89
|
-
|
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 |
|
90
173
|
|
91
|
-
[see example output code](https://gist.github.com/pyramation/a3bf4aa7b60a31287d0720ca1bb5473b)
|
92
174
|
|
175
|
+
#### React Query via CLI
|
93
176
|
|
94
|
-
|
177
|
+
Here is an example without optional client, using v3 for `react-query`, without mutations:
|
95
178
|
|
96
179
|
```sh
|
97
|
-
cosmwasm-ts-codegen
|
180
|
+
cosmwasm-ts-codegen generate \
|
181
|
+
--plugin client \
|
182
|
+
--plugin react-query \
|
98
183
|
--schema ./schema \
|
99
184
|
--out ./ts \
|
100
185
|
--name MyContractName \
|
186
|
+
--version v3 \
|
101
187
|
--no-optionalClient \
|
102
|
-
--no-v4 \
|
103
188
|
--no-mutations
|
104
189
|
```
|
105
190
|
|
106
191
|
Example with optional client, using v4, with mutations:
|
107
192
|
|
108
193
|
```sh
|
109
|
-
cosmwasm-ts-codegen
|
194
|
+
cosmwasm-ts-codegen generate \
|
195
|
+
--plugin react-query \
|
110
196
|
--schema ./schema \
|
111
197
|
--out ./ts \
|
112
198
|
--name MyContractName \
|
113
199
|
--optionalClient \
|
114
|
-
--v4 \
|
200
|
+
--version v4 \
|
115
201
|
--mutations
|
116
202
|
```
|
117
203
|
|
118
|
-
|
204
|
+
### Recoil
|
119
205
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
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
|
128
218
|
```
|
129
219
|
|
130
|
-
####
|
220
|
+
#### Recoil Options
|
131
221
|
|
132
222
|
| option | description |
|
133
223
|
| ------------------------------ | ------------------------------------------------------------------- |
|
134
|
-
| `
|
135
|
-
| `reactQuery.v4` | uses `@tanstack/react-query` and syntax instead of v3 `react-query` |
|
136
|
-
| `reactQuery.mutations` | also generate mutations |
|
137
|
-
| `reactQuery.camelize` | use camelCase style for property names |
|
224
|
+
| `recoil.enabled` | enable the recoil plugin |
|
138
225
|
|
139
|
-
###
|
226
|
+
### Message Composer
|
140
227
|
|
141
|
-
Generate
|
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.
|
142
229
|
|
143
|
-
[see example output code](https://gist.github.com/pyramation/
|
230
|
+
[see example output code](https://gist.github.com/pyramation/f50869d1ecdb6d6ced2bc0a44c6ff492)
|
144
231
|
|
232
|
+
#### Message Composer via CLI
|
145
233
|
|
146
234
|
```sh
|
147
|
-
cosmwasm-ts-codegen
|
235
|
+
cosmwasm-ts-codegen generate \
|
236
|
+
--plugin message-composer \
|
148
237
|
--schema ./schema \
|
149
238
|
--out ./ts \
|
150
239
|
--name MyContractName
|
151
240
|
```
|
241
|
+
#### Message Composer Options
|
242
|
+
|
243
|
+
| option | description |
|
244
|
+
| ------------------------------ | ------------------------------------------------------------------- |
|
245
|
+
| `messageComposer.enabled` | enable the messageComposer plugin |
|
246
|
+
|
247
|
+
### Bundles
|
152
248
|
|
153
|
-
|
249
|
+
The bundler will make a nice package of all your contracts. For example:
|
154
250
|
|
155
251
|
```ts
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
252
|
+
const {
|
253
|
+
MinterQueryClient,
|
254
|
+
useMinterConfigQuery
|
255
|
+
} = contracts.Minter;
|
256
|
+
|
257
|
+
const { CwAdminFactoryClient } = contracts.CwAdminFactory;
|
162
258
|
```
|
163
|
-
|
259
|
+
#### Bundler Options
|
164
260
|
|
165
|
-
|
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 |
|
166
266
|
|
167
|
-
|
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:
|
168
286
|
|
169
287
|
```sh
|
170
|
-
cosmwasm-ts-codegen
|
288
|
+
cosmwasm-ts-codegen generate \
|
289
|
+
--plugin client
|
171
290
|
--schema ./schema \
|
172
291
|
--out ./ts \
|
173
|
-
--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
|
174
304
|
```
|
175
305
|
|
176
|
-
|
306
|
+
#### Bypassing the Prompt
|
177
307
|
|
178
|
-
|
179
|
-
|
180
|
-
|
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
|
335
|
+
```
|
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
|
181
347
|
```
|
182
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
|
+
```
|
183
362
|
### Example Output
|
184
363
|
|
185
|
-
- `cosmwasm-ts-codegen generate`
|
364
|
+
- `cosmwasm-ts-codegen generate --plugin client`
|
186
365
|
|
187
366
|
https://gist.github.com/pyramation/ba67ec56e4e2a39cadea55430f9993e5
|
188
367
|
|
189
|
-
- `cosmwasm-ts-codegen
|
368
|
+
- `cosmwasm-ts-codegen generate --plugin message-composer`
|
190
369
|
|
191
370
|
https://gist.github.com/pyramation/f50869d1ecdb6d6ced2bc0a44c6ff492
|
192
371
|
|
193
|
-
- `cosmwasm-ts-codegen react-query`
|
372
|
+
- `cosmwasm-ts-codegen generate --plugin react-query`
|
194
373
|
|
195
374
|
https://gist.github.com/pyramation/a3bf4aa7b60a31287d0720ca1bb5473b
|
196
375
|
|
197
|
-
- `cosmwasm-ts-codegen recoil`
|
376
|
+
- `cosmwasm-ts-codegen generate --plugin recoil`
|
198
377
|
|
199
378
|
https://gist.github.com/pyramation/48b28a75def1a16b233b369297f05f0e
|
200
379
|
|