@dedot/codegen 0.0.1-next.f5bf4fc2.1 → 0.0.1-next.fecbe32d.13
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/cjs/generator/ConstsGen.js +9 -10
- package/cjs/generator/ErrorsGen.js +12 -13
- package/cjs/generator/EventsGen.js +13 -14
- package/cjs/generator/IndexGen.js +8 -9
- package/cjs/generator/JsonRpcGen.js +59 -0
- package/cjs/generator/QueryGen.js +23 -15
- package/cjs/generator/RuntimeApisGen.js +102 -28
- package/cjs/generator/TxGen.js +15 -15
- package/cjs/generator/TypeImports.js +8 -0
- package/cjs/generator/TypesGen.js +20 -27
- package/cjs/generator/index.js +10 -10
- package/cjs/generator/known-codecs.js +1 -1
- package/cjs/generator/utils.js +30 -1
- package/cjs/index.js +32 -32
- package/cjs/templates/index.hbs +2 -2
- package/cjs/templates/json-rpc.hbs +5 -0
- package/generator/ApiGen.d.ts +4 -4
- package/generator/ConstsGen.d.ts +1 -1
- package/generator/ConstsGen.js +4 -5
- package/generator/ErrorsGen.d.ts +1 -1
- package/generator/ErrorsGen.js +3 -4
- package/generator/EventsGen.d.ts +1 -1
- package/generator/EventsGen.js +3 -4
- package/generator/IndexGen.d.ts +2 -3
- package/generator/IndexGen.js +6 -7
- package/generator/JsonRpcGen.d.ts +7 -0
- package/generator/JsonRpcGen.js +55 -0
- package/generator/QueryGen.d.ts +1 -1
- package/generator/QueryGen.js +19 -11
- package/generator/RuntimeApisGen.d.ts +5 -5
- package/generator/RuntimeApisGen.js +89 -15
- package/generator/TxGen.d.ts +1 -1
- package/generator/TxGen.js +3 -3
- package/generator/TypeImports.d.ts +2 -0
- package/generator/TypeImports.js +8 -0
- package/generator/TypesGen.d.ts +2 -2
- package/generator/TypesGen.js +5 -12
- package/generator/index.d.ts +10 -10
- package/generator/index.js +10 -10
- package/generator/known-codecs.js +1 -1
- package/generator/utils.d.ts +5 -0
- package/generator/utils.js +28 -0
- package/index.d.ts +2 -3
- package/index.js +23 -23
- package/package.json +7 -8
- package/templates/index.hbs +2 -2
- package/templates/json-rpc.hbs +5 -0
- package/cjs/genSupportedChainTypes.js +0 -83
- package/cjs/generator/RpcGen.js +0 -176
- package/cjs/generator/known-types.js +0 -33
- package/cjs/packageInfo.js +0 -5
- package/cjs/templates/rpc.hbs +0 -7
- package/cjs/types.js +0 -2
- package/genSupportedChainTypes.d.ts +0 -1
- package/genSupportedChainTypes.js +0 -78
- package/generator/RpcGen.d.ts +0 -10
- package/generator/RpcGen.js +0 -172
- package/generator/known-types.d.ts +0 -6
- package/generator/known-types.js +0 -30
- package/packageInfo.d.ts +0 -4
- package/packageInfo.js +0 -2
- package/templates/rpc.hbs +0 -7
- package/types.d.ts +0 -6
- package/types.js +0 -1
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TypesGen = exports.BASIC_KNOWN_TYPES = void 0;
|
|
4
|
-
const util_1 = require("@polkadot/util");
|
|
5
4
|
const codecs_1 = require("@dedot/codecs");
|
|
6
5
|
const utils_1 = require("@dedot/utils");
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const known_codecs_1 = require("./known-codecs");
|
|
6
|
+
const TypeImports_js_1 = require("./TypeImports.js");
|
|
7
|
+
const known_codecs_js_1 = require("./known-codecs.js");
|
|
8
|
+
const utils_js_1 = require("./utils.js");
|
|
11
9
|
// Skip generate types for these
|
|
12
10
|
// as we do have native types for them
|
|
13
11
|
const SKIP_TYPES = [
|
|
@@ -43,7 +41,7 @@ class TypesGen {
|
|
|
43
41
|
this.metadata = metadata;
|
|
44
42
|
this.registry = new codecs_1.PortableRegistry(this.metadata);
|
|
45
43
|
this.includedTypes = this.#includedTypes();
|
|
46
|
-
this.typeImports = new
|
|
44
|
+
this.typeImports = new TypeImports_js_1.TypeImports();
|
|
47
45
|
}
|
|
48
46
|
generate() {
|
|
49
47
|
this.clearCache();
|
|
@@ -51,14 +49,14 @@ class TypesGen {
|
|
|
51
49
|
Object.values(this.includedTypes)
|
|
52
50
|
.filter(({ skip, knownType }) => !(skip || knownType))
|
|
53
51
|
.forEach(({ name, nameOut, id, docs }) => {
|
|
54
|
-
defTypeOut += `${(0,
|
|
52
|
+
defTypeOut += `${(0, utils_js_1.commentBlock)(docs)}export type ${nameOut} = ${this.generateType(id, 0, true)};\n\n`;
|
|
55
53
|
if (this.#shouldGenerateTypeIn(id)) {
|
|
56
54
|
defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
|
|
57
55
|
}
|
|
58
56
|
});
|
|
59
57
|
const importTypes = this.typeImports.toImports('./types');
|
|
60
|
-
const template = (0,
|
|
61
|
-
return (0,
|
|
58
|
+
const template = (0, utils_js_1.compileTemplate)('types.hbs');
|
|
59
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
62
60
|
}
|
|
63
61
|
typeCache = {};
|
|
64
62
|
clearCache() {
|
|
@@ -103,7 +101,7 @@ class TypesGen {
|
|
|
103
101
|
const { tag, value } = type;
|
|
104
102
|
switch (tag) {
|
|
105
103
|
case 'Primitive':
|
|
106
|
-
const $codec = (0,
|
|
104
|
+
const $codec = (0, known_codecs_js_1.findKnownCodec)(value.kind);
|
|
107
105
|
if ($codec.nativeType) {
|
|
108
106
|
return $codec.nativeType;
|
|
109
107
|
}
|
|
@@ -151,12 +149,12 @@ class TypesGen {
|
|
|
151
149
|
return 'null';
|
|
152
150
|
}
|
|
153
151
|
else if (members.every((x) => x.fields.length === 0)) {
|
|
154
|
-
return members.map(({ name, docs }) => `${(0,
|
|
152
|
+
return members.map(({ name, docs }) => `${(0, utils_js_1.commentBlock)(docs)}'${(0, utils_1.stringPascalCase)(name)}'`).join(' | ');
|
|
155
153
|
}
|
|
156
154
|
else {
|
|
157
155
|
const membersType = [];
|
|
158
156
|
for (const { fields, name, docs } of members) {
|
|
159
|
-
const keyName = (0,
|
|
157
|
+
const keyName = (0, utils_1.stringPascalCase)(name);
|
|
160
158
|
if (fields.length === 0) {
|
|
161
159
|
membersType.push([keyName, null, docs]);
|
|
162
160
|
}
|
|
@@ -164,7 +162,7 @@ class TypesGen {
|
|
|
164
162
|
const valueType = fields.length === 1
|
|
165
163
|
? this.generateType(fields[0].typeId, nestedLevel + 1, typeOut)
|
|
166
164
|
: `[${fields
|
|
167
|
-
.map(({ typeId, docs }) => `${(0,
|
|
165
|
+
.map(({ typeId, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${this.generateType(typeId, nestedLevel + 1, typeOut)}`)
|
|
168
166
|
.join(', ')}]`;
|
|
169
167
|
membersType.push([keyName, valueType, docs]);
|
|
170
168
|
}
|
|
@@ -179,7 +177,7 @@ class TypesGen {
|
|
|
179
177
|
value: valueType ? `, ${valueKey}${this.#isOptionalType(valueType) ? '?' : ''}: ${valueType} ` : '',
|
|
180
178
|
docs,
|
|
181
179
|
}))
|
|
182
|
-
.map(({ tag, value, docs }) => `${(0,
|
|
180
|
+
.map(({ tag, value, docs }) => `${(0, utils_js_1.commentBlock)(docs)}{ ${tag}${value} }`)
|
|
183
181
|
.join(' | ');
|
|
184
182
|
}
|
|
185
183
|
}
|
|
@@ -226,7 +224,7 @@ class TypesGen {
|
|
|
226
224
|
};
|
|
227
225
|
});
|
|
228
226
|
return `{${props
|
|
229
|
-
.map(({ name, type, optional, docs }) => `${(0,
|
|
227
|
+
.map(({ name, type, optional, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${name}${optional ? '?' : ''}: ${type}`)
|
|
230
228
|
.join(',\n')}}`;
|
|
231
229
|
}
|
|
232
230
|
#isOptionalType(type) {
|
|
@@ -267,8 +265,8 @@ class TypesGen {
|
|
|
267
265
|
const suffix = typeSuffixes.get(id) || '';
|
|
268
266
|
let knownType = false;
|
|
269
267
|
let name, nameOut;
|
|
270
|
-
if ((0,
|
|
271
|
-
const codecType = (0,
|
|
268
|
+
if ((0, known_codecs_js_1.isKnownCodecType)(joinedPath)) {
|
|
269
|
+
const codecType = (0, known_codecs_js_1.findKnownCodecType)(path.at(-1));
|
|
272
270
|
name = codecType.typeIn;
|
|
273
271
|
nameOut = codecType.typeOut;
|
|
274
272
|
knownType = true;
|
|
@@ -315,7 +313,7 @@ class TypesGen {
|
|
|
315
313
|
*/
|
|
316
314
|
#cleanPath(path) {
|
|
317
315
|
return path
|
|
318
|
-
.map((one) => (0,
|
|
316
|
+
.map((one) => (0, utils_1.stringPascalCase)(one))
|
|
319
317
|
.filter((one, idx, currentPath) => idx === 0 || one !== currentPath[idx - 1])
|
|
320
318
|
.join('');
|
|
321
319
|
}
|
|
@@ -390,10 +388,10 @@ class TypesGen {
|
|
|
390
388
|
if (diffParam?.typeId) {
|
|
391
389
|
const diffType = types[diffParam.typeId];
|
|
392
390
|
if (diffType.path.length > 0) {
|
|
393
|
-
return (0,
|
|
391
|
+
return (0, utils_1.stringPascalCase)(diffType.path.at(-1));
|
|
394
392
|
}
|
|
395
393
|
else if (diffType.type.tag === 'Primitive') {
|
|
396
|
-
return (0,
|
|
394
|
+
return (0, utils_1.stringPascalCase)(diffType.type.value.kind);
|
|
397
395
|
}
|
|
398
396
|
}
|
|
399
397
|
// Last resort!
|
|
@@ -404,7 +402,7 @@ class TypesGen {
|
|
|
404
402
|
typeName.forEach((one) => this.addTypeImport(one));
|
|
405
403
|
return;
|
|
406
404
|
}
|
|
407
|
-
if ((0,
|
|
405
|
+
if ((0, utils_js_1.isNativeType)(typeName)) {
|
|
408
406
|
return;
|
|
409
407
|
}
|
|
410
408
|
for (let type of Object.values(this.includedTypes)) {
|
|
@@ -426,12 +424,7 @@ class TypesGen {
|
|
|
426
424
|
this.typeImports.addCodecType(typeName);
|
|
427
425
|
return;
|
|
428
426
|
}
|
|
429
|
-
|
|
430
|
-
this.typeImports.addKnownType(typeName);
|
|
431
|
-
}
|
|
432
|
-
else {
|
|
433
|
-
this.typeImports.addOutType(typeName);
|
|
434
|
-
}
|
|
427
|
+
this.typeImports.addOutType(typeName);
|
|
435
428
|
}
|
|
436
429
|
}
|
|
437
430
|
exports.TypesGen = TypesGen;
|
package/cjs/generator/index.js
CHANGED
|
@@ -14,13 +14,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./TypesGen"), exports);
|
|
18
|
-
__exportStar(require("./ApiGen"), exports);
|
|
19
|
-
__exportStar(require("./ConstsGen"), exports);
|
|
20
|
-
__exportStar(require("./QueryGen"), exports);
|
|
21
|
-
__exportStar(require("./
|
|
22
|
-
__exportStar(require("./IndexGen"), exports);
|
|
23
|
-
__exportStar(require("./ErrorsGen"), exports);
|
|
24
|
-
__exportStar(require("./EventsGen"), exports);
|
|
25
|
-
__exportStar(require("./TxGen"), exports);
|
|
26
|
-
__exportStar(require("./RuntimeApisGen"), exports);
|
|
17
|
+
__exportStar(require("./TypesGen.js"), exports);
|
|
18
|
+
__exportStar(require("./ApiGen.js"), exports);
|
|
19
|
+
__exportStar(require("./ConstsGen.js"), exports);
|
|
20
|
+
__exportStar(require("./QueryGen.js"), exports);
|
|
21
|
+
__exportStar(require("./JsonRpcGen.js"), exports);
|
|
22
|
+
__exportStar(require("./IndexGen.js"), exports);
|
|
23
|
+
__exportStar(require("./ErrorsGen.js"), exports);
|
|
24
|
+
__exportStar(require("./EventsGen.js"), exports);
|
|
25
|
+
__exportStar(require("./TxGen.js"), exports);
|
|
26
|
+
__exportStar(require("./RuntimeApisGen.js"), exports);
|
|
@@ -24,9 +24,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.isKnownCodecType = exports.findKnownWrapperCodec = exports.findKnownCodec = exports.findKnownCodecType = exports.looseTypeCodecs = exports.normalizeCodecName = void 0;
|
|
27
|
-
const $ = __importStar(require("@dedot/shape"));
|
|
28
27
|
const Codecs = __importStar(require("@dedot/codecs"));
|
|
29
28
|
const codecs_1 = require("@dedot/codecs");
|
|
29
|
+
const $ = __importStar(require("@dedot/shape"));
|
|
30
30
|
const utils_1 = require("@dedot/utils");
|
|
31
31
|
const normalizeCodecName = (name) => {
|
|
32
32
|
return name.startsWith('$') ? name : `$${name}`;
|
package/cjs/generator/utils.js
CHANGED
|
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.isReservedWord = exports.compileTemplate = exports.beautifySourceCode = exports.commentBlock = exports.TUPLE_TYPE_REGEX = exports.WRAPPER_TYPE_REGEX = void 0;
|
|
29
|
+
exports.isNativeType = exports.isReservedWord = exports.compileTemplate = exports.beautifySourceCode = exports.commentBlock = exports.TUPLE_TYPE_REGEX = exports.WRAPPER_TYPE_REGEX = void 0;
|
|
30
30
|
const fs = __importStar(require("fs"));
|
|
31
31
|
const handlebars_1 = __importDefault(require("handlebars"));
|
|
32
32
|
const path = __importStar(require("path"));
|
|
@@ -66,3 +66,32 @@ const TS_RESERVED_WORDS = ['new', 'class'];
|
|
|
66
66
|
*/
|
|
67
67
|
const isReservedWord = (word) => TS_RESERVED_WORDS.includes(word);
|
|
68
68
|
exports.isReservedWord = isReservedWord;
|
|
69
|
+
const TS_PRIMITIVE_TYPES = [
|
|
70
|
+
'void',
|
|
71
|
+
'undefined',
|
|
72
|
+
'null',
|
|
73
|
+
'number',
|
|
74
|
+
'boolean',
|
|
75
|
+
'bigint',
|
|
76
|
+
'Map',
|
|
77
|
+
'Set',
|
|
78
|
+
'string',
|
|
79
|
+
'any',
|
|
80
|
+
'Array',
|
|
81
|
+
'Record',
|
|
82
|
+
];
|
|
83
|
+
/**
|
|
84
|
+
* Check if a type is native JS/TS type
|
|
85
|
+
* @param type
|
|
86
|
+
*/
|
|
87
|
+
const isNativeType = (type) => {
|
|
88
|
+
return TS_PRIMITIVE_TYPES.some((one) => {
|
|
89
|
+
if (typeof one === 'string') {
|
|
90
|
+
return one === type;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
return type.match(one);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
exports.isNativeType = isNativeType;
|
package/cjs/index.js
CHANGED
|
@@ -23,50 +23,50 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.generateTypes = exports.
|
|
26
|
+
exports.generateTypes = exports.generateTypesFromEndpoint = void 0;
|
|
27
|
+
const utils_1 = require("@dedot/utils");
|
|
27
28
|
const dedot_1 = require("dedot");
|
|
28
29
|
const fs = __importStar(require("fs"));
|
|
29
30
|
const path = __importStar(require("path"));
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
network.chain = (0, util_1.stringCamelCase)(api.runtimeVersion?.specName || api.runtimeChain || 'local');
|
|
31
|
+
const index_js_1 = require("./generator/index.js");
|
|
32
|
+
async function generateTypesFromEndpoint(chain, endpoint, outDir, extension = 'd.ts') {
|
|
33
|
+
const api = await dedot_1.Dedot.new(endpoint);
|
|
34
|
+
const { methods } = await api.rpc.rpc_methods();
|
|
35
|
+
const apis = api.runtimeVersion.apis || {};
|
|
36
|
+
if (!chain) {
|
|
37
|
+
chain = (0, utils_1.stringCamelCase)(api.runtimeVersion.specName || 'local');
|
|
38
38
|
}
|
|
39
|
-
await generateTypes(
|
|
39
|
+
await generateTypes(chain, api.metadata.latest, methods, apis, outDir, extension);
|
|
40
40
|
await api.disconnect();
|
|
41
41
|
}
|
|
42
|
-
exports.
|
|
43
|
-
async function generateTypes(
|
|
44
|
-
const dirPath = path.resolve(outDir,
|
|
45
|
-
const defTypesFileName = path.join(dirPath, `types
|
|
46
|
-
const constsTypesFileName = path.join(dirPath, `consts
|
|
47
|
-
const queryTypesFileName = path.join(dirPath, `query
|
|
48
|
-
const
|
|
49
|
-
const indexFileName = path.join(dirPath, `index
|
|
50
|
-
const errorsFileName = path.join(dirPath, `errors
|
|
51
|
-
const eventsFileName = path.join(dirPath, `events
|
|
52
|
-
const runtimeApisFileName = path.join(dirPath, `runtime
|
|
53
|
-
const txFileName = path.join(dirPath, `tx
|
|
42
|
+
exports.generateTypesFromEndpoint = generateTypesFromEndpoint;
|
|
43
|
+
async function generateTypes(chain, metadata, rpcMethods, runtimeApis, outDir = '.', extension = 'd.ts') {
|
|
44
|
+
const dirPath = path.resolve(outDir, chain);
|
|
45
|
+
const defTypesFileName = path.join(dirPath, `types.${extension}`);
|
|
46
|
+
const constsTypesFileName = path.join(dirPath, `consts.${extension}`);
|
|
47
|
+
const queryTypesFileName = path.join(dirPath, `query.${extension}`);
|
|
48
|
+
const jsonRpcFileName = path.join(dirPath, `json-rpc.${extension}`);
|
|
49
|
+
const indexFileName = path.join(dirPath, `index.${extension}`);
|
|
50
|
+
const errorsFileName = path.join(dirPath, `errors.${extension}`);
|
|
51
|
+
const eventsFileName = path.join(dirPath, `events.${extension}`);
|
|
52
|
+
const runtimeApisFileName = path.join(dirPath, `runtime.${extension}`);
|
|
53
|
+
const txFileName = path.join(dirPath, `tx.${extension}`);
|
|
54
54
|
if (!fs.existsSync(dirPath)) {
|
|
55
55
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
56
56
|
}
|
|
57
|
-
const typesGen = new
|
|
58
|
-
const constsGen = new
|
|
59
|
-
const queryGen = new
|
|
60
|
-
const
|
|
61
|
-
const indexGen = new
|
|
62
|
-
const errorsGen = new
|
|
63
|
-
const eventsGen = new
|
|
64
|
-
const runtimeApisGen = new
|
|
65
|
-
const txGen = new
|
|
57
|
+
const typesGen = new index_js_1.TypesGen(metadata);
|
|
58
|
+
const constsGen = new index_js_1.ConstsGen(typesGen);
|
|
59
|
+
const queryGen = new index_js_1.QueryGen(typesGen);
|
|
60
|
+
const jsonRpcGen = new index_js_1.JsonRpcGen(typesGen, rpcMethods);
|
|
61
|
+
const indexGen = new index_js_1.IndexGen(chain);
|
|
62
|
+
const errorsGen = new index_js_1.ErrorsGen(typesGen);
|
|
63
|
+
const eventsGen = new index_js_1.EventsGen(typesGen);
|
|
64
|
+
const runtimeApisGen = new index_js_1.RuntimeApisGen(typesGen, runtimeApis);
|
|
65
|
+
const txGen = new index_js_1.TxGen(typesGen);
|
|
66
66
|
fs.writeFileSync(defTypesFileName, await typesGen.generate());
|
|
67
67
|
fs.writeFileSync(errorsFileName, await errorsGen.generate());
|
|
68
68
|
fs.writeFileSync(eventsFileName, await eventsGen.generate());
|
|
69
|
-
fs.writeFileSync(
|
|
69
|
+
fs.writeFileSync(jsonRpcFileName, await jsonRpcGen.generate());
|
|
70
70
|
fs.writeFileSync(queryTypesFileName, await queryGen.generate());
|
|
71
71
|
fs.writeFileSync(constsTypesFileName, await constsGen.generate());
|
|
72
72
|
fs.writeFileSync(txFileName, await txGen.generate());
|
package/cjs/templates/index.hbs
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { GenericSubstrateApi } from '@dedot/types';
|
|
4
4
|
import { ChainConsts } from './consts';
|
|
5
5
|
import { ChainStorage } from './query';
|
|
6
|
-
import {
|
|
6
|
+
import { ChainJsonRpcApis } from './json-rpc';
|
|
7
7
|
import { ChainErrors } from './errors';
|
|
8
8
|
import { ChainEvents } from './events';
|
|
9
9
|
import { RuntimeApis } from './runtime';
|
|
@@ -12,7 +12,7 @@ import { ChainTx } from './tx';
|
|
|
12
12
|
export * from './types';
|
|
13
13
|
|
|
14
14
|
export interface {{{interfaceName}}}Api extends GenericSubstrateApi {
|
|
15
|
-
rpc:
|
|
15
|
+
rpc: ChainJsonRpcApis;
|
|
16
16
|
consts: ChainConsts;
|
|
17
17
|
query: ChainStorage;
|
|
18
18
|
errors: ChainErrors;
|
package/generator/ApiGen.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TypesGen } from '../generator';
|
|
1
|
+
import { TypesGen } from '../generator/index.js';
|
|
2
2
|
export declare abstract class ApiGen {
|
|
3
3
|
readonly typesGen: TypesGen;
|
|
4
4
|
constructor(typesGen: TypesGen);
|
|
@@ -13,14 +13,14 @@ export declare abstract class ApiGen {
|
|
|
13
13
|
type: {
|
|
14
14
|
tag: "Struct";
|
|
15
15
|
value: {
|
|
16
|
-
fields: import("
|
|
16
|
+
fields: import("dedot").Field[];
|
|
17
17
|
};
|
|
18
18
|
} | {
|
|
19
19
|
tag: "Enum";
|
|
20
20
|
value: {
|
|
21
21
|
members: {
|
|
22
22
|
name: string;
|
|
23
|
-
fields: import("
|
|
23
|
+
fields: import("dedot").Field[];
|
|
24
24
|
index: number;
|
|
25
25
|
docs: string[];
|
|
26
26
|
}[];
|
|
@@ -134,5 +134,5 @@ export declare abstract class ApiGen {
|
|
|
134
134
|
}>;
|
|
135
135
|
};
|
|
136
136
|
};
|
|
137
|
-
get registry(): import("
|
|
137
|
+
get registry(): import("dedot").PortableRegistry;
|
|
138
138
|
}
|
package/generator/ConstsGen.d.ts
CHANGED
package/generator/ConstsGen.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
|
|
1
|
+
import { normalizeName, stringCamelCase } from '@dedot/utils';
|
|
2
|
+
import { ApiGen } from './ApiGen.js';
|
|
3
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
5
4
|
export class ConstsGen extends ApiGen {
|
|
6
5
|
generate() {
|
|
7
6
|
const { pallets } = this.metadata;
|
|
@@ -15,7 +14,7 @@ export class ConstsGen extends ApiGen {
|
|
|
15
14
|
docs: one.docs,
|
|
16
15
|
}));
|
|
17
16
|
defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s constants`);
|
|
18
|
-
defTypeOut += `${
|
|
17
|
+
defTypeOut += `${stringCamelCase(pallet.name)}: {
|
|
19
18
|
${typedConstants.map(({ name, type, docs }) => `${commentBlock(docs)}${name}: ${type}`).join(',\n')}
|
|
20
19
|
|
|
21
20
|
${commentBlock('Generic pallet constant')}[name: string]: any,
|
package/generator/ErrorsGen.d.ts
CHANGED
package/generator/ErrorsGen.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { assert } from '@dedot/utils';
|
|
1
|
+
import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import { ApiGen } from './ApiGen.js';
|
|
3
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
5
4
|
export class ErrorsGen extends ApiGen {
|
|
6
5
|
generate() {
|
|
7
6
|
const { pallets } = this.metadata;
|
package/generator/EventsGen.d.ts
CHANGED
package/generator/EventsGen.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { assert } from '@dedot/utils';
|
|
1
|
+
import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import { ApiGen } from './ApiGen.js';
|
|
3
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
5
4
|
export class EventsGen extends ApiGen {
|
|
6
5
|
generate() {
|
|
7
6
|
const { pallets } = this.metadata;
|
package/generator/IndexGen.d.ts
CHANGED
package/generator/IndexGen.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import { beautifySourceCode, compileTemplate } from './utils.js';
|
|
3
3
|
export class IndexGen {
|
|
4
|
-
|
|
5
|
-
constructor(
|
|
6
|
-
this.
|
|
4
|
+
chain;
|
|
5
|
+
constructor(chain) {
|
|
6
|
+
this.chain = chain;
|
|
7
7
|
}
|
|
8
8
|
async generate() {
|
|
9
|
-
const
|
|
10
|
-
const interfaceName = stringPascalCase(chain);
|
|
9
|
+
const interfaceName = stringPascalCase(this.chain);
|
|
11
10
|
const template = compileTemplate('index.hbs');
|
|
12
11
|
return beautifySourceCode(template({ interfaceName }));
|
|
13
12
|
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { subscriptionsInfo } from '@dedot/specs';
|
|
2
|
+
import { ApiGen } from '../generator/index.js';
|
|
3
|
+
import { beautifySourceCode, compileTemplate } from './utils.js';
|
|
4
|
+
const HIDDEN_RPCS = [
|
|
5
|
+
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
6
|
+
'rpc_methods',
|
|
7
|
+
];
|
|
8
|
+
const ALIAS_RPCS = [
|
|
9
|
+
// TODO include these into the specs
|
|
10
|
+
'account_nextIndex',
|
|
11
|
+
'system_dryRunAt',
|
|
12
|
+
'state_callAt',
|
|
13
|
+
'state_getKeysPagedAt',
|
|
14
|
+
'state_getStorageAt',
|
|
15
|
+
'state_getStorageHashAt',
|
|
16
|
+
'state_getStorageSizeAt',
|
|
17
|
+
'chain_getRuntimeVersion',
|
|
18
|
+
'chain_getHead',
|
|
19
|
+
'chain_getFinalisedHead',
|
|
20
|
+
'childstate_getKeysPagedAt',
|
|
21
|
+
// Subscription pairs
|
|
22
|
+
'chain_subscribeRuntimeVersion',
|
|
23
|
+
'chain_unsubscribeRuntimeVersion',
|
|
24
|
+
'chain_unsubscribeNewHead',
|
|
25
|
+
'chain_subscribeNewHead',
|
|
26
|
+
'subscribe_newHead',
|
|
27
|
+
'unsubscribe_newHead',
|
|
28
|
+
'chain_subscribeFinalisedHeads',
|
|
29
|
+
'chain_unsubscribeFinalisedHeads',
|
|
30
|
+
];
|
|
31
|
+
export class JsonRpcGen extends ApiGen {
|
|
32
|
+
typesGen;
|
|
33
|
+
rpcMethods;
|
|
34
|
+
constructor(typesGen, rpcMethods) {
|
|
35
|
+
super(typesGen);
|
|
36
|
+
this.typesGen = typesGen;
|
|
37
|
+
this.rpcMethods = rpcMethods;
|
|
38
|
+
HIDDEN_RPCS.filter((one) => !rpcMethods.includes(one)).forEach((one) => rpcMethods.push(one));
|
|
39
|
+
rpcMethods.sort();
|
|
40
|
+
}
|
|
41
|
+
generate() {
|
|
42
|
+
this.typesGen.clearCache();
|
|
43
|
+
this.typesGen.typeImports.addKnownType('GenericJsonRpcApis');
|
|
44
|
+
this.typesGen.typeImports.addSpecType('JsonRpcApis');
|
|
45
|
+
const toExclude = Object.values(subscriptionsInfo).flat();
|
|
46
|
+
const importTypes = this.typesGen.typeImports.toImports();
|
|
47
|
+
const template = compileTemplate('json-rpc.hbs');
|
|
48
|
+
const jsonRpcMethods = this.rpcMethods
|
|
49
|
+
.filter((one) => !ALIAS_RPCS.includes(one)) // exclude alias rpcs
|
|
50
|
+
.filter((one) => !toExclude.includes(one)) // exclude unsubscribe methods
|
|
51
|
+
.map((one) => `'${one}'`)
|
|
52
|
+
.join(' | ');
|
|
53
|
+
return beautifySourceCode(template({ importTypes, jsonRpcMethods }));
|
|
54
|
+
}
|
|
55
|
+
}
|
package/generator/QueryGen.d.ts
CHANGED
package/generator/QueryGen.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
|
|
1
|
+
import { normalizeName, stringCamelCase } from '@dedot/utils';
|
|
2
|
+
import { ApiGen } from './ApiGen.js';
|
|
3
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
5
4
|
export class QueryGen extends ApiGen {
|
|
6
5
|
generate() {
|
|
7
6
|
const { pallets } = this.metadata;
|
|
@@ -13,9 +12,16 @@ export class QueryGen extends ApiGen {
|
|
|
13
12
|
if (!storage)
|
|
14
13
|
continue;
|
|
15
14
|
const queries = storage.entries.map((one) => this.#generateEntry(one));
|
|
16
|
-
const queryDefs = queries.map(({ name, valueType, keyType, docs
|
|
15
|
+
const queryDefs = queries.map(({ name, valueType, keyType, docs, keyTypeOut }) => {
|
|
16
|
+
if (keyTypeOut) {
|
|
17
|
+
return `${commentBlock(docs)}${name}: GenericStorageQuery<(${keyType}) => ${valueType}, ${keyTypeOut}>`;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
return `${commentBlock(docs)}${name}: GenericStorageQuery<(${keyType}) => ${valueType}>`;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
17
23
|
defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s storage queries`);
|
|
18
|
-
defTypeOut += `${
|
|
24
|
+
defTypeOut += `${stringCamelCase(pallet.name)}: {
|
|
19
25
|
${queryDefs.join(',\n')}
|
|
20
26
|
|
|
21
27
|
${commentBlock('Generic pallet storage query')}[storage: string]: GenericStorageQuery;
|
|
@@ -27,13 +33,14 @@ export class QueryGen extends ApiGen {
|
|
|
27
33
|
}
|
|
28
34
|
#generateEntry(entry) {
|
|
29
35
|
const { name, type, docs, modifier } = entry;
|
|
30
|
-
let valueType,
|
|
36
|
+
let valueType, keyTypeOut, keyTypeIn;
|
|
31
37
|
if (type.tag === 'Plain') {
|
|
32
38
|
valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
|
|
33
39
|
}
|
|
34
40
|
else if (type.tag === 'Map') {
|
|
35
41
|
valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
|
|
36
|
-
|
|
42
|
+
keyTypeOut = this.typesGen.generateType(type.value.keyTypeId, 1, true);
|
|
43
|
+
keyTypeIn = this.typesGen.generateType(type.value.keyTypeId, 1);
|
|
37
44
|
}
|
|
38
45
|
else {
|
|
39
46
|
throw Error('Invalid entry type!');
|
|
@@ -41,14 +48,15 @@ export class QueryGen extends ApiGen {
|
|
|
41
48
|
const isOptional = modifier === 'Optional';
|
|
42
49
|
valueType = `${valueType}${isOptional ? ' | undefined' : ''}`;
|
|
43
50
|
docs.push('\n');
|
|
44
|
-
if (
|
|
45
|
-
docs.push(`@param {${
|
|
51
|
+
if (keyTypeIn) {
|
|
52
|
+
docs.push(`@param {${keyTypeIn}} arg`);
|
|
46
53
|
}
|
|
47
54
|
docs.push(`@param {Callback<${valueType}> =} callback`);
|
|
48
55
|
return {
|
|
49
56
|
name: normalizeName(name),
|
|
50
57
|
valueType,
|
|
51
|
-
keyType:
|
|
58
|
+
keyType: keyTypeIn ? `arg: ${keyTypeIn}` : '',
|
|
59
|
+
keyTypeOut,
|
|
52
60
|
docs,
|
|
53
61
|
};
|
|
54
62
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
export declare class RuntimeApisGen extends
|
|
1
|
+
import { ApiGen } from './ApiGen.js';
|
|
2
|
+
import { TypesGen } from './TypesGen.js';
|
|
3
|
+
export declare class RuntimeApisGen extends ApiGen {
|
|
4
4
|
#private;
|
|
5
5
|
readonly typesGen: TypesGen;
|
|
6
|
-
readonly runtimeApis:
|
|
7
|
-
constructor(typesGen: TypesGen, runtimeApis:
|
|
6
|
+
readonly runtimeApis: Record<string, number>;
|
|
7
|
+
constructor(typesGen: TypesGen, runtimeApis: Record<string, number>);
|
|
8
8
|
generate(): Promise<string>;
|
|
9
9
|
}
|