@dedot/codegen 0.0.1-next.3 → 0.0.1-next.3662494a.39
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 +10 -11
- package/cjs/generator/ErrorsGen.js +13 -14
- package/cjs/generator/EventsGen.js +14 -15
- package/cjs/generator/IndexGen.js +8 -9
- package/cjs/generator/JsonRpcGen.js +59 -0
- package/cjs/generator/QueryGen.js +24 -16
- package/cjs/generator/RuntimeApisGen.js +107 -29
- package/cjs/generator/TxGen.js +21 -19
- package/cjs/generator/TypeImports.js +8 -0
- package/cjs/generator/TypesGen.js +23 -29
- package/cjs/generator/index.js +10 -10
- package/cjs/generator/known-codecs.js +151 -0
- package/cjs/generator/utils.js +30 -1
- package/cjs/index.js +32 -32
- 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 +5 -5
- package/generator/ConstsGen.d.ts +1 -1
- package/generator/ConstsGen.js +5 -6
- package/generator/ErrorsGen.d.ts +1 -1
- package/generator/ErrorsGen.js +6 -7
- package/generator/EventsGen.d.ts +1 -1
- package/generator/EventsGen.js +6 -7
- package/generator/IndexGen.d.ts +2 -3
- package/generator/IndexGen.js +6 -7
- package/generator/JsonRpcGen.d.ts +7 -0
- package/generator/JsonRpcGen.js +55 -0
- package/generator/QueryGen.d.ts +1 -1
- package/generator/QueryGen.js +21 -13
- package/generator/RuntimeApisGen.d.ts +5 -5
- package/generator/RuntimeApisGen.js +99 -21
- package/generator/TxGen.d.ts +1 -1
- package/generator/TxGen.js +11 -9
- package/generator/TypeImports.d.ts +2 -0
- package/generator/TypeImports.js +8 -0
- package/generator/TypesGen.d.ts +3 -3
- package/generator/TypesGen.js +12 -18
- package/generator/index.d.ts +10 -10
- package/generator/index.js +10 -10
- package/generator/known-codecs.d.ts +23 -0
- package/generator/known-codecs.js +120 -0
- package/generator/utils.d.ts +5 -0
- package/generator/utils.js +28 -0
- package/index.d.ts +2 -3
- package/index.js +24 -24
- package/package.json +14 -18
- 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/genSupportedChainTypes.js +0 -79
- package/cjs/generator/RpcGen.js +0 -175
- package/cjs/packageInfo.js +0 -5
- package/cjs/templates/rpc.hbs +0 -7
- package/cjs/types.js +0 -2
- package/genSupportedChainTypes.d.ts +0 -1
- package/genSupportedChainTypes.js +0 -74
- package/generator/RpcGen.d.ts +0 -10
- package/generator/RpcGen.js +0 -171
- package/packageInfo.d.ts +0 -4
- package/packageInfo.js +0 -2
- package/templates/rpc.hbs +0 -7
- package/types.d.ts +0 -6
- package/types.js +0 -1
package/cjs/generator/RpcGen.js
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RpcGen = void 0;
|
|
4
|
-
const specs_1 = require("@dedot/specs");
|
|
5
|
-
const utils_1 = require("@dedot/utils");
|
|
6
|
-
const generator_1 = require("../generator");
|
|
7
|
-
const utils_2 = require("./utils");
|
|
8
|
-
const HIDDEN_RPCS = [
|
|
9
|
-
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
10
|
-
'rpc_methods',
|
|
11
|
-
];
|
|
12
|
-
class RpcGen extends generator_1.ApiGen {
|
|
13
|
-
typesGen;
|
|
14
|
-
rpcMethods;
|
|
15
|
-
constructor(typesGen, rpcMethods) {
|
|
16
|
-
super(typesGen);
|
|
17
|
-
this.typesGen = typesGen;
|
|
18
|
-
this.rpcMethods = rpcMethods;
|
|
19
|
-
HIDDEN_RPCS.filter((one) => !rpcMethods.includes(one)).forEach((one) => rpcMethods.push(one));
|
|
20
|
-
rpcMethods.sort();
|
|
21
|
-
}
|
|
22
|
-
generate() {
|
|
23
|
-
this.typesGen.clearCache();
|
|
24
|
-
this.typesGen.typeImports.addKnownType('GenericRpcCalls', 'Unsub', 'Callback', 'GenericRpcCall');
|
|
25
|
-
const specsByModule = this.rpcMethods
|
|
26
|
-
.filter((one) => !(0, specs_1.findAliasRpcSpec)(one)) // we'll ignore alias rpc for now if defined in the specs! TODO should we generate alias rpc as well?
|
|
27
|
-
.filter((one) => !(0, specs_1.isUnsubscribeMethod)(one)) // we'll ignore unsubscribe method as well
|
|
28
|
-
.map((one) => {
|
|
29
|
-
const spec = (0, specs_1.findRpcSpec)(one);
|
|
30
|
-
if (spec) {
|
|
31
|
-
return spec;
|
|
32
|
-
}
|
|
33
|
-
const [module, ...methodParts] = one.split('_');
|
|
34
|
-
const method = methodParts.join('_');
|
|
35
|
-
return {
|
|
36
|
-
params: [],
|
|
37
|
-
type: 'GenericRpcCall',
|
|
38
|
-
module,
|
|
39
|
-
method,
|
|
40
|
-
};
|
|
41
|
-
})
|
|
42
|
-
.reduce((o, spec) => {
|
|
43
|
-
const { module, method } = spec;
|
|
44
|
-
// ignore if rpc name does not confront with the general convention
|
|
45
|
-
if (!module || !method) {
|
|
46
|
-
return o;
|
|
47
|
-
}
|
|
48
|
-
return {
|
|
49
|
-
...o,
|
|
50
|
-
[module]: o[module] ? [...o[module], spec] : [spec],
|
|
51
|
-
};
|
|
52
|
-
}, {});
|
|
53
|
-
let rpcCallsOut = '';
|
|
54
|
-
Object.keys(specsByModule).forEach((module) => {
|
|
55
|
-
const specs = specsByModule[module];
|
|
56
|
-
// TODO add alias info to docs block!
|
|
57
|
-
rpcCallsOut += `${module}: {
|
|
58
|
-
${specs.map((spec) => this.#generateMethodDef(spec)).join(',\n')},
|
|
59
|
-
|
|
60
|
-
[method: string]: GenericRpcCall,
|
|
61
|
-
},`;
|
|
62
|
-
});
|
|
63
|
-
// TODO include & define external types
|
|
64
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
65
|
-
const template = (0, utils_2.compileTemplate)('rpc.hbs');
|
|
66
|
-
return (0, utils_2.beautifySourceCode)(template({ importTypes, rpcCallsOut }));
|
|
67
|
-
}
|
|
68
|
-
#generateMethodDef(spec) {
|
|
69
|
-
const { name, type, module, method, docs = [], params, pubsub, deprecated } = spec;
|
|
70
|
-
const rpcName = name || `${module}_${method}`;
|
|
71
|
-
let defaultDocs = [`@rpcname: ${rpcName}`];
|
|
72
|
-
if (deprecated) {
|
|
73
|
-
defaultDocs.push(`@deprecated: ${deprecated}`);
|
|
74
|
-
}
|
|
75
|
-
if (type === 'GenericRpcCall' && params.length === 0) {
|
|
76
|
-
return `${(0, utils_2.commentBlock)(defaultDocs)}${method}: GenericRpcCall`;
|
|
77
|
-
}
|
|
78
|
-
this.addTypeImport(type, false);
|
|
79
|
-
params.forEach(({ type, isScale }) => {
|
|
80
|
-
this.addTypeImport(type, !!isScale);
|
|
81
|
-
});
|
|
82
|
-
const typedParams = params.map((param) => ({
|
|
83
|
-
...param,
|
|
84
|
-
plainType: this.getGeneratedTypeName(param.type, !!param.isScale),
|
|
85
|
-
}));
|
|
86
|
-
const isSubscription = !!pubsub;
|
|
87
|
-
const paramsOut = typedParams.map(({ name, type, isOptional, isScale, plainType }) => `${name}${isOptional ? '?' : ''}: ${plainType}`);
|
|
88
|
-
const paramsDoc = typedParams.map(({ name, plainType }) => `@param {${plainType}} ${name}`);
|
|
89
|
-
const typeOut = this.getGeneratedTypeName(type, false);
|
|
90
|
-
if (isSubscription) {
|
|
91
|
-
defaultDocs.shift();
|
|
92
|
-
defaultDocs.unshift(`@pubsub: ${pubsub?.join(', ')}`);
|
|
93
|
-
paramsOut.push(`callback: Callback<${typeOut}>`);
|
|
94
|
-
return `${(0, utils_2.commentBlock)(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<Unsub>>`;
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
return `${(0, utils_2.commentBlock)(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<${typeOut}>>`;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
// TODO check typeIn, typeOut if param type, or rpc type isScale
|
|
101
|
-
addTypeImport(type, toTypeIn = true) {
|
|
102
|
-
if (Array.isArray(type)) {
|
|
103
|
-
type.forEach((one) => this.addTypeImport(one, toTypeIn));
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
type = type.trim();
|
|
107
|
-
if ((0, utils_1.isNativeType)(type)) {
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
// Handle generic wrapper types
|
|
111
|
-
const matchArray = type.match(utils_2.WRAPPER_TYPE_REGEX);
|
|
112
|
-
if (matchArray) {
|
|
113
|
-
const [_, $1, $2] = matchArray;
|
|
114
|
-
this.addTypeImport($1, toTypeIn);
|
|
115
|
-
if ($2.match(utils_2.WRAPPER_TYPE_REGEX) || $2.match(utils_2.TUPLE_TYPE_REGEX)) {
|
|
116
|
-
this.addTypeImport($2, toTypeIn);
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
this.addTypeImport($2.split(','), toTypeIn);
|
|
120
|
-
}
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
// Check tuple type
|
|
124
|
-
if (type.match(utils_2.TUPLE_TYPE_REGEX)) {
|
|
125
|
-
this.addTypeImport(type.slice(1, -1).split(','), toTypeIn);
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
if (type.includes(' | ')) {
|
|
129
|
-
this.addTypeImport(type.split(' | ').map((one) => one.trim()), toTypeIn);
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
try {
|
|
133
|
-
const codecType = this.#getCodecType(type, toTypeIn);
|
|
134
|
-
if ((0, utils_1.isNativeType)(codecType))
|
|
135
|
-
return;
|
|
136
|
-
this.typesGen.typeImports.addCodecType(codecType);
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
catch (e) { }
|
|
140
|
-
this.typesGen.addTypeImport(type);
|
|
141
|
-
}
|
|
142
|
-
getGeneratedTypeName(type, toTypeIn = true) {
|
|
143
|
-
try {
|
|
144
|
-
const matchArray = type.match(utils_2.WRAPPER_TYPE_REGEX);
|
|
145
|
-
if (matchArray) {
|
|
146
|
-
const [_, $1, $2] = matchArray;
|
|
147
|
-
const wrapperTypeName = this.#getCodecType($1, toTypeIn);
|
|
148
|
-
if ($2.match(utils_2.WRAPPER_TYPE_REGEX) || $2.match(utils_2.TUPLE_TYPE_REGEX)) {
|
|
149
|
-
return `${wrapperTypeName}<${this.getGeneratedTypeName($2, toTypeIn)}>`;
|
|
150
|
-
}
|
|
151
|
-
const innerTypeNames = $2
|
|
152
|
-
.split(',')
|
|
153
|
-
.map((one) => this.getGeneratedTypeName(one.trim(), toTypeIn))
|
|
154
|
-
.join(', ');
|
|
155
|
-
return `${wrapperTypeName}<${innerTypeNames}>`;
|
|
156
|
-
}
|
|
157
|
-
else if (type.match(utils_2.TUPLE_TYPE_REGEX)) {
|
|
158
|
-
const innerTypeNames = type
|
|
159
|
-
.slice(1, -1)
|
|
160
|
-
.split(',')
|
|
161
|
-
.map((one) => this.getGeneratedTypeName(one.trim(), toTypeIn))
|
|
162
|
-
.join(', ');
|
|
163
|
-
return `[${innerTypeNames}]`;
|
|
164
|
-
}
|
|
165
|
-
return this.#getCodecType(type, toTypeIn);
|
|
166
|
-
}
|
|
167
|
-
catch (e) { }
|
|
168
|
-
return type;
|
|
169
|
-
}
|
|
170
|
-
#getCodecType(type, toTypeIn = true) {
|
|
171
|
-
const { typeIn, typeOut } = this.registry.findCodecType(type);
|
|
172
|
-
return toTypeIn ? typeIn : typeOut;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
exports.RpcGen = RpcGen;
|
package/cjs/packageInfo.js
DELETED
package/cjs/templates/rpc.hbs
DELETED
package/cjs/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { generateTypes, generateTypesFromChain } from './index';
|
|
2
|
-
import { rpc } from '@polkadot/types-support/metadata/static-substrate';
|
|
3
|
-
import staticSubstrate from '@polkadot/types-support/metadata/v15/substrate-hex';
|
|
4
|
-
import { $Metadata, CodecRegistry } from '@dedot/codecs';
|
|
5
|
-
import { ConstantExecutor } from 'dedot';
|
|
6
|
-
const NETWORKS = [
|
|
7
|
-
{
|
|
8
|
-
chain: 'substrate',
|
|
9
|
-
metadataHex: staticSubstrate,
|
|
10
|
-
rpcMethods: rpc.methods,
|
|
11
|
-
},
|
|
12
|
-
{
|
|
13
|
-
chain: 'polkadot',
|
|
14
|
-
endpoint: 'wss://rpc.polkadot.io',
|
|
15
|
-
},
|
|
16
|
-
{
|
|
17
|
-
chain: 'kusama',
|
|
18
|
-
endpoint: 'wss://kusama-rpc.polkadot.io',
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
chain: 'astar',
|
|
22
|
-
endpoint: 'wss://rpc.astar.network',
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
chain: 'moonbeam',
|
|
26
|
-
endpoint: 'wss://moonbeam.api.onfinality.io/public-ws',
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
chain: 'polkadotAssetHub',
|
|
30
|
-
endpoint: 'wss://polkadot-asset-hub-rpc.polkadot.io/',
|
|
31
|
-
},
|
|
32
|
-
{
|
|
33
|
-
chain: 'kusamaAssetHub',
|
|
34
|
-
endpoint: 'wss://kusama-asset-hub-rpc.polkadot.io/',
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
chain: 'rococoAssetHub',
|
|
38
|
-
endpoint: 'wss://rococo-asset-hub-rpc.polkadot.io/',
|
|
39
|
-
},
|
|
40
|
-
{
|
|
41
|
-
chain: 'aleph',
|
|
42
|
-
endpoint: 'wss://aleph-zero.api.onfinality.io/public-ws',
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
chain: 'westendAssetHub',
|
|
46
|
-
endpoint: 'wss://westend-asset-hub-rpc.polkadot.io',
|
|
47
|
-
},
|
|
48
|
-
];
|
|
49
|
-
const OUT_DIR = 'packages/chaintypes/src';
|
|
50
|
-
async function run() {
|
|
51
|
-
for (const network of NETWORKS) {
|
|
52
|
-
const { chain, endpoint, metadataHex, rpcMethods } = network;
|
|
53
|
-
if (endpoint) {
|
|
54
|
-
console.log(`Generate types for ${chain} via endpoint ${endpoint}`);
|
|
55
|
-
await generateTypesFromChain(network, endpoint, OUT_DIR);
|
|
56
|
-
}
|
|
57
|
-
else if (metadataHex && rpcMethods) {
|
|
58
|
-
console.log(`Generate types for ${chain} via raw data`);
|
|
59
|
-
const metadata = $Metadata.tryDecode(metadataHex);
|
|
60
|
-
const runtimeVersion = getRuntimeVersion(metadata);
|
|
61
|
-
await generateTypes(network, metadata.latest, rpcMethods, runtimeVersion.apis, OUT_DIR);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
console.log('DONE!');
|
|
65
|
-
}
|
|
66
|
-
const getRuntimeVersion = (metadata) => {
|
|
67
|
-
const registry = new CodecRegistry(metadata.latest);
|
|
68
|
-
const executor = new ConstantExecutor({
|
|
69
|
-
registry,
|
|
70
|
-
metadataLatest: metadata.latest,
|
|
71
|
-
});
|
|
72
|
-
return executor.execute('system', 'version');
|
|
73
|
-
};
|
|
74
|
-
run().catch(console.log);
|
package/generator/RpcGen.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
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
|
-
}
|
package/generator/RpcGen.js
DELETED
|
@@ -1,171 +0,0 @@
|
|
|
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
|
-
}
|
package/packageInfo.d.ts
DELETED
package/packageInfo.js
DELETED
package/templates/rpc.hbs
DELETED
package/types.d.ts
DELETED
package/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|