@dedot/codegen 0.0.1-alpha.4 → 0.0.1-alpha.40
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/{generator → chaintypes/generator}/ApiGen.d.ts +15 -15
- package/chaintypes/generator/ConstsGen.d.ts +4 -0
- package/{generator → chaintypes/generator}/ConstsGen.js +8 -9
- package/chaintypes/generator/ErrorsGen.d.ts +5 -0
- package/{generator → chaintypes/generator}/ErrorsGen.js +11 -12
- package/chaintypes/generator/EventsGen.d.ts +5 -0
- package/{generator → chaintypes/generator}/EventsGen.js +11 -12
- package/chaintypes/generator/IndexGen.d.ts +5 -0
- package/chaintypes/generator/IndexGen.js +17 -0
- package/chaintypes/generator/JsonRpcGen.d.ts +8 -0
- package/chaintypes/generator/JsonRpcGen.js +55 -0
- package/chaintypes/generator/QueryGen.d.ts +5 -0
- package/chaintypes/generator/QueryGen.js +63 -0
- package/chaintypes/generator/RuntimeApisGen.d.ts +9 -0
- package/chaintypes/generator/RuntimeApisGen.js +189 -0
- package/chaintypes/generator/TxGen.d.ts +5 -0
- package/{generator → chaintypes/generator}/TxGen.js +18 -16
- package/chaintypes/generator/TypesGen.d.ts +11 -0
- package/chaintypes/generator/TypesGen.js +55 -0
- package/chaintypes/generator/index.d.ts +10 -0
- package/chaintypes/generator/index.js +10 -0
- package/chaintypes/index.d.ts +3 -0
- package/chaintypes/index.js +49 -0
- package/chaintypes/templates/consts.hbs +7 -0
- package/chaintypes/templates/errors.hbs +7 -0
- package/chaintypes/templates/events.hbs +7 -0
- package/chaintypes/templates/index.hbs +27 -0
- package/chaintypes/templates/json-rpc.hbs +5 -0
- package/chaintypes/templates/query.hbs +7 -0
- package/chaintypes/templates/runtime.hbs +8 -0
- package/chaintypes/templates/tx.hbs +9 -0
- package/chaintypes/templates/types.hbs +5 -0
- package/cjs/{generator → chaintypes/generator}/ConstsGen.js +12 -13
- package/cjs/chaintypes/generator/ErrorsGen.js +40 -0
- package/cjs/{generator → chaintypes/generator}/EventsGen.js +17 -18
- package/cjs/chaintypes/generator/IndexGen.js +21 -0
- package/cjs/chaintypes/generator/JsonRpcGen.js +59 -0
- package/cjs/chaintypes/generator/QueryGen.js +67 -0
- package/cjs/chaintypes/generator/RuntimeApisGen.js +193 -0
- package/cjs/{generator → chaintypes/generator}/TxGen.js +27 -25
- package/cjs/chaintypes/generator/TypesGen.js +59 -0
- package/cjs/chaintypes/generator/index.js +26 -0
- package/cjs/chaintypes/index.js +77 -0
- package/cjs/chaintypes/templates/consts.hbs +7 -0
- package/cjs/chaintypes/templates/errors.hbs +7 -0
- package/cjs/chaintypes/templates/events.hbs +7 -0
- package/cjs/chaintypes/templates/index.hbs +27 -0
- package/cjs/chaintypes/templates/json-rpc.hbs +5 -0
- package/cjs/chaintypes/templates/query.hbs +7 -0
- package/cjs/chaintypes/templates/runtime.hbs +8 -0
- package/cjs/chaintypes/templates/tx.hbs +9 -0
- package/cjs/chaintypes/templates/types.hbs +5 -0
- package/cjs/{types.js → dirname.js} +3 -0
- package/cjs/index.js +4 -62
- package/cjs/{generator/TypesGen.js → shared/BaseTypesGen.js} +123 -177
- package/cjs/{generator → shared}/TypeImports.js +29 -3
- package/cjs/{generator → shared}/index.js +3 -10
- package/cjs/shared/known-codecs.js +170 -0
- package/cjs/typink/generator/ConstructorQueryGen.js +24 -0
- package/cjs/typink/generator/ConstructorTxGen.js +24 -0
- package/cjs/typink/generator/IndexGen.js +24 -0
- package/cjs/typink/generator/QueryGen.js +72 -0
- package/cjs/typink/generator/TxGen.js +25 -0
- package/cjs/typink/generator/TypesGen.js +28 -0
- package/cjs/typink/generator/index.js +22 -0
- package/cjs/typink/index.js +37 -0
- package/cjs/typink/templates/constructor-query.hbs +7 -0
- package/cjs/typink/templates/constructor-tx.hbs +7 -0
- package/cjs/typink/templates/index.hbs +14 -0
- package/cjs/typink/templates/query.hbs +7 -0
- package/cjs/typink/templates/tx.hbs +7 -0
- package/cjs/typink/templates/types.hbs +5 -0
- package/cjs/{generator/utils.js → utils.js} +35 -11
- package/dirname.d.ts +1 -0
- package/dirname.js +6 -0
- package/index.d.ts +2 -4
- package/index.js +2 -48
- package/package.json +18 -19
- package/{generator/TypesGen.d.ts → shared/BaseTypesGen.d.ts} +13 -12
- package/{generator/TypesGen.js → shared/BaseTypesGen.js} +112 -166
- package/{generator → shared}/TypeImports.d.ts +12 -1
- package/{generator → shared}/TypeImports.js +29 -3
- package/shared/index.d.ts +3 -0
- package/shared/index.js +3 -0
- package/shared/known-codecs.d.ts +23 -0
- package/shared/known-codecs.js +139 -0
- package/typink/generator/ConstructorQueryGen.d.ts +6 -0
- package/typink/generator/ConstructorQueryGen.js +20 -0
- package/typink/generator/ConstructorTxGen.d.ts +6 -0
- package/typink/generator/ConstructorTxGen.js +20 -0
- package/typink/generator/IndexGen.d.ts +6 -0
- package/typink/generator/IndexGen.js +20 -0
- package/typink/generator/QueryGen.d.ts +12 -0
- package/typink/generator/QueryGen.js +68 -0
- package/typink/generator/TxGen.d.ts +6 -0
- package/typink/generator/TxGen.js +21 -0
- package/typink/generator/TypesGen.d.ts +7 -0
- package/typink/generator/TypesGen.js +24 -0
- package/typink/generator/index.d.ts +6 -0
- package/typink/generator/index.js +6 -0
- package/typink/index.d.ts +2 -0
- package/typink/index.js +30 -0
- package/typink/templates/constructor-query.hbs +7 -0
- package/typink/templates/constructor-tx.hbs +7 -0
- package/typink/templates/index.hbs +14 -0
- package/typink/templates/query.hbs +7 -0
- package/typink/templates/tx.hbs +7 -0
- package/typink/templates/types.hbs +5 -0
- package/{generator/utils.d.ts → utils.d.ts} +6 -2
- package/{generator/utils.js → utils.js} +33 -9
- package/cjs/genSupportedChainTypes.js +0 -79
- package/cjs/generator/ErrorsGen.js +0 -41
- package/cjs/generator/IndexGen.js +0 -18
- package/cjs/generator/QueryGen.js +0 -59
- package/cjs/generator/RpcGen.js +0 -175
- package/cjs/generator/RuntimeApisGen.js +0 -120
- package/cjs/packageInfo.js +0 -5
- package/genSupportedChainTypes.d.ts +0 -1
- package/genSupportedChainTypes.js +0 -74
- package/generator/ConstsGen.d.ts +0 -4
- package/generator/ErrorsGen.d.ts +0 -5
- package/generator/EventsGen.d.ts +0 -5
- package/generator/IndexGen.d.ts +0 -6
- package/generator/IndexGen.js +0 -14
- package/generator/QueryGen.d.ts +0 -5
- package/generator/QueryGen.js +0 -55
- package/generator/RpcGen.d.ts +0 -10
- package/generator/RpcGen.js +0 -171
- package/generator/RuntimeApisGen.d.ts +0 -9
- package/generator/RuntimeApisGen.js +0 -116
- package/generator/TxGen.d.ts +0 -5
- package/generator/index.d.ts +0 -10
- package/generator/index.js +0 -10
- package/packageInfo.d.ts +0 -4
- package/packageInfo.js +0 -2
- package/types.d.ts +0 -6
- package/types.js +0 -1
- /package/{generator → chaintypes/generator}/ApiGen.js +0 -0
- /package/cjs/{generator → chaintypes/generator}/ApiGen.js +0 -0
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.40",
|
|
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",
|
|
@@ -10,39 +10,38 @@
|
|
|
10
10
|
"url": "https://github.com/dedotdev/dedot.git"
|
|
11
11
|
},
|
|
12
12
|
"main": "./cjs/index.js",
|
|
13
|
+
"sideEffects": false,
|
|
13
14
|
"type": "module",
|
|
14
15
|
"scripts": {
|
|
15
|
-
"build": "tsc --project tsconfig.build.json && tsc --project tsconfig.build.cjs.json",
|
|
16
|
-
"clean": "rm -rf ./dist && rm -rf ./tsconfig.tsbuildinfo ./tsconfig.build.tsbuildinfo"
|
|
16
|
+
"build": "tsc --project tsconfig.build.json && tsc --project tsconfig.build.cjs.json && yarn copy",
|
|
17
|
+
"clean": "rm -rf ./dist && rm -rf ./tsconfig.tsbuildinfo ./tsconfig.build.tsbuildinfo",
|
|
18
|
+
"copy": "cp -R ./src/chaintypes/templates ./dist/chaintypes && cp -R ./src/chaintypes/templates ./dist/cjs/chaintypes && cp -R ./src/typink/templates ./dist/typink && cp -R ./src/typink/templates ./dist/cjs/typink"
|
|
17
19
|
},
|
|
18
20
|
"dependencies": {
|
|
19
|
-
"@dedot/
|
|
20
|
-
"@dedot/
|
|
21
|
-
"@dedot/
|
|
22
|
-
"@dedot/
|
|
23
|
-
"@
|
|
24
|
-
"dedot": "0.0.1-alpha.
|
|
21
|
+
"@dedot/api": "0.0.1-alpha.40",
|
|
22
|
+
"@dedot/codecs": "0.0.1-alpha.40",
|
|
23
|
+
"@dedot/providers": "0.0.1-alpha.40",
|
|
24
|
+
"@dedot/runtime-specs": "0.0.1-alpha.40",
|
|
25
|
+
"@dedot/shape": "0.0.1-alpha.40",
|
|
26
|
+
"@dedot/types": "0.0.1-alpha.40",
|
|
27
|
+
"@dedot/utils": "0.0.1-alpha.40",
|
|
25
28
|
"handlebars": "^4.7.8",
|
|
26
|
-
"prettier": "^3.
|
|
29
|
+
"prettier": "^3.3.2"
|
|
27
30
|
},
|
|
28
31
|
"publishConfig": {
|
|
29
32
|
"access": "public",
|
|
30
33
|
"directory": "dist"
|
|
31
34
|
},
|
|
32
35
|
"license": "Apache-2.0",
|
|
33
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "c6c3d9d748851f01ce79c13541754809be6628fc",
|
|
34
37
|
"module": "./index.js",
|
|
35
38
|
"types": "./index.d.ts",
|
|
36
39
|
"exports": {
|
|
37
40
|
".": {
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"require": {
|
|
43
|
-
"types": "./index.d.ts",
|
|
44
|
-
"default": "./cjs/index.js"
|
|
45
|
-
}
|
|
41
|
+
"types": "./index.d.ts",
|
|
42
|
+
"import": "./index.js",
|
|
43
|
+
"require": "./cjs/index.js",
|
|
44
|
+
"default": "./index.js"
|
|
46
45
|
}
|
|
47
46
|
}
|
|
48
47
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { Field, PortableType, TypeId } from '@dedot/codecs';
|
|
2
|
+
import { EnumOptions } from '@dedot/shape';
|
|
3
|
+
import { TypeImports } from './TypeImports.js';
|
|
4
|
+
export interface NamedType extends PortableType {
|
|
4
5
|
name: string;
|
|
5
6
|
nameOut: string;
|
|
6
7
|
skip?: boolean;
|
|
@@ -8,23 +9,23 @@ interface NamedType extends PortableType {
|
|
|
8
9
|
suffix?: string;
|
|
9
10
|
}
|
|
10
11
|
export declare const BASIC_KNOWN_TYPES: string[];
|
|
11
|
-
export declare class
|
|
12
|
+
export declare abstract class BaseTypesGen {
|
|
12
13
|
#private;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Types will be generated its definition out.
|
|
16
|
-
*/
|
|
14
|
+
types: PortableType[];
|
|
17
15
|
includedTypes: Record<TypeId, NamedType>;
|
|
18
|
-
registry: CodecRegistry;
|
|
19
16
|
typeImports: TypeImports;
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
skipTypes: string[];
|
|
18
|
+
protected constructor(types: PortableType[]);
|
|
19
|
+
shouldGenerateTypeIn(_id: TypeId): boolean;
|
|
22
20
|
typeCache: Record<string, string>;
|
|
23
21
|
clearCache(): void;
|
|
22
|
+
includeTypes(): Record<TypeId, NamedType>;
|
|
24
23
|
generateType(typeId: TypeId, nestedLevel?: number, typeOut?: boolean): string;
|
|
25
24
|
generateObjectType(fields: Field[], nestedLevel?: number, typeOut?: boolean): string;
|
|
25
|
+
cleanPath(path: string[]): string;
|
|
26
26
|
eqlCache: Map<string, boolean>;
|
|
27
27
|
typeEql(idA: number, idB: number, level?: number): boolean;
|
|
28
|
+
extractDupTypeSuffix(dupTypeId: TypeId, originalTypeId: TypeId, dupCount: number): string;
|
|
28
29
|
addTypeImport(typeName: string | string[]): void;
|
|
30
|
+
getEnumOptions(_typeId: TypeId): EnumOptions;
|
|
29
31
|
}
|
|
30
|
-
export {};
|
|
@@ -1,66 +1,93 @@
|
|
|
1
|
-
import { stringPascalCase } from '@
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { registry } from '@dedot/types';
|
|
6
|
-
import { TypeImports } from './TypeImports';
|
|
7
|
-
// Skip generate types for these
|
|
8
|
-
// as we do have native types for them
|
|
9
|
-
const SKIP_TYPES = [
|
|
10
|
-
'BoundedBTreeMap',
|
|
11
|
-
'BoundedBTreeSet',
|
|
12
|
-
'BoundedVec',
|
|
13
|
-
'Box',
|
|
14
|
-
'BTreeMap',
|
|
15
|
-
'BTreeSet',
|
|
16
|
-
'Cow',
|
|
17
|
-
'Option',
|
|
18
|
-
'Range',
|
|
19
|
-
'RangeInclusive',
|
|
20
|
-
'Result',
|
|
21
|
-
'WeakBoundedVec',
|
|
22
|
-
'WrapperKeepOpaque',
|
|
23
|
-
'WrapperOpaque',
|
|
24
|
-
];
|
|
1
|
+
import { normalizeName, stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import { commentBlock, isNativeType, WRAPPER_TYPE_REGEX } from '../utils.js';
|
|
3
|
+
import { TypeImports } from './TypeImports.js';
|
|
4
|
+
import { checkKnownCodecType, findKnownCodec, findKnownCodecType } from './known-codecs.js';
|
|
25
5
|
// These are common & generic types, so we'll remove these from all paths at index 1
|
|
26
6
|
// This helps make the type name shorter
|
|
27
7
|
const PATH_RM_INDEX_1 = ['generic', 'misc', 'pallet', 'traits', 'types'];
|
|
28
8
|
export const BASIC_KNOWN_TYPES = ['BitSequence', 'Bytes', 'BytesLike', 'FixedBytes', 'FixedArray', 'Result'];
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
metadata;
|
|
32
|
-
/**
|
|
33
|
-
* Types will be generated its definition out.
|
|
34
|
-
*/
|
|
9
|
+
export class BaseTypesGen {
|
|
10
|
+
types;
|
|
35
11
|
includedTypes;
|
|
36
|
-
registry;
|
|
37
12
|
typeImports;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
this.
|
|
41
|
-
this.includedTypes =
|
|
13
|
+
skipTypes;
|
|
14
|
+
constructor(types) {
|
|
15
|
+
this.types = types;
|
|
16
|
+
this.includedTypes = {};
|
|
42
17
|
this.typeImports = new TypeImports();
|
|
18
|
+
this.skipTypes = [];
|
|
43
19
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
let defTypeOut = '';
|
|
47
|
-
Object.values(this.includedTypes)
|
|
48
|
-
.filter(({ skip, knownType }) => !(skip || knownType))
|
|
49
|
-
.forEach(({ name, nameOut, id, docs }) => {
|
|
50
|
-
defTypeOut += `${commentBlock(docs)}export type ${nameOut} = ${this.generateType(id, 0, true)};\n\n`;
|
|
51
|
-
if (this.#shouldGenerateTypeIn(id)) {
|
|
52
|
-
defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
const importTypes = this.typeImports.toImports('./types');
|
|
56
|
-
const template = compileTemplate('types.hbs');
|
|
57
|
-
return beautifySourceCode(template({ importTypes, defTypeOut }));
|
|
20
|
+
shouldGenerateTypeIn(_id) {
|
|
21
|
+
return false;
|
|
58
22
|
}
|
|
59
23
|
typeCache = {};
|
|
60
24
|
clearCache() {
|
|
61
25
|
this.typeCache = {};
|
|
62
26
|
this.typeImports.clear();
|
|
63
27
|
}
|
|
28
|
+
includeTypes() {
|
|
29
|
+
const pathsCount = new Map();
|
|
30
|
+
const typesWithPath = this.types.filter((one) => one.path.length > 0);
|
|
31
|
+
const skipIds = [];
|
|
32
|
+
const typeSuffixes = new Map();
|
|
33
|
+
typesWithPath.forEach(({ path, id }) => {
|
|
34
|
+
const joinedPath = path.join('::');
|
|
35
|
+
if (pathsCount.has(joinedPath)) {
|
|
36
|
+
// We compare 2 types with the same path here,
|
|
37
|
+
// if they are the same type -> skip the current one, keep the first occurrence
|
|
38
|
+
// if they are not the same type but has the same path -> we'll try to calculate & add a suffix for the current type name
|
|
39
|
+
const firstOccurrenceTypeId = pathsCount.get(joinedPath)[0];
|
|
40
|
+
const sameType = this.typeEql(firstOccurrenceTypeId, id);
|
|
41
|
+
if (sameType) {
|
|
42
|
+
skipIds.push(id);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
pathsCount.get(joinedPath).push(id);
|
|
46
|
+
typeSuffixes.set(id, this.extractDupTypeSuffix(id, firstOccurrenceTypeId, pathsCount.get(joinedPath).length));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
pathsCount.set(joinedPath, [id]);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return typesWithPath.reduce((o, type) => {
|
|
54
|
+
const { path, id } = type;
|
|
55
|
+
const joinedPath = path.join('::');
|
|
56
|
+
if (this.skipTypes.includes(joinedPath) || this.skipTypes.includes(path.at(-1))) {
|
|
57
|
+
return o;
|
|
58
|
+
}
|
|
59
|
+
const suffix = typeSuffixes.get(id) || '';
|
|
60
|
+
let knownType = false;
|
|
61
|
+
let name, nameOut;
|
|
62
|
+
const [isKnownCodecType, codecName] = checkKnownCodecType(joinedPath);
|
|
63
|
+
if (isKnownCodecType) {
|
|
64
|
+
const codecType = findKnownCodecType(codecName);
|
|
65
|
+
name = codecType.typeIn;
|
|
66
|
+
nameOut = codecType.typeOut;
|
|
67
|
+
knownType = true;
|
|
68
|
+
}
|
|
69
|
+
else if (PATH_RM_INDEX_1.includes(path[1])) {
|
|
70
|
+
const newPath = path.slice();
|
|
71
|
+
newPath.splice(1, 1);
|
|
72
|
+
name = this.cleanPath(newPath);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
name = this.cleanPath(path);
|
|
76
|
+
}
|
|
77
|
+
if (this.shouldGenerateTypeIn(id)) {
|
|
78
|
+
nameOut = name;
|
|
79
|
+
name = name.endsWith('Like') ? name : `${name}Like`;
|
|
80
|
+
}
|
|
81
|
+
o[id] = {
|
|
82
|
+
name: `${name}${suffix}`,
|
|
83
|
+
nameOut: nameOut ? `${nameOut}${suffix}` : `${name}${suffix}`,
|
|
84
|
+
knownType,
|
|
85
|
+
skip: skipIds.includes(id),
|
|
86
|
+
...type,
|
|
87
|
+
};
|
|
88
|
+
return o;
|
|
89
|
+
}, {});
|
|
90
|
+
}
|
|
64
91
|
generateType(typeId, nestedLevel = 0, typeOut = false) {
|
|
65
92
|
if (nestedLevel > 0) {
|
|
66
93
|
const includedDef = this.includedTypes[typeId];
|
|
@@ -91,15 +118,15 @@ export class TypesGen {
|
|
|
91
118
|
return type;
|
|
92
119
|
}
|
|
93
120
|
#generateType(typeId, nestedLevel = 0, typeOut = false) {
|
|
94
|
-
const def = this.
|
|
121
|
+
const def = this.types[typeId];
|
|
95
122
|
if (!def) {
|
|
96
123
|
throw new Error(`Type def not found ${JSON.stringify(def)}`);
|
|
97
124
|
}
|
|
98
|
-
const {
|
|
99
|
-
const {
|
|
100
|
-
switch (
|
|
125
|
+
const { typeDef, path, docs } = def;
|
|
126
|
+
const { type, value } = typeDef;
|
|
127
|
+
switch (type) {
|
|
101
128
|
case 'Primitive':
|
|
102
|
-
const $codec =
|
|
129
|
+
const $codec = findKnownCodec(value.kind);
|
|
103
130
|
if ($codec.nativeType) {
|
|
104
131
|
return $codec.nativeType;
|
|
105
132
|
}
|
|
@@ -168,14 +195,14 @@ export class TypesGen {
|
|
|
168
195
|
membersType.push([keyName, this.generateObjectType(fields, nestedLevel + 1, typeOut), docs]);
|
|
169
196
|
}
|
|
170
197
|
}
|
|
171
|
-
const { tagKey, valueKey } = this.
|
|
198
|
+
const { tagKey, valueKey } = this.getEnumOptions(typeId);
|
|
172
199
|
return membersType
|
|
173
200
|
.map(([keyName, valueType, docs]) => ({
|
|
174
|
-
|
|
201
|
+
type: `${tagKey}: '${keyName}'`,
|
|
175
202
|
value: valueType ? `, ${valueKey}${this.#isOptionalType(valueType) ? '?' : ''}: ${valueType} ` : '',
|
|
176
203
|
docs,
|
|
177
204
|
}))
|
|
178
|
-
.map(({
|
|
205
|
+
.map(({ type, value, docs }) => `${commentBlock(docs)}{ ${type}${value} }`)
|
|
179
206
|
.join(' | ');
|
|
180
207
|
}
|
|
181
208
|
}
|
|
@@ -197,9 +224,9 @@ export class TypesGen {
|
|
|
197
224
|
return this.generateType(value.typeParam, nestedLevel + 1, typeOut);
|
|
198
225
|
case 'Sequence':
|
|
199
226
|
case 'SizedVec': {
|
|
200
|
-
const fixedSize =
|
|
201
|
-
const $innerType = this.
|
|
202
|
-
if ($innerType.
|
|
227
|
+
const fixedSize = type === 'SizedVec' ? `${value.len}` : null;
|
|
228
|
+
const $innerType = this.types[value.typeParam].typeDef;
|
|
229
|
+
if ($innerType.type === 'Primitive' && $innerType.value.kind === 'u8') {
|
|
203
230
|
return fixedSize ? `FixedBytes<${fixedSize}>` : typeOut ? 'Bytes' : 'BytesLike';
|
|
204
231
|
}
|
|
205
232
|
else {
|
|
@@ -208,7 +235,7 @@ export class TypesGen {
|
|
|
208
235
|
}
|
|
209
236
|
}
|
|
210
237
|
default:
|
|
211
|
-
throw new Error(`Invalid type! ${
|
|
238
|
+
throw new Error(`Invalid type! ${type}`);
|
|
212
239
|
}
|
|
213
240
|
}
|
|
214
241
|
generateObjectType(fields, nestedLevel = 0, typeOut = false) {
|
|
@@ -228,69 +255,6 @@ export class TypesGen {
|
|
|
228
255
|
#isOptionalType(type) {
|
|
229
256
|
return type.endsWith('| undefined');
|
|
230
257
|
}
|
|
231
|
-
#includedTypes() {
|
|
232
|
-
const { types } = this.metadata;
|
|
233
|
-
const pathsCount = new Map();
|
|
234
|
-
const typesWithPath = types.filter((one) => one.path.length > 0);
|
|
235
|
-
const skipIds = [];
|
|
236
|
-
const typeSuffixes = new Map();
|
|
237
|
-
typesWithPath.forEach(({ path, id }) => {
|
|
238
|
-
const joinedPath = path.join('::');
|
|
239
|
-
if (pathsCount.has(joinedPath)) {
|
|
240
|
-
// We compare 2 types with the same path here,
|
|
241
|
-
// if they are the same type -> skip the current one, keep the first occurrence
|
|
242
|
-
// if they are not the same type but has the same path -> we'll try to calculate & add a suffix for the current type name
|
|
243
|
-
const firstOccurrenceTypeId = pathsCount.get(joinedPath)[0];
|
|
244
|
-
const sameType = this.typeEql(firstOccurrenceTypeId, id);
|
|
245
|
-
if (sameType) {
|
|
246
|
-
skipIds.push(id);
|
|
247
|
-
}
|
|
248
|
-
else {
|
|
249
|
-
pathsCount.get(joinedPath).push(id);
|
|
250
|
-
typeSuffixes.set(id, this.#extractDupTypeSuffix(id, firstOccurrenceTypeId, pathsCount.get(joinedPath).length));
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
else {
|
|
254
|
-
pathsCount.set(joinedPath, [id]);
|
|
255
|
-
}
|
|
256
|
-
});
|
|
257
|
-
return typesWithPath.reduce((o, type) => {
|
|
258
|
-
const { path, id } = type;
|
|
259
|
-
const joinedPath = path.join('::');
|
|
260
|
-
if (SKIP_TYPES.includes(joinedPath) || SKIP_TYPES.includes(path.at(-1))) {
|
|
261
|
-
return o;
|
|
262
|
-
}
|
|
263
|
-
const suffix = typeSuffixes.get(id) || '';
|
|
264
|
-
let knownType = false;
|
|
265
|
-
let name, nameOut;
|
|
266
|
-
if (this.registry.isKnownType(joinedPath)) {
|
|
267
|
-
const codecType = this.registry.findCodecType(path.at(-1));
|
|
268
|
-
name = codecType.typeIn;
|
|
269
|
-
nameOut = codecType.typeOut;
|
|
270
|
-
knownType = true;
|
|
271
|
-
}
|
|
272
|
-
else if (PATH_RM_INDEX_1.includes(path[1])) {
|
|
273
|
-
const newPath = path.slice();
|
|
274
|
-
newPath.splice(1, 1);
|
|
275
|
-
name = this.#cleanPath(newPath);
|
|
276
|
-
}
|
|
277
|
-
else {
|
|
278
|
-
name = this.#cleanPath(path);
|
|
279
|
-
}
|
|
280
|
-
if (this.#shouldGenerateTypeIn(id)) {
|
|
281
|
-
nameOut = name;
|
|
282
|
-
name = name.endsWith('Like') ? name : `${name}Like`;
|
|
283
|
-
}
|
|
284
|
-
o[id] = {
|
|
285
|
-
name: `${name}${suffix}`,
|
|
286
|
-
nameOut: nameOut ? `${nameOut}${suffix}` : `${name}${suffix}`,
|
|
287
|
-
knownType,
|
|
288
|
-
skip: skipIds.includes(id),
|
|
289
|
-
...type,
|
|
290
|
-
};
|
|
291
|
-
return o;
|
|
292
|
-
}, {});
|
|
293
|
-
}
|
|
294
258
|
#removeGenericPart(typeName) {
|
|
295
259
|
if (typeName.match(WRAPPER_TYPE_REGEX)) {
|
|
296
260
|
return typeName.replace(WRAPPER_TYPE_REGEX, (_, $1) => $1);
|
|
@@ -299,27 +263,12 @@ export class TypesGen {
|
|
|
299
263
|
return typeName;
|
|
300
264
|
}
|
|
301
265
|
}
|
|
302
|
-
|
|
303
|
-
* @description Remove duplicated part of the path
|
|
304
|
-
*
|
|
305
|
-
* Example:
|
|
306
|
-
* ["pallet_staking", "pallet", "pallet", "Event"]
|
|
307
|
-
* => ["pallet_staking", "pallet", "Event"]
|
|
308
|
-
*
|
|
309
|
-
* @param path
|
|
310
|
-
* @private
|
|
311
|
-
*/
|
|
312
|
-
#cleanPath(path) {
|
|
266
|
+
cleanPath(path) {
|
|
313
267
|
return path
|
|
314
268
|
.map((one) => stringPascalCase(one))
|
|
315
269
|
.filter((one, idx, currentPath) => idx === 0 || one !== currentPath[idx - 1])
|
|
316
270
|
.join('');
|
|
317
271
|
}
|
|
318
|
-
#shouldGenerateTypeIn(id) {
|
|
319
|
-
const { callTypeId } = this.metadata.extrinsic;
|
|
320
|
-
const palletCallTypeIds = this.registry.portableRegistry.getPalletCallTypeIds();
|
|
321
|
-
return callTypeId === id || palletCallTypeIds.includes(id);
|
|
322
|
-
}
|
|
323
272
|
eqlCache = new Map();
|
|
324
273
|
typeEql(idA, idB, level = 0) {
|
|
325
274
|
const cacheKey = `${idA}==${idB}`;
|
|
@@ -332,36 +281,36 @@ export class TypesGen {
|
|
|
332
281
|
#typeEql(idA, idB, lvl = 0) {
|
|
333
282
|
if (idA === idB)
|
|
334
283
|
return true;
|
|
335
|
-
const
|
|
336
|
-
const
|
|
337
|
-
const typeB = types[idB];
|
|
284
|
+
const typeA = this.types[idA];
|
|
285
|
+
const typeB = this.types[idB];
|
|
338
286
|
if (typeA.path.join('::') !== typeB.path.join('::'))
|
|
339
287
|
return false;
|
|
340
|
-
const {
|
|
341
|
-
const {
|
|
288
|
+
const { typeDef: defA, params: paramsA } = typeA;
|
|
289
|
+
const { typeDef: defB, params: paramsB } = typeB;
|
|
342
290
|
if (!this.#eqlArray(paramsA, paramsB, (valA, valB) => this.#eqlTypeParam(valA, valB))) {
|
|
343
291
|
return false;
|
|
344
292
|
}
|
|
345
|
-
if (defA.
|
|
293
|
+
if (defA.type !== defB.type)
|
|
346
294
|
return false;
|
|
347
|
-
if (defA.
|
|
295
|
+
if (defA.type === 'BitSequence')
|
|
348
296
|
return true;
|
|
349
|
-
if (defA.
|
|
297
|
+
if (defA.type === 'Primitive' && defB.type === 'Primitive') {
|
|
350
298
|
return defA.value.kind === defB.value.kind;
|
|
351
299
|
}
|
|
352
|
-
if ((defA.
|
|
300
|
+
if ((defA.type === 'Compact' && defB.type === 'Compact') ||
|
|
301
|
+
(defA.type === 'Sequence' && defB.type === 'Sequence')) {
|
|
353
302
|
return this.typeEql(defA.value.typeParam, defB.value.typeParam, lvl + 1);
|
|
354
303
|
}
|
|
355
|
-
if (defA.
|
|
304
|
+
if (defA.type === 'SizedVec' && defB.type === 'SizedVec') {
|
|
356
305
|
return defA.value.len === defB.value.len && this.typeEql(defA.value.typeParam, defB.value.typeParam, lvl + 1);
|
|
357
306
|
}
|
|
358
|
-
if (defA.
|
|
307
|
+
if (defA.type === 'Tuple' && defB.type === 'Tuple') {
|
|
359
308
|
return this.#eqlArray(defA.value.fields, defB.value.fields, (val1, val2) => this.typeEql(val1, val2, lvl + 1));
|
|
360
309
|
}
|
|
361
|
-
if (defA.
|
|
310
|
+
if (defA.type === 'Struct' && defB.type === 'Struct') {
|
|
362
311
|
return this.#eqlFields(defA.value.fields, defB.value.fields, lvl);
|
|
363
312
|
}
|
|
364
|
-
if (defA.
|
|
313
|
+
if (defA.type === 'Enum' && defB.type === 'Enum') {
|
|
365
314
|
return this.#eqlArray(defA.value.members, defB.value.members, (val1, val2) => val1.name === val2.name && val1.index === val2.index && this.#eqlFields(val1.fields, val2.fields, lvl));
|
|
366
315
|
}
|
|
367
316
|
return false;
|
|
@@ -377,19 +326,18 @@ export class TypesGen {
|
|
|
377
326
|
(param1.typeId === undefined) === (param2.typeId === undefined) &&
|
|
378
327
|
(param1.typeId === undefined || this.#typeEql(param1.typeId, param2.typeId)));
|
|
379
328
|
}
|
|
380
|
-
|
|
381
|
-
const
|
|
382
|
-
const
|
|
383
|
-
const dupTypeParams = types[dupTypeId].params;
|
|
329
|
+
extractDupTypeSuffix(dupTypeId, originalTypeId, dupCount) {
|
|
330
|
+
const originalTypeParams = this.types[originalTypeId].params;
|
|
331
|
+
const dupTypeParams = this.types[dupTypeId].params;
|
|
384
332
|
const diffParam = dupTypeParams.find((one, idx) => !this.#eqlTypeParam(one, originalTypeParams[idx]));
|
|
385
333
|
// TODO make sure these suffix is unique if a type is duplicated more than 2 times
|
|
386
334
|
if (diffParam?.typeId) {
|
|
387
|
-
const diffType = types[diffParam.typeId];
|
|
335
|
+
const diffType = this.types[diffParam.typeId];
|
|
388
336
|
if (diffType.path.length > 0) {
|
|
389
337
|
return stringPascalCase(diffType.path.at(-1));
|
|
390
338
|
}
|
|
391
|
-
else if (diffType.type
|
|
392
|
-
return stringPascalCase(diffType.
|
|
339
|
+
else if (diffType.typeDef.type === 'Primitive') {
|
|
340
|
+
return stringPascalCase(diffType.typeDef.value.kind);
|
|
393
341
|
}
|
|
394
342
|
}
|
|
395
343
|
// Last resort!
|
|
@@ -422,11 +370,9 @@ export class TypesGen {
|
|
|
422
370
|
this.typeImports.addCodecType(typeName);
|
|
423
371
|
return;
|
|
424
372
|
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
this.typeImports.addOutType(typeName);
|
|
430
|
-
}
|
|
373
|
+
this.typeImports.addOutType(typeName);
|
|
374
|
+
}
|
|
375
|
+
getEnumOptions(_typeId) {
|
|
376
|
+
return { tagKey: 'type', valueKey: 'value' };
|
|
431
377
|
}
|
|
432
378
|
}
|
|
@@ -1,14 +1,25 @@
|
|
|
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>;
|
|
12
|
+
contractTypes: Set<string>;
|
|
13
|
+
chainTypes: Set<string>;
|
|
7
14
|
constructor();
|
|
8
15
|
clear(): void;
|
|
9
|
-
toImports(
|
|
16
|
+
toImports(config?: ImportConfig): string;
|
|
10
17
|
addPortableType(...types: string[]): void;
|
|
11
18
|
addCodecType(...types: string[]): void;
|
|
12
19
|
addKnownType(...types: string[]): void;
|
|
20
|
+
addKnownJsonRpcType(...types: string[]): void;
|
|
13
21
|
addOutType(...types: string[]): void;
|
|
22
|
+
addContractType(...types: string[]): void;
|
|
23
|
+
addChainType(...types: string[]): void;
|
|
14
24
|
}
|
|
25
|
+
export {};
|
|
@@ -5,30 +5,47 @@ 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;
|
|
11
|
+
// Know types defined in @dedot/contracts
|
|
12
|
+
contractTypes;
|
|
13
|
+
// Know types defined in @dedot/api/chaintypes or dedot/chaintypes
|
|
14
|
+
chainTypes;
|
|
10
15
|
constructor() {
|
|
11
16
|
this.portableTypes = new Set();
|
|
12
17
|
this.codecTypes = new Set();
|
|
13
18
|
this.knownTypes = new Set();
|
|
19
|
+
this.knownJsonRpcTypes = new Set();
|
|
14
20
|
this.outTypes = new Set();
|
|
21
|
+
this.contractTypes = new Set();
|
|
22
|
+
this.chainTypes = new Set();
|
|
15
23
|
}
|
|
16
24
|
clear() {
|
|
17
25
|
this.portableTypes.clear();
|
|
18
26
|
this.codecTypes.clear();
|
|
19
27
|
this.knownTypes.clear();
|
|
28
|
+
this.knownJsonRpcTypes.clear();
|
|
20
29
|
this.outTypes.clear();
|
|
30
|
+
this.contractTypes.clear();
|
|
31
|
+
this.chainTypes.clear();
|
|
21
32
|
}
|
|
22
|
-
toImports(
|
|
33
|
+
toImports(config) {
|
|
34
|
+
const { excludeModules = [], useSubPaths = false } = config || {};
|
|
23
35
|
// TODO generate outTypes!
|
|
36
|
+
const prefix = useSubPaths ? '' : '@';
|
|
24
37
|
const toImports = [
|
|
25
|
-
[this.knownTypes,
|
|
26
|
-
[this.
|
|
38
|
+
[this.knownTypes, `${prefix}dedot/types`],
|
|
39
|
+
[this.knownJsonRpcTypes, `${prefix}dedot/types/json-rpc`],
|
|
40
|
+
[this.codecTypes, `${prefix}dedot/codecs`],
|
|
41
|
+
[this.contractTypes, `${prefix}dedot/contracts`],
|
|
42
|
+
[this.chainTypes, prefix ? '@dedot/api/chaintypes' : 'dedot/chaintypes'],
|
|
27
43
|
[this.portableTypes, './types'],
|
|
28
44
|
];
|
|
29
45
|
return toImports
|
|
30
46
|
.filter(([_, module]) => !excludeModules.includes(module))
|
|
31
47
|
.map(([types, module]) => this.#toImportLine(types, module))
|
|
48
|
+
.filter((line) => line.length > 0)
|
|
32
49
|
.join('\n');
|
|
33
50
|
}
|
|
34
51
|
#toImportLine(types, module) {
|
|
@@ -46,7 +63,16 @@ export class TypeImports {
|
|
|
46
63
|
addKnownType(...types) {
|
|
47
64
|
types.forEach((one) => this.knownTypes.add(one));
|
|
48
65
|
}
|
|
66
|
+
addKnownJsonRpcType(...types) {
|
|
67
|
+
types.forEach((one) => this.knownJsonRpcTypes.add(one));
|
|
68
|
+
}
|
|
49
69
|
addOutType(...types) {
|
|
50
70
|
types.forEach((one) => this.outTypes.add(one));
|
|
51
71
|
}
|
|
72
|
+
addContractType(...types) {
|
|
73
|
+
types.forEach((one) => this.contractTypes.add(one));
|
|
74
|
+
}
|
|
75
|
+
addChainType(...types) {
|
|
76
|
+
types.forEach((one) => this.chainTypes.add(one));
|
|
77
|
+
}
|
|
52
78
|
}
|
package/shared/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as $ from '@dedot/shape';
|
|
2
|
+
import { AnyShape } from '@dedot/shape';
|
|
3
|
+
export type CodecName = `$${string}`;
|
|
4
|
+
export interface CodecType {
|
|
5
|
+
name: CodecName;
|
|
6
|
+
$codec: AnyShape;
|
|
7
|
+
typeIn: string;
|
|
8
|
+
typeOut: string;
|
|
9
|
+
}
|
|
10
|
+
export declare const normalizeCodecName: (name: string | CodecName) => CodecName;
|
|
11
|
+
/**
|
|
12
|
+
* Collection of codec types with loose input types
|
|
13
|
+
*
|
|
14
|
+
* Loose codecs are codecs with different typeIn & typeOut,
|
|
15
|
+
* E.g: Codec `$AccountId32`, we have its typeIn is `AccountId32Like` & typeOut is `AccountId32`
|
|
16
|
+
*
|
|
17
|
+
* This registry keep track the list of codecs which follow this convention
|
|
18
|
+
*/
|
|
19
|
+
export declare const looseTypeCodecs: Record<string, AnyShape>;
|
|
20
|
+
export declare function findKnownCodecType(name: string): CodecType;
|
|
21
|
+
export declare function findKnownCodec<I = unknown, O = I>(typeName: string): $.Shape<I, O>;
|
|
22
|
+
export declare function findKnownWrapperCodec(typeName: string): $.AnyShape | undefined;
|
|
23
|
+
export declare function checkKnownCodecType(path: string | string[]): [boolean, string];
|