@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.
Files changed (65) hide show
  1. package/cjs/generator/ConstsGen.js +5 -5
  2. package/cjs/generator/ErrorsGen.js +6 -6
  3. package/cjs/generator/EventsGen.js +6 -6
  4. package/cjs/generator/IndexGen.js +7 -3
  5. package/cjs/generator/JsonRpcGen.js +59 -0
  6. package/cjs/generator/QueryGen.js +21 -12
  7. package/cjs/generator/RuntimeApisGen.js +96 -21
  8. package/cjs/generator/TxGen.js +11 -9
  9. package/cjs/generator/TypeImports.js +13 -3
  10. package/cjs/generator/TypesGen.js +4 -10
  11. package/cjs/generator/index.js +1 -1
  12. package/cjs/generator/known-codecs.js +1 -1
  13. package/cjs/index.js +29 -28
  14. package/cjs/templates/consts.hbs +1 -1
  15. package/cjs/templates/errors.hbs +1 -1
  16. package/cjs/templates/events.hbs +1 -1
  17. package/cjs/templates/index.hbs +15 -10
  18. package/cjs/templates/json-rpc.hbs +5 -0
  19. package/cjs/templates/query.hbs +1 -1
  20. package/cjs/templates/runtime.hbs +1 -1
  21. package/cjs/templates/tx.hbs +1 -1
  22. package/generator/ApiGen.d.ts +1 -1
  23. package/generator/ConstsGen.d.ts +2 -2
  24. package/generator/ConstsGen.js +4 -4
  25. package/generator/ErrorsGen.d.ts +1 -1
  26. package/generator/ErrorsGen.js +6 -6
  27. package/generator/EventsGen.d.ts +1 -1
  28. package/generator/EventsGen.js +6 -6
  29. package/generator/IndexGen.d.ts +1 -1
  30. package/generator/IndexGen.js +7 -3
  31. package/generator/JsonRpcGen.d.ts +8 -0
  32. package/generator/JsonRpcGen.js +55 -0
  33. package/generator/QueryGen.d.ts +2 -2
  34. package/generator/QueryGen.js +20 -11
  35. package/generator/RuntimeApisGen.d.ts +5 -5
  36. package/generator/RuntimeApisGen.js +94 -19
  37. package/generator/TxGen.d.ts +1 -1
  38. package/generator/TxGen.js +11 -9
  39. package/generator/TypeImports.d.ts +8 -1
  40. package/generator/TypeImports.js +13 -3
  41. package/generator/TypesGen.d.ts +2 -2
  42. package/generator/TypesGen.js +4 -10
  43. package/generator/index.d.ts +1 -1
  44. package/generator/index.js +1 -1
  45. package/generator/known-codecs.d.ts +1 -1
  46. package/generator/known-codecs.js +1 -1
  47. package/index.d.ts +2 -2
  48. package/index.js +30 -29
  49. package/package.json +15 -17
  50. package/templates/consts.hbs +1 -1
  51. package/templates/errors.hbs +1 -1
  52. package/templates/events.hbs +1 -1
  53. package/templates/index.hbs +15 -10
  54. package/templates/json-rpc.hbs +5 -0
  55. package/templates/query.hbs +1 -1
  56. package/templates/runtime.hbs +1 -1
  57. package/templates/tx.hbs +1 -1
  58. package/cjs/generator/RpcGen.js +0 -175
  59. package/cjs/generator/known-types.js +0 -33
  60. package/cjs/templates/rpc.hbs +0 -7
  61. package/generator/RpcGen.d.ts +0 -10
  62. package/generator/RpcGen.js +0 -171
  63. package/generator/known-types.d.ts +0 -6
  64. package/generator/known-types.js +0 -30
  65. package/templates/rpc.hbs +0 -7
