@dedot/codegen 0.0.1-alpha.27 → 0.0.1-alpha.29

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 (47) 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/QueryGen.js +9 -10
  6. package/cjs/generator/RpcGen.js +18 -19
  7. package/cjs/generator/RuntimeApisGen.js +19 -20
  8. package/cjs/generator/TxGen.js +15 -15
  9. package/cjs/generator/TypesGen.js +21 -22
  10. package/cjs/generator/index.js +10 -10
  11. package/cjs/generator/utils.js +30 -1
  12. package/cjs/index.js +20 -20
  13. package/generator/ApiGen.d.ts +1 -1
  14. package/generator/ConstsGen.d.ts +1 -1
  15. package/generator/ConstsGen.js +4 -5
  16. package/generator/ErrorsGen.d.ts +1 -1
  17. package/generator/ErrorsGen.js +3 -4
  18. package/generator/EventsGen.d.ts +1 -1
  19. package/generator/EventsGen.js +3 -4
  20. package/generator/IndexGen.d.ts +2 -3
  21. package/generator/IndexGen.js +6 -7
  22. package/generator/QueryGen.d.ts +1 -1
  23. package/generator/QueryGen.js +4 -5
  24. package/generator/RpcGen.d.ts +1 -1
  25. package/generator/RpcGen.js +3 -4
  26. package/generator/RuntimeApisGen.d.ts +2 -2
  27. package/generator/RuntimeApisGen.js +5 -6
  28. package/generator/TxGen.d.ts +1 -1
  29. package/generator/TxGen.js +3 -3
  30. package/generator/TypesGen.d.ts +1 -1
  31. package/generator/TypesGen.js +5 -6
  32. package/generator/index.d.ts +10 -10
  33. package/generator/index.js +10 -10
  34. package/generator/utils.d.ts +5 -0
  35. package/generator/utils.js +28 -0
  36. package/index.d.ts +2 -3
  37. package/index.js +10 -10
  38. package/package.json +7 -8
  39. package/cjs/genSupportedChainTypes.js +0 -83
  40. package/cjs/packageInfo.js +0 -5
  41. package/cjs/types.js +0 -2
  42. package/genSupportedChainTypes.d.ts +0 -1
  43. package/genSupportedChainTypes.js +0 -78
  44. package/packageInfo.d.ts +0 -4
  45. package/packageInfo.js +0 -3
  46. package/types.d.ts +0 -6
  47. package/types.js +0 -1
@@ -1,13 +1,12 @@
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 known_types_1 = require("./known-types");
9
- const TypeImports_1 = require("./TypeImports");
10
- const known_codecs_1 = require("./known-codecs");
6
+ const utils_js_1 = require("./utils.js");
7
+ const known_types_js_1 = require("./known-types.js");
8
+ const TypeImports_js_1 = require("./TypeImports.js");
9
+ const known_codecs_js_1 = require("./known-codecs.js");
11
10
  // Skip generate types for these
12
11
  // as we do have native types for them
