@dedot/codegen 0.0.1-next.8d06d348.17 → 0.0.1-next.8f4b2c24.6

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 (66) hide show
  1. package/cjs/generator/ConstsGen.js +9 -10
  2. package/cjs/generator/ErrorsGen.js +12 -13
  3. package/cjs/generator/EventsGen.js +13 -14
  4. package/cjs/generator/IndexGen.js +8 -9
  5. package/cjs/generator/JsonRpcGen.js +59 -0
  6. package/cjs/generator/QueryGen.js +23 -15
  7. package/cjs/generator/RuntimeApisGen.js +102 -28
  8. package/cjs/generator/TxGen.js +15 -15
  9. package/cjs/generator/TypeImports.js +8 -0
  10. package/cjs/generator/TypesGen.js +23 -32
  11. package/cjs/generator/index.js +10 -10
  12. package/cjs/{helpers/KnownCodecRegistry.js → generator/known-codecs.js} +56 -68
  13. package/cjs/generator/utils.js +30 -1
  14. package/cjs/index.js +32 -32
  15. package/cjs/templates/index.hbs +2 -2
  16. package/cjs/templates/json-rpc.hbs +5 -0
  17. package/generator/ApiGen.d.ts +4 -4
  18. package/generator/ConstsGen.d.ts +1 -1
  19. package/generator/ConstsGen.js +4 -5
  20. package/generator/ErrorsGen.d.ts +1 -1
  21. package/generator/ErrorsGen.js +3 -4
  22. package/generator/EventsGen.d.ts +1 -1
  23. package/generator/EventsGen.js +3 -4
  24. package/generator/IndexGen.d.ts +2 -3
  25. package/generator/IndexGen.js +6 -7
  26. package/generator/JsonRpcGen.d.ts +7 -0
  27. package/generator/JsonRpcGen.js +55 -0
  28. package/generator/QueryGen.d.ts +1 -1
  29. package/generator/QueryGen.js +19 -11
  30. package/generator/RuntimeApisGen.d.ts +5 -5
  31. package/generator/RuntimeApisGen.js +89 -15
  32. package/generator/TxGen.d.ts +1 -1
  33. package/generator/TxGen.js +3 -3
  34. package/generator/TypeImports.d.ts +2 -0
  35. package/generator/TypeImports.js +8 -0
  36. package/generator/TypesGen.d.ts +3 -5
  37. package/generator/TypesGen.js +12 -21
  38. package/generator/index.d.ts +10 -10
  39. package/generator/index.js +10 -10
  40. package/{helpers/KnownCodecRegistry.d.ts → generator/known-codecs.d.ts} +11 -10
  41. package/generator/known-codecs.js +120 -0
  42. package/generator/utils.d.ts +5 -0
  43. package/generator/utils.js +28 -0
  44. package/index.d.ts +2 -3
  45. package/index.js +23 -23
  46. package/package.json +7 -8
  47. package/templates/index.hbs +2 -2
  48. package/templates/json-rpc.hbs +5 -0
  49. package/cjs/genSupportedChainTypes.js +0 -79
  50. package/cjs/generator/RpcGen.js +0 -175
  51. package/cjs/helpers/index.js +0 -17
  52. package/cjs/packageInfo.js +0 -5
  53. package/cjs/templates/rpc.hbs +0 -7
  54. package/cjs/types.js +0 -2
  55. package/genSupportedChainTypes.d.ts +0 -1
  56. package/genSupportedChainTypes.js +0 -74
  57. package/generator/RpcGen.d.ts +0 -10
  58. package/generator/RpcGen.js +0 -171
  59. package/helpers/KnownCodecRegistry.js +0 -135
  60. package/helpers/index.d.ts +0 -1
  61. package/helpers/index.js +0 -1
  62. package/packageInfo.d.ts +0 -4
  63. package/packageInfo.js +0 -2
  64. package/templates/rpc.hbs +0 -7
  65. package/types.d.ts +0 -14
  66. package/types.js +0 -1
@@ -1,13 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TypesGen = exports.BASIC_KNOWN_TYPES = void 0;
4
- const util_1 = require("@polkadot/util");
5
4
  const codecs_1 = require("@dedot/codecs");
6
5
  const utils_1 = require("@dedot/utils");
7
- const utils_2 = require("./utils");
8
- const types_1 = require("@dedot/types");
9
- const TypeImports_1 = require("./TypeImports");
10
- const helpers_1 = require("../helpers");
6
+ const utils_js_1 = require("./utils.js");
7
+ const TypeImports_js_1 = require("./TypeImports.js");
8
+ const known_codecs_js_1 = require("./known-codecs.js");
11
9
  // Skip generate types for these