@@ -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 index_js_1 = require("../generator/index.js");
6
- const utils_js_1 = require("./utils.js");
7
- const known_codecs_js_1 = require("./known-codecs.js");
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 index_js_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_js_1.compileTemplate)('rpc.hbs');
66
- return (0, utils_js_1.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_js_1.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_js_1.commentBlock)(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<Unsub>>`;
95
- }
96
- else {
97
- return `${(0, utils_js_1.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_js_1.isNativeType)(type)) {
108
- return;
109
- }
110
- // Handle generic wrapper types
111
- const matchArray = type.match(utils_js_1.WRAPPER_TYPE_REGEX);
112
- if (matchArray) {
113
- const [_, $1, $2] = matchArray;
114
- this.addTypeImport($1, toTypeIn);
115
- if ($2.match(utils_js_1.WRAPPER_TYPE_REGEX) || $2.match(utils_js_1.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_js_1.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_js_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_js_1.WRAPPER_TYPE_REGEX);
145
- if (matchArray) {
146
- const [_, $1, $2] = matchArray;
147
- const wrapperTypeName = this.#getCodecType($1, toTypeIn);
148
- if ($2.match(utils_js_1.WRAPPER_TYPE_REGEX) || $2.match(utils_js_1.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_js_1.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 } = (0, known_codecs_js_1.findKnownCodecType)(type);
172
- return toTypeIn ? typeIn : typeOut;
173
- }
174
- }
175
- exports.RpcGen = RpcGen;
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.knownTypes = void 0;
4
- /**
5
- * Known type names registered in @dedot/types
6
- * Since TS interfaces are trim-off when compiling,
7
- * So we need to register them explicitly here for RPC codegen process
8
- */
9
- exports.knownTypes = [
10
- 'ExtrinsicOrHash',
11
- 'EpochAuthorship',
12
- 'BlockStats',
13
- 'Prevotes',
14
- 'Precommits',
15
- 'RoundState',
16
- 'ReportedRoundStates',
17
- 'JustificationNotification',
18
- 'EncodedFinalityProofs',
19
- 'LeavesProof',
20
- 'StorageKind',
21
- 'RpcMethods',
22
- 'ReadProof',
23
- 'StorageChangeSet',
24
- 'TraceBlockResponse',
25
- 'MigrationStatusResult',
26
- 'ChainType',
27
- 'ChainProperties',
28
- 'Health',
29
- 'SyncState',
30
- 'PeerInfo',
31
- 'NodeRole',
32
- 'NetworkState',
33
- ];
@@ -1,7 +0,0 @@
1
- // Generated by @dedot/codegen
2
-
3
- {{{ importTypes }}}
4
-
5
- export interface RpcCalls extends GenericRpcCalls {
6
- {{{rpcCallsOut}}}
7
- }
@@ -1,10 +0,0 @@
1
- import { ApiGen, TypesGen } from '../generator/index.js';
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
- }
@@ -1,171 +0,0 @@
1
- import { findAliasRpcSpec, findRpcSpec, isUnsubscribeMethod } from '@dedot/specs';
2
- import { ApiGen } from '../generator/index.js';
3
- import { isNativeType, beautifySourceCode, commentBlock, compileTemplate, TUPLE_TYPE_REGEX, WRAPPER_TYPE_REGEX, } from './utils.js';
4
- import { findKnownCodecType } from './known-codecs.js';
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 } = findKnownCodecType(type);
169
- return toTypeIn ? typeIn : typeOut;
170
- }
171
- }
@@ -1,6 +0,0 @@
1
- /**
2
- * Known type names registered in @dedot/types
3
- * Since TS interfaces are trim-off when compiling,
4
- * So we need to register them explicitly here for RPC codegen process
5
- */
6
- export declare const knownTypes: string[];
@@ -1,30 +0,0 @@
1
- /**
2
- * Known type names registered in @dedot/types
3
- * Since TS interfaces are trim-off when compiling,
4
- * So we need to register them explicitly here for RPC codegen process
5
- */
6
- export const knownTypes = [
7
- 'ExtrinsicOrHash',
8
- 'EpochAuthorship',
9
- 'BlockStats',
10
- 'Prevotes',
11
- 'Precommits',
12
- 'RoundState',
13
- 'ReportedRoundStates',
14
- 'JustificationNotification',
15
- 'EncodedFinalityProofs',
16
- 'LeavesProof',
17
- 'StorageKind',
18
- 'RpcMethods',
19
- 'ReadProof',
20
- 'StorageChangeSet',
21
- 'TraceBlockResponse',
22
- 'MigrationStatusResult',
23
- 'ChainType',
24
- 'ChainProperties',
25
- 'Health',
26
- 'SyncState',
27
- 'PeerInfo',
28
- 'NodeRole',
29
- 'NetworkState',
30
- ];
package/templates/rpc.hbs DELETED
@@ -1,7 +0,0 @@
1
- // Generated by @dedot/codegen
2
-
3
- {{{ importTypes }}}
4
-
5
- export interface RpcCalls extends GenericRpcCalls {
6
- {{{rpcCallsOut}}}
7
- }