13
12
  const SKIP_TYPES = [
@@ -43,7 +42,7 @@ class TypesGen {
43
42
  this.metadata = metadata;
44
43
  this.registry = new codecs_1.PortableRegistry(this.metadata);
45
44
  this.includedTypes = this.#includedTypes();
46
- this.typeImports = new TypeImports_1.TypeImports();
45
+ this.typeImports = new TypeImports_js_1.TypeImports();
47
46
  }
48
47
  generate() {
49
48
  this.clearCache();
@@ -51,14 +50,14 @@ class TypesGen {
51
50
  Object.values(this.includedTypes)
52
51
  .filter(({ skip, knownType }) => !(skip || knownType))
53
52
  .forEach(({ name, nameOut, id, docs }) => {
54
- defTypeOut += `${(0, utils_2.commentBlock)(docs)}export type ${nameOut} = ${this.generateType(id, 0, true)};\n\n`;
53
+ defTypeOut += `${(0, utils_js_1.commentBlock)(docs)}export type ${nameOut} = ${this.generateType(id, 0, true)};\n\n`;
55
54
  if (this.#shouldGenerateTypeIn(id)) {
56
55
  defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
57
56
  }
58
57
  });
59
58
  const importTypes = this.typeImports.toImports('./types');
60
- const template = (0, utils_2.compileTemplate)('types.hbs');
61
- return (0, utils_2.beautifySourceCode)(template({ importTypes, defTypeOut }));
59
+ const template = (0, utils_js_1.compileTemplate)('types.hbs');
60
+ return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
62
61
  }
63
62
  typeCache = {};
64
63
  clearCache() {
@@ -103,7 +102,7 @@ class TypesGen {
103
102
  const { tag, value } = type;
104
103
  switch (tag) {
105
104
  case 'Primitive':
106
- const $codec = (0, known_codecs_1.findKnownCodec)(value.kind);
105
+ const $codec = (0, known_codecs_js_1.findKnownCodec)(value.kind);
107
106
  if ($codec.nativeType) {
108
107
  return $codec.nativeType;
109
108
  }
@@ -151,12 +150,12 @@ class TypesGen {
151
150
  return 'null';
152
151
  }
153
152
  else if (members.every((x) => x.fields.length === 0)) {
154
- return members.map(({ name, docs }) => `${(0, utils_2.commentBlock)(docs)}'${(0, util_1.stringPascalCase)(name)}'`).join(' | ');
153
+ return members.map(({ name, docs }) => `${(0, utils_js_1.commentBlock)(docs)}'${(0, utils_1.stringPascalCase)(name)}'`).join(' | ');
155
154
  }
156
155
  else {
157
156
  const membersType = [];
158
157
  for (const { fields, name, docs } of members) {
159
- const keyName = (0, util_1.stringPascalCase)(name);
158
+ const keyName = (0, utils_1.stringPascalCase)(name);
160
159
  if (fields.length === 0) {
161
160
  membersType.push([keyName, null, docs]);
162
161
  }
@@ -164,7 +163,7 @@ class TypesGen {
164
163
  const valueType = fields.length === 1
165
164
  ? this.generateType(fields[0].typeId, nestedLevel + 1, typeOut)
166
165
  : `[${fields
167
- .map(({ typeId, docs }) => `${(0, utils_2.commentBlock)(docs)}${this.generateType(typeId, nestedLevel + 1, typeOut)}`)
166
+ .map(({ typeId, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${this.generateType(typeId, nestedLevel + 1, typeOut)}`)
168
167
  .join(', ')}]`;
169
168
  membersType.push([keyName, valueType, docs]);
170
169
  }
@@ -179,7 +178,7 @@ class TypesGen {
179
178
  value: valueType ? `, ${valueKey}${this.#isOptionalType(valueType) ? '?' : ''}: ${valueType} ` : '',
180
179
  docs,
181
180
  }))
182
- .map(({ tag, value, docs }) => `${(0, utils_2.commentBlock)(docs)}{ ${tag}${value} }`)
181
+ .map(({ tag, value, docs }) => `${(0, utils_js_1.commentBlock)(docs)}{ ${tag}${value} }`)
183
182
  .join(' | ');
184
183
  }
185
184
  }
@@ -226,7 +225,7 @@ class TypesGen {
226
225
  };
227
226
  });
