@dedot/codegen 0.0.1-alpha.30 → 0.0.1-alpha.32
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 +5 -5
- package/cjs/generator/ErrorsGen.js +6 -6
- package/cjs/generator/EventsGen.js +6 -6
- package/cjs/generator/IndexGen.js +7 -3
- package/cjs/generator/JsonRpcGen.js +59 -0
- package/cjs/generator/QueryGen.js +21 -12
- package/cjs/generator/RuntimeApisGen.js +96 -21
- package/cjs/generator/TxGen.js +11 -9
- package/cjs/generator/TypeImports.js +13 -3
- package/cjs/generator/TypesGen.js +4 -10
- package/cjs/generator/index.js +1 -1
- package/cjs/generator/known-codecs.js +1 -1
- package/cjs/index.js +29 -28
- 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/ApiGen.d.ts +1 -1
- package/generator/ConstsGen.d.ts +2 -2
- package/generator/ConstsGen.js +4 -4
- package/generator/ErrorsGen.d.ts +1 -1
- package/generator/ErrorsGen.js +6 -6
- package/generator/EventsGen.d.ts +1 -1
- package/generator/EventsGen.js +6 -6
- package/generator/IndexGen.d.ts +1 -1
- package/generator/IndexGen.js +7 -3
- package/generator/JsonRpcGen.d.ts +8 -0
- package/generator/JsonRpcGen.js +55 -0
- package/generator/QueryGen.d.ts +2 -2
- package/generator/QueryGen.js +20 -11
- package/generator/RuntimeApisGen.d.ts +5 -5
- package/generator/RuntimeApisGen.js +94 -19
- 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 +4 -10
- package/generator/index.d.ts +1 -1
- package/generator/index.js +1 -1
- package/generator/known-codecs.d.ts +1 -1
- package/generator/known-codecs.js +1 -1
- package/index.d.ts +2 -2
- package/index.js +30 -29
- package/package.json +15 -17
- 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/generator/RpcGen.js +0 -175
- package/cjs/generator/known-types.js +0 -33
- package/cjs/templates/rpc.hbs +0 -7
- package/generator/RpcGen.d.ts +0 -10
- package/generator/RpcGen.js +0 -171
- package/generator/known-types.d.ts +0 -6
- package/generator/known-types.js +0 -30
- package/templates/rpc.hbs +0 -7
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import { getRuntimeApiNames, getRuntimeApiSpecs } from '@dedot/specs';
|
|
2
|
-
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
1
|
+
import { getRuntimeApiNames, getRuntimeApiSpecs } from '@dedot/runtime-specs';
|
|
3
2
|
import { calcRuntimeApiHash, stringSnakeCase, stringCamelCase } from '@dedot/utils';
|
|
4
|
-
import {
|
|
5
|
-
|
|
3
|
+
import { ApiGen } from './ApiGen.js';
|
|
4
|
+
import { findKnownCodecType } from './known-codecs.js';
|
|
5
|
+
import { beautifySourceCode, commentBlock, compileTemplate, isNativeType, TUPLE_TYPE_REGEX, WRAPPER_TYPE_REGEX, } from './utils.js';
|
|
6
|
+
export class RuntimeApisGen extends ApiGen {
|
|
6
7
|
typesGen;
|
|
7
8
|
runtimeApis;
|
|
8
9
|
constructor(typesGen, runtimeApis) {
|
|
9
|
-
super(typesGen
|
|
10
|
+
super(typesGen);
|
|
10
11
|
this.typesGen = typesGen;
|
|
11
12
|
this.runtimeApis = runtimeApis;
|
|
12
13
|
}
|
|
13
|
-
generate() {
|
|
14
|
+
generate(useSubPaths = false) {
|
|
14
15
|
this.typesGen.clearCache();
|
|
15
|
-
this.typesGen.typeImports.addKnownType('GenericRuntimeApis', 'GenericRuntimeApiMethod');
|
|
16
|
+
this.typesGen.typeImports.addKnownType('GenericRuntimeApis', 'GenericRuntimeApiMethod', 'RpcVersion');
|
|
16
17
|
let runtimeCallsOut = '';
|
|
17
18
|
if (this.metadata.apis.length > 0) {
|
|
18
19
|
this.metadata.apis.forEach((runtimeApi) => {
|
|
@@ -21,7 +22,7 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
21
22
|
runtimeCallsOut += `${stringCamelCase(runtimeApiName)}: {
|
|
22
23
|
${methods.map((method) => this.#generateMethodDef(runtimeApiName, method)).join('\n')}
|
|
23
24
|
|
|
24
|
-
${commentBlock('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
|
|
25
|
+
${commentBlock('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod<Rv>
|
|
25
26
|
}`;
|
|
26
27
|
});
|
|
27
28
|
}
|
|
@@ -38,11 +39,11 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
38
39
|
}))
|
|
39
40
|
.join('\n')}
|
|
40
41
|
|
|
41
|
-
${commentBlock('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
|
|
42
|
+
${commentBlock('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod<Rv>
|
|
42
43
|
}`;
|
|
43
44
|
});
|
|
44
45
|
}
|
|
45
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
46
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
46
47
|
const template = compileTemplate('runtime.hbs');
|
|
47
48
|
return beautifySourceCode(template({ importTypes, runtimeCallsOut }));
|
|
48
49
|
}
|
|
@@ -56,25 +57,25 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
56
57
|
const { docs = [], params, type, runtimeApiName, methodName } = spec;
|
|
57
58
|
const callName = `${runtimeApiName}_${stringSnakeCase(methodName)}`;
|
|
58
59
|
const defaultDocs = [`@callname: ${callName}`];
|
|
59
|
-
this
|
|
60
|
-
params.forEach(({ type }) => this
|
|
60
|
+
this.#addTypeImport(type, false);
|
|
61
|
+
params.forEach(({ type }) => this.#addTypeImport(type));
|
|
61
62
|
const typedParams = params.map((param, idx) => ({
|
|
62
63
|
...param,
|
|
63
64
|
isOptional: this.#isOptionalParam(params, param.type, idx),
|
|
64
|
-
plainType: this
|
|
65
|
+
plainType: this.#getGeneratedTypeName(param.type),
|
|
65
66
|
}));
|
|
66
67
|
const paramsOut = typedParams
|
|
67
68
|
.map(({ name, isOptional, plainType }) => `${stringCamelCase(name)}${isOptional ? '?' : ''}: ${plainType}`)
|
|
68
69
|
.join(', ');
|
|
69
|
-
const typeOut = this
|
|
70
|
-
return `${commentBlock(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
|
|
70
|
+
const typeOut = this.#getGeneratedTypeName(type, false);
|
|
71
|
+
return `${commentBlock(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<Rv, (${paramsOut}) => Promise<${typeOut}>>`;
|
|
71
72
|
}
|
|
72
73
|
#generateMethodDef(runtimeApiName, methodDef) {
|
|
73
74
|
const { name: methodName, inputs, output, docs } = methodDef;
|
|
74
75
|
const callName = `${runtimeApiName}_${stringSnakeCase(methodName)}`;
|
|
75
76
|
const defaultDocs = [`@callname: ${callName}`];
|
|
76
77
|
const typeOut = this.typesGen.generateType(output, 1, true);
|
|
77
|
-
this
|
|
78
|
+
this.#addTypeImport(typeOut, false);
|
|
78
79
|
const typedInputs = inputs
|
|
79
80
|
.map((input, idx) => ({
|
|
80
81
|
...input,
|
|
@@ -84,14 +85,14 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
84
85
|
...input,
|
|
85
86
|
isOptional: this.#isOptionalParam(inputs, input.type, idx),
|
|
86
87
|
}));
|
|
87
|
-
this
|
|
88
|
+
this.#addTypeImport(typedInputs.map((t) => t.type));
|
|
88
89
|
const paramsOut = typedInputs
|
|
89
90
|
.map(({ name, type, isOptional }) => `${stringCamelCase(name)}${isOptional ? '?' : ''}: ${type}`)
|
|
90
91
|
.join(', ');
|
|
91
|
-
return `${commentBlock(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`))}${stringCamelCase(methodName)}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
|
|
92
|
+
return `${commentBlock(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`))}${stringCamelCase(methodName)}: GenericRuntimeApiMethod<Rv, (${paramsOut}) => Promise<${typeOut}>>`;
|
|
92
93
|
}
|
|
93
94
|
#targetRuntimeApiSpecs() {
|
|
94
|
-
const specs = this.runtimeApis.map(([runtimeApiHash, version]) => {
|
|
95
|
+
const specs = Object.entries(this.runtimeApis).map(([runtimeApiHash, version]) => {
|
|
95
96
|
const runtimeApiSpec = this.#findRuntimeApiSpec(runtimeApiHash, version);
|
|
96
97
|
if (!runtimeApiSpec)
|
|
97
98
|
return;
|
|
@@ -111,4 +112,78 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
111
112
|
const runtimeApiName = getRuntimeApiNames().find((one) => calcRuntimeApiHash(one) === runtimeApiHash);
|
|
112
113
|
return getRuntimeApiSpecs().find((one) => one.runtimeApiName === runtimeApiName && one.version === version);
|
|
113
114
|
};
|
|
115
|
+
// TODO check typeIn, typeOut if param type, or rpc type isScale
|
|
116
|
+
#addTypeImport(type, toTypeIn = true) {
|
|
117
|
+
if (Array.isArray(type)) {
|
|
118
|
+
type.forEach((one) => this.#addTypeImport(one, toTypeIn));
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
type = type.trim();
|
|
122
|
+
if (isNativeType(type)) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
// Handle generic wrapper types
|
|
126
|
+
const matchArray = type.match(WRAPPER_TYPE_REGEX);
|
|
127
|
+
if (matchArray) {
|
|
128
|
+
const [_, $1, $2] = matchArray;
|
|
129
|
+
this.#addTypeImport($1, toTypeIn);
|
|
130
|
+
if ($2.match(WRAPPER_TYPE_REGEX) || $2.match(TUPLE_TYPE_REGEX)) {
|
|
131
|
+
this.#addTypeImport($2, toTypeIn);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
this.#addTypeImport($2.split(','), toTypeIn);
|
|
135
|
+
}
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
// Check tuple type
|
|
139
|
+
if (type.match(TUPLE_TYPE_REGEX)) {
|
|
140
|
+
this.#addTypeImport(type.slice(1, -1).split(','), toTypeIn);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (type.includes(' | ')) {
|
|
144
|
+
this.#addTypeImport(type.split(' | ').map((one) => one.trim()), toTypeIn);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
const codecType = this.#getCodecType(type, toTypeIn);
|
|
149
|
+
if (isNativeType(codecType))
|
|
150
|
+
return;
|
|
151
|
+
this.typesGen.typeImports.addCodecType(codecType);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
catch (e) { }
|
|
155
|
+
this.typesGen.addTypeImport(type);
|
|
156
|
+
}
|
|
157
|
+
#getGeneratedTypeName(type, toTypeIn = true) {
|
|
158
|
+
try {
|
|
159
|
+
const matchArray = type.match(WRAPPER_TYPE_REGEX);
|
|
160
|
+
if (matchArray) {
|
|
161
|
+
const [_, $1, $2] = matchArray;
|
|
162
|
+
const wrapperTypeName = this.#getCodecType($1, toTypeIn);
|
|
163
|
+
if ($2.match(WRAPPER_TYPE_REGEX) || $2.match(TUPLE_TYPE_REGEX)) {
|
|
164
|
+
return `${wrapperTypeName}<${this.#getGeneratedTypeName($2, toTypeIn)}>`;
|
|
165
|
+
}
|
|
166
|
+
const innerTypeNames = $2
|
|
167
|
+
.split(',')
|
|
168
|
+
.map((one) => this.#getGeneratedTypeName(one.trim(), toTypeIn))
|
|
169
|
+
.join(', ');
|
|
170
|
+
return `${wrapperTypeName}<${innerTypeNames}>`;
|
|
171
|
+
}
|
|
172
|
+
else if (type.match(TUPLE_TYPE_REGEX)) {
|
|
173
|
+
const innerTypeNames = type
|
|
174
|
+
.slice(1, -1)
|
|
175
|
+
.split(',')
|
|
176
|
+
.map((one) => this.#getGeneratedTypeName(one.trim(), toTypeIn))
|
|
177
|
+
.join(', ');
|
|
178
|
+
return `[${innerTypeNames}]`;
|
|
179
|
+
}
|
|
180
|
+
return this.#getCodecType(type, toTypeIn);
|
|
181
|
+
}
|
|
182
|
+
catch (e) { }
|
|
183
|
+
return type;
|
|
184
|
+
}
|
|
185
|
+
#getCodecType(type, toTypeIn = true) {
|
|
186
|
+
const { typeIn, typeOut } = findKnownCodecType(type);
|
|
187
|
+
return toTypeIn ? typeIn : typeOut;
|
|
188
|
+
}
|
|
114
189
|
}
|
package/generator/TxGen.d.ts
CHANGED
package/generator/TxGen.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { ApiGen } from '../generator/index.js';
|
|
2
1
|
import { stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import { ApiGen } from '../generator/index.js';
|
|
3
3
|
import { beautifySourceCode, commentBlock, compileTemplate, isReservedWord } from './utils.js';
|
|
4
4
|
export class TxGen extends ApiGen {
|
|
5
|
-
generate() {
|
|
5
|
+
generate(useSubPaths = false) {
|
|
6
6
|
const { pallets, types } = this.metadata;
|
|
7
7
|
this.typesGen.clearCache();
|
|
8
|
-
this.typesGen.typeImports.addKnownType('GenericChainTx', 'GenericTxCall', 'ISubmittableExtrinsic', 'ISubmittableResult', 'IRuntimeTxCall');
|
|
8
|
+
this.typesGen.typeImports.addKnownType('GenericChainTx', 'GenericTxCall', 'ISubmittableExtrinsic', 'ISubmittableResult', 'IRuntimeTxCall', 'RpcVersion', 'RpcV2', 'ISubmittableExtrinsicLegacy', 'TransactionStatusLegacy', 'TransactionStatusV2');
|
|
9
9
|
const { callTypeId, addressTypeId, signatureTypeId } = this.metadata.extrinsic;
|
|
10
10
|
const callTypeIn = this.typesGen.generateType(callTypeId, 1);
|
|
11
11
|
const addressTypeIn = this.typesGen.generateType(addressTypeId, 1);
|
|
@@ -41,20 +41,22 @@ export class TxGen extends ApiGen {
|
|
|
41
41
|
txDefsOut += commentBlock(`Pallet \`${pallet.name}\`'s transaction calls`);
|
|
42
42
|
txDefsOut += `${stringCamelCase(pallet.name)}: {
|
|
43
43
|
${typedTxs
|
|
44
|
-
.map(({ functionName, params, docs, callInput }) => `${commentBlock(docs, '\n', params.map((p) => `@param {${p.type}} ${p.normalizedName} ${p.docs}`))}${functionName}: GenericTxCall<(${params.map((p) => `${p.normalizedName}: ${p.type}`).join(', ')}) => ChainSubmittableExtrinsic
|
|
44
|
+
.map(({ functionName, params, docs, callInput }) => `${commentBlock(docs, '\n', params.map((p) => `@param {${p.type}} ${p.normalizedName} ${p.docs}`))}${functionName}: GenericTxCall<Rv, (${params.map((p) => `${p.normalizedName}: ${p.type}`).join(', ')}) => ChainSubmittableExtrinsic<Rv, ${callInput}>>`)
|
|
45
45
|
.join(',\n')}
|
|
46
46
|
|
|
47
|
-
${commentBlock('Generic pallet tx call')}[callName: string]: GenericTxCall<TxCall
|
|
47
|
+
${commentBlock('Generic pallet tx call')}[callName: string]: GenericTxCall<Rv, TxCall<Rv>>,
|
|
48
48
|
},`;
|
|
49
49
|
}
|
|
50
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
50
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
51
51
|
// TODO make explicit separate type for Extra
|
|
52
52
|
const defTypes = `
|
|
53
|
-
export type ChainSubmittableExtrinsic<T extends IRuntimeTxCall = ${callTypeIn}> =
|
|
53
|
+
export type ChainSubmittableExtrinsic<Rv extends RpcVersion, T extends IRuntimeTxCall = ${callTypeIn}> =
|
|
54
54
|
Extrinsic<${addressTypeIn}, T, ${signatureTypeIn}, any[]> &
|
|
55
|
-
|
|
55
|
+
(Rv extends RpcV2
|
|
56
|
+
? ISubmittableExtrinsic<ISubmittableResult<FrameSystemEventRecord, TransactionStatusV2>>
|
|
57
|
+
: ISubmittableExtrinsicLegacy<ISubmittableResult<FrameSystemEventRecord, TransactionStatusLegacy>>)
|
|
56
58
|
|
|
57
|
-
export type TxCall = (...args: any[]) => ChainSubmittableExtrinsic
|
|
59
|
+
export type TxCall<Rv extends RpcVersion> = (...args: any[]) => ChainSubmittableExtrinsic<Rv>;
|
|
58
60
|
`;
|
|
59
61
|
const template = compileTemplate('tx.hbs');
|
|
60
62
|
return beautifySourceCode(template({ importTypes, defTypes, txDefsOut }));
|
|
@@ -1,14 +1,21 @@
|
|
|
1
|
+
type ImportConfig = {
|
|
2
|
+
excludeModules?: string[];
|
|
3
|
+
useSubPaths?: boolean;
|
|
4
|
+
};
|
|
1
5
|
export declare class TypeImports {
|
|
2
6
|
#private;
|
|
3
7
|
portableTypes: Set<string>;
|
|
4
8
|
codecTypes: Set<string>;
|
|
5
9
|
knownTypes: Set<string>;
|
|
10
|
+
knownJsonRpcTypes: Set<string>;
|
|
6
11
|
outTypes: Set<string>;
|
|
7
12
|
constructor();
|
|
8
13
|
clear(): void;
|
|
9
|
-
toImports(
|
|
14
|
+
toImports(config?: ImportConfig): string;
|
|
10
15
|
addPortableType(...types: string[]): void;
|
|
11
16
|
addCodecType(...types: string[]): void;
|
|
12
17
|
addKnownType(...types: string[]): void;
|
|
18
|
+
addKnownJsonRpcType(...types: string[]): void;
|
|
13
19
|
addOutType(...types: string[]): void;
|
|
14
20
|
}
|
|
21
|
+
export {};
|
package/generator/TypeImports.js
CHANGED
|
@@ -5,30 +5,37 @@ export class TypeImports {
|
|
|
5
5
|
codecTypes;
|
|
6
6
|
// Known types that're not codecs or chain/portable types defined in @dedot/types
|
|
7
7
|
knownTypes;
|
|
8
|
+
knownJsonRpcTypes;
|
|
8
9
|
// External types to define explicitly
|
|
9
10
|
outTypes;
|
|
10
11
|
constructor() {
|
|
11
12
|
this.portableTypes = new Set();
|
|
12
13
|
this.codecTypes = new Set();
|
|
13
14
|
this.knownTypes = new Set();
|
|
15
|
+
this.knownJsonRpcTypes = new Set();
|
|
14
16
|
this.outTypes = new Set();
|
|
15
17
|
}
|
|
16
18
|
clear() {
|
|
17
19
|
this.portableTypes.clear();
|
|
18
20
|
this.codecTypes.clear();
|
|
19
21
|
this.knownTypes.clear();
|
|
22
|
+
this.knownJsonRpcTypes.clear();
|
|
20
23
|
this.outTypes.clear();
|
|
21
24
|
}
|
|
22
|
-
toImports(
|
|
25
|
+
toImports(config) {
|
|
26
|
+
const { excludeModules = [], useSubPaths = false } = config || {};
|
|
23
27
|
// TODO generate outTypes!
|
|
28
|
+
const prefix = useSubPaths ? '' : '@';
|
|
24
29
|
const toImports = [
|
|
25
|
-
[this.knownTypes,
|
|
26
|
-
[this.
|
|
30
|
+
[this.knownTypes, `${prefix}dedot/types`],
|
|
31
|
+
[this.knownJsonRpcTypes, `${prefix}dedot/types/json-rpc`],
|
|
32
|
+
[this.codecTypes, `${prefix}dedot/codecs`],
|
|
27
33
|
[this.portableTypes, './types'],
|
|
28
34
|
];
|
|
29
35
|
return toImports
|
|
30
36
|
.filter(([_, module]) => !excludeModules.includes(module))
|
|
31
37
|
.map(([types, module]) => this.#toImportLine(types, module))
|
|
38
|
+
.filter((line) => line.length > 0)
|
|
32
39
|
.join('\n');
|
|
33
40
|
}
|
|
34
41
|
#toImportLine(types, module) {
|
|
@@ -46,6 +53,9 @@ export class TypeImports {
|
|
|
46
53
|
addKnownType(...types) {
|
|
47
54
|
types.forEach((one) => this.knownTypes.add(one));
|
|
48
55
|
}
|
|
56
|
+
addKnownJsonRpcType(...types) {
|
|
57
|
+
types.forEach((one) => this.knownJsonRpcTypes.add(one));
|
|
58
|
+
}
|
|
49
59
|
addOutType(...types) {
|
|
50
60
|
types.forEach((one) => this.outTypes.add(one));
|
|
51
61
|
}
|
package/generator/TypesGen.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Field, MetadataLatest, PortableRegistry, PortableType, TypeId } from '@dedot/codecs';
|
|
2
2
|
import { TypeImports } from './TypeImports.js';
|
|
3
3
|
interface NamedType extends PortableType {
|
|
4
4
|
name: string;
|
|
@@ -18,7 +18,7 @@ export declare class TypesGen {
|
|
|
18
18
|
registry: PortableRegistry;
|
|
19
19
|
typeImports: TypeImports;
|
|
20
20
|
constructor(metadata: MetadataLatest);
|
|
21
|
-
generate(): Promise<string>;
|
|
21
|
+
generate(useSubPaths?: boolean): Promise<string>;
|
|
22
22
|
typeCache: Record<string, string>;
|
|
23
23
|
clearCache(): void;
|
|
24
24
|
generateType(typeId: TypeId, nestedLevel?: number, typeOut?: boolean): string;
|
package/generator/TypesGen.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { PortableRegistry } from '@dedot/codecs';
|
|
2
2
|
import { normalizeName, stringPascalCase } from '@dedot/utils';
|
|
3
|
-
import { isNativeType, beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
4
|
-
import { knownTypes } from './known-types.js';
|
|
5
3
|
import { TypeImports } from './TypeImports.js';
|
|
6
4
|
import { findKnownCodec, findKnownCodecType, isKnownCodecType } from './known-codecs.js';
|
|
5
|
+
import { beautifySourceCode, commentBlock, compileTemplate, isNativeType } from './utils.js';
|
|
7
6
|
// Skip generate types for these
|
|
8
7
|
// as we do have native types for them
|
|
9
8
|
const SKIP_TYPES = [
|
|
@@ -41,7 +40,7 @@ export class TypesGen {
|
|
|
41
40
|
this.includedTypes = this.#includedTypes();
|
|
42
41
|
this.typeImports = new TypeImports();
|
|
43
42
|
}
|
|
44
|
-
generate() {
|
|
43
|
+
generate(useSubPaths = false) {
|
|
45
44
|
this.clearCache();
|
|
46
45
|
let defTypeOut = '';
|
|
47
46
|
Object.values(this.includedTypes)
|
|
@@ -52,7 +51,7 @@ export class TypesGen {
|
|
|
52
51
|
defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
|
|
53
52
|
}
|
|
54
53
|
});
|
|
55
|
-
const importTypes = this.typeImports.toImports('./types');
|
|
54
|
+
const importTypes = this.typeImports.toImports({ useSubPaths, excludeModules: ['./types'] });
|
|
56
55
|
const template = compileTemplate('types.hbs');
|
|
57
56
|
return beautifySourceCode(template({ importTypes, defTypeOut }));
|
|
58
57
|
}
|
|
@@ -422,11 +421,6 @@ export class TypesGen {
|
|
|
422
421
|
this.typeImports.addCodecType(typeName);
|
|
423
422
|
return;
|
|
424
423
|
}
|
|
425
|
-
|
|
426
|
-
this.typeImports.addKnownType(typeName);
|
|
427
|
-
}
|
|
428
|
-
else {
|
|
429
|
-
this.typeImports.addOutType(typeName);
|
|
430
|
-
}
|
|
424
|
+
this.typeImports.addOutType(typeName);
|
|
431
425
|
}
|
|
432
426
|
}
|
package/generator/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export * from './TypesGen.js';
|
|
|
2
2
|
export * from './ApiGen.js';
|
|
3
3
|
export * from './ConstsGen.js';
|
|
4
4
|
export * from './QueryGen.js';
|
|
5
|
-
export * from './
|
|
5
|
+
export * from './JsonRpcGen.js';
|
|
6
6
|
export * from './IndexGen.js';
|
|
7
7
|
export * from './ErrorsGen.js';
|
|
8
8
|
export * from './EventsGen.js';
|
package/generator/index.js
CHANGED
|
@@ -2,7 +2,7 @@ export * from './TypesGen.js';
|
|
|
2
2
|
export * from './ApiGen.js';
|
|
3
3
|
export * from './ConstsGen.js';
|
|
4
4
|
export * from './QueryGen.js';
|
|
5
|
-
export * from './
|
|
5
|
+
export * from './JsonRpcGen.js';
|
|
6
6
|
export * from './IndexGen.js';
|
|
7
7
|
export * from './ErrorsGen.js';
|
|
8
8
|
export * from './EventsGen.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as $ from '@dedot/shape';
|
|
2
1
|
import * as Codecs from '@dedot/codecs';
|
|
3
2
|
import { $AccountId20, $AccountId32, $Bytes, $ConsensusEngineId, $Era, $EthereumAddress, $MultiAddress, $OpaqueExtrinsic, $PrefixedStorageKey, $RawBytes, $StorageData, $StorageKey, $UncheckedExtrinsic, } from '@dedot/codecs';
|
|
3
|
+
import * as $ from '@dedot/shape';
|
|
4
4
|
import { assert } from '@dedot/utils';
|
|
5
5
|
export const normalizeCodecName = (name) => {
|
|
6
6
|
return name.startsWith('$') ? name : `$${name}`;
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { MetadataLatest } from '@dedot/codecs';
|
|
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:
|
|
2
|
+
export declare function generateTypesFromEndpoint(chain: string, endpoint: string, outDir?: string, extension?: string, useSubPaths?: boolean): Promise<void>;
|
|
3
|
+
export declare function generateTypes(chain: string, metadata: MetadataLatest, rpcMethods: string[], runtimeApis: Record<string, number>, outDir?: string, extension?: string, useSubPaths?: boolean): Promise<void>;
|
package/index.js
CHANGED
|
@@ -1,48 +1,49 @@
|
|
|
1
|
-
import { Dedot } from 'dedot';
|
|
1
|
+
import { Dedot } from '@dedot/api';
|
|
2
|
+
import { WsProvider } from '@dedot/providers';
|
|
3
|
+
import { stringCamelCase } from '@dedot/utils';
|
|
2
4
|
import * as fs from 'fs';
|
|
3
5
|
import * as path from 'path';
|
|
4
|
-
import { ConstsGen, ErrorsGen, EventsGen, IndexGen,
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const apis = api.runtimeVersion?.apis || [];
|
|
6
|
+
import { ConstsGen, ErrorsGen, EventsGen, IndexGen, JsonRpcGen, QueryGen, RuntimeApisGen, TxGen, TypesGen, } from './generator/index.js';
|
|
7
|
+
export async function generateTypesFromEndpoint(chain, endpoint, outDir, extension = 'd.ts', useSubPaths = false) {
|
|
8
|
+
const api = await Dedot.new(new WsProvider(endpoint));
|
|
9
|
+
const { methods } = await api.rpc.rpc_methods();
|
|
10
|
+
const apis = api.runtimeVersion.apis || {};
|
|
10
11
|
if (!chain) {
|
|
11
|
-
chain = stringCamelCase(api.runtimeVersion
|
|
12
|
+
chain = stringCamelCase(api.runtimeVersion.specName || 'local');
|
|
12
13
|
}
|
|
13
|
-
await generateTypes(chain, api.
|
|
14
|
+
await generateTypes(chain, api.metadata.latest, methods, apis, outDir, extension, useSubPaths);
|
|
14
15
|
await api.disconnect();
|
|
15
16
|
}
|
|
16
|
-
export async function generateTypes(chain, metadata, rpcMethods, runtimeApis, outDir = '.') {
|
|
17
|
+
export async function generateTypes(chain, metadata, rpcMethods, runtimeApis, outDir = '.', extension = 'd.ts', useSubPaths = false) {
|
|
17
18
|
const dirPath = path.resolve(outDir, chain);
|
|
18
|
-
const defTypesFileName = path.join(dirPath, `types
|
|
19
|
-
const constsTypesFileName = path.join(dirPath, `consts
|
|
20
|
-
const queryTypesFileName = path.join(dirPath, `query
|
|
21
|
-
const
|
|
22
|
-
const indexFileName = path.join(dirPath, `index
|
|
23
|
-
const errorsFileName = path.join(dirPath, `errors
|
|
24
|
-
const eventsFileName = path.join(dirPath, `events
|
|
25
|
-
const runtimeApisFileName = path.join(dirPath, `runtime
|
|
26
|
-
const txFileName = path.join(dirPath, `tx
|
|
19
|
+
const defTypesFileName = path.join(dirPath, `types.${extension}`);
|
|
20
|
+
const constsTypesFileName = path.join(dirPath, `consts.${extension}`);
|
|
21
|
+
const queryTypesFileName = path.join(dirPath, `query.${extension}`);
|
|
22
|
+
const jsonRpcFileName = path.join(dirPath, `json-rpc.${extension}`);
|
|
23
|
+
const indexFileName = path.join(dirPath, `index.${extension}`);
|
|
24
|
+
const errorsFileName = path.join(dirPath, `errors.${extension}`);
|
|
25
|
+
const eventsFileName = path.join(dirPath, `events.${extension}`);
|
|
26
|
+
const runtimeApisFileName = path.join(dirPath, `runtime.${extension}`);
|
|
27
|
+
const txFileName = path.join(dirPath, `tx.${extension}`);
|
|
27
28
|
if (!fs.existsSync(dirPath)) {
|
|
28
29
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
29
30
|
}
|
|
30
31
|
const typesGen = new TypesGen(metadata);
|
|
31
32
|
const constsGen = new ConstsGen(typesGen);
|
|
32
33
|
const queryGen = new QueryGen(typesGen);
|
|
33
|
-
const
|
|
34
|
+
const jsonRpcGen = new JsonRpcGen(typesGen, rpcMethods);
|
|
34
35
|
const indexGen = new IndexGen(chain);
|
|
35
36
|
const errorsGen = new ErrorsGen(typesGen);
|
|
36
37
|
const eventsGen = new EventsGen(typesGen);
|
|
37
38
|
const runtimeApisGen = new RuntimeApisGen(typesGen, runtimeApis);
|
|
38
39
|
const txGen = new TxGen(typesGen);
|
|
39
|
-
fs.writeFileSync(defTypesFileName, await typesGen.generate());
|
|
40
|
-
fs.writeFileSync(errorsFileName, await errorsGen.generate());
|
|
41
|
-
fs.writeFileSync(eventsFileName, await eventsGen.generate());
|
|
42
|
-
fs.writeFileSync(
|
|
43
|
-
fs.writeFileSync(queryTypesFileName, await queryGen.generate());
|
|
44
|
-
fs.writeFileSync(constsTypesFileName, await constsGen.generate());
|
|
45
|
-
fs.writeFileSync(txFileName, await txGen.generate());
|
|
46
|
-
fs.writeFileSync(indexFileName, await indexGen.generate());
|
|
47
|
-
fs.writeFileSync(runtimeApisFileName, await runtimeApisGen.generate());
|
|
40
|
+
fs.writeFileSync(defTypesFileName, await typesGen.generate(useSubPaths));
|
|
41
|
+
fs.writeFileSync(errorsFileName, await errorsGen.generate(useSubPaths));
|
|
42
|
+
fs.writeFileSync(eventsFileName, await eventsGen.generate(useSubPaths));
|
|
43
|
+
fs.writeFileSync(jsonRpcFileName, await jsonRpcGen.generate(useSubPaths));
|
|
44
|
+
fs.writeFileSync(queryTypesFileName, await queryGen.generate(useSubPaths));
|
|
45
|
+
fs.writeFileSync(constsTypesFileName, await constsGen.generate(useSubPaths));
|
|
46
|
+
fs.writeFileSync(txFileName, await txGen.generate(useSubPaths));
|
|
47
|
+
fs.writeFileSync(indexFileName, await indexGen.generate(useSubPaths));
|
|
48
|
+
fs.writeFileSync(runtimeApisFileName, await runtimeApisGen.generate(useSubPaths));
|
|
48
49
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dedot/codegen",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.32",
|
|
4
4
|
"description": "Generate types",
|
|
5
5
|
"author": "Thang X. Vu <thang@coongcrafts.io>",
|
|
6
6
|
"homepage": "https://github.com/dedotdev/dedot/tree/main/packages/codegen",
|
|
@@ -18,32 +18,30 @@
|
|
|
18
18
|
"copy": "cp -R ./src/templates ./dist && cp -R ./src/templates ./dist/cjs"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@dedot/
|
|
22
|
-
"@dedot/
|
|
23
|
-
"@dedot/
|
|
24
|
-
"@dedot/
|
|
25
|
-
"dedot": "0.0.1-alpha.
|
|
26
|
-
"
|
|
27
|
-
"
|
|
21
|
+
"@dedot/api": "0.0.1-alpha.32",
|
|
22
|
+
"@dedot/codecs": "0.0.1-alpha.32",
|
|
23
|
+
"@dedot/providers": "0.0.1-alpha.32",
|
|
24
|
+
"@dedot/runtime-specs": "0.0.1-alpha.32",
|
|
25
|
+
"@dedot/shape": "0.0.1-alpha.32",
|
|
26
|
+
"@dedot/types": "0.0.1-alpha.32",
|
|
27
|
+
"@dedot/utils": "0.0.1-alpha.32",
|
|
28
|
+
"handlebars": "^4.7.7",
|
|
29
|
+
"prettier": "^3.2.5"
|
|
28
30
|
},
|
|
29
31
|
"publishConfig": {
|
|
30
32
|
"access": "public",
|
|
31
33
|
"directory": "dist"
|
|
32
34
|
},
|
|
33
35
|
"license": "Apache-2.0",
|
|
34
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "43501104eee360a19ae18a70fe506e16d9357835",
|
|
35
37
|
"module": "./index.js",
|
|
36
38
|
"types": "./index.d.ts",
|
|
37
39
|
"exports": {
|
|
38
40
|
".": {
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
"require": {
|
|
44
|
-
"types": "./index.d.ts",
|
|
45
|
-
"default": "./cjs/index.js"
|
|
46
|
-
}
|
|
41
|
+
"types": "./index.d.ts",
|
|
42
|
+
"import": "./index.js",
|
|
43
|
+
"require": "./cjs/index.js",
|
|
44
|
+
"default": "./index.js"
|
|
47
45
|
}
|
|
48
46
|
}
|
|
49
47
|
}
|
package/templates/consts.hbs
CHANGED
package/templates/errors.hbs
CHANGED
package/templates/events.hbs
CHANGED
package/templates/index.hbs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// Generated by @dedot/codegen
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
{{{importTypes}}}
|
|
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/templates/query.hbs
CHANGED
package/templates/runtime.hbs
CHANGED