@cosmwasm/ts-codegen 0.5.8 → 0.7.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/main/cli.js +1 -1
- package/main/cmds.js +0 -3
- package/main/commands/from-partial.js +9 -5
- package/main/commands/generate.js +10 -6
- package/main/commands/react-query.js +14 -5
- package/main/commands/recoil.js +8 -4
- package/main/file.js +1 -1
- package/main/{from-partial.js → generators/from-partial.js} +8 -7
- package/main/{react-query.js → generators/react-query.js} +49 -19
- package/main/{recoil.js → generators/recoil.js} +8 -7
- package/main/{generate.js → generators/ts-client.js} +16 -17
- package/main/index.js +25 -11
- package/main/{clean.js → utils/clean.js} +0 -0
- package/main/{cleanse.js → utils/cleanse.js} +0 -0
- package/main/{header.js → utils/header.js} +1 -1
- package/main/utils/imports.js +26 -0
- package/main/utils/index.js +18 -0
- package/main/{parse.js → utils/parse.js} +0 -0
- package/main/{prompt.js → utils/prompt.js} +0 -0
- package/main/utils/schemas.js +167 -0
- package/module/cli.js +1 -1
- package/module/cmds.js +0 -2
- package/module/commands/from-partial.js +3 -3
- package/module/commands/generate.js +4 -4
- package/module/commands/react-query.js +8 -3
- package/module/commands/recoil.js +2 -2
- package/module/file.js +1 -1
- package/module/{from-partial.js → generators/from-partial.js} +7 -5
- package/module/generators/react-query.js +67 -0
- package/module/{recoil.js → generators/recoil.js} +7 -5
- package/module/{generate.js → generators/ts-client.js} +14 -15
- package/module/index.js +6 -5
- package/module/{clean.js → utils/clean.js} +0 -0
- package/module/{cleanse.js → utils/cleanse.js} +0 -0
- package/module/{header.js → utils/header.js} +1 -1
- package/module/utils/imports.js +10 -0
- package/module/utils/index.js +1 -0
- package/module/{parse.js → utils/parse.js} +0 -0
- package/module/{prompt.js → utils/prompt.js} +0 -0
- package/module/{utils.js → utils/schemas.js} +19 -2
- package/package.json +25 -25
- package/types/cleanse.d.ts +1 -0
- package/types/imports.d.ts +1 -0
- package/types/index.d.ts +1 -1
- package/types/react-query.d.ts +1 -1
- package/types/ts-codegen.d.ts +2 -0
- package/types/utils.d.ts +8 -2
- package/main/commands/boilerplate.js +0 -195
- package/main/utils.js +0 -123
- package/module/commands/boilerplate.js +0 -116
- package/module/react-query.js +0 -36
@@ -0,0 +1,167 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4
|
+
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
6
|
+
value: true
|
7
|
+
});
|
8
|
+
exports.readSchemas = exports.getDefinitionSchema = exports.findQueryMsg = exports.findExecuteMsg = exports.findAndParseTypes = void 0;
|
9
|
+
|
10
|
+
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
11
|
+
|
12
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
13
|
+
|
14
|
+
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
15
|
+
|
16
|
+
var _glob = require("glob");
|
17
|
+
|
18
|
+
var _fs = require("fs");
|
19
|
+
|
20
|
+
var _cleanse = require("./cleanse");
|
21
|
+
|
22
|
+
var _jsonSchemaToTypescript = require("json-schema-to-typescript");
|
23
|
+
|
24
|
+
var _parse = require("./parse");
|
25
|
+
|
26
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
27
|
+
|
28
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
29
|
+
|
30
|
+
var readSchemas = /*#__PURE__*/function () {
|
31
|
+
var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(_ref) {
|
32
|
+
var schemaDir, argv, _ref$clean, clean, fn, files, schemas;
|
33
|
+
|
34
|
+
return _regenerator["default"].wrap(function _callee$(_context) {
|
35
|
+
while (1) {
|
36
|
+
switch (_context.prev = _context.next) {
|
37
|
+
case 0:
|
38
|
+
schemaDir = _ref.schemaDir, argv = _ref.argv, _ref$clean = _ref.clean, clean = _ref$clean === void 0 ? true : _ref$clean;
|
39
|
+
fn = clean ? _cleanse.cleanse : function (str) {
|
40
|
+
return str;
|
41
|
+
};
|
42
|
+
files = (0, _glob.sync)(schemaDir + '/**/*.json');
|
43
|
+
schemas = files.map(function (file) {
|
44
|
+
return JSON.parse((0, _fs.readFileSync)(file, 'utf-8'));
|
45
|
+
});
|
46
|
+
|
47
|
+
if (!argv.packed) {
|
48
|
+
_context.next = 8;
|
49
|
+
break;
|
50
|
+
}
|
51
|
+
|
52
|
+
if (!(schemas.length !== 1)) {
|
53
|
+
_context.next = 7;
|
54
|
+
break;
|
55
|
+
}
|
56
|
+
|
57
|
+
throw new Error('packed option only supports one file');
|
58
|
+
|
59
|
+
case 7:
|
60
|
+
return _context.abrupt("return", Object.values(fn(schemas[0])));
|
61
|
+
|
62
|
+
case 8:
|
63
|
+
return _context.abrupt("return", fn(schemas));
|
64
|
+
|
65
|
+
case 9:
|
66
|
+
case "end":
|
67
|
+
return _context.stop();
|
68
|
+
}
|
69
|
+
}
|
70
|
+
}, _callee);
|
71
|
+
}));
|
72
|
+
|
73
|
+
return function readSchemas(_x) {
|
74
|
+
return _ref2.apply(this, arguments);
|
75
|
+
};
|
76
|
+
}();
|
77
|
+
|
78
|
+
exports.readSchemas = readSchemas;
|
79
|
+
|
80
|
+
var findQueryMsg = function findQueryMsg(schemas) {
|
81
|
+
var QueryMsg = schemas.find(function (schema) {
|
82
|
+
return schema.title === 'QueryMsg';
|
83
|
+
});
|
84
|
+
return QueryMsg;
|
85
|
+
};
|
86
|
+
|
87
|
+
exports.findQueryMsg = findQueryMsg;
|
88
|
+
|
89
|
+
var findExecuteMsg = function findExecuteMsg(schemas) {
|
90
|
+
var ExecuteMsg = schemas.find(function (schema) {
|
91
|
+
return schema.title === 'ExecuteMsg' || schema.title === 'ExecuteMsg_for_Empty' || // if cleanse is used, this is never
|
92
|
+
schema.title === 'ExecuteMsgForEmpty';
|
93
|
+
});
|
94
|
+
return ExecuteMsg;
|
95
|
+
};
|
96
|
+
|
97
|
+
exports.findExecuteMsg = findExecuteMsg;
|
98
|
+
|
99
|
+
var findAndParseTypes = /*#__PURE__*/function () {
|
100
|
+
var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(schemas) {
|
101
|
+
var Types, allTypes, typ, _i, _Object$keys, key, result, typeHash;
|
102
|
+
|
103
|
+
return _regenerator["default"].wrap(function _callee2$(_context2) {
|
104
|
+
while (1) {
|
105
|
+
switch (_context2.prev = _context2.next) {
|
106
|
+
case 0:
|
107
|
+
Types = schemas;
|
108
|
+
allTypes = [];
|
109
|
+
_context2.t0 = _regenerator["default"].keys(Types);
|
110
|
+
|
111
|
+
case 3:
|
112
|
+
if ((_context2.t1 = _context2.t0()).done) {
|
113
|
+
_context2.next = 12;
|
114
|
+
break;
|
115
|
+
}
|
116
|
+
|
117
|
+
typ = _context2.t1.value;
|
118
|
+
|
119
|
+
if (Types[typ].definitions) {
|
120
|
+
for (_i = 0, _Object$keys = Object.keys(Types[typ].definitions); _i < _Object$keys.length; _i++) {
|
121
|
+
key = _Object$keys[_i];
|
122
|
+
// set title
|
123
|
+
Types[typ].definitions[key].title = key;
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
_context2.next = 8;
|
128
|
+
return (0, _jsonSchemaToTypescript.compile)(Types[typ], Types[typ].title);
|
129
|
+
|
130
|
+
case 8:
|
131
|
+
result = _context2.sent;
|
132
|
+
allTypes.push(result);
|
133
|
+
_context2.next = 3;
|
134
|
+
break;
|
135
|
+
|
136
|
+
case 12:
|
137
|
+
typeHash = (0, _parse.parser)(allTypes);
|
138
|
+
return _context2.abrupt("return", typeHash);
|
139
|
+
|
140
|
+
case 14:
|
141
|
+
case "end":
|
142
|
+
return _context2.stop();
|
143
|
+
}
|
144
|
+
}
|
145
|
+
}, _callee2);
|
146
|
+
}));
|
147
|
+
|
148
|
+
return function findAndParseTypes(_x2) {
|
149
|
+
return _ref3.apply(this, arguments);
|
150
|
+
};
|
151
|
+
}();
|
152
|
+
|
153
|
+
exports.findAndParseTypes = findAndParseTypes;
|
154
|
+
|
155
|
+
var getDefinitionSchema = function getDefinitionSchema(schemas) {
|
156
|
+
var aggregateSchema = {
|
157
|
+
definitions: {//
|
158
|
+
}
|
159
|
+
};
|
160
|
+
schemas.forEach(function (schema) {
|
161
|
+
schema.definitions = schema.definitions || {};
|
162
|
+
aggregateSchema.definitions = _objectSpread(_objectSpread({}, aggregateSchema.definitions), schema.definitions);
|
163
|
+
});
|
164
|
+
return aggregateSchema;
|
165
|
+
};
|
166
|
+
|
167
|
+
exports.getDefinitionSchema = getDefinitionSchema;
|
package/module/cli.js
CHANGED
package/module/cmds.js
CHANGED
@@ -1,10 +1,8 @@
|
|
1
|
-
import _boilerplate from './commands/boilerplate';
|
2
1
|
import _from_partial from './commands/from-partial';
|
3
2
|
import _generate from './commands/generate';
|
4
3
|
import _react_query from './commands/react-query';
|
5
4
|
import _recoil from './commands/recoil';
|
6
5
|
const Commands = {};
|
7
|
-
Commands['boilerplate'] = _boilerplate;
|
8
6
|
Commands['from-partial'] = _from_partial;
|
9
7
|
Commands['generate'] = _generate;
|
10
8
|
Commands['react-query'] = _react_query;
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { prompt } from '../prompt';
|
2
|
-
import fromPartial from '../from-partial';
|
1
|
+
import { prompt } from '../utils/prompt';
|
2
|
+
import fromPartial from '../generators/from-partial';
|
3
3
|
import { readSchemas } from '../utils';
|
4
4
|
export default (async argv => {
|
5
5
|
const questions = [{
|
@@ -25,7 +25,7 @@ export default (async argv => {
|
|
25
25
|
out,
|
26
26
|
name
|
27
27
|
} = await prompt(questions, argv);
|
28
|
-
const schemas = readSchemas({
|
28
|
+
const schemas = await readSchemas({
|
29
29
|
schemaDir: schema,
|
30
30
|
argv
|
31
31
|
});
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { prompt } from '../prompt';
|
2
|
-
import
|
1
|
+
import { prompt } from '../utils/prompt';
|
2
|
+
import tsClient from '../generators/ts-client';
|
3
3
|
import { readSchemas } from '../utils';
|
4
4
|
export default (async argv => {
|
5
5
|
const questions = [{
|
@@ -25,9 +25,9 @@ export default (async argv => {
|
|
25
25
|
out,
|
26
26
|
name
|
27
27
|
} = await prompt(questions, argv);
|
28
|
-
const schemas = readSchemas({
|
28
|
+
const schemas = await readSchemas({
|
29
29
|
schemaDir: schema,
|
30
30
|
argv
|
31
31
|
});
|
32
|
-
await
|
32
|
+
await tsClient(name, schemas, out);
|
33
33
|
});
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
2
2
|
const _excluded = ["schema", "out", "name"];
|
3
|
-
import { prompt } from '../prompt';
|
4
|
-
import reactQuery from '../react-query';
|
3
|
+
import { prompt } from '../utils/prompt';
|
4
|
+
import reactQuery from '../generators/react-query';
|
5
5
|
import { readSchemas } from '../utils';
|
6
6
|
export default (async argv => {
|
7
7
|
const questions = [{
|
@@ -31,6 +31,11 @@ export default (async argv => {
|
|
31
31
|
name: 'v4',
|
32
32
|
message: 'Use react-query v4?',
|
33
33
|
default: false
|
34
|
+
}, {
|
35
|
+
type: 'confirm',
|
36
|
+
name: 'mutations',
|
37
|
+
message: 'Geneate useMutation hooks? Must be used with v4.',
|
38
|
+
default: false
|
34
39
|
}];
|
35
40
|
|
36
41
|
const _await$prompt = await prompt(questions, argv),
|
@@ -41,7 +46,7 @@ export default (async argv => {
|
|
41
46
|
} = _await$prompt,
|
42
47
|
options = _objectWithoutProperties(_await$prompt, _excluded);
|
43
48
|
|
44
|
-
const schemas = readSchemas({
|
49
|
+
const schemas = await readSchemas({
|
45
50
|
schemaDir: schema,
|
46
51
|
argv
|
47
52
|
});
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { prompt } from '../prompt';
|
1
|
+
import { prompt } from '../utils/prompt';
|
2
2
|
import recoil from '../recoil';
|
3
3
|
import { readSchemas } from '../utils';
|
4
4
|
export default (async argv => {
|
@@ -25,7 +25,7 @@ export default (async argv => {
|
|
25
25
|
out,
|
26
26
|
name
|
27
27
|
} = await prompt(questions, argv);
|
28
|
-
const schemas = readSchemas({
|
28
|
+
const schemas = await readSchemas({
|
29
29
|
schemaDir: schema,
|
30
30
|
argv
|
31
31
|
});
|
package/module/file.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { pascal } from "case";
|
2
|
-
import { header } from '
|
2
|
+
import { header } from '../utils/header';
|
3
3
|
import { join } from "path";
|
4
4
|
import { sync as mkdirp } from "mkdirp";
|
5
5
|
import * as w from 'wasm-ast-types';
|
@@ -7,7 +7,8 @@ import * as t from '@babel/types';
|
|
7
7
|
import { writeFileSync } from 'fs';
|
8
8
|
import generate from "@babel/generator";
|
9
9
|
import { getMessageProperties } from "wasm-ast-types";
|
10
|
-
import { findAndParseTypes, findExecuteMsg } from "
|
10
|
+
import { findAndParseTypes, findExecuteMsg, getDefinitionSchema } from "../utils";
|
11
|
+
import { RenderContext } from "wasm-ast-types";
|
11
12
|
export default (async (name, schemas, outPath) => {
|
12
13
|
const FromPartialFile = pascal(`${name}Contract`) + '.from-partial.ts';
|
13
14
|
const Contract = pascal(`${name}Contract`) + '.ts';
|
@@ -22,7 +23,8 @@ export default (async (name, schemas, outPath) => {
|
|
22
23
|
body.push(w.importStmt(['Coin'], '@cosmjs/amino'));
|
23
24
|
}
|
24
25
|
|
25
|
-
body.push(w.importStmt(Object.keys(typeHash), `./${Contract}`.replace(/\.ts$/, '')));
|
26
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${Contract}`.replace(/\.ts$/, '')));
|
27
|
+
const context = new RenderContext(getDefinitionSchema(schemas)); // execute messages
|
26
28
|
|
27
29
|
if (ExecuteMsg) {
|
28
30
|
const children = getMessageProperties(ExecuteMsg);
|
@@ -30,8 +32,8 @@ export default (async (name, schemas, outPath) => {
|
|
30
32
|
if (children.length > 0) {
|
31
33
|
const TheClass = pascal(`${name}MessageComposer`);
|
32
34
|
const Interface = pascal(`${name}Message`);
|
33
|
-
body.push(w.createFromPartialInterface(Interface, ExecuteMsg));
|
34
|
-
body.push(w.createFromPartialClass(TheClass, Interface, ExecuteMsg));
|
35
|
+
body.push(w.createFromPartialInterface(context, Interface, ExecuteMsg));
|
36
|
+
body.push(w.createFromPartialClass(context, TheClass, Interface, ExecuteMsg));
|
35
37
|
}
|
36
38
|
}
|
37
39
|
|
@@ -0,0 +1,67 @@
|
|
1
|
+
import { pascal } from "case";
|
2
|
+
import { header } from '../utils/header';
|
3
|
+
import { join } from "path";
|
4
|
+
import { sync as mkdirp } from "mkdirp";
|
5
|
+
import * as w from 'wasm-ast-types';
|
6
|
+
import { RenderContext } from 'wasm-ast-types';
|
7
|
+
import * as t from '@babel/types';
|
8
|
+
import { writeFileSync } from 'fs';
|
9
|
+
import generate from "@babel/generator";
|
10
|
+
import { findAndParseTypes, findExecuteMsg, findQueryMsg, getDefinitionSchema } from '../utils';
|
11
|
+
import { getMessageProperties } from "wasm-ast-types";
|
12
|
+
import { cosmjsAminoImportStatements } from '../utils/imports';
|
13
|
+
export default (async (contractName, schemas, outPath, reactQueryOptions) => {
|
14
|
+
const context = new RenderContext(getDefinitionSchema(schemas), {
|
15
|
+
reactQuery: reactQueryOptions ?? {}
|
16
|
+
});
|
17
|
+
const options = context.options.reactQuery;
|
18
|
+
const ReactQueryFile = pascal(`${contractName}Contract`) + '.react-query.ts';
|
19
|
+
const Contract = pascal(`${contractName}Contract`);
|
20
|
+
const QueryMsg = findQueryMsg(schemas);
|
21
|
+
const ExecuteMsg = findExecuteMsg(schemas);
|
22
|
+
const typeHash = await findAndParseTypes(schemas);
|
23
|
+
const ExecuteClient = pascal(`${contractName}Client`);
|
24
|
+
const QueryClient = pascal(`${contractName}QueryClient`);
|
25
|
+
const body = [];
|
26
|
+
const reactQueryImports = ['useQuery', 'UseQueryOptions'];
|
27
|
+
const clientImports = [];
|
28
|
+
QueryMsg && clientImports.push(QueryClient); // check that there are commands within the exec msg
|
29
|
+
|
30
|
+
const shouldGenerateMutationHooks = ExecuteMsg && options?.v4 && options?.mutations && getMessageProperties(ExecuteMsg).length > 0;
|
31
|
+
|
32
|
+
if (shouldGenerateMutationHooks) {
|
33
|
+
body.push(w.importStmt(['ExecuteResult'], '@cosmjs/cosmwasm-stargate'));
|
34
|
+
body.push(cosmjsAminoImportStatements(typeHash));
|
35
|
+
reactQueryImports.push('useMutation', 'UseMutationOptions');
|
36
|
+
clientImports.push(ExecuteClient);
|
37
|
+
} // react-query imports
|
38
|
+
|
39
|
+
|
40
|
+
body.push(w.importStmt(reactQueryImports, options?.v4 ? '@tanstack/react-query' : 'react-query')); // general contract imports
|
41
|
+
|
42
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${Contract}`)); // client imports
|
43
|
+
|
44
|
+
body.push(w.importStmt(clientImports, `./${Contract}`)); // query messages
|
45
|
+
|
46
|
+
if (QueryMsg) {
|
47
|
+
[].push.apply(body, w.createReactQueryHooks({
|
48
|
+
context,
|
49
|
+
queryMsg: QueryMsg,
|
50
|
+
contractName: contractName,
|
51
|
+
QueryClient
|
52
|
+
}));
|
53
|
+
}
|
54
|
+
|
55
|
+
if (shouldGenerateMutationHooks) {
|
56
|
+
[].push.apply(body, w.createReactQueryMutationHooks({
|
57
|
+
context,
|
58
|
+
execMsg: ExecuteMsg,
|
59
|
+
contractName: contractName,
|
60
|
+
ExecuteClient
|
61
|
+
}));
|
62
|
+
}
|
63
|
+
|
64
|
+
const code = header + generate(t.program(body)).code;
|
65
|
+
mkdirp(outPath);
|
66
|
+
writeFileSync(join(outPath, ReactQueryFile), code);
|
67
|
+
});
|
@@ -1,12 +1,13 @@
|
|
1
1
|
import { pascal } from "case";
|
2
|
-
import { header } from '
|
2
|
+
import { header } from '../utils/header';
|
3
3
|
import { join } from "path";
|
4
4
|
import { sync as mkdirp } from "mkdirp";
|
5
5
|
import * as w from 'wasm-ast-types';
|
6
6
|
import * as t from '@babel/types';
|
7
7
|
import { writeFileSync } from 'fs';
|
8
8
|
import generate from "@babel/generator";
|
9
|
-
import { findAndParseTypes, findQueryMsg } from "
|
9
|
+
import { findAndParseTypes, findQueryMsg, getDefinitionSchema } from "../utils";
|
10
|
+
import { RenderContext } from "wasm-ast-types";
|
10
11
|
export default (async (name, schemas, outPath) => {
|
11
12
|
const RecoilFile = pascal(`${name}Contract`) + '.recoil.ts';
|
12
13
|
const Contract = pascal(`${name}Contract`) + '.ts';
|
@@ -17,15 +18,16 @@ export default (async (name, schemas, outPath) => {
|
|
17
18
|
const body = [];
|
18
19
|
body.push(w.importStmt(['selectorFamily'], 'recoil'));
|
19
20
|
body.push(w.importStmt(['cosmWasmClient'], './chain'));
|
20
|
-
body.push(w.importStmt(Object.keys(typeHash), `./${Contract}`.replace(/\.ts$/, '')));
|
21
|
+
body.push(w.importStmt(Object.keys(typeHash), `./${Contract}`.replace(/\.ts$/, '')));
|
22
|
+
const context = new RenderContext(getDefinitionSchema(schemas)); // query messages
|
21
23
|
|
22
24
|
if (QueryMsg) {
|
23
25
|
QueryClient = pascal(`${name}QueryClient`);
|
24
26
|
ReadOnlyInstance = pascal(`${name}ReadOnlyInterface`);
|
25
27
|
body.push(w.importStmt([QueryClient], `./${Contract}`));
|
26
28
|
body.push(w.createRecoilQueryClientType());
|
27
|
-
body.push(w.createRecoilQueryClient(name, QueryClient));
|
28
|
-
[].push.apply(body, w.createRecoilSelectors(name, QueryClient, QueryMsg));
|
29
|
+
body.push(w.createRecoilQueryClient(context, name, QueryClient));
|
30
|
+
[].push.apply(body, w.createRecoilSelectors(context, name, QueryClient, QueryMsg));
|
29
31
|
}
|
30
32
|
|
31
33
|
const code = header + generate(t.program(body)).code;
|
@@ -1,14 +1,16 @@
|
|
1
1
|
import { pascal } from "case";
|
2
|
-
import { header } from '
|
2
|
+
import { header } from '../utils/header';
|
3
3
|
import { join } from "path";
|
4
4
|
import { sync as mkdirp } from "mkdirp";
|
5
5
|
import * as w from 'wasm-ast-types';
|
6
6
|
import * as t from '@babel/types';
|
7
7
|
import { writeFileSync } from 'fs';
|
8
8
|
import generate from "@babel/generator";
|
9
|
-
import { clean } from "
|
9
|
+
import { clean } from "../utils/clean";
|
10
10
|
import { getMessageProperties } from "wasm-ast-types";
|
11
|
-
import { findAndParseTypes, findExecuteMsg, findQueryMsg } from
|
11
|
+
import { findAndParseTypes, findExecuteMsg, findQueryMsg, getDefinitionSchema } from '../utils';
|
12
|
+
import { cosmjsAminoImportStatements } from '../utils/imports';
|
13
|
+
import { RenderContext } from "wasm-ast-types";
|
12
14
|
export default (async (name, schemas, outPath) => {
|
13
15
|
const Contract = pascal(`${name}Contract`) + '.ts';
|
14
16
|
const QueryMsg = findQueryMsg(schemas);
|
@@ -20,23 +22,20 @@ export default (async (name, schemas, outPath) => {
|
|
20
22
|
let ReadOnlyInstance = null;
|
21
23
|
const body = [];
|
22
24
|
body.push(w.importStmt(['CosmWasmClient', 'ExecuteResult', 'SigningCosmWasmClient'], '@cosmjs/cosmwasm-stargate'));
|
23
|
-
|
24
|
-
if (typeHash.hasOwnProperty('Coin')) {
|
25
|
-
body.push(w.importStmt(['StdFee'], '@cosmjs/amino'));
|
26
|
-
} else {
|
27
|
-
body.push(w.importStmt(['Coin', 'StdFee'], '@cosmjs/amino'));
|
28
|
-
} // TYPES
|
29
|
-
|
25
|
+
body.push(cosmjsAminoImportStatements(typeHash)); // TYPES
|
30
26
|
|
31
27
|
Object.values(typeHash).forEach(type => {
|
32
28
|
body.push(clean(type));
|
33
|
-
}); //
|
29
|
+
}); // alias the ExecuteMsg
|
30
|
+
|
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
|
34
33
|
|
35
34
|
if (QueryMsg) {
|
36
35
|
QueryClient = pascal(`${name}QueryClient`);
|
37
36
|
ReadOnlyInstance = pascal(`${name}ReadOnlyInterface`);
|
38
|
-
body.push(w.createQueryInterface(ReadOnlyInstance, QueryMsg));
|
39
|
-
body.push(w.createQueryClass(QueryClient, ReadOnlyInstance, QueryMsg));
|
37
|
+
body.push(w.createQueryInterface(context, ReadOnlyInstance, QueryMsg));
|
38
|
+
body.push(w.createQueryClass(context, QueryClient, ReadOnlyInstance, QueryMsg));
|
40
39
|
} // execute messages
|
41
40
|
|
42
41
|
|
@@ -46,8 +45,8 @@ export default (async (name, schemas, outPath) => {
|
|
46
45
|
if (children.length > 0) {
|
47
46
|
Client = pascal(`${name}Client`);
|
48
47
|
Instance = pascal(`${name}Interface`);
|
49
|
-
body.push(w.createExecuteInterface(Instance, ReadOnlyInstance, ExecuteMsg));
|
50
|
-
body.push(w.createExecuteClass(Client, Instance, QueryClient, ExecuteMsg));
|
48
|
+
body.push(w.createExecuteInterface(context, Instance, ReadOnlyInstance, ExecuteMsg));
|
49
|
+
body.push(w.createExecuteClass(context, Client, Instance, QueryClient, ExecuteMsg));
|
51
50
|
}
|
52
51
|
}
|
53
52
|
|
package/module/index.js
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
export { default as
|
2
|
-
export { default as fromPartial } from './from-partial';
|
3
|
-
export { default as reactQuery } from './react-query';
|
4
|
-
export { default as recoil } from './recoil';
|
5
|
-
export * from './utils';
|
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
|
+
export * from './utils';
|
6
|
+
export * from './utils/imports';
|
File without changes
|
File without changes
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import * as w from 'wasm-ast-types';
|
2
|
+
export const cosmjsAminoImportStatements = typeHash => {
|
3
|
+
const cosmjsImports = ['StdFee']; // If the coin is not provided by the generation already, import it from cosmjs
|
4
|
+
|
5
|
+
if (!typeHash.hasOwnProperty('Coin')) {
|
6
|
+
cosmjsImports.unshift('Coin');
|
7
|
+
}
|
8
|
+
|
9
|
+
return w.importStmt(cosmjsImports, '@cosmjs/amino');
|
10
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './schemas';
|
File without changes
|
File without changes
|
@@ -1,9 +1,15 @@
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
2
|
+
|
3
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
4
|
+
|
5
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
6
|
+
|
1
7
|
import { sync as glob } from 'glob';
|
2
8
|
import { readFileSync } from 'fs';
|
3
9
|
import { cleanse } from './cleanse';
|
4
10
|
import { compile } from 'json-schema-to-typescript';
|
5
|
-
import { parser } from
|
6
|
-
export const readSchemas = ({
|
11
|
+
import { parser } from './parse';
|
12
|
+
export const readSchemas = async ({
|
7
13
|
schemaDir,
|
8
14
|
argv,
|
9
15
|
clean = true
|
@@ -49,4 +55,15 @@ export const findAndParseTypes = async schemas => {
|
|
49
55
|
|
50
56
|
const typeHash = parser(allTypes);
|
51
57
|
return typeHash;
|
58
|
+
};
|
59
|
+
export const getDefinitionSchema = schemas => {
|
60
|
+
const aggregateSchema = {
|
61
|
+
definitions: {//
|
62
|
+
}
|
63
|
+
};
|
64
|
+
schemas.forEach(schema => {
|
65
|
+
schema.definitions = schema.definitions || {};
|
66
|
+
aggregateSchema.definitions = _objectSpread(_objectSpread({}, aggregateSchema.definitions), schema.definitions);
|
67
|
+
});
|
68
|
+
return aggregateSchema;
|
52
69
|
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@cosmwasm/ts-codegen",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.7.0",
|
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",
|
@@ -47,50 +47,50 @@
|
|
47
47
|
"url": "https://github.com/cosmwasm/ts-codegen/issues"
|
48
48
|
},
|
49
49
|
"devDependencies": {
|
50
|
-
"@babel/cli": "7.
|
51
|
-
"@babel/eslint-parser": "^7.18.
|
52
|
-
"@babel/node": "^7.18.
|
53
|
-
"@types/jest": "^28.1.
|
50
|
+
"@babel/cli": "7.18.10",
|
51
|
+
"@babel/eslint-parser": "^7.18.9",
|
52
|
+
"@babel/node": "^7.18.10",
|
53
|
+
"@types/jest": "^28.1.6",
|
54
54
|
"ast-stringify": "0.1.0",
|
55
55
|
"babel-core": "7.0.0-bridge.0",
|
56
|
-
"babel-jest": "28.1.
|
56
|
+
"babel-jest": "28.1.3",
|
57
57
|
"babel-watch": "^7.0.0",
|
58
58
|
"cosmwasm": "1.1.1",
|
59
59
|
"cross-env": "^7.0.2",
|
60
|
-
"eslint": "8.
|
60
|
+
"eslint": "8.21.0",
|
61
61
|
"eslint-config-prettier": "^8.5.0",
|
62
|
-
"eslint-plugin-prettier": "^4.
|
63
|
-
"jest": "^28.1.
|
62
|
+
"eslint-plugin-prettier": "^4.2.1",
|
63
|
+
"jest": "^28.1.3",
|
64
64
|
"jest-in-case": "^1.0.2",
|
65
65
|
"prettier": "^2.7.1",
|
66
66
|
"regenerator-runtime": "^0.13.7",
|
67
|
-
"ts-jest": "^28.0.
|
67
|
+
"ts-jest": "^28.0.7",
|
68
68
|
"typescript": "^4.7.4"
|
69
69
|
},
|
70
70
|
"dependencies": {
|
71
|
-
"@babel/core": "7.18.
|
72
|
-
"@babel/generator": "7.18.
|
73
|
-
"@babel/parser": "7.18.
|
74
|
-
"@babel/plugin-proposal-class-properties": "7.
|
75
|
-
"@babel/plugin-proposal-export-default-from": "7.
|
76
|
-
"@babel/plugin-proposal-object-rest-spread": "7.18.
|
77
|
-
"@babel/plugin-transform-runtime": "7.18.
|
78
|
-
"@babel/preset-env": "7.18.
|
79
|
-
"@babel/preset-typescript": "^7.
|
80
|
-
"@babel/runtime": "^7.18.
|
81
|
-
"@babel/traverse": "7.18.
|
82
|
-
"@babel/types": "7.18.
|
71
|
+
"@babel/core": "7.18.10",
|
72
|
+
"@babel/generator": "7.18.12",
|
73
|
+
"@babel/parser": "7.18.11",
|
74
|
+
"@babel/plugin-proposal-class-properties": "7.18.6",
|
75
|
+
"@babel/plugin-proposal-export-default-from": "7.18.10",
|
76
|
+
"@babel/plugin-proposal-object-rest-spread": "7.18.9",
|
77
|
+
"@babel/plugin-transform-runtime": "7.18.10",
|
78
|
+
"@babel/preset-env": "7.18.10",
|
79
|
+
"@babel/preset-typescript": "^7.18.6",
|
80
|
+
"@babel/runtime": "^7.18.9",
|
81
|
+
"@babel/traverse": "7.18.11",
|
82
|
+
"@babel/types": "7.18.10",
|
83
83
|
"case": "1.6.3",
|
84
84
|
"dargs": "7.0.0",
|
85
85
|
"fuzzy": "0.1.3",
|
86
86
|
"glob": "8.0.3",
|
87
87
|
"inquirerer": "0.1.3",
|
88
|
-
"json-schema-to-typescript": "
|
88
|
+
"json-schema-to-typescript": "11.0.1",
|
89
89
|
"long": "^5.2.0",
|
90
90
|
"minimist": "1.2.6",
|
91
91
|
"mkdirp": "1.0.4",
|
92
92
|
"shelljs": "0.8.5",
|
93
|
-
"wasm-ast-types": "^0.
|
93
|
+
"wasm-ast-types": "^0.6.0"
|
94
94
|
},
|
95
|
-
"gitHead": "
|
95
|
+
"gitHead": "51e6e530adc95d8118606379848080dd9bc3c25a"
|
96
96
|
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const cleanse: (obj: any) => any;
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const cosmjsAminoImportStatements: (typeHash: any) => any;
|