12
10
  // as we do have native types for them
13
11
  const SKIP_TYPES = [
@@ -38,14 +36,12 @@ class TypesGen {
38
36
  */
39
37
  includedTypes;
40
38
  registry;
41
- knownCodecRegistry;
42
39
  typeImports;
43
40
  constructor(metadata) {
44
41
  this.metadata = metadata;
45
- this.registry = new codecs_1.CodecRegistry(this.metadata);
46
- this.knownCodecRegistry = new helpers_1.KnownCodecRegistry();
42
+ this.registry = new codecs_1.PortableRegistry(this.metadata);
47
43
  this.includedTypes = this.#includedTypes();
48
- this.typeImports = new TypeImports_1.TypeImports();
44
+ this.typeImports = new TypeImports_js_1.TypeImports();
49
45
  }
50
46
  generate() {
51
47
  this.clearCache();
@@ -53,14 +49,14 @@ class TypesGen {
53
49
  Object.values(this.includedTypes)
54
50
  .filter(({ skip, knownType }) => !(skip || knownType))
55
51
  .forEach(({ name, nameOut, id, docs }) => {
56
- defTypeOut += `${(0, utils_2.commentBlock)(docs)}export type ${nameOut} = ${this.generateType(id, 0, true)};\n\n`;
52
+ defTypeOut += `${(0, utils_js_1.commentBlock)(docs)}export type ${nameOut} = ${this.generateType(id, 0, true)};\n\n`;
57
53
  if (this.#shouldGenerateTypeIn(id)) {
58
54
  defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
59
55
  }
60
56
  });
61
57
  const importTypes = this.typeImports.toImports('./types');
62
- const template = (0, utils_2.compileTemplate)('types.hbs');
63
- return (0, utils_2.beautifySourceCode)(template({ importTypes, defTypeOut }));
58
+ const template = (0, utils_js_1.compileTemplate)('types.hbs');
59
+ return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
64
60
  }
65
61
  typeCache = {};
66
62
  clearCache() {
@@ -105,7 +101,7 @@ class TypesGen {
105
101
  const { tag, value } = type;
106
102
  switch (tag) {
107
103
  case 'Primitive':
108
- const $codec = this.knownCodecRegistry.findCodec(value.kind);
104
+ const $codec = (0, known_codecs_js_1.findKnownCodec)(value.kind);
109
105
  if ($codec.nativeType) {
110
106
  return $codec.nativeType;
111
107
  }
@@ -153,12 +149,12 @@ class TypesGen {
153
149
  return 'null';
154
150
  }
155
151
  else if (members.every((x) => x.fields.length === 0)) {
156
- return members.map(({ name, docs }) => `${(0, utils_2.commentBlock)(docs)}'${(0, util_1.stringPascalCase)(name)}'`).join(' | ');
152
+ return members.map(({ name, docs }) => `${(0, utils_js_1.commentBlock)(docs)}'${(0, utils_1.stringPascalCase)(name)}'`).join(' | ');
157
153
  }
158
154
  else {
159
155
  const membersType = [];
160
156
  for (const { fields, name, docs } of members) {
161
- const keyName = (0, util_1.stringPascalCase)(name);
157
+ const keyName = (0, utils_1.stringPascalCase)(name);
162
158
  if (fields.length === 0) {
163
159
  membersType.push([keyName, null, docs]);
164
160
  }
@@ -166,7 +162,7 @@ class TypesGen {
166
162
  const valueType = fields.length === 1
167
163
  ? this.generateType(fields[0].typeId, nestedLevel + 1, typeOut)
168
164
  : `[${fields
169
- .map(({ typeId, docs }) => `${(0, utils_2.commentBlock)(docs)}${this.generateType(typeId, nestedLevel + 1, typeOut)}`)
165
+ .map(({ typeId, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${this.generateType(typeId, nestedLevel + 1, typeOut)}`)
170
166
  .join(', ')}]`;
171
167
  membersType.push([keyName, valueType, docs]);
172
168
  }
@@ -174,14 +170,14 @@ class TypesGen {
174
170
  membersType.push([keyName, this.generateObjectType(fields, nestedLevel + 1, typeOut), docs]);
175
171
  }
176
172
  }
177
- const { tagKey, valueKey } = this.registry.portableRegistry.getEnumOptions(typeId);
173
+ const { tagKey, valueKey } = this.registry.getEnumOptions(typeId);
178
174
  return membersType
179
175
  .map(([keyName, valueType, docs]) => ({
180
176
  tag: `${tagKey}: '${keyName}'`,
181
177
  value: valueType ? `, ${valueKey}${this.#isOptionalType(valueType) ? '?' : ''}: ${valueType} ` : '',
182
178
  docs,
183
179
  }))
184
- .map(({ tag, value, docs }) => `${(0, utils_2.commentBlock)(docs)}{ ${tag}${value} }`)
180
+ .map(({ tag, value, docs }) => `${(0, utils_js_1.commentBlock)(docs)}{ ${tag}${value} }`)
185
181
  .join(' | ');
186
182
  }
187
183
  }
@@ -228,7 +224,7 @@ class TypesGen {
228
224
  };
229
225
  });
