@dedot/codegen 0.0.1-next.2d39ff2d.5 → 0.0.1-next.324f6c64.55
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 +7 -8
- package/cjs/generator/ErrorsGen.js +7 -8
- package/cjs/generator/EventsGen.js +7 -8
- package/cjs/generator/IndexGen.js +6 -7
- package/cjs/generator/JsonRpcGen.js +59 -0
- package/cjs/generator/QueryGen.js +23 -15
- package/cjs/generator/RuntimeApisGen.js +101 -27
- package/cjs/generator/TxGen.js +16 -14
- package/cjs/generator/TypeImports.js +13 -3
- package/cjs/generator/TypesGen.js +12 -19
- package/cjs/generator/index.js +1 -1
- package/cjs/generator/known-codecs.js +8 -8
- package/cjs/generator/utils.js +30 -1
- package/cjs/index.js +32 -32
- package/cjs/templates/consts.hbs +1 -1
- package/cjs/templates/errors.hbs +1 -1
- package/cjs/templates/events.hbs +1 -1
- package/cjs/templates/index.hbs +15 -10
- package/cjs/templates/json-rpc.hbs +5 -0
- package/cjs/templates/query.hbs +1 -1
- package/cjs/templates/runtime.hbs +1 -1
- package/cjs/templates/tx.hbs +1 -1
- package/generator/ConstsGen.d.ts +2 -2
- package/generator/ConstsGen.js +6 -7
- package/generator/ErrorsGen.d.ts +1 -1
- package/generator/ErrorsGen.js +6 -7
- package/generator/EventsGen.d.ts +1 -1
- package/generator/EventsGen.js +6 -7
- package/generator/IndexGen.d.ts +3 -4
- package/generator/IndexGen.js +6 -7
- package/generator/JsonRpcGen.d.ts +7 -0
- package/generator/JsonRpcGen.js +55 -0
- package/generator/QueryGen.d.ts +2 -2
- package/generator/QueryGen.js +22 -14
- package/generator/RuntimeApisGen.d.ts +5 -5
- package/generator/RuntimeApisGen.js +97 -23
- package/generator/TxGen.d.ts +1 -1
- package/generator/TxGen.js +11 -9
- package/generator/TypeImports.d.ts +8 -1
- package/generator/TypeImports.js +13 -3
- package/generator/TypesGen.d.ts +2 -2
- package/generator/TypesGen.js +6 -13
- package/generator/index.d.ts +1 -1
- package/generator/index.js +1 -1
- package/generator/known-codecs.d.ts +2 -2
- package/generator/known-codecs.js +4 -4
- package/generator/utils.d.ts +5 -0
- package/generator/utils.js +28 -0
- package/index.d.ts +3 -4
- package/index.js +32 -32
- package/package.json +9 -18
- package/templates/consts.hbs +1 -1
- package/templates/errors.hbs +1 -1
- package/templates/events.hbs +1 -1
- package/templates/index.hbs +15 -10
- package/templates/json-rpc.hbs +5 -0
- package/templates/query.hbs +1 -1
- package/templates/runtime.hbs +1 -1
- package/templates/tx.hbs +1 -1
- 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
|
|
5
|
-
const
|
|
6
|
-
const utils_1 = require("@dedot/utils");
|
|
7
|
-
const utils_js_1 = require("./utils.js");
|
|
8
|
-
const known_types_js_1 = require("./known-types.js");
|
|
4
|
+
const codecs_1 = require("dedot/codecs");
|
|
5
|
+
const utils_1 = require("dedot/utils");
|
|
9
6
|
const TypeImports_js_1 = require("./TypeImports.js");
|
|
10
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 = [
|
|
@@ -45,7 +43,7 @@ class TypesGen {
|
|
|
45
43
|
this.includedTypes = this.#includedTypes();
|
|
46
44
|
this.typeImports = new TypeImports_js_1.TypeImports();
|
|
47
45
|
}
|
|
48
|
-
generate() {
|
|
46
|
+
generate(useSubPaths = false) {
|
|
49
47
|
this.clearCache();
|
|
50
48
|
let defTypeOut = '';
|
|
51
49
|
Object.values(this.includedTypes)
|
|
@@ -56,7 +54,7 @@ class TypesGen {
|
|
|
56
54
|
defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
|
|
57
55
|
}
|
|
58
56
|
});
|
|
59
|
-
const importTypes = this.typeImports.toImports('./types');
|
|
57
|
+
const importTypes = this.typeImports.toImports({ useSubPaths, excludeModules: ['./types'] });
|
|
60
58
|
const template = (0, utils_js_1.compileTemplate)('types.hbs');
|
|
61
59
|
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
62
60
|
}
|
|
@@ -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, utils_js_1.commentBlock)(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
|
}
|
|
@@ -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
|
@@ -18,7 +18,7 @@ __exportStar(require("./TypesGen.js"), exports);
|
|
|
18
18
|
__exportStar(require("./ApiGen.js"), exports);
|
|
19
19
|
__exportStar(require("./ConstsGen.js"), exports);
|
|
20
20
|
__exportStar(require("./QueryGen.js"), exports);
|
|
21
|
-
__exportStar(require("./
|
|
21
|
+
__exportStar(require("./JsonRpcGen.js"), exports);
|
|
22
22
|
__exportStar(require("./IndexGen.js"), exports);
|
|
23
23
|
__exportStar(require("./ErrorsGen.js"), exports);
|
|
24
24
|
__exportStar(require("./EventsGen.js"), exports);
|
|
@@ -24,10 +24,10 @@ 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
|
|
28
|
-
const Codecs = __importStar(require("
|
|
29
|
-
const codecs_1 = require("
|
|
30
|
-
const utils_1 = require("
|
|
27
|
+
const dedot_1 = require("dedot");
|
|
28
|
+
const Codecs = __importStar(require("dedot/codecs"));
|
|
29
|
+
const codecs_1 = require("dedot/codecs");
|
|
30
|
+
const utils_1 = require("dedot/utils");
|
|
31
31
|
const normalizeCodecName = (name) => {
|
|
32
32
|
return name.startsWith('$') ? name : `$${name}`;
|
|
33
33
|
};
|
|
@@ -96,7 +96,7 @@ function findKnownCodecType(name) {
|
|
|
96
96
|
};
|
|
97
97
|
}
|
|
98
98
|
const $codec = findKnownCodec(name);
|
|
99
|
-
if ($codec.nativeType &&
|
|
99
|
+
if ($codec.nativeType && dedot_1.$[name]) {
|
|
100
100
|
return {
|
|
101
101
|
name: normalizedName,
|
|
102
102
|
$codec,
|
|
@@ -114,7 +114,7 @@ function findKnownCodecType(name) {
|
|
|
114
114
|
exports.findKnownCodecType = findKnownCodecType;
|
|
115
115
|
function findKnownCodec(typeName) {
|
|
116
116
|
// @ts-ignore
|
|
117
|
-
const $codec = findKnownWrapperCodec(typeName) || Codecs[(0, exports.normalizeCodecName)(typeName)] ||
|
|
117
|
+
const $codec = findKnownWrapperCodec(typeName) || Codecs[(0, exports.normalizeCodecName)(typeName)] || dedot_1.$[typeName];
|
|
118
118
|
(0, utils_1.assert)($codec, `Known codec not found - ${typeName}`);
|
|
119
119
|
return $codec;
|
|
120
120
|
}
|
|
@@ -125,7 +125,7 @@ function findKnownWrapperCodec(typeName) {
|
|
|
125
125
|
const [_, wrapper, inner] = matchNames;
|
|
126
126
|
if (KNOWN_WRAPPER_TYPES.includes(wrapper)) {
|
|
127
127
|
// @ts-ignore
|
|
128
|
-
const $Wrapper =
|
|
128
|
+
const $Wrapper = dedot_1.$[wrapper];
|
|
129
129
|
if (inner.match(TUPLE_TYPE_REGEX) || inner.match(WRAPPER_TYPE_REGEX)) {
|
|
130
130
|
return $Wrapper(findKnownWrapperCodec(inner));
|
|
131
131
|
}
|
|
@@ -140,7 +140,7 @@ function findKnownWrapperCodec(typeName) {
|
|
|
140
140
|
.split(',')
|
|
141
141
|
.filter((x) => x)
|
|
142
142
|
.map((one) => findKnownCodec(one.trim()));
|
|
143
|
-
return
|
|
143
|
+
return dedot_1.$.Tuple(...$inner);
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
146
|
exports.findKnownWrapperCodec = findKnownWrapperCodec;
|
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,54 +23,54 @@ 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
27
|
const dedot_1 = require("dedot");
|
|
28
|
+
const utils_1 = require("dedot/utils");
|
|
28
29
|
const fs = __importStar(require("fs"));
|
|
29
30
|
const path = __importStar(require("path"));
|
|
30
31
|
const index_js_1 = require("./generator/index.js");
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
network.chain = (0, util_1.stringCamelCase)(api.runtimeVersion?.specName || api.runtimeChain || 'local');
|
|
32
|
+
async function generateTypesFromEndpoint(chain, endpoint, outDir, extension = 'd.ts', useSubPaths = false) {
|
|
33
|
+
const api = await dedot_1.Dedot.new(new dedot_1.WsProvider(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, useSubPaths);
|
|
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', useSubPaths = false) {
|
|
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
57
|
const typesGen = new index_js_1.TypesGen(metadata);
|
|
58
58
|
const constsGen = new index_js_1.ConstsGen(typesGen);
|
|
59
59
|
const queryGen = new index_js_1.QueryGen(typesGen);
|
|
60
|
-
const
|
|
61
|
-
const indexGen = new index_js_1.IndexGen(
|
|
60
|
+
const jsonRpcGen = new index_js_1.JsonRpcGen(typesGen, rpcMethods);
|
|
61
|
+
const indexGen = new index_js_1.IndexGen(chain);
|
|
62
62
|
const errorsGen = new index_js_1.ErrorsGen(typesGen);
|
|
63
63
|
const eventsGen = new index_js_1.EventsGen(typesGen);
|
|
64
64
|
const runtimeApisGen = new index_js_1.RuntimeApisGen(typesGen, runtimeApis);
|
|
65
65
|
const txGen = new index_js_1.TxGen(typesGen);
|
|
66
|
-
fs.writeFileSync(defTypesFileName, await typesGen.generate());
|
|
67
|
-
fs.writeFileSync(errorsFileName, await errorsGen.generate());
|
|
68
|
-
fs.writeFileSync(eventsFileName, await eventsGen.generate());
|
|
69
|
-
fs.writeFileSync(
|
|
70
|
-
fs.writeFileSync(queryTypesFileName, await queryGen.generate());
|
|
71
|
-
fs.writeFileSync(constsTypesFileName, await constsGen.generate());
|
|
72
|
-
fs.writeFileSync(txFileName, await txGen.generate());
|
|
73
|
-
fs.writeFileSync(indexFileName, await indexGen.generate());
|
|
74
|
-
fs.writeFileSync(runtimeApisFileName, await runtimeApisGen.generate());
|
|
66
|
+
fs.writeFileSync(defTypesFileName, await typesGen.generate(useSubPaths));
|
|
67
|
+
fs.writeFileSync(errorsFileName, await errorsGen.generate(useSubPaths));
|
|
68
|
+
fs.writeFileSync(eventsFileName, await eventsGen.generate(useSubPaths));
|
|
69
|
+
fs.writeFileSync(jsonRpcFileName, await jsonRpcGen.generate(useSubPaths));
|
|
70
|
+
fs.writeFileSync(queryTypesFileName, await queryGen.generate(useSubPaths));
|
|
71
|
+
fs.writeFileSync(constsTypesFileName, await constsGen.generate(useSubPaths));
|
|
72
|
+
fs.writeFileSync(txFileName, await txGen.generate(useSubPaths));
|
|
73
|
+
fs.writeFileSync(indexFileName, await indexGen.generate(useSubPaths));
|
|
74
|
+
fs.writeFileSync(runtimeApisFileName, await runtimeApisGen.generate(useSubPaths));
|
|
75
75
|
}
|
|
76
76
|
exports.generateTypes = generateTypes;
|
package/cjs/templates/consts.hbs
CHANGED
package/cjs/templates/errors.hbs
CHANGED
package/cjs/templates/events.hbs
CHANGED
package/cjs/templates/index.hbs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// Generated by @dedot/codegen
|
|
2
2
|
|
|
3
|
-
import { GenericSubstrateApi } from '@dedot/types';
|
|
3
|
+
import { GenericSubstrateApi, RpcLegacy, RpcV2, RpcVersion } 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';
|
|
@@ -11,12 +11,17 @@ import { ChainTx } from './tx';
|
|
|
11
11
|
|
|
12
12
|
export * from './types';
|
|
13
13
|
|
|
14
|
-
export interface {{{interfaceName}}}Api extends GenericSubstrateApi {
|
|
15
|
-
rpc:
|
|
16
|
-
consts: ChainConsts
|
|
17
|
-
query: ChainStorage
|
|
18
|
-
errors: ChainErrors
|
|
19
|
-
events: ChainEvents
|
|
20
|
-
call: RuntimeApis
|
|
21
|
-
tx: ChainTx
|
|
14
|
+
export interface Versioned{{{interfaceName}}}Api<Rv extends RpcVersion> extends GenericSubstrateApi<Rv> {
|
|
15
|
+
rpc: ChainJsonRpcApis<Rv>;
|
|
16
|
+
consts: ChainConsts<Rv>;
|
|
17
|
+
query: ChainStorage<Rv>;
|
|
18
|
+
errors: ChainErrors<Rv>;
|
|
19
|
+
events: ChainEvents<Rv>;
|
|
20
|
+
call: RuntimeApis<Rv>;
|
|
21
|
+
tx: ChainTx<Rv>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface {{{interfaceName}}}Api {
|
|
25
|
+
legacy: Versioned{{{interfaceName}}}Api<RpcLegacy>;
|
|
26
|
+
v2: Versioned{{{interfaceName}}}Api<RpcV2>;
|
|
22
27
|
}
|
package/cjs/templates/query.hbs
CHANGED
package/cjs/templates/tx.hbs
CHANGED
package/generator/ConstsGen.d.ts
CHANGED
package/generator/ConstsGen.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { ApiGen } from '../generator/index.js';
|
|
1
|
+
import { normalizeName, stringCamelCase } from 'dedot/utils';
|
|
2
|
+
import { ApiGen } from './ApiGen.js';
|
|
4
3
|
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
5
4
|
export class ConstsGen extends ApiGen {
|
|
6
|
-
generate() {
|
|
5
|
+
generate(useSubPaths = false) {
|
|
7
6
|
const { pallets } = this.metadata;
|
|
8
7
|
this.typesGen.clearCache();
|
|
9
|
-
this.typesGen.typeImports.addKnownType('GenericChainConsts');
|
|
8
|
+
this.typesGen.typeImports.addKnownType('GenericChainConsts', 'RpcVersion');
|
|
10
9
|
let defTypeOut = '';
|
|
11
10
|
for (let pallet of pallets) {
|
|
12
11
|
const typedConstants = pallet.constants.map((one) => ({
|
|
@@ -15,13 +14,13 @@ 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,
|
|
22
21
|
},`;
|
|
23
22
|
}
|
|
24
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
23
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
25
24
|
const template = compileTemplate('consts.hbs');
|
|
26
25
|
return beautifySourceCode(template({ importTypes, defTypeOut }));
|
|
27
26
|
}
|
package/generator/ErrorsGen.d.ts
CHANGED
package/generator/ErrorsGen.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
+
import { assert, stringCamelCase, stringPascalCase } from 'dedot/utils';
|
|
1
2
|
import { ApiGen } from './ApiGen.js';
|
|
2
3
|
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
3
|
-
import { stringCamelCase, stringPascalCase } from '@polkadot/util';
|
|
4
|
-
import { assert } from '@dedot/utils';
|
|
5
4
|
export class ErrorsGen extends ApiGen {
|
|
6
|
-
generate() {
|
|
5
|
+
generate(useSubPaths = false) {
|
|
7
6
|
const { pallets } = this.metadata;
|
|
8
7
|
this.typesGen.clearCache();
|
|
9
|
-
this.typesGen.typeImports.addKnownType('GenericChainErrors', 'GenericPalletError');
|
|
8
|
+
this.typesGen.typeImports.addKnownType('GenericChainErrors', 'GenericPalletError', 'RpcVersion');
|
|
10
9
|
let defTypeOut = '';
|
|
11
10
|
for (let pallet of pallets) {
|
|
12
11
|
const errorTypeId = pallet.error;
|
|
@@ -17,13 +16,13 @@ export class ErrorsGen extends ApiGen {
|
|
|
17
16
|
defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s errors`);
|
|
18
17
|
defTypeOut += `${stringCamelCase(pallet.name)}: {
|
|
19
18
|
${errorDefs
|
|
20
|
-
.map(({ name, docs }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletError
|
|
19
|
+
.map(({ name, docs }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletError<Rv>`)
|
|
21
20
|
.join(',\n')}
|
|
22
21
|
|
|
23
|
-
${commentBlock('Generic pallet error')}[error: string]: GenericPalletError
|
|
22
|
+
${commentBlock('Generic pallet error')}[error: string]: GenericPalletError<Rv>,
|
|
24
23
|
},`;
|
|
25
24
|
}
|
|
26
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
25
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
27
26
|
const template = compileTemplate('errors.hbs');
|
|
28
27
|
return beautifySourceCode(template({ importTypes, defTypeOut }));
|
|
29
28
|
}
|
package/generator/EventsGen.d.ts
CHANGED
package/generator/EventsGen.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
+
import { assert, stringCamelCase, stringPascalCase } from 'dedot/utils';
|
|
1
2
|
import { ApiGen } from './ApiGen.js';
|
|
2
3
|
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
3
|
-
import { stringCamelCase, stringPascalCase } from '@polkadot/util';
|
|
4
|
-
import { assert } from '@dedot/utils';
|
|
5
4
|
export class EventsGen extends ApiGen {
|
|
6
|
-
generate() {
|
|
5
|
+
generate(useSubPaths = false) {
|
|
7
6
|
const { pallets } = this.metadata;
|
|
8
7
|
this.typesGen.clearCache();
|
|
9
|
-
this.typesGen.typeImports.addKnownType('GenericChainEvents', 'GenericPalletEvent');
|
|
8
|
+
this.typesGen.typeImports.addKnownType('GenericChainEvents', 'GenericPalletEvent', 'RpcVersion');
|
|
10
9
|
let defTypeOut = '';
|
|
11
10
|
for (let pallet of pallets) {
|
|
12
11
|
const eventTypeId = pallet.event;
|
|
@@ -29,13 +28,13 @@ export class EventsGen extends ApiGen {
|
|
|
29
28
|
}
|
|
30
29
|
return { ...def, genericParts };
|
|
31
30
|
})
|
|
32
|
-
.map(({ name, docs, fields, genericParts }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletEvent
|
|
31
|
+
.map(({ name, docs, fields, genericParts }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletEvent<Rv, ${genericParts.join(', ')}>`)
|
|
33
32
|
.join(',\n')}
|
|
34
33
|
|
|
35
|
-
${commentBlock('Generic pallet event')}[prop: string]: GenericPalletEvent
|
|
34
|
+
${commentBlock('Generic pallet event')}[prop: string]: GenericPalletEvent<Rv>,
|
|
36
35
|
},`;
|
|
37
36
|
}
|
|
38
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
37
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
39
38
|
const template = compileTemplate('events.hbs');
|
|
40
39
|
return beautifySourceCode(template({ importTypes, defTypeOut }));
|
|
41
40
|
}
|
package/generator/IndexGen.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { NetworkInfo } from '../types.js';
|
|
2
1
|
export declare class IndexGen {
|
|
3
|
-
readonly
|
|
4
|
-
constructor(
|
|
5
|
-
generate(): Promise<string>;
|
|
2
|
+
readonly chain: string;
|
|
3
|
+
constructor(chain: string);
|
|
4
|
+
generate(useSubPaths?: boolean): Promise<string>;
|
|
6
5
|
}
|
package/generator/IndexGen.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
+
import { stringPascalCase } from 'dedot/utils';
|
|
1
2
|
import { beautifySourceCode, compileTemplate } from './utils.js';
|
|
2
|
-
import { stringPascalCase } from '@polkadot/util';
|
|
3
3
|
export class IndexGen {
|
|
4
|
-
|
|
5
|
-
constructor(
|
|
6
|
-
this.
|
|
4
|
+
chain;
|
|
5
|
+
constructor(chain) {
|
|
6
|
+
this.chain = chain;
|
|
7
7
|
}
|
|
8
|
-
async generate() {
|
|
9
|
-
const
|
|
10
|
-
const interfaceName = stringPascalCase(chain);
|
|
8
|
+
async generate(useSubPaths = false) {
|
|
9
|
+
const interfaceName = stringPascalCase(this.chain);
|
|
11
10
|
const template = compileTemplate('index.hbs');
|
|
12
11
|
return beautifySourceCode(template({ interfaceName }));
|
|
13
12
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ApiGen, TypesGen } from '../generator/index.js';
|
|
2
|
+
export declare class JsonRpcGen extends ApiGen {
|
|
3
|
+
readonly typesGen: TypesGen;
|
|
4
|
+
readonly rpcMethods: string[];
|
|
5
|
+
constructor(typesGen: TypesGen, rpcMethods: string[]);
|
|
6
|
+
generate(useSubPaths?: boolean): Promise<string>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { subscriptionsInfo } from 'dedot';
|
|
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(useSubPaths = false) {
|
|
42
|
+
this.typesGen.clearCache();
|
|
43
|
+
this.typesGen.typeImports.addKnownType('GenericJsonRpcApis', 'RpcVersion');
|
|
44
|
+
this.typesGen.typeImports.addKnownJsonRpcType('JsonRpcApis');
|
|
45
|
+
const toExclude = Object.values(subscriptionsInfo).flat();
|
|
46
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
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