@dedot/codegen 0.0.1-next.be305dd5.15 → 0.0.1-next.c26d3c25.42

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 (74) hide show
  1. package/cjs/generator/ConstsGen.js +10 -11
  2. package/cjs/generator/ErrorsGen.js +13 -14
  3. package/cjs/generator/EventsGen.js +14 -15
  4. package/cjs/generator/IndexGen.js +8 -9
  5. package/cjs/generator/JsonRpcGen.js +59 -0
  6. package/cjs/generator/QueryGen.js +24 -16
  7. package/cjs/generator/RuntimeApisGen.js +105 -31
  8. package/cjs/generator/TxGen.js +21 -19
  9. package/cjs/generator/TypeImports.js +8 -0
  10. package/cjs/generator/TypesGen.js +20 -27
  11. package/cjs/generator/index.js +10 -10
  12. package/cjs/generator/utils.js +30 -1
  13. package/cjs/index.js +32 -32
  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 +1 -1
  24. package/generator/ConstsGen.js +5 -6
  25. package/generator/ErrorsGen.d.ts +1 -1
  26. package/generator/ErrorsGen.js +6 -7
  27. package/generator/EventsGen.d.ts +1 -1
  28. package/generator/EventsGen.js +6 -7
  29. package/generator/IndexGen.d.ts +2 -3
  30. package/generator/IndexGen.js +6 -7
  31. package/generator/JsonRpcGen.d.ts +7 -0
  32. package/generator/JsonRpcGen.js +55 -0
  33. package/generator/QueryGen.d.ts +1 -1
  34. package/generator/QueryGen.js +21 -13
  35. package/generator/RuntimeApisGen.d.ts +5 -5
  36. package/generator/RuntimeApisGen.js +95 -21
  37. package/generator/TxGen.d.ts +1 -1
  38. package/generator/TxGen.js +11 -9
  39. package/generator/TypeImports.d.ts +2 -0
  40. package/generator/TypeImports.js +8 -0
  41. package/generator/TypesGen.d.ts +2 -2
  42. package/generator/TypesGen.js +5 -12
  43. package/generator/index.d.ts +10 -10
  44. package/generator/index.js +10 -10
  45. package/generator/utils.d.ts +5 -0
  46. package/generator/utils.js +28 -0
  47. package/index.d.ts +2 -3
  48. package/index.js +24 -24
  49. package/package.json +13 -18
  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/genSupportedChainTypes.js +0 -79
  59. package/cjs/generator/RpcGen.js +0 -176
  60. package/cjs/packageInfo.js +0 -5
  61. package/cjs/templates/rpc.hbs +0 -7
  62. package/cjs/types.js +0 -2
  63. package/genSupportedChainTypes.d.ts +0 -1
  64. package/genSupportedChainTypes.js +0 -74
  65. package/generator/RpcGen.d.ts +0 -10
  66. package/generator/RpcGen.js +0 -172
  67. package/packageInfo.d.ts +0 -4
  68. package/packageInfo.js +0 -2
  69. package/templates/rpc.hbs +0 -7
  70. package/types.d.ts +0 -6
  71. package/types.js +0 -1
  72. package/cjs/generator/{known_codecs.js → known-codecs.js} +1 -1
  73. package/generator/{known_codecs.d.ts → known-codecs.d.ts} +0 -0
  74. package/generator/{known_codecs.js → known-codecs.js} +1 -1