228
227
  return `{${props
229
- .map(({ name, type, optional, docs }) => `${(0, utils_2.commentBlock)(docs)}${name}${optional ? '?' : ''}: ${type}`)
228
+ .map(({ name, type, optional, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${name}${optional ? '?' : ''}: ${type}`)
230
229
  .join(',\n')}}`;
231
230
  }
232
231
  #isOptionalType(type) {
@@ -267,8 +266,8 @@ class TypesGen {
267
266
  const suffix = typeSuffixes.get(id) || '';
268
267
  let knownType = false;
269
268
  let name, nameOut;
270
- if ((0, known_codecs_1.isKnownCodecType)(joinedPath)) {
271
- const codecType = (0, known_codecs_1.findKnownCodecType)(path.at(-1));
269
+ if ((0, known_codecs_js_1.isKnownCodecType)(joinedPath)) {
270
+ const codecType = (0, known_codecs_js_1.findKnownCodecType)(path.at(-1));
272
271
  name = codecType.typeIn;
273
272
  nameOut = codecType.typeOut;
274
273
  knownType = true;
@@ -315,7 +314,7 @@ class TypesGen {
315
314
  */
316
315
  #cleanPath(path) {
317
316
  return path
318
- .map((one) => (0, util_1.stringPascalCase)(one))
317
+ .map((one) => (0, utils_1.stringPascalCase)(one))
319
318
  .filter((one, idx, currentPath) => idx === 0 || one !== currentPath[idx - 1])
320
319
  .join('');
321
320
  }
@@ -390,10 +389,10 @@ class TypesGen {
390
389
  if (diffParam?.typeId) {
391
390
  const diffType = types[diffParam.typeId];
392
391
  if (diffType.path.length > 0) {
393
- return (0, util_1.stringPascalCase)(diffType.path.at(-1));
392
+ return (0, utils_1.stringPascalCase)(diffType.path.at(-1));
394
393
  }
395
394
  else if (diffType.type.tag === 'Primitive') {
396
- return (0, util_1.stringPascalCase)(diffType.type.value.kind);
395
+ return (0, utils_1.stringPascalCase)(diffType.type.value.kind);
397
396
  }
398
397
  }
399
398
  // Last resort!
@@ -404,7 +403,7 @@ class TypesGen {
404
403
  typeName.forEach((one) => this.addTypeImport(one));
405
404
  return;
406
405
  }
407
- if ((0, utils_1.isNativeType)(typeName)) {
406
+ if ((0, utils_js_1.isNativeType)(typeName)) {
408
407
  return;
409
408
  }
410
409
  for (let type of Object.values(this.includedTypes)) {
@@ -426,7 +425,7 @@ class TypesGen {
426
425
  this.typeImports.addCodecType(typeName);
427
426
  return;
428
427
  }
429
- if (known_types_1.knownTypes.includes(typeName)) {
428
+ if (known_types_js_1.knownTypes.includes(typeName)) {
430
429
  this.typeImports.addKnownType(typeName);
431
430
  }
432
431
  else {
@@ -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("./RpcGen.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);
@@ -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,25 +23,25 @@ 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);
30
+ const index_js_1 = require("./generator/index.js");
31
+ const utils_1 = require("@dedot/utils");
32
+ async function generateTypesFromEndpoint(chain, endpoint, outDir) {
33
+ const api = await dedot_1.Dedot.new(endpoint);
34
34
  const { methods } = await api.rpc.rpc.methods();
35
35
  const apis = api.runtimeVersion?.apis || [];
36
- if (!network.chain) {
37
- network.chain = (0, util_1.stringCamelCase)(api.runtimeVersion?.specName || api.runtimeChain || 'local');
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.metadataLatest, methods, apis, outDir);
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);
42
+ exports.generateTypesFromEndpoint = generateTypesFromEndpoint;
43
+ async function generateTypes(chain, metadata, rpcMethods, runtimeApis, outDir = '.') {
44
+ const dirPath = path.resolve(outDir, chain);
45
45
  const defTypesFileName = path.join(dirPath, `types.d.ts`);
46
46
  const constsTypesFileName = path.join(dirPath, `consts.d.ts`);
47
47
  const queryTypesFileName = path.join(dirPath, `query.d.ts`);
@@ -54,15 +54,15 @@ async function generateTypes(network, metadata, rpcMethods, runtimeApis, outDir
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 rpcGen = new index_js_1.RpcGen(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());
@@ -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);
@@ -1,4 +1,4 @@
1
- import { ApiGen } from '../generator';
1
+ import { ApiGen } from '../generator/index.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 '../generator/index.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
  }
@@ -1,4 +1,4 @@
1
- import { ApiGen } from '../generator';
1
+ import { ApiGen } from '../generator/index.js';
2
2
  export declare class QueryGen extends ApiGen {
3
3
  #private;
4
4
  generate(): Promise<string>;
@@ -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 '../generator/index.js';
3
+ import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
5
4
  export class QueryGen extends ApiGen {
6
5
  generate() {
7
6
  const { pallets } = this.metadata;
@@ -15,7 +14,7 @@ export class QueryGen extends ApiGen {
15
14
  const queries = storage.entries.map((one) => this.#generateEntry(one));
16
15
  const queryDefs = queries.map(({ name, valueType, keyType, docs }) => `${commentBlock(docs)}${name}: GenericStorageQuery<(${keyType}) => ${valueType}>`);
17
16
  defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s storage queries`);
