@dedot/codegen 0.0.1-next.41a5fa17.16 → 0.0.1-next.4f5df6a3.2
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 +2 -2
- package/cjs/generator/IndexGen.js +4 -5
- package/cjs/generator/JsonRpcGen.js +59 -0
- package/cjs/generator/QueryGen.js +2 -2
- package/cjs/generator/RuntimeApisGen.js +84 -9
- package/cjs/generator/TypeImports.js +8 -0
- package/cjs/generator/TypesGen.js +1 -7
- package/cjs/generator/index.js +1 -1
- package/cjs/index.js +22 -22
- package/cjs/templates/index.hbs +2 -2
- package/cjs/templates/json-rpc.hbs +5 -0
- package/generator/ApiGen.d.ts +3 -3
- package/generator/ConstsGen.d.ts +1 -1
- package/generator/ConstsGen.js +1 -1
- package/generator/IndexGen.d.ts +2 -3
- package/generator/IndexGen.js +4 -5
- package/generator/{RpcGen.d.ts → JsonRpcGen.d.ts} +1 -4
- package/generator/JsonRpcGen.js +55 -0
- package/generator/QueryGen.d.ts +1 -1
- package/generator/QueryGen.js +1 -1
- package/generator/RuntimeApisGen.d.ts +2 -2
- package/generator/RuntimeApisGen.js +85 -10
- package/generator/TypeImports.d.ts +2 -0
- package/generator/TypeImports.js +8 -0
- package/generator/TypesGen.d.ts +1 -1
- package/generator/TypesGen.js +2 -8
- package/generator/index.d.ts +1 -1
- package/generator/index.js +1 -1
- package/index.d.ts +2 -3
- package/index.js +21 -21
- package/package.json +7 -7
- 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 -175
- 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.js +0 -171
- 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
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ConstsGen = void 0;
|
|
4
4
|
const utils_1 = require("@dedot/utils");
|
|
5
|
-
const
|
|
5
|
+
const ApiGen_js_1 = require("./ApiGen.js");
|
|
6
6
|
const utils_js_1 = require("./utils.js");
|
|
7
|
-
class ConstsGen extends
|
|
7
|
+
class ConstsGen extends ApiGen_js_1.ApiGen {
|
|
8
8
|
generate() {
|
|
9
9
|
const { pallets } = this.metadata;
|
|
10
10
|
this.typesGen.clearCache();
|
|
@@ -4,13 +4,12 @@ exports.IndexGen = void 0;
|
|
|
4
4
|
const utils_js_1 = require("./utils.js");
|
|
5
5
|
const utils_1 = require("@dedot/utils");
|
|
6
6
|
class IndexGen {
|
|
7
|
-
|
|
8
|
-
constructor(
|
|
9
|
-
this.
|
|
7
|
+
chain;
|
|
8
|
+
constructor(chain) {
|
|
9
|
+
this.chain = chain;
|
|
10
10
|
}
|
|
11
11
|
async generate() {
|
|
12
|
-
const
|
|
13
|
-
const interfaceName = (0, utils_1.stringPascalCase)(chain);
|
|
12
|
+
const interfaceName = (0, utils_1.stringPascalCase)(this.chain);
|
|
14
13
|
const template = (0, utils_js_1.compileTemplate)('index.hbs');
|
|
15
14
|
return (0, utils_js_1.beautifySourceCode)(template({ interfaceName }));
|
|
16
15
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JsonRpcGen = void 0;
|
|
4
|
+
const index_js_1 = require("../generator/index.js");
|
|
5
|
+
const utils_js_1 = require("./utils.js");
|
|
6
|
+
const specs_1 = require("@dedot/specs");
|
|
7
|
+
const HIDDEN_RPCS = [
|
|
8
|
+
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
9
|
+
'rpc_methods',
|
|
10
|
+
];
|
|
11
|
+
const ALIAS_RPCS = [
|
|
12
|
+
// TODO include these into the specs
|
|
13
|
+
'account_nextIndex',
|
|
14
|
+
'system_dryRunAt',
|
|
15
|
+
'state_callAt',
|
|
16
|
+
'state_getKeysPagedAt',
|
|
17
|
+
'state_getStorageAt',
|
|
18
|
+
'state_getStorageHashAt',
|
|
19
|
+
'state_getStorageSizeAt',
|
|
20
|
+
'chain_getRuntimeVersion',
|
|
21
|
+
'chain_getHead',
|
|
22
|
+
'chain_getFinalisedHead',
|
|
23
|
+
'childstate_getKeysPagedAt',
|
|
24
|
+
// Subscription pairs
|
|
25
|
+
'chain_subscribeRuntimeVersion',
|
|
26
|
+
'chain_unsubscribeRuntimeVersion',
|
|
27
|
+
'chain_unsubscribeNewHead',
|
|
28
|
+
'chain_subscribeNewHead',
|
|
29
|
+
'subscribe_newHead',
|
|
30
|
+
'unsubscribe_newHead',
|
|
31
|
+
'chain_subscribeFinalisedHeads',
|
|
32
|
+
'chain_unsubscribeFinalisedHeads',
|
|
33
|
+
];
|
|
34
|
+
class JsonRpcGen extends index_js_1.ApiGen {
|
|
35
|
+
typesGen;
|
|
36
|
+
rpcMethods;
|
|
37
|
+
constructor(typesGen, rpcMethods) {
|
|
38
|
+
super(typesGen);
|
|
39
|
+
this.typesGen = typesGen;
|
|
40
|
+
this.rpcMethods = rpcMethods;
|
|
41
|
+
HIDDEN_RPCS.filter((one) => !rpcMethods.includes(one)).forEach((one) => rpcMethods.push(one));
|
|
42
|
+
rpcMethods.sort();
|
|
43
|
+
}
|
|
44
|
+
generate() {
|
|
45
|
+
this.typesGen.clearCache();
|
|
46
|
+
this.typesGen.typeImports.addKnownType('GenericJsonRpcApis');
|
|
47
|
+
this.typesGen.typeImports.addSpecType('JsonRpcApis');
|
|
48
|
+
const toExclude = Object.values(specs_1.subscriptionsInfo).flat();
|
|
49
|
+
const importTypes = this.typesGen.typeImports.toImports();
|
|
50
|
+
const template = (0, utils_js_1.compileTemplate)('json-rpc.hbs');
|
|
51
|
+
const jsonRpcMethods = this.rpcMethods
|
|
52
|
+
.filter((one) => !ALIAS_RPCS.includes(one)) // exclude alias rpcs
|
|
53
|
+
.filter((one) => !toExclude.includes(one)) // exclude unsubscribe methods
|
|
54
|
+
.map((one) => `'${one}'`)
|
|
55
|
+
.join(' | ');
|
|
56
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, jsonRpcMethods }));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.JsonRpcGen = JsonRpcGen;
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.QueryGen = void 0;
|
|
4
4
|
const utils_1 = require("@dedot/utils");
|
|
5
|
-
const
|
|
5
|
+
const ApiGen_js_1 = require("./ApiGen.js");
|
|
6
6
|
const utils_js_1 = require("./utils.js");
|
|
7
|
-
class QueryGen extends
|
|
7
|
+
class QueryGen extends ApiGen_js_1.ApiGen {
|
|
8
8
|
generate() {
|
|
9
9
|
const { pallets } = this.metadata;
|
|
10
10
|
this.typesGen.clearCache();
|
|
@@ -4,12 +4,13 @@ exports.RuntimeApisGen = void 0;
|
|
|
4
4
|
const specs_1 = require("@dedot/specs");
|
|
5
5
|
const utils_js_1 = require("./utils.js");
|
|
6
6
|
const utils_1 = require("@dedot/utils");
|
|
7
|
-
const
|
|
8
|
-
|
|
7
|
+
const ApiGen_js_1 = require("./ApiGen.js");
|
|
8
|
+
const known_codecs_js_1 = require("./known-codecs.js");
|
|
9
|
+
class RuntimeApisGen extends ApiGen_js_1.ApiGen {
|
|
9
10
|
typesGen;
|
|
10
11
|
runtimeApis;
|
|
11
12
|
constructor(typesGen, runtimeApis) {
|
|
12
|
-
super(typesGen
|
|
13
|
+
super(typesGen);
|
|
13
14
|
this.typesGen = typesGen;
|
|
14
15
|
this.runtimeApis = runtimeApis;
|
|
15
16
|
}
|
|
@@ -59,17 +60,17 @@ class RuntimeApisGen extends RpcGen_js_1.RpcGen {
|
|
|
59
60
|
const { docs = [], params, type, runtimeApiName, methodName } = spec;
|
|
60
61
|
const callName = `${runtimeApiName}_${(0, utils_1.stringSnakeCase)(methodName)}`;
|
|
61
62
|
const defaultDocs = [`@callname: ${callName}`];
|
|
62
|
-
this
|
|
63
|
-
params.forEach(({ type }) => this
|
|
63
|
+
this.#addTypeImport(type, false);
|
|
64
|
+
params.forEach(({ type }) => this.#addTypeImport(type));
|
|
64
65
|
const typedParams = params.map((param, idx) => ({
|
|
65
66
|
...param,
|
|
66
67
|
isOptional: this.#isOptionalParam(params, param.type, idx),
|
|
67
|
-
plainType: this
|
|
68
|
+
plainType: this.#getGeneratedTypeName(param.type),
|
|
68
69
|
}));
|
|
69
70
|
const paramsOut = typedParams
|
|
70
71
|
.map(({ name, isOptional, plainType }) => `${(0, utils_1.stringCamelCase)(name)}${isOptional ? '?' : ''}: ${plainType}`)
|
|
71
72
|
.join(', ');
|
|
72
|
-
const typeOut = this
|
|
73
|
+
const typeOut = this.#getGeneratedTypeName(type, false);
|
|
73
74
|
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
|
|
74
75
|
}
|
|
75
76
|
#generateMethodDef(runtimeApiName, methodDef) {
|
|
@@ -77,7 +78,7 @@ class RuntimeApisGen extends RpcGen_js_1.RpcGen {
|
|
|
77
78
|
const callName = `${runtimeApiName}_${(0, utils_1.stringSnakeCase)(methodName)}`;
|
|
78
79
|
const defaultDocs = [`@callname: ${callName}`];
|
|
79
80
|
const typeOut = this.typesGen.generateType(output, 1, true);
|
|
80
|
-
this
|
|
81
|
+
this.#addTypeImport(typeOut, false);
|
|
81
82
|
const typedInputs = inputs
|
|
82
83
|
.map((input, idx) => ({
|
|
83
84
|
...input,
|
|
@@ -87,7 +88,7 @@ class RuntimeApisGen extends RpcGen_js_1.RpcGen {
|
|
|
87
88
|
...input,
|
|
88
89
|
isOptional: this.#isOptionalParam(inputs, input.type, idx),
|
|
89
90
|
}));
|
|
90
|
-
this
|
|
91
|
+
this.#addTypeImport(typedInputs.map((t) => t.type));
|
|
91
92
|
const paramsOut = typedInputs
|
|
92
93
|
.map(({ name, type, isOptional }) => `${(0, utils_1.stringCamelCase)(name)}${isOptional ? '?' : ''}: ${type}`)
|
|
93
94
|
.join(', ');
|
|
@@ -114,5 +115,79 @@ class RuntimeApisGen extends RpcGen_js_1.RpcGen {
|
|
|
114
115
|
const runtimeApiName = (0, specs_1.getRuntimeApiNames)().find((one) => (0, utils_1.calcRuntimeApiHash)(one) === runtimeApiHash);
|
|
115
116
|
return (0, specs_1.getRuntimeApiSpecs)().find((one) => one.runtimeApiName === runtimeApiName && one.version === version);
|
|
116
117
|
};
|
|
118
|
+
// TODO check typeIn, typeOut if param type, or rpc type isScale
|
|
119
|
+
#addTypeImport(type, toTypeIn = true) {
|
|
120
|
+
if (Array.isArray(type)) {
|
|
121
|
+
type.forEach((one) => this.#addTypeImport(one, toTypeIn));
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
type = type.trim();
|
|
125
|
+
if ((0, utils_js_1.isNativeType)(type)) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
// Handle generic wrapper types
|
|
129
|
+
const matchArray = type.match(utils_js_1.WRAPPER_TYPE_REGEX);
|
|
130
|
+
if (matchArray) {
|
|
131
|
+
const [_, $1, $2] = matchArray;
|
|
132
|
+
this.#addTypeImport($1, toTypeIn);
|
|
133
|
+
if ($2.match(utils_js_1.WRAPPER_TYPE_REGEX) || $2.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
134
|
+
this.#addTypeImport($2, toTypeIn);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
this.#addTypeImport($2.split(','), toTypeIn);
|
|
138
|
+
}
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
// Check tuple type
|
|
142
|
+
if (type.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
143
|
+
this.#addTypeImport(type.slice(1, -1).split(','), toTypeIn);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (type.includes(' | ')) {
|
|
147
|
+
this.#addTypeImport(type.split(' | ').map((one) => one.trim()), toTypeIn);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
const codecType = this.#getCodecType(type, toTypeIn);
|
|
152
|
+
if ((0, utils_js_1.isNativeType)(codecType))
|
|
153
|
+
return;
|
|
154
|
+
this.typesGen.typeImports.addCodecType(codecType);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
catch (e) { }
|
|
158
|
+
this.typesGen.addTypeImport(type);
|
|
159
|
+
}
|
|
160
|
+
#getGeneratedTypeName(type, toTypeIn = true) {
|
|
161
|
+
try {
|
|
162
|
+
const matchArray = type.match(utils_js_1.WRAPPER_TYPE_REGEX);
|
|
163
|
+
if (matchArray) {
|
|
164
|
+
const [_, $1, $2] = matchArray;
|
|
165
|
+
const wrapperTypeName = this.#getCodecType($1, toTypeIn);
|
|
166
|
+
if ($2.match(utils_js_1.WRAPPER_TYPE_REGEX) || $2.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
167
|
+
return `${wrapperTypeName}<${this.#getGeneratedTypeName($2, toTypeIn)}>`;
|
|
168
|
+
}
|
|
169
|
+
const innerTypeNames = $2
|
|
170
|
+
.split(',')
|
|
171
|
+
.map((one) => this.#getGeneratedTypeName(one.trim(), toTypeIn))
|
|
172
|
+
.join(', ');
|
|
173
|
+
return `${wrapperTypeName}<${innerTypeNames}>`;
|
|
174
|
+
}
|
|
175
|
+
else if (type.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
176
|
+
const innerTypeNames = type
|
|
177
|
+
.slice(1, -1)
|
|
178
|
+
.split(',')
|
|
179
|
+
.map((one) => this.#getGeneratedTypeName(one.trim(), toTypeIn))
|
|
180
|
+
.join(', ');
|
|
181
|
+
return `[${innerTypeNames}]`;
|
|
182
|
+
}
|
|
183
|
+
return this.#getCodecType(type, toTypeIn);
|
|
184
|
+
}
|
|
185
|
+
catch (e) { }
|
|
186
|
+
return type;
|
|
187
|
+
}
|
|
188
|
+
#getCodecType(type, toTypeIn = true) {
|
|
189
|
+
const { typeIn, typeOut } = (0, known_codecs_js_1.findKnownCodecType)(type);
|
|
190
|
+
return toTypeIn ? typeIn : typeOut;
|
|
191
|
+
}
|
|
117
192
|
}
|
|
118
193
|
exports.RuntimeApisGen = RuntimeApisGen;
|
|
@@ -8,30 +8,35 @@ class TypeImports {
|
|
|
8
8
|
codecTypes;
|
|
9
9
|
// Known types that're not codecs or chain/portable types defined in @dedot/types
|
|
10
10
|
knownTypes;
|
|
11
|
+
specTypes;
|
|
11
12
|
// External types to define explicitly
|
|
12
13
|
outTypes;
|
|
13
14
|
constructor() {
|
|
14
15
|
this.portableTypes = new Set();
|
|
15
16
|
this.codecTypes = new Set();
|
|
16
17
|
this.knownTypes = new Set();
|
|
18
|
+
this.specTypes = new Set();
|
|
17
19
|
this.outTypes = new Set();
|
|
18
20
|
}
|
|
19
21
|
clear() {
|
|
20
22
|
this.portableTypes.clear();
|
|
21
23
|
this.codecTypes.clear();
|
|
22
24
|
this.knownTypes.clear();
|
|
25
|
+
this.specTypes.clear();
|
|
23
26
|
this.outTypes.clear();
|
|
24
27
|
}
|
|
25
28
|
toImports(...excludeModules) {
|
|
26
29
|
// TODO generate outTypes!
|
|
27
30
|
const toImports = [
|
|
28
31
|
[this.knownTypes, '@dedot/types'],
|
|
32
|
+
[this.specTypes, '@dedot/specs'],
|
|
29
33
|
[this.codecTypes, '@dedot/codecs'],
|
|
30
34
|
[this.portableTypes, './types'],
|
|
31
35
|
];
|
|
32
36
|
return toImports
|
|
33
37
|
.filter(([_, module]) => !excludeModules.includes(module))
|
|
34
38
|
.map(([types, module]) => this.#toImportLine(types, module))
|
|
39
|
+
.filter((line) => line.length > 0)
|
|
35
40
|
.join('\n');
|
|
36
41
|
}
|
|
37
42
|
#toImportLine(types, module) {
|
|
@@ -49,6 +54,9 @@ class TypeImports {
|
|
|
49
54
|
addKnownType(...types) {
|
|
50
55
|
types.forEach((one) => this.knownTypes.add(one));
|
|
51
56
|
}
|
|
57
|
+
addSpecType(...types) {
|
|
58
|
+
types.forEach((one) => this.specTypes.add(one));
|
|
59
|
+
}
|
|
52
60
|
addOutType(...types) {
|
|
53
61
|
types.forEach((one) => this.outTypes.add(one));
|
|
54
62
|
}
|
|
@@ -4,7 +4,6 @@ exports.TypesGen = exports.BASIC_KNOWN_TYPES = void 0;
|
|
|
4
4
|
const codecs_1 = require("@dedot/codecs");
|
|
5
5
|
const utils_1 = require("@dedot/utils");
|
|
6
6
|
const utils_js_1 = require("./utils.js");
|
|
7
|
-
const known_types_js_1 = require("./known-types.js");
|
|
8
7
|
const TypeImports_js_1 = require("./TypeImports.js");
|
|
9
8
|
const known_codecs_js_1 = require("./known-codecs.js");
|
|
10
9
|
// Skip generate types for these
|
|
@@ -425,12 +424,7 @@ class TypesGen {
|
|
|
425
424
|
this.typeImports.addCodecType(typeName);
|
|
426
425
|
return;
|
|
427
426
|
}
|
|
428
|
-
|
|
429
|
-
this.typeImports.addKnownType(typeName);
|
|
430
|
-
}
|
|
431
|
-
else {
|
|
432
|
-
this.typeImports.addOutType(typeName);
|
|
433
|
-
}
|
|
427
|
+
this.typeImports.addOutType(typeName);
|
|
434
428
|
}
|
|
435
429
|
}
|
|
436
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);
|
package/cjs/index.js
CHANGED
|
@@ -23,42 +23,42 @@ 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
28
|
const fs = __importStar(require("fs"));
|
|
29
29
|
const path = __importStar(require("path"));
|
|
30
30
|
const index_js_1 = require("./generator/index.js");
|
|
31
31
|
const utils_1 = require("@dedot/utils");
|
|
32
|
-
async function
|
|
33
|
-
const api = await dedot_1.Dedot.
|
|
34
|
-
const { methods } = await api.rpc.
|
|
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
35
|
const apis = api.runtimeVersion?.apis || [];
|
|
36
|
-
if (!
|
|
37
|
-
|
|
36
|
+
if (!chain) {
|
|
37
|
+
chain = (0, utils_1.stringCamelCase)(api.runtimeVersion?.specName || api.runtimeChain || 'local');
|
|
38
38
|
}
|
|
39
|
-
await generateTypes(
|
|
39
|
+
await generateTypes(chain, api.metadataLatest, 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
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);
|
|
@@ -66,7 +66,7 @@ async function generateTypes(network, metadata, rpcMethods, runtimeApis, outDir
|
|
|
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
|
-
|
|
15
|
+
jsonrpc: ChainJsonRpcApis;
|
|
16
16
|
consts: ChainConsts;
|
|
17
17
|
query: ChainStorage;
|
|
18
18
|
errors: ChainErrors;
|
package/generator/ApiGen.d.ts
CHANGED
|
@@ -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,5 +1,5 @@
|
|
|
1
1
|
import { normalizeName, stringCamelCase } from '@dedot/utils';
|
|
2
|
-
import { ApiGen } from '
|
|
2
|
+
import { ApiGen } from './ApiGen.js';
|
|
3
3
|
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
4
4
|
export class ConstsGen extends ApiGen {
|
|
5
5
|
generate() {
|
package/generator/IndexGen.d.ts
CHANGED
package/generator/IndexGen.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { beautifySourceCode, compileTemplate } from './utils.js';
|
|
2
2
|
import { stringPascalCase } from '@dedot/utils';
|
|
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
|
}
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import { ApiGen, TypesGen } from '../generator/index.js';
|
|
2
|
-
export declare class
|
|
3
|
-
#private;
|
|
2
|
+
export declare class JsonRpcGen extends ApiGen {
|
|
4
3
|
readonly typesGen: TypesGen;
|
|
5
4
|
readonly rpcMethods: string[];
|
|
6
5
|
constructor(typesGen: TypesGen, rpcMethods: string[]);
|
|
7
6
|
generate(): Promise<string>;
|
|
8
|
-
addTypeImport(type: string | string[], toTypeIn?: boolean): void;
|
|
9
|
-
getGeneratedTypeName(type: string, toTypeIn?: boolean): string;
|
|
10
7
|
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { ApiGen } from '../generator/index.js';
|
|
2
|
+
import { beautifySourceCode, compileTemplate } from './utils.js';
|
|
3
|
+
import { subscriptionsInfo } from '@dedot/specs';
|
|
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,5 +1,5 @@
|
|
|
1
1
|
import { normalizeName, stringCamelCase } from '@dedot/utils';
|
|
2
|
-
import { ApiGen } from '
|
|
2
|
+
import { ApiGen } from './ApiGen.js';
|
|
3
3
|
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
4
4
|
export class QueryGen extends ApiGen {
|
|
5
5
|
generate() {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { TypesGen } from './TypesGen.js';
|
|
2
|
-
import {
|
|
3
|
-
export declare class RuntimeApisGen extends
|
|
2
|
+
import { ApiGen } from './ApiGen.js';
|
|
3
|
+
export declare class RuntimeApisGen extends ApiGen {
|
|
4
4
|
#private;
|
|
5
5
|
readonly typesGen: TypesGen;
|
|
6
6
|
readonly runtimeApis: [string, number][];
|