@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.
- package/README.md +238 -64
- 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} +24 -24
- 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} +17 -17
- 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 +4 -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,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
|
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
|
-
|
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
|
-
|
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
|
-
|
164
|
+
#### React Query Options
|
93
165
|
|
94
|
-
|
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
|
-
|
177
|
+
Here is an example without optional client, using v3 for `react-query`, without mutations:
|
100
178
|
|
101
179
|
```sh
|
102
|
-
cosmwasm-ts-codegen
|
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
|
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
|
-
|
204
|
+
### Recoil
|
124
205
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
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
|
-
####
|
220
|
+
#### Recoil Options
|
136
221
|
|
137
222
|
| option | description |
|
138
223
|
| ------------------------------ | ------------------------------------------------------------------- |
|
139
|
-
| `
|
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
|
-
###
|
226
|
+
### Message Composer
|
145
227
|
|
146
|
-
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.
|
147
229
|
|
148
|
-
[see example output code](https://gist.github.com/pyramation/
|
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
|
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
|
-
|
247
|
+
### Bundles
|
248
|
+
|
249
|
+
The bundler will make a nice package of all your contracts. For example:
|
159
250
|
|
160
251
|
```ts
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
252
|
+
const {
|
253
|
+
MinterQueryClient,
|
254
|
+
useMinterConfigQuery
|
255
|
+
} = contracts.Minter;
|
256
|
+
|
257
|
+
const { CwAdminFactoryClient } = contracts.CwAdminFactory;
|
167
258
|
```
|
168
|
-
|
259
|
+
#### Bundler Options
|
169
260
|
|
170
|
-
|
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
|
-
|
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
|
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
|
-
|
306
|
+
#### Bypassing the Prompt
|
182
307
|
|
183
|
-
|
184
|
-
|
185
|
-
|
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
|
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
|
|