18
- defTypeOut += `${stringLowerFirst(pallet.name)}: {
17
+ defTypeOut += `${stringCamelCase(pallet.name)}: {
19
18
  ${queryDefs.join(',\n')}
20
19
 
21
20
  ${commentBlock('Generic pallet storage query')}[storage: string]: GenericStorageQuery;
@@ -1,4 +1,4 @@
1
- import { ApiGen, TypesGen } from '../generator';
1
+ import { ApiGen, TypesGen } from '../generator/index.js';
2
2
  export declare class RpcGen extends ApiGen {
3
3
  #private;
4
4
  readonly typesGen: TypesGen;
@@ -1,8 +1,7 @@
1
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';
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';
6
5
  const HIDDEN_RPCS = [
7
6
  // Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
8
7
  'rpc_methods',
@@ -1,5 +1,5 @@
1
- import { TypesGen } from './TypesGen';
2
- import { RpcGen } from './RpcGen';
1
+ import { TypesGen } from './TypesGen.js';
2
+ import { RpcGen } from './RpcGen.js';
3
3
  export declare class RuntimeApisGen extends RpcGen {
4
4
  #private;
5
5
  readonly typesGen: TypesGen;
@@ -1,8 +1,7 @@
1
1
  import { getRuntimeApiNames, getRuntimeApiSpecs } from '@dedot/specs';
2
- import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
3
- import { calculateRuntimeApiHash, stringSnakeCase } from '@dedot/utils';
4
- import { RpcGen } from './RpcGen';
5
- import { stringCamelCase } from '@polkadot/util';
2
+ import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
3
+ import { calcRuntimeApiHash, stringSnakeCase, stringCamelCase } from '@dedot/utils';
4
+ import { RpcGen } from './RpcGen.js';
6
5
  export class RuntimeApisGen extends RpcGen {
7
6
  typesGen;
8
7
  runtimeApis;
@@ -18,7 +17,7 @@ export class RuntimeApisGen extends RpcGen {
18
17
  if (this.metadata.apis.length > 0) {
19
18
  this.metadata.apis.forEach((runtimeApi) => {
20
19
  const { name: runtimeApiName, methods } = runtimeApi;
21
- runtimeCallsOut += commentBlock(`@runtimeapi: ${runtimeApiName} - ${calculateRuntimeApiHash(runtimeApiName)}`);
20
+ runtimeCallsOut += commentBlock(`@runtimeapi: ${runtimeApiName} - ${calcRuntimeApiHash(runtimeApiName)}`);
22
21
  runtimeCallsOut += `${stringCamelCase(runtimeApiName)}: {
23
22
  ${methods.map((method) => this.#generateMethodDef(runtimeApiName, method)).join('\n')}
24
23
 
@@ -109,7 +108,7 @@ export class RuntimeApisGen extends RpcGen {
109
108
  }, []);
110
109
  }
111
110
  #findRuntimeApiSpec = (runtimeApiHash, version) => {
112
- const runtimeApiName = getRuntimeApiNames().find((one) => calculateRuntimeApiHash(one) === runtimeApiHash);
111
+ const runtimeApiName = getRuntimeApiNames().find((one) => calcRuntimeApiHash(one) === runtimeApiHash);
113
112
  return getRuntimeApiSpecs().find((one) => one.runtimeApiName === runtimeApiName && one.version === version);
114
113
  };
115
114
  }
@@ -1,4 +1,4 @@
1
- import { ApiGen } from '../generator';
1
+ import { ApiGen } from '../generator/index.js';
2
2
  export declare class TxGen extends ApiGen {
3
3
  #private;
4
4
  generate(): Promise<string>;
@@ -1,6 +1,6 @@
1
- import { ApiGen } from '../generator';
2
- import { stringCamelCase, stringPascalCase } from '@polkadot/util';
3
- import { beautifySourceCode, commentBlock, compileTemplate, isReservedWord } from './utils';
1
+ import { ApiGen } from '../generator/index.js';
2
+ import { stringCamelCase, stringPascalCase } from '@dedot/utils';
3
+ import { beautifySourceCode, commentBlock, compileTemplate, isReservedWord } from './utils.js';
4
4
  export class TxGen extends ApiGen {
5
5
  generate() {
6
6
  const { pallets, types } = this.metadata;
@@ -1,5 +1,5 @@
1
1
  import { PortableRegistry, Field, MetadataLatest, PortableType, TypeId } from '@dedot/codecs';
2
- import { TypeImports } from './TypeImports';
2
+ import { TypeImports } from './TypeImports.js';
3
3
  interface NamedType extends PortableType {
4
4
  name: string;
5
5
  nameOut: string;
@@ -1,10 +1,9 @@
1
- import { stringPascalCase } from '@polkadot/util';
2
1
  import { PortableRegistry } from '@dedot/codecs';
3
- import { isNativeType, normalizeName } from '@dedot/utils';
4
- import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
5
- import { knownTypes } from './known-types';
6
- import { TypeImports } from './TypeImports';
7
- import { findKnownCodec, findKnownCodecType, isKnownCodecType } from './known-codecs';
2
+ import { normalizeName, stringPascalCase } from '@dedot/utils';
3
+ import { isNativeType, beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
4
+ import { knownTypes } from './known-types.js';
5
+ import { TypeImports } from './TypeImports.js';
6
+ import { findKnownCodec, findKnownCodecType, isKnownCodecType } from './known-codecs.js';
8
7
  // Skip generate types for these
9
8
  // as we do have native types for them
10
9
  const SKIP_TYPES = [
@@ -1,10 +1,10 @@
1
- export * from './TypesGen';
2
- export * from './ApiGen';
3
- export * from './ConstsGen';
4
- export * from './QueryGen';
5
- export * from './RpcGen';
6
- export * from './IndexGen';
7
- export * from './ErrorsGen';
8
- export * from './EventsGen';
9
- export * from './TxGen';
10
- export * from './RuntimeApisGen';
1
+ export * from './TypesGen.js';
2
+ export * from './ApiGen.js';
3
+ export * from './ConstsGen.js';
4
+ export * from './QueryGen.js';
5
+ export * from './RpcGen.js';
6
+ export * from './IndexGen.js';
7
+ export * from './ErrorsGen.js';
8
+ export * from './EventsGen.js';
9
+ export * from './TxGen.js';
10
+ export * from './RuntimeApisGen.js';
@@ -1,10 +1,10 @@
1
- export * from './TypesGen';
2
- export * from './ApiGen';
3
- export * from './ConstsGen';
4
- export * from './QueryGen';
5
- export * from './RpcGen';
6
- export * from './IndexGen';
7
- export * from './ErrorsGen';
8
- export * from './EventsGen';
9
- export * from './TxGen';
10
- export * from './RuntimeApisGen';
1
+ export * from './TypesGen.js';
2
+ export * from './ApiGen.js';
3
+ export * from './ConstsGen.js';
4
+ export * from './QueryGen.js';
5
+ export * from './RpcGen.js';
6
+ export * from './IndexGen.js';
7
+ export * from './ErrorsGen.js';
8
+ export * from './EventsGen.js';
9
+ export * from './TxGen.js';
10
+ export * from './RuntimeApisGen.js';
@@ -8,3 +8,8 @@ export declare const compileTemplate: (templateFileName: string) => HandlebarsTe
8
8
  * @param word
9
9
  */
10
10
  export declare const isReservedWord: (word: string) => boolean;
11
+ /**
12
+ * Check if a type is native JS/TS type
13
+ * @param type
14
+ */
15
+ export declare const isNativeType: (type: string) => boolean;
@@ -33,3 +33,31 @@ const TS_RESERVED_WORDS = ['new', 'class'];
33
33
  * @param word
34
34
  */
35
35
  export const isReservedWord = (word) => TS_RESERVED_WORDS.includes(word);
36
+ const TS_PRIMITIVE_TYPES = [
37
+ 'void',
38
+ 'undefined',
39
+ 'null',
40
+ 'number',
41
+ 'boolean',
42
+ 'bigint',
43
+ 'Map',
44
+ 'Set',
45
+ 'string',
46
+ 'any',
47
+ 'Array',
48
+ 'Record',
49
+ ];
50
+ /**
51
+ * Check if a type is native JS/TS type
52
+ * @param type
53
+ */
54
+ export const isNativeType = (type) => {
55
+ return TS_PRIMITIVE_TYPES.some((one) => {
56
+ if (typeof one === 'string') {
57
+ return one === type;
58
+ }
59
+ else {
60
+ return type.match(one);
61
+ }
62
+ });
63
+ };
package/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
1
  import { MetadataLatest } from '@dedot/codecs';
2
- import { NetworkInfo } from './types';
3
- export declare function generateTypesFromChain(network: NetworkInfo, endpoint: string, outDir: string): Promise<void>;
4
- export declare function generateTypes(network: NetworkInfo, metadata: MetadataLatest, rpcMethods: string[], runtimeApis: any[], outDir?: string): Promise<void>;
2
+ export declare function generateTypesFromEndpoint(chain: string, endpoint: string, outDir?: string): Promise<void>;
3
+ export declare function generateTypes(chain: string, metadata: MetadataLatest, rpcMethods: string[], runtimeApis: any[], outDir?: string): Promise<void>;