@dedot/codegen 0.0.1-alpha.28 → 0.0.1-alpha.29
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/QueryGen.js +9 -10
- package/cjs/generator/RpcGen.js +18 -19
- package/cjs/generator/RuntimeApisGen.js +19 -20
- package/cjs/generator/TxGen.js +15 -15
- package/cjs/generator/TypesGen.js +21 -22
- package/cjs/generator/index.js +10 -10
- package/cjs/generator/utils.js +30 -1
- package/cjs/index.js +20 -20
- package/generator/ApiGen.d.ts +1 -1
- 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/QueryGen.d.ts +1 -1
- package/generator/QueryGen.js +4 -5
- package/generator/RpcGen.d.ts +1 -1
- package/generator/RpcGen.js +3 -4
- package/generator/RuntimeApisGen.d.ts +2 -2
- package/generator/RuntimeApisGen.js +5 -6
- package/generator/TxGen.d.ts +1 -1
- package/generator/TxGen.js +3 -3
- package/generator/TypesGen.d.ts +1 -1
- package/generator/TypesGen.js +5 -6
- package/generator/index.d.ts +10 -10
- package/generator/index.js +10 -10
- package/generator/utils.d.ts +5 -0
- package/generator/utils.js +28 -0
- package/index.d.ts +2 -3
- package/index.js +10 -10
- package/package.json +7 -8
- package/cjs/genSupportedChainTypes.js +0 -83
- package/cjs/packageInfo.js +0 -5
- package/cjs/types.js +0 -2
- package/genSupportedChainTypes.d.ts +0 -1
- package/genSupportedChainTypes.js +0 -78
- package/packageInfo.d.ts +0 -4
- package/packageInfo.js +0 -3
- package/types.d.ts +0 -6
- package/types.js +0 -1
|
@@ -1,13 +1,12 @@
|
|
|
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
|
|
6
|
+
const utils_js_1 = require("./utils.js");
|
|
7
|
+
const known_types_js_1 = require("./known-types.js");
|
|
8
|
+
const TypeImports_js_1 = require("./TypeImports.js");
|
|
9
|
+
const known_codecs_js_1 = require("./known-codecs.js");
|
|
11
10
|
// Skip generate types for these
|
|
12
11
|
// as we do have native types for them
|
|
13
12
|
const SKIP_TYPES = [
|
|
@@ -43,7 +42,7 @@ class TypesGen {
|
|
|
43
42
|
this.metadata = metadata;
|
|
44
43
|
this.registry = new codecs_1.PortableRegistry(this.metadata);
|
|
45
44
|
this.includedTypes = this.#includedTypes();
|
|
46
|
-
this.typeImports = new
|
|
45
|
+
this.typeImports = new TypeImports_js_1.TypeImports();
|
|
47
46
|
}
|
|
48
47
|
generate() {
|
|
49
48
|
this.clearCache();
|
|
@@ -51,14 +50,14 @@ class TypesGen {
|
|
|
51
50
|
Object.values(this.includedTypes)
|
|
52
51
|
.filter(({ skip, knownType }) => !(skip || knownType))
|
|
53
52
|
.forEach(({ name, nameOut, id, docs }) => {
|
|
54
|
-
defTypeOut += `${(0,
|
|
53
|
+
defTypeOut += `${(0, utils_js_1.commentBlock)(docs)}export type ${nameOut} = ${this.generateType(id, 0, true)};\n\n`;
|
|
55
54
|
if (this.#shouldGenerateTypeIn(id)) {
|
|
56
55
|
defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
|
|
57
56
|
}
|
|
58
57
|
});
|
|
59
58
|
const importTypes = this.typeImports.toImports('./types');
|
|
60
|
-
const template = (0,
|
|
61
|
-
return (0,
|
|
59
|
+
const template = (0, utils_js_1.compileTemplate)('types.hbs');
|
|
60
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
62
61
|
}
|
|
63
62
|
typeCache = {};
|
|
64
63
|
clearCache() {
|
|
@@ -103,7 +102,7 @@ class TypesGen {
|
|
|
103
102
|
const { tag, value } = type;
|
|
104
103
|
switch (tag) {
|
|
105
104
|
case 'Primitive':
|
|
106
|
-
const $codec = (0,
|
|
105
|
+
const $codec = (0, known_codecs_js_1.findKnownCodec)(value.kind);
|
|
107
106
|
if ($codec.nativeType) {
|
|
108
107
|
return $codec.nativeType;
|
|
109
108
|
}
|
|
@@ -151,12 +150,12 @@ class TypesGen {
|
|
|
151
150
|
return 'null';
|
|
152
151
|
}
|
|
153
152
|
else if (members.every((x) => x.fields.length === 0)) {
|
|
154
|
-
return members.map(({ name, docs }) => `${(0,
|
|
153
|
+
return members.map(({ name, docs }) => `${(0, utils_js_1.commentBlock)(docs)}'${(0, utils_1.stringPascalCase)(name)}'`).join(' | ');
|
|
155
154
|
}
|
|
156
155
|
else {
|
|
157
156
|
const membersType = [];
|
|
158
157
|
for (const { fields, name, docs } of members) {
|
|
159
|
-
const keyName = (0,
|
|
158
|
+
const keyName = (0, utils_1.stringPascalCase)(name);
|
|
160
159
|
if (fields.length === 0) {
|
|
161
160
|
membersType.push([keyName, null, docs]);
|
|
162
161
|
}
|
|
@@ -164,7 +163,7 @@ class TypesGen {
|
|
|
164
163
|
const valueType = fields.length === 1
|
|
165
164
|
? this.generateType(fields[0].typeId, nestedLevel + 1, typeOut)
|
|
166
165
|
: `[${fields
|
|
167
|
-
.map(({ typeId, docs }) => `${(0,
|
|
166
|
+
.map(({ typeId, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${this.generateType(typeId, nestedLevel + 1, typeOut)}`)
|
|
168
167
|
.join(', ')}]`;
|
|
169
168
|
membersType.push([keyName, valueType, docs]);
|
|
170
169
|
}
|
|
@@ -179,7 +178,7 @@ class TypesGen {
|
|
|
179
178
|
value: valueType ? `, ${valueKey}${this.#isOptionalType(valueType) ? '?' : ''}: ${valueType} ` : '',
|
|
180
179
|
docs,
|
|
181
180
|
}))
|
|
182
|
-
.map(({ tag, value, docs }) => `${(0,
|
|
181
|
+
.map(({ tag, value, docs }) => `${(0, utils_js_1.commentBlock)(docs)}{ ${tag}${value} }`)
|
|
183
182
|
.join(' | ');
|
|
184
183
|
}
|
|
185
184
|
}
|
|
@@ -226,7 +225,7 @@ class TypesGen {
|
|
|
226
225
|
};
|
|
227
226
|
});
|
|
228
227
|
return `{${props
|
|
229
|
-
.map(({ name, type, optional, docs }) => `${(0,
|
|
228
|
+
.map(({ name, type, optional, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${name}${optional ? '?' : ''}: ${type}`)
|
|
230
229
|
.join(',\n')}}`;
|
|
231
230
|
}
|
|
232
231
|
#isOptionalType(type) {
|
|
@@ -267,8 +266,8 @@ class TypesGen {
|
|
|
267
266
|
const suffix = typeSuffixes.get(id) || '';
|
|
268
267
|
let knownType = false;
|
|
269
268
|
let name, nameOut;
|
|
270
|
-
if ((0,
|
|
271
|
-
const codecType = (0,
|
|
269
|
+
if ((0, known_codecs_js_1.isKnownCodecType)(joinedPath)) {
|
|
270
|
+
const codecType = (0, known_codecs_js_1.findKnownCodecType)(path.at(-1));
|
|
272
271
|
name = codecType.typeIn;
|
|
273
272
|
nameOut = codecType.typeOut;
|
|
274
273
|
knownType = true;
|
|
@@ -315,7 +314,7 @@ class TypesGen {
|
|
|
315
314
|
*/
|
|
316
315
|
#cleanPath(path) {
|
|
317
316
|
return path
|
|
318
|
-
.map((one) => (0,
|
|
317
|
+
.map((one) => (0, utils_1.stringPascalCase)(one))
|
|
319
318
|
.filter((one, idx, currentPath) => idx === 0 || one !== currentPath[idx - 1])
|
|
320
319
|
.join('');
|
|
321
320
|
}
|
|
@@ -390,10 +389,10 @@ class TypesGen {
|
|
|
390
389
|
if (diffParam?.typeId) {
|
|
391
390
|
const diffType = types[diffParam.typeId];
|
|
392
391
|
if (diffType.path.length > 0) {
|
|
393
|
-
return (0,
|
|
392
|
+
return (0, utils_1.stringPascalCase)(diffType.path.at(-1));
|
|
394
393
|
}
|
|
395
394
|
else if (diffType.type.tag === 'Primitive') {
|
|
396
|
-
return (0,
|
|
395
|
+
return (0, utils_1.stringPascalCase)(diffType.type.value.kind);
|
|
397
396
|
}
|
|
398
397
|
}
|
|
399
398
|
// Last resort!
|
|
@@ -404,7 +403,7 @@ class TypesGen {
|
|
|
404
403
|
typeName.forEach((one) => this.addTypeImport(one));
|
|
405
404
|
return;
|
|
406
405
|
}
|
|
407
|
-
if ((0,
|
|
406
|
+
if ((0, utils_js_1.isNativeType)(typeName)) {
|
|
408
407
|
return;
|
|
409
408
|
}
|
|
410
409
|
for (let type of Object.values(this.includedTypes)) {
|
|
@@ -426,7 +425,7 @@ class TypesGen {
|
|
|
426
425
|
this.typeImports.addCodecType(typeName);
|
|
427
426
|
return;
|
|
428
427
|
}
|
|
429
|
-
if (
|
|
428
|
+
if (known_types_js_1.knownTypes.includes(typeName)) {
|
|
430
429
|
this.typeImports.addKnownType(typeName);
|
|
431
430
|
}
|
|
432
431
|
else {
|
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("./RpcGen"), exports);
|
|
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("./RpcGen.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);
|
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,25 +23,25 @@ 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
|
-
const
|
|
31
|
-
const
|
|
32
|
-
async function
|
|
33
|
-
const api = await dedot_1.Dedot.
|
|
30
|
+
const index_js_1 = require("./generator/index.js");
|
|
31
|
+
const utils_1 = require("@dedot/utils");
|
|
32
|
+
async function generateTypesFromEndpoint(chain, endpoint, outDir) {
|
|
33
|
+
const api = await dedot_1.Dedot.new(endpoint);
|
|
34
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);
|
|
40
40
|
await api.disconnect();
|
|
41
41
|
}
|
|
42
|
-
exports.
|
|
43
|
-
async function generateTypes(
|
|
44
|
-
const dirPath = path.resolve(outDir,
|
|
42
|
+
exports.generateTypesFromEndpoint = generateTypesFromEndpoint;
|
|
43
|
+
async function generateTypes(chain, metadata, rpcMethods, runtimeApis, outDir = '.') {
|
|
44
|
+
const dirPath = path.resolve(outDir, chain);
|
|
45
45
|
const defTypesFileName = path.join(dirPath, `types.d.ts`);
|
|
46
46
|
const constsTypesFileName = path.join(dirPath, `consts.d.ts`);
|
|
47
47
|
const queryTypesFileName = path.join(dirPath, `query.d.ts`);
|
|
@@ -54,15 +54,15 @@ async function generateTypes(network, metadata, rpcMethods, runtimeApis, outDir
|
|
|
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 rpcGen = new
|
|
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 rpcGen = new index_js_1.RpcGen(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());
|
package/generator/ApiGen.d.ts
CHANGED
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 '../generator/index.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 { ApiGen } from './ApiGen';
|
|
2
|
-
import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
|
|
3
|
-
import { stringCamelCase, stringPascalCase } from '@
|
|
4
|
-
import { assert } from '@dedot/utils';
|
|
1
|
+
import { ApiGen } from './ApiGen.js';
|
|
2
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
3
|
+
import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
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 { ApiGen } from './ApiGen';
|
|
2
|
-
import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
|
|
3
|
-
import { stringCamelCase, stringPascalCase } from '@
|
|
4
|
-
import { assert } from '@dedot/utils';
|
|
1
|
+
import { ApiGen } from './ApiGen.js';
|
|
2
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
3
|
+
import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
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 { beautifySourceCode, compileTemplate } from './utils';
|
|
2
|
-
import { stringPascalCase } from '@
|
|
1
|
+
import { beautifySourceCode, compileTemplate } from './utils.js';
|
|
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
|
}
|
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 '../generator/index.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;
|
|
@@ -15,7 +14,7 @@ export class QueryGen extends ApiGen {
|
|
|
15
14
|
const queries = storage.entries.map((one) => this.#generateEntry(one));
|
|
16
15
|
const queryDefs = queries.map(({ name, valueType, keyType, docs }) => `${commentBlock(docs)}${name}: GenericStorageQuery<(${keyType}) => ${valueType}>`);
|
|
17
16
|
defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s storage queries`);
|
|
18
|
-
defTypeOut += `${
|
|
17
|
+
defTypeOut += `${stringCamelCase(pallet.name)}: {
|
|
19
18
|
${queryDefs.join(',\n')}
|
|
20
19
|
|
|
21
20
|
${commentBlock('Generic pallet storage query')}[storage: string]: GenericStorageQuery;
|
package/generator/RpcGen.d.ts
CHANGED
package/generator/RpcGen.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { findAliasRpcSpec, findRpcSpec, isUnsubscribeMethod } from '@dedot/specs';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { findKnownCodecType } from './known-codecs';
|
|
2
|
+
import { ApiGen } from '../generator/index.js';
|
|
3
|
+
import { isNativeType, beautifySourceCode, commentBlock, compileTemplate, TUPLE_TYPE_REGEX, WRAPPER_TYPE_REGEX, } from './utils.js';
|
|
4
|
+
import { findKnownCodecType } from './known-codecs.js';
|
|
6
5
|
const HIDDEN_RPCS = [
|
|
7
6
|
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
8
7
|
'rpc_methods',
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { getRuntimeApiNames, getRuntimeApiSpecs } from '@dedot/specs';
|
|
2
|
-
import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
|
|
3
|
-
import {
|
|
4
|
-
import { RpcGen } from './RpcGen';
|
|
5
|
-
import { stringCamelCase } from '@polkadot/util';
|
|
2
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
3
|
+
import { calcRuntimeApiHash, stringSnakeCase, stringCamelCase } from '@dedot/utils';
|
|
4
|
+
import { RpcGen } from './RpcGen.js';
|
|
6
5
|
export class RuntimeApisGen extends RpcGen {
|
|
7
6
|
typesGen;
|
|
8
7
|
runtimeApis;
|
|
@@ -18,7 +17,7 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
18
17
|
if (this.metadata.apis.length > 0) {
|
|
19
18
|
this.metadata.apis.forEach((runtimeApi) => {
|
|
20
19
|
const { name: runtimeApiName, methods } = runtimeApi;
|
|
21
|
-
runtimeCallsOut += commentBlock(`@runtimeapi: ${runtimeApiName} - ${
|
|
20
|
+
runtimeCallsOut += commentBlock(`@runtimeapi: ${runtimeApiName} - ${calcRuntimeApiHash(runtimeApiName)}`);
|
|
22
21
|
runtimeCallsOut += `${stringCamelCase(runtimeApiName)}: {
|
|
23
22
|
${methods.map((method) => this.#generateMethodDef(runtimeApiName, method)).join('\n')}
|
|
24
23
|
|
|
@@ -109,7 +108,7 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
109
108
|
}, []);
|
|
110
109
|
}
|
|
111
110
|
#findRuntimeApiSpec = (runtimeApiHash, version) => {
|
|
112
|
-
const runtimeApiName = getRuntimeApiNames().find((one) =>
|
|
111
|
+
const runtimeApiName = getRuntimeApiNames().find((one) => calcRuntimeApiHash(one) === runtimeApiHash);
|
|
113
112
|
return getRuntimeApiSpecs().find((one) => one.runtimeApiName === runtimeApiName && one.version === version);
|
|
114
113
|
};
|
|
115
114
|
}
|
package/generator/TxGen.d.ts
CHANGED
package/generator/TxGen.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ApiGen } from '../generator';
|
|
2
|
-
import { stringCamelCase, stringPascalCase } from '@
|
|
3
|
-
import { beautifySourceCode, commentBlock, compileTemplate, isReservedWord } from './utils';
|
|
1
|
+
import { ApiGen } from '../generator/index.js';
|
|
2
|
+
import { stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
3
|
+
import { beautifySourceCode, commentBlock, compileTemplate, isReservedWord } from './utils.js';
|
|
4
4
|
export class TxGen extends ApiGen {
|
|
5
5
|
generate() {
|
|
6
6
|
const { pallets, types } = this.metadata;
|
package/generator/TypesGen.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PortableRegistry, Field, MetadataLatest, PortableType, TypeId } from '@dedot/codecs';
|
|
2
|
-
import { TypeImports } from './TypeImports';
|
|
2
|
+
import { TypeImports } from './TypeImports.js';
|
|
3
3
|
interface NamedType extends PortableType {
|
|
4
4
|
name: string;
|
|
5
5
|
nameOut: string;
|
package/generator/TypesGen.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { stringPascalCase } from '@polkadot/util';
|
|
2
1
|
import { PortableRegistry } from '@dedot/codecs';
|
|
3
|
-
import {
|
|
4
|
-
import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
|
|
5
|
-
import { knownTypes } from './known-types';
|
|
6
|
-
import { TypeImports } from './TypeImports';
|
|
7
|
-
import { findKnownCodec, findKnownCodecType, isKnownCodecType } from './known-codecs';
|
|
2
|
+
import { normalizeName, stringPascalCase } from '@dedot/utils';
|
|
3
|
+
import { isNativeType, beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
4
|
+
import { knownTypes } from './known-types.js';
|
|
5
|
+
import { TypeImports } from './TypeImports.js';
|
|
6
|
+
import { findKnownCodec, findKnownCodecType, isKnownCodecType } from './known-codecs.js';
|
|
8
7
|
// Skip generate types for these
|
|
9
8
|
// as we do have native types for them
|
|
10
9
|
const SKIP_TYPES = [
|
package/generator/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export * from './TypesGen';
|
|
2
|
-
export * from './ApiGen';
|
|
3
|
-
export * from './ConstsGen';
|
|
4
|
-
export * from './QueryGen';
|
|
5
|
-
export * from './RpcGen';
|
|
6
|
-
export * from './IndexGen';
|
|
7
|
-
export * from './ErrorsGen';
|
|
8
|
-
export * from './EventsGen';
|
|
9
|
-
export * from './TxGen';
|
|
10
|
-
export * from './RuntimeApisGen';
|
|
1
|
+
export * from './TypesGen.js';
|
|
2
|
+
export * from './ApiGen.js';
|
|
3
|
+
export * from './ConstsGen.js';
|
|
4
|
+
export * from './QueryGen.js';
|
|
5
|
+
export * from './RpcGen.js';
|
|
6
|
+
export * from './IndexGen.js';
|
|
7
|
+
export * from './ErrorsGen.js';
|
|
8
|
+
export * from './EventsGen.js';
|
|
9
|
+
export * from './TxGen.js';
|
|
10
|
+
export * from './RuntimeApisGen.js';
|
package/generator/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export * from './TypesGen';
|
|
2
|
-
export * from './ApiGen';
|
|
3
|
-
export * from './ConstsGen';
|
|
4
|
-
export * from './QueryGen';
|
|
5
|
-
export * from './RpcGen';
|
|
6
|
-
export * from './IndexGen';
|
|
7
|
-
export * from './ErrorsGen';
|
|
8
|
-
export * from './EventsGen';
|
|
9
|
-
export * from './TxGen';
|
|
10
|
-
export * from './RuntimeApisGen';
|
|
1
|
+
export * from './TypesGen.js';
|
|
2
|
+
export * from './ApiGen.js';
|
|
3
|
+
export * from './ConstsGen.js';
|
|
4
|
+
export * from './QueryGen.js';
|
|
5
|
+
export * from './RpcGen.js';
|
|
6
|
+
export * from './IndexGen.js';
|
|
7
|
+
export * from './ErrorsGen.js';
|
|
8
|
+
export * from './EventsGen.js';
|
|
9
|
+
export * from './TxGen.js';
|
|
10
|
+
export * from './RuntimeApisGen.js';
|
package/generator/utils.d.ts
CHANGED
|
@@ -8,3 +8,8 @@ export declare const compileTemplate: (templateFileName: string) => HandlebarsTe
|
|
|
8
8
|
* @param word
|
|
9
9
|
*/
|
|
10
10
|
export declare const isReservedWord: (word: string) => boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Check if a type is native JS/TS type
|
|
13
|
+
* @param type
|
|
14
|
+
*/
|
|
15
|
+
export declare const isNativeType: (type: string) => boolean;
|
package/generator/utils.js
CHANGED
|
@@ -33,3 +33,31 @@ const TS_RESERVED_WORDS = ['new', 'class'];
|
|
|
33
33
|
* @param word
|
|
34
34
|
*/
|
|
35
35
|
export const isReservedWord = (word) => TS_RESERVED_WORDS.includes(word);
|
|
36
|
+
const TS_PRIMITIVE_TYPES = [
|
|
37
|
+
'void',
|
|
38
|
+
'undefined',
|
|
39
|
+
'null',
|
|
40
|
+
'number',
|
|
41
|
+
'boolean',
|
|
42
|
+
'bigint',
|
|
43
|
+
'Map',
|
|
44
|
+
'Set',
|
|
45
|
+
'string',
|
|
46
|
+
'any',
|
|
47
|
+
'Array',
|
|
48
|
+
'Record',
|
|
49
|
+
];
|
|
50
|
+
/**
|
|
51
|
+
* Check if a type is native JS/TS type
|
|
52
|
+
* @param type
|
|
53
|
+
*/
|
|
54
|
+
export const isNativeType = (type) => {
|
|
55
|
+
return TS_PRIMITIVE_TYPES.some((one) => {
|
|
56
|
+
if (typeof one === 'string') {
|
|
57
|
+
return one === type;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
return type.match(one);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
};
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
import { MetadataLatest } from '@dedot/codecs';
|
|
2
|
-
|
|
3
|
-
export declare function
|
|
4
|
-
export declare function generateTypes(network: NetworkInfo, metadata: MetadataLatest, rpcMethods: string[], runtimeApis: any[], outDir?: string): Promise<void>;
|
|
2
|
+
export declare function generateTypesFromEndpoint(chain: string, endpoint: string, outDir?: string): Promise<void>;
|
|
3
|
+
export declare function generateTypes(chain: string, metadata: MetadataLatest, rpcMethods: string[], runtimeApis: any[], outDir?: string): Promise<void>;
|