230
226
  return `{${props
231
- .map(({ name, type, optional, docs }) => `${(0, utils_2.commentBlock)(docs)}${name}${optional ? '?' : ''}: ${type}`)
227
+ .map(({ name, type, optional, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${name}${optional ? '?' : ''}: ${type}`)
232
228
  .join(',\n')}}`;
233
229
  }
234
230
  #isOptionalType(type) {
@@ -269,8 +265,8 @@ class TypesGen {
269
265
  const suffix = typeSuffixes.get(id) || '';
270
266
  let knownType = false;
271
267
  let name, nameOut;
272
- if (this.knownCodecRegistry.isKnownType(joinedPath)) {
273
- const codecType = this.knownCodecRegistry.findCodecType(path.at(-1));
268
+ if ((0, known_codecs_js_1.isKnownCodecType)(joinedPath)) {
269
+ const codecType = (0, known_codecs_js_1.findKnownCodecType)(path.at(-1));
274
270
  name = codecType.typeIn;
275
271
  nameOut = codecType.typeOut;
276
272
  knownType = true;
@@ -317,13 +313,13 @@ class TypesGen {
317
313
  */
318
314
  #cleanPath(path) {
319
315
  return path
320
- .map((one) => (0, util_1.stringPascalCase)(one))
316
+ .map((one) => (0, utils_1.stringPascalCase)(one))
321
317
  .filter((one, idx, currentPath) => idx === 0 || one !== currentPath[idx - 1])
322
318
  .join('');
323
319
  }
324
320
  #shouldGenerateTypeIn(id) {
325
321
  const { callTypeId } = this.metadata.extrinsic;
326
- const palletCallTypeIds = this.registry.portableRegistry.getPalletCallTypeIds();
322
+ const palletCallTypeIds = this.registry.getPalletCallTypeIds();
327
323
  return callTypeId === id || palletCallTypeIds.includes(id);
328
324
  }
329
325
  eqlCache = new Map();
@@ -392,10 +388,10 @@ class TypesGen {
392
388
  if (diffParam?.typeId) {
393
389
  const diffType = types[diffParam.typeId];
394
390
  if (diffType.path.length > 0) {
395
- return (0, util_1.stringPascalCase)(diffType.path.at(-1));
391
+ return (0, utils_1.stringPascalCase)(diffType.path.at(-1));
396
392
  }
397
393
  else if (diffType.type.tag === 'Primitive') {
398
- return (0, util_1.stringPascalCase)(diffType.type.value.kind);
394
+ return (0, utils_1.stringPascalCase)(diffType.type.value.kind);
399
395
  }
400
396
  }
401
397
  // Last resort!
@@ -406,7 +402,7 @@ class TypesGen {
406
402
  typeName.forEach((one) => this.addTypeImport(one));
407
403
  return;
408
404
  }
409
- if ((0, utils_1.isNativeType)(typeName)) {
405
+ if ((0, utils_js_1.isNativeType)(typeName)) {
410
406
  return;
411
407
  }
412
408
  for (let type of Object.values(this.includedTypes)) {
@@ -428,12 +424,7 @@ class TypesGen {
428
424
  this.typeImports.addCodecType(typeName);
429
425
  return;
430
426
  }
431
- if (types_1.registry.has(typeName)) {
432
- this.typeImports.addKnownType(typeName);
433
- }
434
- else {
435
- this.typeImports.addOutType(typeName);
436
- }
427
+ this.typeImports.addOutType(typeName);
437
428
  }
438
429
  }
439
430
  exports.TypesGen = TypesGen;
@@ -14,13 +14,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./TypesGen"), exports);
18
- __exportStar(require("./ApiGen"), exports);
19
- __exportStar(require("./ConstsGen"), exports);
20
- __exportStar(require("./QueryGen"), exports);
21
- __exportStar(require("./RpcGen"), exports);
22
- __exportStar(require("./IndexGen"), exports);
23
- __exportStar(require("./ErrorsGen"), exports);
24
- __exportStar(require("./EventsGen"), exports);
25
- __exportStar(require("./TxGen"), exports);
26
- __exportStar(require("./RuntimeApisGen"), exports);
17
+ __exportStar(require("./TypesGen.js"), exports);
18
+ __exportStar(require("./ApiGen.js"), exports);
19
+ __exportStar(require("./ConstsGen.js"), exports);
20
+ __exportStar(require("./QueryGen.js"), exports);
21
+ __exportStar(require("./JsonRpcGen.js"), exports);
22
+ __exportStar(require("./IndexGen.js"), exports);
23
+ __exportStar(require("./ErrorsGen.js"), exports);
24
+ __exportStar(require("./EventsGen.js"), exports);
25
+ __exportStar(require("./TxGen.js"), exports);
26
+ __exportStar(require("./RuntimeApisGen.js"), exports);
@@ -23,10 +23,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.KnownCodecRegistry = exports.looseTypeCodecs = exports.normalizeCodecName = void 0;
26
+ exports.isKnownCodecType = exports.findKnownWrapperCodec = exports.findKnownCodec = exports.findKnownCodecType = exports.looseTypeCodecs = exports.normalizeCodecName = void 0;
27
27
  const $ = __importStar(require("@dedot/shape"));
28
28
  const Codecs = __importStar(require("@dedot/codecs"));
29
29
  const codecs_1 = require("@dedot/codecs");
30
+ const utils_1 = require("@dedot/utils");
30
31
  const normalizeCodecName = (name) => {
31
32
  return name.startsWith('$') ? name : `$${name}`;
32
33
  };
@@ -83,81 +84,68 @@ exports.looseTypeCodecs = {
83
84
  $UncheckedExtrinsic: codecs_1.$UncheckedExtrinsic,
84
85
  $Era: codecs_1.$Era,
85
86
  };
86
- /**
87
- * A codec registry for both known and portable codecs,
88
- */
89
- class KnownCodecRegistry {
90
- findCodec(name) {
91
- return this.#findKnownCodec(name);
92
- }
93
- findCodecType(name) {
94
- const normalizedName = (0, exports.normalizeCodecName)(name);
95
- const $knownCodec = exports.looseTypeCodecs[normalizedName];
96
- if ($knownCodec) {
97
- return {
98
- name: normalizedName,
99
- $codec: $knownCodec,
100
- typeIn: `${name}Like`,
101
- typeOut: name,
102
- };
103
- }
104
- const $codec = this.findCodec(name);
105
- if ($codec.nativeType && $[name]) {
106
- return {
107
- name: normalizedName,
108
- $codec,
109
- typeIn: $codec.nativeType,
110
- typeOut: $codec.nativeType,
111
- };
112
- }
87
+ function findKnownCodecType(name) {
88
+ const normalizedName = (0, exports.normalizeCodecName)(name);
89
+ const $knownCodec = exports.looseTypeCodecs[normalizedName];
90
+ if ($knownCodec) {
113
91
  return {
114
92
  name: normalizedName,
115
- $codec,
116
- typeIn: name,
93
+ $codec: $knownCodec,
94
+ typeIn: `${name}Like`,
117
95
  typeOut: name,
118
96
  };
119
97
  }
120
- #findKnownCodec(typeName) {
121
- // @ts-ignore
122
- const $codec = this.#findKnownWrapperCodec(typeName) || Codecs[(0, exports.normalizeCodecName)(typeName)] || $[typeName];
123
- if (!$codec) {
124
- throw new Error(`Unsupported codec - ${typeName}`);
125
- }
126
- // This only works with top-level $.deferred codecs
127
- // We should make it work for nested $.deferred codecs as well
128
- if ($codec.metadata && $codec.metadata[0].name === '$.deferred') {
129
- const getShape = $codec.metadata[0].args[0];
130
- return $.deferred(() => getShape(this));
131
- }
132
- return $codec;
98
+ const $codec = findKnownCodec(name);
99
+ if ($codec.nativeType && $[name]) {
100
+ return {
101
+ name: normalizedName,
102
+ $codec,
103
+ typeIn: $codec.nativeType,
104
+ typeOut: $codec.nativeType,
105
+ };
133
106
  }
134
- #findKnownWrapperCodec(typeName) {
135
- const matchNames = typeName.match(WRAPPER_TYPE_REGEX);
136
- if (matchNames) {
137
- const [_, wrapper, inner] = matchNames;
138
- if (KNOWN_WRAPPER_TYPES.includes(wrapper)) {
139
- // @ts-ignore
140
- const $Wrapper = $[wrapper];
141
- if (inner.match(TUPLE_TYPE_REGEX) || inner.match(WRAPPER_TYPE_REGEX)) {
142
- return $Wrapper(this.#findKnownWrapperCodec(inner));
143
- }
144
- const $inners = inner.split(',').map((one) => this.#findKnownCodec(one.trim()));
145
- return $Wrapper(...$inners);
107
+ return {
108
+ name: normalizedName,
109
+ $codec,
110
+ typeIn: name,
111
+ typeOut: name,
112
+ };
113
+ }
114
+ exports.findKnownCodecType = findKnownCodecType;
115
+ function findKnownCodec(typeName) {
116
+ // @ts-ignore
117
+ const $codec = findKnownWrapperCodec(typeName) || Codecs[(0, exports.normalizeCodecName)(typeName)] || $[typeName];
118
+ (0, utils_1.assert)($codec, `Known codec not found - ${typeName}`);
119
+ return $codec;
120
+ }
121
+ exports.findKnownCodec = findKnownCodec;
122
+ function findKnownWrapperCodec(typeName) {
123
+ const matchNames = typeName.match(WRAPPER_TYPE_REGEX);
124
+ if (matchNames) {
125
+ const [_, wrapper, inner] = matchNames;
126
+ if (KNOWN_WRAPPER_TYPES.includes(wrapper)) {
127
+ // @ts-ignore
128
+ const $Wrapper = $[wrapper];
129
+ if (inner.match(TUPLE_TYPE_REGEX) || inner.match(WRAPPER_TYPE_REGEX)) {
130
+ return $Wrapper(findKnownWrapperCodec(inner));
146
131
  }
147
- throw new Error(`Unknown wrapper type ${wrapper} from ${typeName}`);
148
- }
149
- else if (typeName.match(TUPLE_TYPE_REGEX)) {
150
- const $inner = typeName
151
- .slice(1, -1)
152
- .split(',')
153
- .filter((x) => x)
154
- .map((one) => this.#findKnownCodec(one.trim()));
155
- return $.Tuple(...$inner);
132
+ const $inners = inner.split(',').map((one) => findKnownCodec(one.trim()));
133
+ return $Wrapper(...$inners);
156
134
  }
135
+ throw new Error(`Unknown wrapper type ${wrapper} from ${typeName}`);
157
136
  }
158
- isKnownType(path) {
159
- const joinedPath = Array.isArray(path) ? path.join('::') : path;
160
- return KNOWN_PATHS.some((one) => joinedPath.match(one));
137
+ else if (typeName.match(TUPLE_TYPE_REGEX)) {
138
+ const $inner = typeName
139
+ .slice(1, -1)
140
+ .split(',')
141
+ .filter((x) => x)
142
+ .map((one) => findKnownCodec(one.trim()));
143
+ return $.Tuple(...$inner);
161
144
  }
162
145
  }
163
- exports.KnownCodecRegistry = KnownCodecRegistry;
146
+ exports.findKnownWrapperCodec = findKnownWrapperCodec;
147
+ function isKnownCodecType(path) {
148
+ const joinedPath = Array.isArray(path) ? path.join('::') : path;
149
+ return KNOWN_PATHS.some((one) => joinedPath.match(one));
150
+ }
151
+ exports.isKnownCodecType = isKnownCodecType;
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.isReservedWord = exports.compileTemplate = exports.beautifySourceCode = exports.commentBlock = exports.TUPLE_TYPE_REGEX = exports.WRAPPER_TYPE_REGEX = void 0;
29
+ exports.isNativeType = exports.isReservedWord = exports.compileTemplate = exports.beautifySourceCode = exports.commentBlock = exports.TUPLE_TYPE_REGEX = exports.WRAPPER_TYPE_REGEX = void 0;
30
30
  const fs = __importStar(require("fs"));
31
31
  const handlebars_1 = __importDefault(require("handlebars"));
32
32
  const path = __importStar(require("path"));
@@ -66,3 +66,32 @@ const TS_RESERVED_WORDS = ['new', 'class'];
66
66
  */
67
67
  const isReservedWord = (word) => TS_RESERVED_WORDS.includes(word);
68
68
  exports.isReservedWord = isReservedWord;
69
+ const TS_PRIMITIVE_TYPES = [
70
+ 'void',
71
+ 'undefined',
72
+ 'null',
73
+ 'number',
74
+ 'boolean',
75
+ 'bigint',
76
+ 'Map',
77
+ 'Set',
78
+ 'string',
79
+ 'any',
80
+ 'Array',
81
+ 'Record',
82
+ ];
83
+ /**
84
+ * Check if a type is native JS/TS type
85
+ * @param type
86
+ */
87
+ const isNativeType = (type) => {
88
+ return TS_PRIMITIVE_TYPES.some((one) => {
89
+ if (typeof one === 'string') {
90
+ return one === type;
91
+ }
92
+ else {
93
+ return type.match(one);
94
+ }
95
+ });
96
+ };
97
+ exports.isNativeType = isNativeType;
package/cjs/index.js CHANGED
@@ -23,50 +23,50 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.generateTypes = exports.generateTypesFromChain = void 0;
26
+ exports.generateTypes = exports.generateTypesFromEndpoint = void 0;
27
27
  const dedot_1 = require("dedot");
28
28
  const fs = __importStar(require("fs"));
29
29
  const path = __importStar(require("path"));
30
- const generator_1 = require("./generator");
31
- const util_1 = require("@polkadot/util");
32
- async function generateTypesFromChain(network, endpoint, outDir) {
33
- const api = await dedot_1.Dedot.create(endpoint);
34
- const { methods } = await api.rpc.rpc.methods();
35
- const apis = api.runtimeVersion?.apis || [];
36
- if (!network.chain) {
37
- network.chain = (0, util_1.stringCamelCase)(api.runtimeVersion?.specName || api.runtimeChain || 'local');
30
+ const index_js_1 = require("./generator/index.js");
31
+ const utils_1 = require("@dedot/utils");
32
+ async function generateTypesFromEndpoint(chain, endpoint, outDir, extension = 'd.ts') {
33
+ const api = await dedot_1.Dedot.new(endpoint);
34
+ const { methods } = await api.rpc.rpc_methods();
35
+ const apis = api.runtimeVersion.apis || {};
36
+ if (!chain) {
37
+ chain = (0, utils_1.stringCamelCase)(api.runtimeVersion.specName || api.runtimeChain || 'local');
38
38
  }
39
- await generateTypes(network, api.metadataLatest, methods, apis, outDir);
39
+ await generateTypes(chain, api.metadata.latest, methods, apis, outDir, extension);
40
40
  await api.disconnect();
41
41
  }
42
- exports.generateTypesFromChain = generateTypesFromChain;
43
- async function generateTypes(network, metadata, rpcMethods, runtimeApis, outDir = '.') {
44
- const dirPath = path.resolve(outDir, network.chain);
45
- const defTypesFileName = path.join(dirPath, `types.d.ts`);
46
- const constsTypesFileName = path.join(dirPath, `consts.d.ts`);
47
- const queryTypesFileName = path.join(dirPath, `query.d.ts`);
48
- const rpcCallsFileName = path.join(dirPath, `rpc.d.ts`);
49
- const indexFileName = path.join(dirPath, `index.d.ts`);
50
- const errorsFileName = path.join(dirPath, `errors.d.ts`);
51
- const eventsFileName = path.join(dirPath, `events.d.ts`);
52
- const runtimeApisFileName = path.join(dirPath, `runtime.d.ts`);
53
- const txFileName = path.join(dirPath, `tx.d.ts`);
42
+ exports.generateTypesFromEndpoint = generateTypesFromEndpoint;
43
+ async function generateTypes(chain, metadata, rpcMethods, runtimeApis, outDir = '.', extension = 'd.ts') {
44
+ const dirPath = path.resolve(outDir, chain);
45
+ const defTypesFileName = path.join(dirPath, `types.${extension}`);
46
+ const constsTypesFileName = path.join(dirPath, `consts.${extension}`);
47
+ const queryTypesFileName = path.join(dirPath, `query.${extension}`);
48
+ const jsonRpcFileName = path.join(dirPath, `json-rpc.${extension}`);
49
+ const indexFileName = path.join(dirPath, `index.${extension}`);
50
+ const errorsFileName = path.join(dirPath, `errors.${extension}`);
51
+ const eventsFileName = path.join(dirPath, `events.${extension}`);
52
+ const runtimeApisFileName = path.join(dirPath, `runtime.${extension}`);
53
+ const txFileName = path.join(dirPath, `tx.${extension}`);
54
54
  if (!fs.existsSync(dirPath)) {
55
55
  fs.mkdirSync(dirPath, { recursive: true });
56
56
  }
57
- const typesGen = new generator_1.TypesGen(metadata);
58
- const constsGen = new generator_1.ConstsGen(typesGen);
59
- const queryGen = new generator_1.QueryGen(typesGen);
60
- const rpcGen = new generator_1.RpcGen(typesGen, rpcMethods);
61
- const indexGen = new generator_1.IndexGen(network);
62
- const errorsGen = new generator_1.ErrorsGen(typesGen);
63
- const eventsGen = new generator_1.EventsGen(typesGen);
64
- const runtimeApisGen = new generator_1.RuntimeApisGen(typesGen, runtimeApis);
65
- const txGen = new generator_1.TxGen(typesGen);
57
+ const typesGen = new index_js_1.TypesGen(metadata);
58
+ const constsGen = new index_js_1.ConstsGen(typesGen);
59
+ const queryGen = new index_js_1.QueryGen(typesGen);
60
+ const jsonRpcGen = new index_js_1.JsonRpcGen(typesGen, rpcMethods);
61
+ const indexGen = new index_js_1.IndexGen(chain);
62
+ const errorsGen = new index_js_1.ErrorsGen(typesGen);
63
+ const eventsGen = new index_js_1.EventsGen(typesGen);
64
+ const runtimeApisGen = new index_js_1.RuntimeApisGen(typesGen, runtimeApis);
65
+ const txGen = new index_js_1.TxGen(typesGen);
66
66
  fs.writeFileSync(defTypesFileName, await typesGen.generate());
67
67
  fs.writeFileSync(errorsFileName, await errorsGen.generate());
68
68
  fs.writeFileSync(eventsFileName, await eventsGen.generate());
69
- fs.writeFileSync(rpcCallsFileName, await rpcGen.generate());
69
+ fs.writeFileSync(jsonRpcFileName, await jsonRpcGen.generate());
70
70
  fs.writeFileSync(queryTypesFileName, await queryGen.generate());
71
71
  fs.writeFileSync(constsTypesFileName, await constsGen.generate());
72
72
  fs.writeFileSync(txFileName, await txGen.generate());
@@ -3,7 +3,7 @@
3
3
  import { GenericSubstrateApi } from '@dedot/types';
4
4
  import { ChainConsts } from './consts';
5
5
  import { ChainStorage } from './query';
6
- import { RpcCalls } from './rpc';
6
+ import { ChainJsonRpcApis } from './json-rpc';
7
7
  import { ChainErrors } from './errors';
8
8
  import { ChainEvents } from './events';
9
9
  import { RuntimeApis } from './runtime';
@@ -12,7 +12,7 @@ import { ChainTx } from './tx';
12
12
  export * from './types';
13
13
 
14
14
  export interface {{{interfaceName}}}Api extends GenericSubstrateApi {
15
- rpc: RpcCalls;
15
+ rpc: ChainJsonRpcApis;
16
16
  consts: ChainConsts;
17
17
  query: ChainStorage;
18
18
  errors: ChainErrors;
@@ -0,0 +1,5 @@
1
+ // Generated by @dedot/codegen
2
+
3
+ {{{ importTypes }}}
4
+
5
+ export type ChainJsonRpcApis = Pick<JsonRpcApis, {{{jsonRpcMethods}}}> & GenericJsonRpcApis;
@@ -1,4 +1,4 @@
1
- import { TypesGen } from '../generator';
1
+ import { TypesGen } from '../generator/index.js';
2
2
  export declare abstract class ApiGen {
3
3
  readonly typesGen: TypesGen;
4
4
  constructor(typesGen: TypesGen);
@@ -13,14 +13,14 @@ export declare abstract class ApiGen {
13
13
  type: {
14
14
  tag: "Struct";
15
15
  value: {
16
- fields: import("@dedot/codecs").Field[];
16
+ fields: import("dedot").Field[];
17
17
  };
18
18
  } | {
19
19
  tag: "Enum";
20
20
  value: {
21
21
  members: {
22
22
  name: string;
23
- fields: import("@dedot/codecs").Field[];
23
+ fields: import("dedot").Field[];
24
24
  index: number;
25
25
  docs: string[];
26
26
  }[];
@@ -134,5 +134,5 @@ export declare abstract class ApiGen {
134
134
  }>;
135
135
  };
136
136
  };
137
- get registry(): import("@dedot/codecs").CodecRegistry;
137
+ get registry(): import("dedot").PortableRegistry;
138
138
  }
@@ -1,4 +1,4 @@
1
- import { ApiGen } from '../generator';
1
+ import { ApiGen } from './ApiGen.js';
2
2
  export declare class ConstsGen extends ApiGen {
3
3
  generate(): Promise<string>;
4
4
  }
@@ -1,7 +1,6 @@
1
- import { stringLowerFirst } from '@polkadot/util';
2
- import { normalizeName } from '@dedot/utils';
3
- import { ApiGen } from '../generator';
4
- import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
1
+ import { normalizeName, stringCamelCase } from '@dedot/utils';
2
+ import { ApiGen } from './ApiGen.js';
3
+ import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
5
4
  export class ConstsGen extends ApiGen {
6
5
  generate() {
7
6
  const { pallets } = this.metadata;
@@ -15,7 +14,7 @@ export class ConstsGen extends ApiGen {
15
14
  docs: one.docs,
16
15
  }));
17
16
  defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s constants`);
