@dedot/codegen 0.0.1-alpha.22
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/LICENSE +201 -0
- package/README.md +3 -0
- package/cjs/genSupportedChainTypes.js +76 -0
- package/cjs/generator/ApiGen.js +16 -0
- package/cjs/generator/ConstsGen.js +32 -0
- package/cjs/generator/ErrorsGen.js +41 -0
- package/cjs/generator/EventsGen.js +68 -0
- package/cjs/generator/IndexGen.js +18 -0
- package/cjs/generator/QueryGen.js +59 -0
- package/cjs/generator/RpcGen.js +175 -0
- package/cjs/generator/RuntimeApisGen.js +120 -0
- package/cjs/generator/TxGen.js +90 -0
- package/cjs/generator/TypeImports.js +56 -0
- package/cjs/generator/TypesGen.js +436 -0
- package/cjs/generator/index.js +26 -0
- package/cjs/generator/utils.js +47 -0
- package/cjs/index.js +50 -0
- package/cjs/package.json +1 -0
- package/cjs/packageInfo.js +5 -0
- package/cjs/types.js +2 -0
- package/genSupportedChainTypes.d.ts +1 -0
- package/genSupportedChainTypes.js +74 -0
- package/generator/ApiGen.d.ts +138 -0
- package/generator/ApiGen.js +12 -0
- package/generator/ConstsGen.d.ts +4 -0
- package/generator/ConstsGen.js +28 -0
- package/generator/ErrorsGen.d.ts +5 -0
- package/generator/ErrorsGen.js +37 -0
- package/generator/EventsGen.d.ts +5 -0
- package/generator/EventsGen.js +64 -0
- package/generator/IndexGen.d.ts +6 -0
- package/generator/IndexGen.js +14 -0
- package/generator/QueryGen.d.ts +5 -0
- package/generator/QueryGen.js +55 -0
- package/generator/RpcGen.d.ts +10 -0
- package/generator/RpcGen.js +171 -0
- package/generator/RuntimeApisGen.d.ts +9 -0
- package/generator/RuntimeApisGen.js +116 -0
- package/generator/TxGen.d.ts +5 -0
- package/generator/TxGen.js +86 -0
- package/generator/TypeImports.d.ts +14 -0
- package/generator/TypeImports.js +52 -0
- package/generator/TypesGen.d.ts +30 -0
- package/generator/TypesGen.js +432 -0
- package/generator/index.d.ts +10 -0
- package/generator/index.js +10 -0
- package/generator/utils.d.ts +11 -0
- package/generator/utils.js +39 -0
- package/index.d.ts +4 -0
- package/index.js +45 -0
- package/package.json +48 -0
- package/packageInfo.d.ts +4 -0
- package/packageInfo.js +3 -0
- package/types.d.ts +6 -0
- package/types.js +1 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { TypesGen } from '../generator';
|
|
2
|
+
export declare abstract class ApiGen {
|
|
3
|
+
readonly typesGen: TypesGen;
|
|
4
|
+
constructor(typesGen: TypesGen);
|
|
5
|
+
get metadata(): {
|
|
6
|
+
types: {
|
|
7
|
+
id: number;
|
|
8
|
+
path: string[];
|
|
9
|
+
params: {
|
|
10
|
+
name: string;
|
|
11
|
+
typeId: number | undefined;
|
|
12
|
+
}[];
|
|
13
|
+
type: {
|
|
14
|
+
tag: "Struct";
|
|
15
|
+
value: {
|
|
16
|
+
fields: import("@dedot/codecs").Field[];
|
|
17
|
+
};
|
|
18
|
+
} | {
|
|
19
|
+
tag: "Enum";
|
|
20
|
+
value: {
|
|
21
|
+
members: {
|
|
22
|
+
name: string;
|
|
23
|
+
fields: import("@dedot/codecs").Field[];
|
|
24
|
+
index: number;
|
|
25
|
+
docs: string[];
|
|
26
|
+
}[];
|
|
27
|
+
};
|
|
28
|
+
} | {
|
|
29
|
+
tag: "Sequence";
|
|
30
|
+
value: {
|
|
31
|
+
typeParam: number;
|
|
32
|
+
};
|
|
33
|
+
} | {
|
|
34
|
+
tag: "SizedVec";
|
|
35
|
+
value: {
|
|
36
|
+
len: number;
|
|
37
|
+
typeParam: number;
|
|
38
|
+
};
|
|
39
|
+
} | {
|
|
40
|
+
tag: "Tuple";
|
|
41
|
+
value: {
|
|
42
|
+
fields: number[];
|
|
43
|
+
};
|
|
44
|
+
} | {
|
|
45
|
+
tag: "Primitive";
|
|
46
|
+
value: {
|
|
47
|
+
kind: "bool" | "char" | "str" | "u8" | "u16" | "u32" | "u64" | "u128" | "u256" | "i8" | "i16" | "i32" | "i64" | "i128" | "i256";
|
|
48
|
+
};
|
|
49
|
+
} | {
|
|
50
|
+
tag: "Compact";
|
|
51
|
+
value: {
|
|
52
|
+
typeParam: number;
|
|
53
|
+
};
|
|
54
|
+
} | {
|
|
55
|
+
tag: "BitSequence";
|
|
56
|
+
value: {
|
|
57
|
+
bitOrderType: number;
|
|
58
|
+
bitStoreType: number;
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
docs: string[];
|
|
62
|
+
}[];
|
|
63
|
+
pallets: {
|
|
64
|
+
name: string;
|
|
65
|
+
storage: {
|
|
66
|
+
prefix: string;
|
|
67
|
+
entries: {
|
|
68
|
+
name: string;
|
|
69
|
+
modifier: string;
|
|
70
|
+
type: {
|
|
71
|
+
tag: "Plain";
|
|
72
|
+
value: {
|
|
73
|
+
valueTypeId: number;
|
|
74
|
+
};
|
|
75
|
+
} | {
|
|
76
|
+
tag: "Map";
|
|
77
|
+
value: {
|
|
78
|
+
hashers: ("identity" | "blake2_128" | "blake2_256" | "blake2_128Concat" | "twox128" | "twox256" | "twox64Concat")[];
|
|
79
|
+
keyTypeId: number;
|
|
80
|
+
valueTypeId: number;
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
default: `0x${string}`;
|
|
84
|
+
docs: string[];
|
|
85
|
+
}[];
|
|
86
|
+
} | undefined;
|
|
87
|
+
calls: number | undefined;
|
|
88
|
+
event: number | undefined;
|
|
89
|
+
constants: {
|
|
90
|
+
name: string;
|
|
91
|
+
typeId: number;
|
|
92
|
+
value: `0x${string}`;
|
|
93
|
+
docs: string[];
|
|
94
|
+
}[];
|
|
95
|
+
error: number | undefined;
|
|
96
|
+
index: number;
|
|
97
|
+
docs: string[];
|
|
98
|
+
}[];
|
|
99
|
+
extrinsic: {
|
|
100
|
+
version: number;
|
|
101
|
+
addressTypeId: number;
|
|
102
|
+
callTypeId: number;
|
|
103
|
+
signatureTypeId: number;
|
|
104
|
+
extraTypeId: number;
|
|
105
|
+
signedExtensions: {
|
|
106
|
+
ident: string;
|
|
107
|
+
typeId: number;
|
|
108
|
+
additionalSigned: number;
|
|
109
|
+
}[];
|
|
110
|
+
};
|
|
111
|
+
runtimeType: number;
|
|
112
|
+
apis: {
|
|
113
|
+
name: string;
|
|
114
|
+
methods: {
|
|
115
|
+
name: string;
|
|
116
|
+
inputs: {
|
|
117
|
+
name: string;
|
|
118
|
+
typeId: number;
|
|
119
|
+
}[];
|
|
120
|
+
output: number;
|
|
121
|
+
docs: string[];
|
|
122
|
+
}[];
|
|
123
|
+
docs: string[];
|
|
124
|
+
}[];
|
|
125
|
+
outerEnums: {
|
|
126
|
+
callEnumTypeId: number;
|
|
127
|
+
eventEnumTypeId: number;
|
|
128
|
+
errorEnumTypeId: number;
|
|
129
|
+
};
|
|
130
|
+
custom: {
|
|
131
|
+
map: ReadonlyMap<string, {
|
|
132
|
+
typeId: number;
|
|
133
|
+
value: `0x${string}`;
|
|
134
|
+
}>;
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
get registry(): import("@dedot/codecs").CodecRegistry;
|
|
138
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { stringLowerFirst } from '@polkadot/util';
|
|
2
|
+
import { normalizeName } from '@dedot/utils';
|
|
3
|
+
import { ApiGen } from '../generator';
|
|
4
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
|
|
5
|
+
export class ConstsGen extends ApiGen {
|
|
6
|
+
generate() {
|
|
7
|
+
const { pallets } = this.metadata;
|
|
8
|
+
this.typesGen.clearCache();
|
|
9
|
+
this.typesGen.typeImports.addKnownType('GenericChainConsts');
|
|
10
|
+
let defTypeOut = '';
|
|
11
|
+
for (let pallet of pallets) {
|
|
12
|
+
const typedConstants = pallet.constants.map((one) => ({
|
|
13
|
+
name: normalizeName(one.name),
|
|
14
|
+
type: this.typesGen.generateType(one.typeId, 1, true),
|
|
15
|
+
docs: one.docs,
|
|
16
|
+
}));
|
|
17
|
+
defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s constants`);
|
|
18
|
+
defTypeOut += `${stringLowerFirst(pallet.name)}: {
|
|
19
|
+
${typedConstants.map(({ name, type, docs }) => `${commentBlock(docs)}${name}: ${type}`).join(',\n')}
|
|
20
|
+
|
|
21
|
+
${commentBlock('Generic pallet constant')}[name: string]: any,
|
|
22
|
+
},`;
|
|
23
|
+
}
|
|
24
|
+
const importTypes = this.typesGen.typeImports.toImports();
|
|
25
|
+
const template = compileTemplate('consts.hbs');
|
|
26
|
+
return beautifySourceCode(template({ importTypes, defTypeOut }));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ApiGen } from './ApiGen';
|
|
2
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
|
|
3
|
+
import { stringCamelCase, stringPascalCase } from '@polkadot/util';
|
|
4
|
+
import { assert } from '@dedot/utils';
|
|
5
|
+
export class ErrorsGen extends ApiGen {
|
|
6
|
+
generate() {
|
|
7
|
+
const { pallets } = this.metadata;
|
|
8
|
+
this.typesGen.clearCache();
|
|
9
|
+
this.typesGen.typeImports.addKnownType('GenericChainErrors', 'GenericPalletError');
|
|
10
|
+
let defTypeOut = '';
|
|
11
|
+
for (let pallet of pallets) {
|
|
12
|
+
const errorTypeId = pallet.error;
|
|
13
|
+
if (!errorTypeId) {
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
const errorDefs = this.#getErrorDefs(errorTypeId);
|
|
17
|
+
defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s errors`);
|
|
18
|
+
defTypeOut += `${stringCamelCase(pallet.name)}: {
|
|
19
|
+
${errorDefs
|
|
20
|
+
.map(({ name, docs }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletError`)
|
|
21
|
+
.join(',\n')}
|
|
22
|
+
|
|
23
|
+
${commentBlock('Generic pallet error')}[error: string]: GenericPalletError,
|
|
24
|
+
},`;
|
|
25
|
+
}
|
|
26
|
+
const importTypes = this.typesGen.typeImports.toImports();
|
|
27
|
+
const template = compileTemplate('errors.hbs');
|
|
28
|
+
return beautifySourceCode(template({ importTypes, defTypeOut }));
|
|
29
|
+
}
|
|
30
|
+
#getErrorDefs(errorTypeId) {
|
|
31
|
+
const def = this.metadata.types[errorTypeId];
|
|
32
|
+
assert(def, `Error def not found for id ${errorTypeId}`);
|
|
33
|
+
const { tag, value } = def.type;
|
|
34
|
+
assert(tag === 'Enum', `Invalid pallet error type!`);
|
|
35
|
+
return value.members;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { ApiGen } from './ApiGen';
|
|
2
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
|
|
3
|
+
import { stringCamelCase, stringPascalCase } from '@polkadot/util';
|
|
4
|
+
import { assert } from '@dedot/utils';
|
|
5
|
+
export class EventsGen extends ApiGen {
|
|
6
|
+
generate() {
|
|
7
|
+
const { pallets } = this.metadata;
|
|
8
|
+
this.typesGen.clearCache();
|
|
9
|
+
this.typesGen.typeImports.addKnownType('GenericChainEvents', 'GenericPalletEvent');
|
|
10
|
+
let defTypeOut = '';
|
|
11
|
+
for (let pallet of pallets) {
|
|
12
|
+
const eventTypeId = pallet.event;
|
|
13
|
+
if (!eventTypeId) {
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
const eventDefs = this.#getEventDefs(eventTypeId);
|
|
17
|
+
const flatMembers = eventDefs.every((d) => d.fields.length === 0);
|
|
18
|
+
defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s events`);
|
|
19
|
+
defTypeOut += `${stringCamelCase(pallet.name)}: {
|
|
20
|
+
${eventDefs
|
|
21
|
+
.map((def) => {
|
|
22
|
+
const genericParts = [`'${pallet.name}'`, `'${def.name}'`];
|
|
23
|
+
const eventDataPart = this.#generateMemberDefType(def.fields);
|
|
24
|
+
if (eventDataPart) {
|
|
25
|
+
genericParts.push(eventDataPart);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
genericParts.push(flatMembers ? 'undefined' : 'null');
|
|
29
|
+
}
|
|
30
|
+
return { ...def, genericParts };
|
|
31
|
+
})
|
|
32
|
+
.map(({ name, docs, fields, genericParts }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletEvent<${genericParts.join(', ')}>`)
|
|
33
|
+
.join(',\n')}
|
|
34
|
+
|
|
35
|
+
${commentBlock('Generic pallet event')}[prop: string]: GenericPalletEvent,
|
|
36
|
+
},`;
|
|
37
|
+
}
|
|
38
|
+
const importTypes = this.typesGen.typeImports.toImports();
|
|
39
|
+
const template = compileTemplate('events.hbs');
|
|
40
|
+
return beautifySourceCode(template({ importTypes, defTypeOut }));
|
|
41
|
+
}
|
|
42
|
+
#getEventDefs(typeId) {
|
|
43
|
+
const def = this.metadata.types[typeId];
|
|
44
|
+
assert(def, `Event def not found for id ${typeId}`);
|
|
45
|
+
const { tag, value } = def.type;
|
|
46
|
+
assert(tag === 'Enum', 'Invalid pallet event type!');
|
|
47
|
+
return value.members;
|
|
48
|
+
}
|
|
49
|
+
#generateMemberDefType(fields) {
|
|
50
|
+
if (fields.length === 0) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
else if (fields[0].name === undefined) {
|
|
54
|
+
return fields.length === 1
|
|
55
|
+
? this.typesGen.generateType(fields[0].typeId, 1, true)
|
|
56
|
+
: `[${fields
|
|
57
|
+
.map(({ typeId, docs }) => `${commentBlock(docs)}${this.typesGen.generateType(typeId, 1, true)}`)
|
|
58
|
+
.join(', ')}]`;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
return this.typesGen.generateObjectType(fields, 1, true);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { beautifySourceCode, compileTemplate } from './utils';
|
|
2
|
+
import { stringPascalCase } from '@polkadot/util';
|
|
3
|
+
export class IndexGen {
|
|
4
|
+
networkInfo;
|
|
5
|
+
constructor(networkInfo) {
|
|
6
|
+
this.networkInfo = networkInfo;
|
|
7
|
+
}
|
|
8
|
+
async generate() {
|
|
9
|
+
const { chain } = this.networkInfo;
|
|
10
|
+
const interfaceName = stringPascalCase(chain);
|
|
11
|
+
const template = compileTemplate('index.hbs');
|
|
12
|
+
return beautifySourceCode(template({ interfaceName }));
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { stringLowerFirst } from '@polkadot/util';
|
|
2
|
+
import { normalizeName } from '@dedot/utils';
|
|
3
|
+
import { ApiGen } from '../generator';
|
|
4
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
|
|
5
|
+
export class QueryGen extends ApiGen {
|
|
6
|
+
generate() {
|
|
7
|
+
const { pallets } = this.metadata;
|
|
8
|
+
this.typesGen.clearCache();
|
|
9
|
+
this.typesGen.typeImports.addKnownType('GenericChainStorage', 'GenericStorageQuery', 'Callback');
|
|
10
|
+
let defTypeOut = '';
|
|
11
|
+
for (let pallet of pallets) {
|
|
12
|
+
const storage = pallet.storage;
|
|
13
|
+
if (!storage)
|
|
14
|
+
continue;
|
|
15
|
+
const queries = storage.entries.map((one) => this.#generateEntry(one));
|
|
16
|
+
const queryDefs = queries.map(({ name, valueType, keyType, docs }) => `${commentBlock(docs)}${name}: GenericStorageQuery<(${keyType}) => ${valueType}>`);
|
|
17
|
+
defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s storage queries`);
|
|
18
|
+
defTypeOut += `${stringLowerFirst(pallet.name)}: {
|
|
19
|
+
${queryDefs.join(',\n')}
|
|
20
|
+
|
|
21
|
+
${commentBlock('Generic pallet storage query')}[storage: string]: GenericStorageQuery;
|
|
22
|
+
},`;
|
|
23
|
+
}
|
|
24
|
+
const importTypes = this.typesGen.typeImports.toImports();
|
|
25
|
+
const template = compileTemplate('query.hbs');
|
|
26
|
+
return beautifySourceCode(template({ importTypes, defTypeOut }));
|
|
27
|
+
}
|
|
28
|
+
#generateEntry(entry) {
|
|
29
|
+
const { name, type, docs, modifier } = entry;
|
|
30
|
+
let valueType, keyType;
|
|
31
|
+
if (type.tag === 'Plain') {
|
|
32
|
+
valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
|
|
33
|
+
}
|
|
34
|
+
else if (type.tag === 'Map') {
|
|
35
|
+
valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
|
|
36
|
+
keyType = this.typesGen.generateType(type.value.keyTypeId, 1);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
throw Error('Invalid entry type!');
|
|
40
|
+
}
|
|
41
|
+
const isOptional = modifier === 'Optional';
|
|
42
|
+
valueType = `${valueType}${isOptional ? ' | undefined' : ''}`;
|
|
43
|
+
docs.push('\n');
|
|
44
|
+
if (keyType) {
|
|
45
|
+
docs.push(`@param {${keyType}} arg`);
|
|
46
|
+
}
|
|
47
|
+
docs.push(`@param {Callback<${valueType}> =} callback`);
|
|
48
|
+
return {
|
|
49
|
+
name: normalizeName(name),
|
|
50
|
+
valueType,
|
|
51
|
+
keyType: keyType ? `arg: ${keyType}` : '',
|
|
52
|
+
docs,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ApiGen, TypesGen } from '../generator';
|
|
2
|
+
export declare class RpcGen extends ApiGen {
|
|
3
|
+
#private;
|
|
4
|
+
readonly typesGen: TypesGen;
|
|
5
|
+
readonly rpcMethods: string[];
|
|
6
|
+
constructor(typesGen: TypesGen, rpcMethods: string[]);
|
|
7
|
+
generate(): Promise<string>;
|
|
8
|
+
addTypeImport(type: string | string[], toTypeIn?: boolean): void;
|
|
9
|
+
getGeneratedTypeName(type: string, toTypeIn?: boolean): string;
|
|
10
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { findAliasRpcSpec, findRpcSpec, isUnsubscribeMethod } from '@dedot/specs';
|
|
2
|
+
import { isNativeType } from '@dedot/utils';
|
|
3
|
+
import { ApiGen } from '../generator';
|
|
4
|
+
import { beautifySourceCode, commentBlock, compileTemplate, TUPLE_TYPE_REGEX, WRAPPER_TYPE_REGEX } from './utils';
|
|
5
|
+
const HIDDEN_RPCS = [
|
|
6
|
+
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
7
|
+
'rpc_methods',
|
|
8
|
+
];
|
|
9
|
+
export class RpcGen extends ApiGen {
|
|
10
|
+
typesGen;
|
|
11
|
+
rpcMethods;
|
|
12
|
+
constructor(typesGen, rpcMethods) {
|
|
13
|
+
super(typesGen);
|
|
14
|
+
this.typesGen = typesGen;
|
|
15
|
+
this.rpcMethods = rpcMethods;
|
|
16
|
+
HIDDEN_RPCS.filter((one) => !rpcMethods.includes(one)).forEach((one) => rpcMethods.push(one));
|
|
17
|
+
rpcMethods.sort();
|
|
18
|
+
}
|
|
19
|
+
generate() {
|
|
20
|
+
this.typesGen.clearCache();
|
|
21
|
+
this.typesGen.typeImports.addKnownType('GenericRpcCalls', 'Unsub', 'Callback', 'GenericRpcCall');
|
|
22
|
+
const specsByModule = this.rpcMethods
|
|
23
|
+
.filter((one) => !findAliasRpcSpec(one)) // we'll ignore alias rpc for now if defined in the specs! TODO should we generate alias rpc as well?
|
|
24
|
+
.filter((one) => !isUnsubscribeMethod(one)) // we'll ignore unsubscribe method as well
|
|
25
|
+
.map((one) => {
|
|
26
|
+
const spec = findRpcSpec(one);
|
|
27
|
+
if (spec) {
|
|
28
|
+
return spec;
|
|
29
|
+
}
|
|
30
|
+
const [module, ...methodParts] = one.split('_');
|
|
31
|
+
const method = methodParts.join('_');
|
|
32
|
+
return {
|
|
33
|
+
params: [],
|
|
34
|
+
type: 'GenericRpcCall',
|
|
35
|
+
module,
|
|
36
|
+
method,
|
|
37
|
+
};
|
|
38
|
+
})
|
|
39
|
+
.reduce((o, spec) => {
|
|
40
|
+
const { module, method } = spec;
|
|
41
|
+
// ignore if rpc name does not confront with the general convention
|
|
42
|
+
if (!module || !method) {
|
|
43
|
+
return o;
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
...o,
|
|
47
|
+
[module]: o[module] ? [...o[module], spec] : [spec],
|
|
48
|
+
};
|
|
49
|
+
}, {});
|
|
50
|
+
let rpcCallsOut = '';
|
|
51
|
+
Object.keys(specsByModule).forEach((module) => {
|
|
52
|
+
const specs = specsByModule[module];
|
|
53
|
+
// TODO add alias info to docs block!
|
|
54
|
+
rpcCallsOut += `${module}: {
|
|
55
|
+
${specs.map((spec) => this.#generateMethodDef(spec)).join(',\n')},
|
|
56
|
+
|
|
57
|
+
[method: string]: GenericRpcCall,
|
|
58
|
+
},`;
|
|
59
|
+
});
|
|
60
|
+
// TODO include & define external types
|
|
61
|
+
const importTypes = this.typesGen.typeImports.toImports();
|
|
62
|
+
const template = compileTemplate('rpc.hbs');
|
|
63
|
+
return beautifySourceCode(template({ importTypes, rpcCallsOut }));
|
|
64
|
+
}
|
|
65
|
+
#generateMethodDef(spec) {
|
|
66
|
+
const { name, type, module, method, docs = [], params, pubsub, deprecated } = spec;
|
|
67
|
+
const rpcName = name || `${module}_${method}`;
|
|
68
|
+
let defaultDocs = [`@rpcname: ${rpcName}`];
|
|
69
|
+
if (deprecated) {
|
|
70
|
+
defaultDocs.push(`@deprecated: ${deprecated}`);
|
|
71
|
+
}
|
|
72
|
+
if (type === 'GenericRpcCall' && params.length === 0) {
|
|
73
|
+
return `${commentBlock(defaultDocs)}${method}: GenericRpcCall`;
|
|
74
|
+
}
|
|
75
|
+
this.addTypeImport(type, false);
|
|
76
|
+
params.forEach(({ type, isScale }) => {
|
|
77
|
+
this.addTypeImport(type, !!isScale);
|
|
78
|
+
});
|
|
79
|
+
const typedParams = params.map((param) => ({
|
|
80
|
+
...param,
|
|
81
|
+
plainType: this.getGeneratedTypeName(param.type, !!param.isScale),
|
|
82
|
+
}));
|
|
83
|
+
const isSubscription = !!pubsub;
|
|
84
|
+
const paramsOut = typedParams.map(({ name, type, isOptional, isScale, plainType }) => `${name}${isOptional ? '?' : ''}: ${plainType}`);
|
|
85
|
+
const paramsDoc = typedParams.map(({ name, plainType }) => `@param {${plainType}} ${name}`);
|
|
86
|
+
const typeOut = this.getGeneratedTypeName(type, false);
|
|
87
|
+
if (isSubscription) {
|
|
88
|
+
defaultDocs.shift();
|
|
89
|
+
defaultDocs.unshift(`@pubsub: ${pubsub?.join(', ')}`);
|
|
90
|
+
paramsOut.push(`callback: Callback<${typeOut}>`);
|
|
91
|
+
return `${commentBlock(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<Unsub>>`;
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
return `${commentBlock(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<${typeOut}>>`;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// TODO check typeIn, typeOut if param type, or rpc type isScale
|
|
98
|
+
addTypeImport(type, toTypeIn = true) {
|
|
99
|
+
if (Array.isArray(type)) {
|
|
100
|
+
type.forEach((one) => this.addTypeImport(one, toTypeIn));
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
type = type.trim();
|
|
104
|
+
if (isNativeType(type)) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
// Handle generic wrapper types
|
|
108
|
+
const matchArray = type.match(WRAPPER_TYPE_REGEX);
|
|
109
|
+
if (matchArray) {
|
|
110
|
+
const [_, $1, $2] = matchArray;
|
|
111
|
+
this.addTypeImport($1, toTypeIn);
|
|
112
|
+
if ($2.match(WRAPPER_TYPE_REGEX) || $2.match(TUPLE_TYPE_REGEX)) {
|
|
113
|
+
this.addTypeImport($2, toTypeIn);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
this.addTypeImport($2.split(','), toTypeIn);
|
|
117
|
+
}
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
// Check tuple type
|
|
121
|
+
if (type.match(TUPLE_TYPE_REGEX)) {
|
|
122
|
+
this.addTypeImport(type.slice(1, -1).split(','), toTypeIn);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (type.includes(' | ')) {
|
|
126
|
+
this.addTypeImport(type.split(' | ').map((one) => one.trim()), toTypeIn);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
try {
|
|
130
|
+
const codecType = this.#getCodecType(type, toTypeIn);
|
|
131
|
+
if (isNativeType(codecType))
|
|
132
|
+
return;
|
|
133
|
+
this.typesGen.typeImports.addCodecType(codecType);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
catch (e) { }
|
|
137
|
+
this.typesGen.addTypeImport(type);
|
|
138
|
+
}
|
|
139
|
+
getGeneratedTypeName(type, toTypeIn = true) {
|
|
140
|
+
try {
|
|
141
|
+
const matchArray = type.match(WRAPPER_TYPE_REGEX);
|
|
142
|
+
if (matchArray) {
|
|
143
|
+
const [_, $1, $2] = matchArray;
|
|
144
|
+
const wrapperTypeName = this.#getCodecType($1, toTypeIn);
|
|
145
|
+
if ($2.match(WRAPPER_TYPE_REGEX) || $2.match(TUPLE_TYPE_REGEX)) {
|
|
146
|
+
return `${wrapperTypeName}<${this.getGeneratedTypeName($2, toTypeIn)}>`;
|
|
147
|
+
}
|
|
148
|
+
const innerTypeNames = $2
|
|
149
|
+
.split(',')
|
|
150
|
+
.map((one) => this.getGeneratedTypeName(one.trim(), toTypeIn))
|
|
151
|
+
.join(', ');
|
|
152
|
+
return `${wrapperTypeName}<${innerTypeNames}>`;
|
|
153
|
+
}
|
|
154
|
+
else if (type.match(TUPLE_TYPE_REGEX)) {
|
|
155
|
+
const innerTypeNames = type
|
|
156
|
+
.slice(1, -1)
|
|
157
|
+
.split(',')
|
|
158
|
+
.map((one) => this.getGeneratedTypeName(one.trim(), toTypeIn))
|
|
159
|
+
.join(', ');
|
|
160
|
+
return `[${innerTypeNames}]`;
|
|
161
|
+
}
|
|
162
|
+
return this.#getCodecType(type, toTypeIn);
|
|
163
|
+
}
|
|
164
|
+
catch (e) { }
|
|
165
|
+
return type;
|
|
166
|
+
}
|
|
167
|
+
#getCodecType(type, toTypeIn = true) {
|
|
168
|
+
const { typeIn, typeOut } = this.registry.findCodecType(type);
|
|
169
|
+
return toTypeIn ? typeIn : typeOut;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { TypesGen } from './TypesGen';
|
|
2
|
+
import { RpcGen } from './RpcGen';
|
|
3
|
+
export declare class RuntimeApisGen extends RpcGen {
|
|
4
|
+
#private;
|
|
5
|
+
readonly typesGen: TypesGen;
|
|
6
|
+
readonly runtimeApis: [string, number][];
|
|
7
|
+
constructor(typesGen: TypesGen, runtimeApis: [string, number][]);
|
|
8
|
+
generate(): Promise<string>;
|
|
9
|
+
}
|