@@ -1,172 +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
- import { findKnownCodecType } from './known_codecs';
6
- const HIDDEN_RPCS = [
7
- // Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
8
- 'rpc_methods',
9
- ];
10
- export class RpcGen extends ApiGen {
11
- typesGen;
12
- rpcMethods;
13
- constructor(typesGen, rpcMethods) {
14
- super(typesGen);
15
- this.typesGen = typesGen;
16
- this.rpcMethods = rpcMethods;
17
- HIDDEN_RPCS.filter((one) => !rpcMethods.includes(one)).forEach((one) => rpcMethods.push(one));
18
- rpcMethods.sort();
19
- }
20
- generate() {
21
- this.typesGen.clearCache();
22
- this.typesGen.typeImports.addKnownType('GenericRpcCalls', 'Unsub', 'Callback', 'GenericRpcCall');
23
- const specsByModule = this.rpcMethods
24
- .filter((one) => !findAliasRpcSpec(one)) // we'll ignore alias rpc for now if defined in the specs! TODO should we generate alias rpc as well?
25
- .filter((one) => !isUnsubscribeMethod(one)) // we'll ignore unsubscribe method as well
26
- .map((one) => {
27
- const spec = findRpcSpec(one);
28
- if (spec) {
29
- return spec;
30
- }
31
- const [module, ...methodParts] = one.split('_');
32
- const method = methodParts.join('_');
33
- return {
34
- params: [],
35
- type: 'GenericRpcCall',
36
- module,
37
- method,
38
- };
39
- })
40
- .reduce((o, spec) => {
41
- const { module, method } = spec;
42
- // ignore if rpc name does not confront with the general convention
43
- if (!module || !method) {
44
- return o;
45
- }
46
- return {
47
- ...o,
48
- [module]: o[module] ? [...o[module], spec] : [spec],
49
- };
50
- }, {});
51
- let rpcCallsOut = '';
52
- Object.keys(specsByModule).forEach((module) => {
53
- const specs = specsByModule[module];
54
- // TODO add alias info to docs block!
55
- rpcCallsOut += `${module}: {
56
- ${specs.map((spec) => this.#generateMethodDef(spec)).join(',\n')},
57
-
58
- [method: string]: GenericRpcCall,
59
- },`;
60
- });
61
- // TODO include & define external types
62
- const importTypes = this.typesGen.typeImports.toImports();
63
- const template = compileTemplate('rpc.hbs');
64
- return beautifySourceCode(template({ importTypes, rpcCallsOut }));
65
- }
66
- #generateMethodDef(spec) {
67
- const { name, type, module, method, docs = [], params, pubsub, deprecated } = spec;
68
- const rpcName = name || `${module}_${method}`;
69
- let defaultDocs = [`@rpcname: ${rpcName}`];
70
- if (deprecated) {
71
- defaultDocs.push(`@deprecated: ${deprecated}`);
72
- }
73
- if (type === 'GenericRpcCall' && params.length === 0) {
74
- return `${commentBlock(defaultDocs)}${method}: GenericRpcCall`;
75
- }
76
- this.addTypeImport(type, false);
77
- params.forEach(({ type, isScale }) => {
78
- this.addTypeImport(type, !!isScale);
79
- });
80
- const typedParams = params.map((param) => ({
81
- ...param,
82
- plainType: this.getGeneratedTypeName(param.type, !!param.isScale),
83
- }));
84
- const isSubscription = !!pubsub;
85
- const paramsOut = typedParams.map(({ name, type, isOptional, isScale, plainType }) => `${name}${isOptional ? '?' : ''}: ${plainType}`);
86
- const paramsDoc = typedParams.map(({ name, plainType }) => `@param {${plainType}} ${name}`);
87
- const typeOut = this.getGeneratedTypeName(type, false);
88
- if (isSubscription) {
89
- defaultDocs.shift();
90
- defaultDocs.unshift(`@pubsub: ${pubsub?.join(', ')}`);
91
- paramsOut.push(`callback: Callback<${typeOut}>`);
92
- return `${commentBlock(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<Unsub>>`;
93
- }
94
- else {
95
- return `${commentBlock(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<${typeOut}>>`;
96
- }
97
- }
98
- // TODO check typeIn, typeOut if param type, or rpc type isScale
99
- addTypeImport(type, toTypeIn = true) {
100
- if (Array.isArray(type)) {
101
- type.forEach((one) => this.addTypeImport(one, toTypeIn));
102
- return;
103
- }
104
- type = type.trim();
105
- if (isNativeType(type)) {
106
- return;
107
- }
108
- // Handle generic wrapper types
109
- const matchArray = type.match(WRAPPER_TYPE_REGEX);
110
- if (matchArray) {
111
- const [_, $1, $2] = matchArray;
112
- this.addTypeImport($1, toTypeIn);
113
- if ($2.match(WRAPPER_TYPE_REGEX) || $2.match(TUPLE_TYPE_REGEX)) {
114
- this.addTypeImport($2, toTypeIn);
115
- }
116
- else {
117
- this.addTypeImport($2.split(','), toTypeIn);
118
- }
119
- return;
120
- }
121
- // Check tuple type
122
- if (type.match(TUPLE_TYPE_REGEX)) {
123
- this.addTypeImport(type.slice(1, -1).split(','), toTypeIn);
124
- return;
125
- }
126
- if (type.includes(' | ')) {
127
- this.addTypeImport(type.split(' | ').map((one) => one.trim()), toTypeIn);
128
- return;
129
- }
130
- try {
131
- const codecType = this.#getCodecType(type, toTypeIn);
132
- if (isNativeType(codecType))
133
- return;
134
- this.typesGen.typeImports.addCodecType(codecType);
135
- return;
136
- }
137
- catch (e) { }
138
- this.typesGen.addTypeImport(type);
139
- }
140
- getGeneratedTypeName(type, toTypeIn = true) {
141
- try {
142
- const matchArray = type.match(WRAPPER_TYPE_REGEX);
143
- if (matchArray) {
144
- const [_, $1, $2] = matchArray;
145
- const wrapperTypeName = this.#getCodecType($1, toTypeIn);
146
- if ($2.match(WRAPPER_TYPE_REGEX) || $2.match(TUPLE_TYPE_REGEX)) {
147
- return `${wrapperTypeName}<${this.getGeneratedTypeName($2, toTypeIn)}>`;
148
- }
149
- const innerTypeNames = $2
150
- .split(',')
151
- .map((one) => this.getGeneratedTypeName(one.trim(), toTypeIn))
152
- .join(', ');
153
- return `${wrapperTypeName}<${innerTypeNames}>`;
154
- }
155
- else if (type.match(TUPLE_TYPE_REGEX)) {
156
- const innerTypeNames = type
157
- .slice(1, -1)
158
- .split(',')
159
- .map((one) => this.getGeneratedTypeName(one.trim(), toTypeIn))
160
- .join(', ');
161
- return `[${innerTypeNames}]`;
162
- }
163
- return this.#getCodecType(type, toTypeIn);
164
- }
165
- catch (e) { }
166
- return type;
167
- }
168
- #getCodecType(type, toTypeIn = true) {
169
- const { typeIn, typeOut } = findKnownCodecType(type);
170
- return toTypeIn ? typeIn : typeOut;
171
- }
172
- }
package/packageInfo.d.ts DELETED
@@ -1,4 +0,0 @@
1
- export declare const packageInfo: {
2
- name: string;
3
- version: string;
4
- };
package/packageInfo.js DELETED
@@ -1,2 +0,0 @@
1
- // THIS FILE IS AUTO-GENERATED, DO NOT EDIT!
2
- export const packageInfo = { name: '@dedot/codegen', version: '0.0.1-alpha.26' };
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
- }
package/types.d.ts DELETED
@@ -1,6 +0,0 @@
1
- export type NetworkInfo = {
2
- chain: string;
3
- endpoint?: string;
4
- metadataHex?: string;
5
- rpcMethods?: string[];
6
- };
package/types.js DELETED
@@ -1 +0,0 @@
1
- export {};
@@ -24,9 +24,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.isKnownCodecType = exports.findKnownWrapperCodec = exports.findKnownCodec = exports.findKnownCodecType = exports.looseTypeCodecs = exports.normalizeCodecName = void 0;
27
- const $ = __importStar(require("@dedot/shape"));
28
27
  const Codecs = __importStar(require("@dedot/codecs"));
29
28
  const codecs_1 = require("@dedot/codecs");
29
+ const $ = __importStar(require("@dedot/shape"));
30
30
  const utils_1 = require("@dedot/utils");
31
31
  const normalizeCodecName = (name) => {
32
32
  return name.startsWith('$') ? name : `$${name}`;
@@ -1,6 +1,6 @@
1
- import * as $ from '@dedot/shape';
2
1
  import * as Codecs from '@dedot/codecs';
3
2
  import { $AccountId20, $AccountId32, $Bytes, $ConsensusEngineId, $Era, $EthereumAddress, $MultiAddress, $OpaqueExtrinsic, $PrefixedStorageKey, $RawBytes, $StorageData, $StorageKey, $UncheckedExtrinsic, } from '@dedot/codecs';
3
+ import * as $ from '@dedot/shape';
4
4
  import { assert } from '@dedot/utils';
5
5
  export const normalizeCodecName = (name) => {
6
6
  return name.startsWith('$') ? name : `$${name}`;