18
- defTypeOut += `${stringLowerFirst(pallet.name)}: {
17
+ defTypeOut += `${stringCamelCase(pallet.name)}: {
19
18
  ${typedConstants.map(({ name, type, docs }) => `${commentBlock(docs)}${name}: ${type}`).join(',\n')}
20
19
 
21
20
  ${commentBlock('Generic pallet constant')}[name: string]: any,
@@ -1,4 +1,4 @@
1
- import { ApiGen } from './ApiGen';
1
+ import { ApiGen } from './ApiGen.js';
2
2
  export declare class ErrorsGen extends ApiGen {
3
3
  #private;
4
4
  generate(): Promise<string>;
@@ -1,7 +1,6 @@
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';
1
+ import { ApiGen } from './ApiGen.js';
2
+ import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
3
+ import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
5
4
  export class ErrorsGen extends ApiGen {
6
5
  generate() {
7
6
  const { pallets } = this.metadata;
@@ -1,4 +1,4 @@
1
- import { ApiGen } from './ApiGen';
1
+ import { ApiGen } from './ApiGen.js';
2
2
  export declare class EventsGen extends ApiGen {
3
3
  #private;
4
4
  generate(): Promise<string>;
@@ -1,7 +1,6 @@
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';
1
+ import { ApiGen } from './ApiGen.js';
2
+ import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
3
+ import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
5
4
  export class EventsGen extends ApiGen {
6
5
  generate() {
7
6
  const { pallets } = this.metadata;
@@ -1,6 +1,5 @@
1
- import { NetworkInfo } from '../types';
2
1
  export declare class IndexGen {
3
- readonly networkInfo: NetworkInfo;
4
- constructor(networkInfo: NetworkInfo);
2
+ readonly chain: string;
3
+ constructor(chain: string);
5
4
  generate(): Promise<string>;
6
5
  }
@@ -1,13 +1,12 @@
1
- import { beautifySourceCode, compileTemplate } from './utils';
2
- import { stringPascalCase } from '@polkadot/util';
1
+ import { beautifySourceCode, compileTemplate } from './utils.js';
2
+ import { stringPascalCase } from '@dedot/utils';
3
3
  export class IndexGen {
4
- networkInfo;
5
- constructor(networkInfo) {
6
- this.networkInfo = networkInfo;
4
+ chain;
5
+ constructor(chain) {
6
+ this.chain = chain;
7
7
  }
8
8
  async generate() {
9
- const { chain } = this.networkInfo;
10
- const interfaceName = stringPascalCase(chain);
9
+ const interfaceName = stringPascalCase(this.chain);
11
10
  const template = compileTemplate('index.hbs');
12
11
  return beautifySourceCode(template({ interfaceName }));
13
12
  }
@@ -0,0 +1,7 @@
1
+ import { ApiGen, TypesGen } from '../generator/index.js';
2
+ export declare class JsonRpcGen extends ApiGen {
3
+ readonly typesGen: TypesGen;
4
+ readonly rpcMethods: string[];
5
+ constructor(typesGen: TypesGen, rpcMethods: string[]);
6
+ generate(): Promise<string>;
7
+ }