@dedot/codegen 0.0.1-next.c3d95ac1.3 → 0.0.1-next.d9aff57d.57

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 (54) hide show
  1. package/cjs/generator/ConstsGen.js +3 -3
  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 +8 -8
  6. package/cjs/generator/QueryGen.js +19 -10
  7. package/cjs/generator/RuntimeApisGen.js +11 -11
  8. package/cjs/generator/TxGen.js +11 -9
  9. package/cjs/generator/TypeImports.js +11 -9
  10. package/cjs/generator/TypesGen.js +3 -3
  11. package/cjs/generator/known-codecs.js +1 -1
  12. package/cjs/index.js +17 -16
  13. package/cjs/templates/consts.hbs +1 -1
  14. package/cjs/templates/errors.hbs +1 -1
  15. package/cjs/templates/events.hbs +1 -1
  16. package/cjs/templates/index.hbs +14 -9
  17. package/cjs/templates/json-rpc.hbs +1 -1
  18. package/cjs/templates/query.hbs +1 -1
  19. package/cjs/templates/runtime.hbs +1 -1
  20. package/cjs/templates/tx.hbs +1 -1
  21. package/generator/ApiGen.d.ts +4 -4
  22. package/generator/ConstsGen.d.ts +1 -1
  23. package/generator/ConstsGen.js +3 -3
  24. package/generator/ErrorsGen.d.ts +1 -1
  25. package/generator/ErrorsGen.js +6 -6
  26. package/generator/EventsGen.d.ts +1 -1
  27. package/generator/EventsGen.js +6 -6
  28. package/generator/IndexGen.d.ts +1 -1
  29. package/generator/IndexGen.js +7 -3
  30. package/generator/JsonRpcGen.d.ts +3 -2
  31. package/generator/JsonRpcGen.js +6 -6
  32. package/generator/QueryGen.d.ts +1 -1
  33. package/generator/QueryGen.js +19 -10
  34. package/generator/RuntimeApisGen.d.ts +2 -2
  35. package/generator/RuntimeApisGen.js +9 -9
  36. package/generator/TxGen.d.ts +1 -1
  37. package/generator/TxGen.js +11 -9
  38. package/generator/TypeImports.d.ts +8 -3
  39. package/generator/TypeImports.js +11 -9
  40. package/generator/TypesGen.d.ts +1 -1
  41. package/generator/TypesGen.js +3 -3
  42. package/generator/known-codecs.d.ts +1 -1
  43. package/generator/known-codecs.js +1 -1
  44. package/index.d.ts +2 -2
  45. package/index.js +18 -17
  46. package/package.json +15 -17
  47. package/templates/consts.hbs +1 -1
  48. package/templates/errors.hbs +1 -1
  49. package/templates/events.hbs +1 -1
  50. package/templates/index.hbs +14 -9
  51. package/templates/json-rpc.hbs +1 -1
  52. package/templates/query.hbs +1 -1
  53. package/templates/runtime.hbs +1 -1
  54. package/templates/tx.hbs +1 -1
@@ -2,10 +2,10 @@ import { normalizeName, stringCamelCase } from '@dedot/utils';
2
2
  import { ApiGen } from './ApiGen.js';
3
3
  import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
4
4
  export class ConstsGen extends ApiGen {
5
- generate() {
5
+ generate(useSubPaths = false) {
6
6
  const { pallets } = this.metadata;
7
7
  this.typesGen.clearCache();
8
- this.typesGen.typeImports.addKnownType('GenericChainConsts');
8
+ this.typesGen.typeImports.addKnownType('GenericChainConsts', 'RpcVersion');
9
9
  let defTypeOut = '';
10
10
  for (let pallet of pallets) {
11
11
  const typedConstants = pallet.constants.map((one) => ({
@@ -20,7 +20,7 @@ export class ConstsGen extends ApiGen {
20
20
  ${commentBlock('Generic pallet constant')}[name: string]: any,
21
21
  },`;
22
22
  }
23
- const importTypes = this.typesGen.typeImports.toImports();
23
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
24
24
  const template = compileTemplate('consts.hbs');
25
25
  return beautifySourceCode(template({ importTypes, defTypeOut }));
26
26
  }
@@ -1,5 +1,5 @@
1
1
  import { ApiGen } from './ApiGen.js';
2
2
  export declare class ErrorsGen extends ApiGen {
3
3
  #private;
4
- generate(): Promise<string>;
4
+ generate(useSubPaths?: boolean): Promise<string>;
5
5
  }
@@ -1,11 +1,11 @@
1
+ import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
1
2
  import { ApiGen } from './ApiGen.js';
2
3
  import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
3
- import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
4
4
  export class ErrorsGen extends ApiGen {
5
- generate() {
5
+ generate(useSubPaths = false) {
6
6
  const { pallets } = this.metadata;
7
7
  this.typesGen.clearCache();
8
- this.typesGen.typeImports.addKnownType('GenericChainErrors', 'GenericPalletError');
8
+ this.typesGen.typeImports.addKnownType('GenericChainErrors', 'GenericPalletError', 'RpcVersion');
9
9
  let defTypeOut = '';
10
10
  for (let pallet of pallets) {
11
11
  const errorTypeId = pallet.error;
@@ -16,13 +16,13 @@ export class ErrorsGen extends ApiGen {
16
16
  defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s errors`);
17
17
  defTypeOut += `${stringCamelCase(pallet.name)}: {
18
18
  ${errorDefs
19
- .map(({ name, docs }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletError`)
19
+ .map(({ name, docs }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletError<Rv>`)
20
20
  .join(',\n')}
21
21
 
22
- ${commentBlock('Generic pallet error')}[error: string]: GenericPalletError,
22
+ ${commentBlock('Generic pallet error')}[error: string]: GenericPalletError<Rv>,
23
23
  },`;
24
24
  }
25
- const importTypes = this.typesGen.typeImports.toImports();
25
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
26
26
  const template = compileTemplate('errors.hbs');
27
27
  return beautifySourceCode(template({ importTypes, defTypeOut }));
28
28
  }
@@ -1,5 +1,5 @@
1
1
  import { ApiGen } from './ApiGen.js';
2
2
  export declare class EventsGen extends ApiGen {
3
3
  #private;
4
- generate(): Promise<string>;
4
+ generate(useSubPaths?: boolean): Promise<string>;
5
5
  }
@@ -1,11 +1,11 @@
1
+ import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
1
2
  import { ApiGen } from './ApiGen.js';
2
3
  import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
3
- import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
4
4
  export class EventsGen extends ApiGen {
5
- generate() {
5
+ generate(useSubPaths = false) {
6
6
  const { pallets } = this.metadata;
7
7
  this.typesGen.clearCache();
8
- this.typesGen.typeImports.addKnownType('GenericChainEvents', 'GenericPalletEvent');
8
+ this.typesGen.typeImports.addKnownType('GenericChainEvents', 'GenericPalletEvent', 'RpcVersion');
9
9
  let defTypeOut = '';
10
10
  for (let pallet of pallets) {
11
11
  const eventTypeId = pallet.event;
@@ -28,13 +28,13 @@ export class EventsGen extends ApiGen {
28
28
  }
29
29
  return { ...def, genericParts };
30
30
  })
31
- .map(({ name, docs, fields, genericParts }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletEvent<${genericParts.join(', ')}>`)
31
+ .map(({ name, docs, fields, genericParts }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletEvent<Rv, ${genericParts.join(', ')}>`)
32
32
  .join(',\n')}
33
33
 
34
- ${commentBlock('Generic pallet event')}[prop: string]: GenericPalletEvent,
34
+ ${commentBlock('Generic pallet event')}[prop: string]: GenericPalletEvent<Rv>,
35
35
  },`;
36
36
  }
37
- const importTypes = this.typesGen.typeImports.toImports();
37
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
38
38
  const template = compileTemplate('events.hbs');
39
39
  return beautifySourceCode(template({ importTypes, defTypeOut }));
40
40
  }
@@ -1,5 +1,5 @@
1
1
  export declare class IndexGen {
2
2
  readonly chain: string;
3
3
  constructor(chain: string);
4
- generate(): Promise<string>;
4
+ generate(useSubPaths?: boolean): Promise<string>;
5
5
  }
@@ -1,13 +1,17 @@
1
- import { beautifySourceCode, compileTemplate } from './utils.js';
2
1
  import { stringPascalCase } from '@dedot/utils';
2
+ import { TypeImports } from './TypeImports.js';
3
+ import { beautifySourceCode, compileTemplate } from './utils.js';
3
4
  export class IndexGen {
4
5
  chain;
5
6
  constructor(chain) {
6
7
  this.chain = chain;
7
8
  }
8
- async generate() {
9
+ async generate(useSubPaths = false) {
9
10
  const interfaceName = stringPascalCase(this.chain);
11
+ const typeImports = new TypeImports();
12
+ typeImports.addKnownType('GenericSubstrateApi', 'RpcLegacy', 'RpcV2', 'RpcVersion');
13
+ const importTypes = typeImports.toImports({ useSubPaths });
10
14
  const template = compileTemplate('index.hbs');
11
- return beautifySourceCode(template({ interfaceName }));
15
+ return beautifySourceCode(template({ importTypes, interfaceName }));
12
16
  }
13
17
  }
@@ -1,7 +1,8 @@
1
- import { ApiGen, TypesGen } from '../generator/index.js';
1
+ import { ApiGen } from './ApiGen.js';
2
+ import { TypesGen } from './TypesGen.js';
2
3
  export declare class JsonRpcGen extends ApiGen {
3
4
  readonly typesGen: TypesGen;
4
5
  readonly rpcMethods: string[];
5
6
  constructor(typesGen: TypesGen, rpcMethods: string[]);
6
- generate(): Promise<string>;
7
+ generate(useSubPaths?: boolean): Promise<string>;
7
8
  }
@@ -1,6 +1,6 @@
1
- import { ApiGen } from '../generator/index.js';
1
+ import { subscriptionsInfo } from '@dedot/api';
2
+ import { ApiGen } from './ApiGen.js';
2
3
  import { beautifySourceCode, compileTemplate } from './utils.js';
3
- import { subscriptionsInfo } from '@dedot/specs';
4
4
  const HIDDEN_RPCS = [
5
5
  // Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
6
6
  'rpc_methods',
@@ -38,12 +38,12 @@ export class JsonRpcGen extends ApiGen {
38
38
  HIDDEN_RPCS.filter((one) => !rpcMethods.includes(one)).forEach((one) => rpcMethods.push(one));
39
39
  rpcMethods.sort();
40
40
  }
41
- generate() {
41
+ generate(useSubPaths = false) {
42
42
  this.typesGen.clearCache();
43
- this.typesGen.typeImports.addKnownType('GenericJsonRpcApis');
44
- this.typesGen.typeImports.addSpecType('JsonRpcApis');
43
+ this.typesGen.typeImports.addKnownType('GenericJsonRpcApis', 'RpcVersion');
44
+ this.typesGen.typeImports.addKnownJsonRpcType('JsonRpcApis');
45
45
  const toExclude = Object.values(subscriptionsInfo).flat();
46
- const importTypes = this.typesGen.typeImports.toImports();
46
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
47
47
  const template = compileTemplate('json-rpc.hbs');
48
48
  const jsonRpcMethods = this.rpcMethods
49
49
  .filter((one) => !ALIAS_RPCS.includes(one)) // exclude alias rpcs
@@ -1,5 +1,5 @@
1
1
  import { ApiGen } from './ApiGen.js';
2
2
  export declare class QueryGen extends ApiGen {
3
3
  #private;
4
- generate(): Promise<string>;
4
+ generate(useSubPaths?: boolean): Promise<string>;
5
5
  }
@@ -2,37 +2,45 @@ import { normalizeName, stringCamelCase } from '@dedot/utils';
2
2
  import { ApiGen } from './ApiGen.js';
3
3
  import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
4
4
  export class QueryGen extends ApiGen {
5
- generate() {
5
+ generate(useSubPaths = false) {
6
6
  const { pallets } = this.metadata;
7
7
  this.typesGen.clearCache();
8
- this.typesGen.typeImports.addKnownType('GenericChainStorage', 'GenericStorageQuery', 'Callback');
8
+ this.typesGen.typeImports.addKnownType('GenericChainStorage', 'GenericStorageQuery', 'Callback', 'RpcVersion');
9
9
  let defTypeOut = '';
10
10
  for (let pallet of pallets) {
11
11
  const storage = pallet.storage;
12
12
  if (!storage)
13
13
  continue;
14
14
  const queries = storage.entries.map((one) => this.#generateEntry(one));
15
- const queryDefs = queries.map(({ name, valueType, keyType, docs }) => `${commentBlock(docs)}${name}: GenericStorageQuery<(${keyType}) => ${valueType}>`);
15
+ const queryDefs = queries.map(({ name, valueType, keyType, docs, keyTypeOut }) => {
16
+ if (keyTypeOut) {
17
+ return `${commentBlock(docs)}${name}: GenericStorageQuery<Rv, (${keyType}) => ${valueType}, ${keyTypeOut}>`;
18
+ }
19
+ else {
20
+ return `${commentBlock(docs)}${name}: GenericStorageQuery<Rv, (${keyType}) => ${valueType}>`;
21
+ }
22
+ });
16
23
  defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s storage queries`);
17
24
  defTypeOut += `${stringCamelCase(pallet.name)}: {
18
25
  ${queryDefs.join(',\n')}
19
26
 
20
- ${commentBlock('Generic pallet storage query')}[storage: string]: GenericStorageQuery;
27
+ ${commentBlock('Generic pallet storage query')}[storage: string]: GenericStorageQuery<Rv>;
21
28
  },`;
22
29
  }
23
- const importTypes = this.typesGen.typeImports.toImports();
30
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
24
31
  const template = compileTemplate('query.hbs');
25
32
  return beautifySourceCode(template({ importTypes, defTypeOut }));
26
33
  }
27
34
  #generateEntry(entry) {
28
35
  const { name, type, docs, modifier } = entry;
29
- let valueType, keyType;
36
+ let valueType, keyTypeOut, keyTypeIn;
30
37
  if (type.tag === 'Plain') {
31
38
  valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
32
39
  }
33
40
  else if (type.tag === 'Map') {
34
41
  valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
35
- keyType = this.typesGen.generateType(type.value.keyTypeId, 1);
42
+ keyTypeOut = this.typesGen.generateType(type.value.keyTypeId, 1, true);
43
+ keyTypeIn = this.typesGen.generateType(type.value.keyTypeId, 1);
36
44
  }
37
45
  else {
38
46
  throw Error('Invalid entry type!');
@@ -40,14 +48,15 @@ export class QueryGen extends ApiGen {
40
48
  const isOptional = modifier === 'Optional';
41
49
  valueType = `${valueType}${isOptional ? ' | undefined' : ''}`;
42
50
  docs.push('\n');
43
- if (keyType) {
44
- docs.push(`@param {${keyType}} arg`);
51
+ if (keyTypeIn) {
52
+ docs.push(`@param {${keyTypeIn}} arg`);
45
53
  }
46
54
  docs.push(`@param {Callback<${valueType}> =} callback`);
47
55
  return {
48
56
  name: normalizeName(name),
49
57
  valueType,
50
- keyType: keyType ? `arg: ${keyType}` : '',
58
+ keyType: keyTypeIn ? `arg: ${keyTypeIn}` : '',
59
+ keyTypeOut,
51
60
  docs,
52
61
  };
53
62
  }
@@ -1,9 +1,9 @@
1
- import { TypesGen } from './TypesGen.js';
2
1
  import { ApiGen } from './ApiGen.js';
2
+ import { TypesGen } from './TypesGen.js';
3
3
  export declare class RuntimeApisGen extends ApiGen {
4
4
  #private;
5
5
  readonly typesGen: TypesGen;
6
6
  readonly runtimeApis: Record<string, number>;
7
7
  constructor(typesGen: TypesGen, runtimeApis: Record<string, number>);
8
- generate(): Promise<string>;
8
+ generate(useSubPaths?: boolean): Promise<string>;
9
9
  }
@@ -1,8 +1,8 @@
1
- import { getRuntimeApiNames, getRuntimeApiSpecs } from '@dedot/specs';
2
- import { beautifySourceCode, commentBlock, compileTemplate, isNativeType, TUPLE_TYPE_REGEX, WRAPPER_TYPE_REGEX, } from './utils.js';
1
+ import { getRuntimeApiNames, getRuntimeApiSpecs } from '@dedot/runtime-specs';
3
2
  import { calcRuntimeApiHash, stringSnakeCase, stringCamelCase } from '@dedot/utils';
4
3
  import { ApiGen } from './ApiGen.js';
5
4
  import { findKnownCodecType } from './known-codecs.js';
5
+ import { beautifySourceCode, commentBlock, compileTemplate, isNativeType, TUPLE_TYPE_REGEX, WRAPPER_TYPE_REGEX, } from './utils.js';
6
6
  export class RuntimeApisGen extends ApiGen {
7
7
  typesGen;
8
8
  runtimeApis;
@@ -11,9 +11,9 @@ export class RuntimeApisGen extends ApiGen {
11
11
  this.typesGen = typesGen;
12
12
  this.runtimeApis = runtimeApis;
13
13
  }
14
- generate() {
14
+ generate(useSubPaths = false) {
15
15
  this.typesGen.clearCache();
16
- this.typesGen.typeImports.addKnownType('GenericRuntimeApis', 'GenericRuntimeApiMethod');
16
+ this.typesGen.typeImports.addKnownType('GenericRuntimeApis', 'GenericRuntimeApiMethod', 'RpcVersion');
17
17
  let runtimeCallsOut = '';
18
18
  if (this.metadata.apis.length > 0) {
19
19
  this.metadata.apis.forEach((runtimeApi) => {
@@ -22,7 +22,7 @@ export class RuntimeApisGen extends ApiGen {
22
22
  runtimeCallsOut += `${stringCamelCase(runtimeApiName)}: {
23
23
  ${methods.map((method) => this.#generateMethodDef(runtimeApiName, method)).join('\n')}
24
24
 
25
- ${commentBlock('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
25
+ ${commentBlock('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod<Rv>
26
26
  }`;
27
27
  });
28
28
  }
@@ -39,11 +39,11 @@ export class RuntimeApisGen extends ApiGen {
39
39
  }))
40
40
  .join('\n')}
41
41
 
42
- ${commentBlock('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
42
+ ${commentBlock('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod<Rv>
43
43
  }`;
44
44
  });
45
45
  }
46
- const importTypes = this.typesGen.typeImports.toImports();
46
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
47
47
  const template = compileTemplate('runtime.hbs');
48
48
  return beautifySourceCode(template({ importTypes, runtimeCallsOut }));
49
49
  }
@@ -68,7 +68,7 @@ export class RuntimeApisGen extends ApiGen {
68
68
  .map(({ name, isOptional, plainType }) => `${stringCamelCase(name)}${isOptional ? '?' : ''}: ${plainType}`)
69
69
  .join(', ');
70
70
  const typeOut = this.#getGeneratedTypeName(type, false);
71
- return `${commentBlock(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
71
+ return `${commentBlock(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<Rv, (${paramsOut}) => Promise<${typeOut}>>`;
72
72
  }
73
73
  #generateMethodDef(runtimeApiName, methodDef) {
74
74
  const { name: methodName, inputs, output, docs } = methodDef;
@@ -89,7 +89,7 @@ export class RuntimeApisGen extends ApiGen {
89
89
  const paramsOut = typedInputs
90
90
  .map(({ name, type, isOptional }) => `${stringCamelCase(name)}${isOptional ? '?' : ''}: ${type}`)
91
91
  .join(', ');
92
- return `${commentBlock(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`))}${stringCamelCase(methodName)}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
92
+ return `${commentBlock(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`))}${stringCamelCase(methodName)}: GenericRuntimeApiMethod<Rv, (${paramsOut}) => Promise<${typeOut}>>`;
93
93
  }
94
94
  #targetRuntimeApiSpecs() {
95
95
  const specs = Object.entries(this.runtimeApis).map(([runtimeApiHash, version]) => {
@@ -1,5 +1,5 @@
1
1
  import { ApiGen } from '../generator/index.js';
2
2
  export declare class TxGen extends ApiGen {
3
3
  #private;
4
- generate(): Promise<string>;
4
+ generate(useSubPaths?: boolean): Promise<string>;
5
5
  }
@@ -1,11 +1,11 @@
1
- import { ApiGen } from '../generator/index.js';
2
1
  import { stringCamelCase, stringPascalCase } from '@dedot/utils';
2
+ import { ApiGen } from '../generator/index.js';
3
3
  import { beautifySourceCode, commentBlock, compileTemplate, isReservedWord } from './utils.js';
4
4
  export class TxGen extends ApiGen {
5
- generate() {
5
+ generate(useSubPaths = false) {
6
6
  const { pallets, types } = this.metadata;
7
7
  this.typesGen.clearCache();
8
- this.typesGen.typeImports.addKnownType('GenericChainTx', 'GenericTxCall', 'ISubmittableExtrinsic', 'ISubmittableResult', 'IRuntimeTxCall');
8
+ this.typesGen.typeImports.addKnownType('GenericChainTx', 'GenericTxCall', 'ISubmittableExtrinsic', 'ISubmittableResult', 'IRuntimeTxCall', 'RpcVersion', 'RpcV2', 'ISubmittableExtrinsicLegacy', 'TransactionStatusLegacy', 'TransactionStatusV2');
9
9
  const { callTypeId, addressTypeId, signatureTypeId } = this.metadata.extrinsic;
10
10
  const callTypeIn = this.typesGen.generateType(callTypeId, 1);
11
11
  const addressTypeIn = this.typesGen.generateType(addressTypeId, 1);
@@ -41,20 +41,22 @@ export class TxGen extends ApiGen {
41
41
  txDefsOut += commentBlock(`Pallet \`${pallet.name}\`'s transaction calls`);
42
42
  txDefsOut += `${stringCamelCase(pallet.name)}: {
43
43
  ${typedTxs
44
- .map(({ functionName, params, docs, callInput }) => `${commentBlock(docs, '\n', params.map((p) => `@param {${p.type}} ${p.normalizedName} ${p.docs}`))}${functionName}: GenericTxCall<(${params.map((p) => `${p.normalizedName}: ${p.type}`).join(', ')}) => ChainSubmittableExtrinsic<${callInput}>>`)
44
+ .map(({ functionName, params, docs, callInput }) => `${commentBlock(docs, '\n', params.map((p) => `@param {${p.type}} ${p.normalizedName} ${p.docs}`))}${functionName}: GenericTxCall<Rv, (${params.map((p) => `${p.normalizedName}: ${p.type}`).join(', ')}) => ChainSubmittableExtrinsic<Rv, ${callInput}>>`)
45
45
  .join(',\n')}
46
46
 
47
- ${commentBlock('Generic pallet tx call')}[callName: string]: GenericTxCall<TxCall>,
47
+ ${commentBlock('Generic pallet tx call')}[callName: string]: GenericTxCall<Rv, TxCall<Rv>>,
48
48
  },`;
49
49
  }
50
- const importTypes = this.typesGen.typeImports.toImports();
50
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
51
51
  // TODO make explicit separate type for Extra
52
52
  const defTypes = `
53
- export type ChainSubmittableExtrinsic<T extends IRuntimeTxCall = ${callTypeIn}> =
53
+ export type ChainSubmittableExtrinsic<Rv extends RpcVersion, T extends IRuntimeTxCall = ${callTypeIn}> =
54
54
  Extrinsic<${addressTypeIn}, T, ${signatureTypeIn}, any[]> &
55
- ISubmittableExtrinsic<ISubmittableResult<FrameSystemEventRecord>>
55
+ (Rv extends RpcV2
56
+ ? ISubmittableExtrinsic<ISubmittableResult<FrameSystemEventRecord, TransactionStatusV2>>
57
+ : ISubmittableExtrinsicLegacy<ISubmittableResult<FrameSystemEventRecord, TransactionStatusLegacy>>)
56
58
 
57
- export type TxCall = (...args: any[]) => ChainSubmittableExtrinsic;
59
+ export type TxCall<Rv extends RpcVersion> = (...args: any[]) => ChainSubmittableExtrinsic<Rv>;
58
60
  `;
59
61
  const template = compileTemplate('tx.hbs');
60
62
  return beautifySourceCode(template({ importTypes, defTypes, txDefsOut }));
@@ -1,16 +1,21 @@
1
+ type ImportConfig = {
2
+ excludeModules?: string[];
3
+ useSubPaths?: boolean;
4
+ };
1
5
  export declare class TypeImports {
2
6
  #private;
3
7
  portableTypes: Set<string>;
4
8
  codecTypes: Set<string>;
5
9
  knownTypes: Set<string>;
6
- specTypes: Set<string>;
10
+ knownJsonRpcTypes: Set<string>;
7
11
  outTypes: Set<string>;
8
12
  constructor();
9
13
  clear(): void;
10
- toImports(...excludeModules: string[]): string;
14
+ toImports(config?: ImportConfig): string;
11
15
  addPortableType(...types: string[]): void;
12
16
  addCodecType(...types: string[]): void;
13
17
  addKnownType(...types: string[]): void;
14
- addSpecType(...types: string[]): void;
18
+ addKnownJsonRpcType(...types: string[]): void;
15
19
  addOutType(...types: string[]): void;
16
20
  }
21
+ export {};
@@ -5,29 +5,31 @@ export class TypeImports {
5
5
  codecTypes;
6
6
  // Known types that're not codecs or chain/portable types defined in @dedot/types
7
7
  knownTypes;
8
- specTypes;
8
+ knownJsonRpcTypes;
9
9
  // External types to define explicitly
10
10
  outTypes;
11
11
  constructor() {
12
12
  this.portableTypes = new Set();
13
13
  this.codecTypes = new Set();
14
14
  this.knownTypes = new Set();
15
- this.specTypes = new Set();
15
+ this.knownJsonRpcTypes = new Set();
16
16
  this.outTypes = new Set();
17
17
  }
18
18
  clear() {
19
19
  this.portableTypes.clear();
20
20
  this.codecTypes.clear();
21
21
  this.knownTypes.clear();
22
- this.specTypes.clear();
22
+ this.knownJsonRpcTypes.clear();
23
23
  this.outTypes.clear();
24
24
  }
25
- toImports(...excludeModules) {
25
+ toImports(config) {
26
+ const { excludeModules = [], useSubPaths = false } = config || {};
26
27
  // TODO generate outTypes!
28
+ const prefix = useSubPaths ? '' : '@';
27
29
  const toImports = [
28
- [this.knownTypes, '@dedot/types'],
29
- [this.specTypes, '@dedot/specs'],
30
- [this.codecTypes, '@dedot/codecs'],
30
+ [this.knownTypes, `${prefix}dedot/types`],
31
+ [this.knownJsonRpcTypes, `${prefix}dedot/types/json-rpc`],
32
+ [this.codecTypes, `${prefix}dedot/codecs`],
31
33
  [this.portableTypes, './types'],
32
34
  ];
33
35
  return toImports
@@ -51,8 +53,8 @@ export class TypeImports {
51
53
  addKnownType(...types) {
52
54
  types.forEach((one) => this.knownTypes.add(one));
53
55
  }
54
- addSpecType(...types) {
55
- types.forEach((one) => this.specTypes.add(one));
56
+ addKnownJsonRpcType(...types) {
57
+ types.forEach((one) => this.knownJsonRpcTypes.add(one));
56
58
  }
57
59
  addOutType(...types) {
58
60
  types.forEach((one) => this.outTypes.add(one));
@@ -18,7 +18,7 @@ export declare class TypesGen {
18
18
  registry: PortableRegistry;
19
19
  typeImports: TypeImports;
20
20
  constructor(metadata: MetadataLatest);
21
- generate(): Promise<string>;
21
+ generate(useSubPaths?: boolean): Promise<string>;
22
22
  typeCache: Record<string, string>;
23
23
  clearCache(): void;
24
24
  generateType(typeId: TypeId, nestedLevel?: number, typeOut?: boolean): string;
@@ -1,8 +1,8 @@
1
1
  import { PortableRegistry } from '@dedot/codecs';
2
2
  import { normalizeName, stringPascalCase } from '@dedot/utils';
3
- import { beautifySourceCode, commentBlock, compileTemplate, isNativeType } from './utils.js';
4
3
  import { TypeImports } from './TypeImports.js';
5
4
  import { findKnownCodec, findKnownCodecType, isKnownCodecType } from './known-codecs.js';
5
+ import { beautifySourceCode, commentBlock, compileTemplate, isNativeType } from './utils.js';
6
6
  // Skip generate types for these
7
7
  // as we do have native types for them
8
8
  const SKIP_TYPES = [
@@ -40,7 +40,7 @@ export class TypesGen {
40
40
  this.includedTypes = this.#includedTypes();
41
41
  this.typeImports = new TypeImports();
42
42
  }
43
- generate() {
43
+ generate(useSubPaths = false) {
44
44
  this.clearCache();
45
45
  let defTypeOut = '';
46
46
  Object.values(this.includedTypes)
@@ -51,7 +51,7 @@ export class TypesGen {
51
51
  defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
52
52
  }
53
53
  });
54
- const importTypes = this.typeImports.toImports('./types');
54
+ const importTypes = this.typeImports.toImports({ useSubPaths, excludeModules: ['./types'] });
55
55
  const template = compileTemplate('types.hbs');
56
56
  return beautifySourceCode(template({ importTypes, defTypeOut }));
57
57
  }
@@ -1,5 +1,5 @@
1
- import * as $ from '@dedot/shape';
2
1
  import { AnyShape } from '@dedot/shape';
2
+ import * as $ from '@dedot/shape';
3
3
  export type CodecName = `$${string}`;
4
4
  export interface CodecType {
5
5
  name: CodecName;
@@ -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}`;
package/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import { MetadataLatest } from '@dedot/codecs';
2
- export declare function generateTypesFromEndpoint(chain: string, endpoint: string, outDir?: string, extension?: string): Promise<void>;
3
- export declare function generateTypes(chain: string, metadata: MetadataLatest, rpcMethods: string[], runtimeApis: Record<string, number>, outDir?: string, extension?: string): Promise<void>;
2
+ export declare function generateTypesFromEndpoint(chain: string, endpoint: string, outDir?: string, extension?: string, useSubPaths?: boolean): Promise<void>;
3
+ export declare function generateTypes(chain: string, metadata: MetadataLatest, rpcMethods: string[], runtimeApis: Record<string, number>, outDir?: string, extension?: string, useSubPaths?: boolean): Promise<void>;
package/index.js CHANGED
@@ -1,19 +1,20 @@
1
- import { Dedot } from 'dedot';
1
+ import { Dedot } from '@dedot/api';
2
+ import { WsProvider } from '@dedot/providers';
3
+ import { stringCamelCase } from '@dedot/utils';
2
4
  import * as fs from 'fs';
3
5
  import * as path from 'path';
4
- import { ConstsGen, ErrorsGen, EventsGen, IndexGen, QueryGen, JsonRpcGen, RuntimeApisGen, TxGen, TypesGen, } from './generator/index.js';
5
- import { stringCamelCase } from '@dedot/utils';
6
- export async function generateTypesFromEndpoint(chain, endpoint, outDir, extension = 'd.ts') {
7
- const api = await Dedot.new(endpoint);
6
+ import { ConstsGen, ErrorsGen, EventsGen, IndexGen, JsonRpcGen, QueryGen, RuntimeApisGen, TxGen, TypesGen, } from './generator/index.js';
7
+ export async function generateTypesFromEndpoint(chain, endpoint, outDir, extension = 'd.ts', useSubPaths = false) {
8
+ const api = await Dedot.new(new WsProvider(endpoint));
8
9
  const { methods } = await api.rpc.rpc_methods();
9
10
  const apis = api.runtimeVersion.apis || {};
10
11
  if (!chain) {
11
- chain = stringCamelCase(api.runtimeVersion.specName || api.runtimeChain || 'local');
12
+ chain = stringCamelCase(api.runtimeVersion.specName || 'local');
12
13
  }
13
- await generateTypes(chain, api.metadata.latest, methods, apis, outDir, extension);
14
+ await generateTypes(chain, api.metadata.latest, methods, apis, outDir, extension, useSubPaths);
14
15
  await api.disconnect();
15
16
  }
16
- export async function generateTypes(chain, metadata, rpcMethods, runtimeApis, outDir = '.', extension = 'd.ts') {
17
+ export async function generateTypes(chain, metadata, rpcMethods, runtimeApis, outDir = '.', extension = 'd.ts', useSubPaths = false) {
17
18
  const dirPath = path.resolve(outDir, chain);
18
19
  const defTypesFileName = path.join(dirPath, `types.${extension}`);
19
20
  const constsTypesFileName = path.join(dirPath, `consts.${extension}`);
@@ -36,13 +37,13 @@ export async function generateTypes(chain, metadata, rpcMethods, runtimeApis, ou
36
37
  const eventsGen = new EventsGen(typesGen);
37
38
  const runtimeApisGen = new RuntimeApisGen(typesGen, runtimeApis);
38
39
  const txGen = new TxGen(typesGen);
39
- fs.writeFileSync(defTypesFileName, await typesGen.generate());
40
- fs.writeFileSync(errorsFileName, await errorsGen.generate());
41
- fs.writeFileSync(eventsFileName, await eventsGen.generate());
42
- fs.writeFileSync(jsonRpcFileName, await jsonRpcGen.generate());
43
- fs.writeFileSync(queryTypesFileName, await queryGen.generate());
44
- fs.writeFileSync(constsTypesFileName, await constsGen.generate());
45
- fs.writeFileSync(txFileName, await txGen.generate());
46
- fs.writeFileSync(indexFileName, await indexGen.generate());
47
- fs.writeFileSync(runtimeApisFileName, await runtimeApisGen.generate());
40
+ fs.writeFileSync(defTypesFileName, await typesGen.generate(useSubPaths));
41
+ fs.writeFileSync(errorsFileName, await errorsGen.generate(useSubPaths));
42
+ fs.writeFileSync(eventsFileName, await eventsGen.generate(useSubPaths));
43
+ fs.writeFileSync(jsonRpcFileName, await jsonRpcGen.generate(useSubPaths));
44
+ fs.writeFileSync(queryTypesFileName, await queryGen.generate(useSubPaths));
45
+ fs.writeFileSync(constsTypesFileName, await constsGen.generate(useSubPaths));
46
+ fs.writeFileSync(txFileName, await txGen.generate(useSubPaths));
47
+ fs.writeFileSync(indexFileName, await indexGen.generate(useSubPaths));
48
+ fs.writeFileSync(runtimeApisFileName, await runtimeApisGen.generate(useSubPaths));
48
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dedot/codegen",
3
- "version": "0.0.1-next.c3d95ac1.3+c3d95ac",
3
+ "version": "0.0.1-next.d9aff57d.57+d9aff57",
4
4
  "description": "Generate types",
5
5
  "author": "Thang X. Vu <thang@coongcrafts.io>",
6
6
  "homepage": "https://github.com/dedotdev/dedot/tree/main/packages/codegen",
@@ -18,32 +18,30 @@
18
18
  "copy": "cp -R ./src/templates ./dist && cp -R ./src/templates ./dist/cjs"
19
19
  },
20
20
  "dependencies": {
21
- "@dedot/codecs": "0.0.1-next.c3d95ac1.3+c3d95ac",
22
- "@dedot/shape": "0.0.1-next.c3d95ac1.3+c3d95ac",
23
- "@dedot/specs": "0.0.1-next.c3d95ac1.3+c3d95ac",
24
- "@dedot/utils": "0.0.1-next.c3d95ac1.3+c3d95ac",
25
- "dedot": "0.0.1-next.c3d95ac1.3+c3d95ac",
26
- "handlebars": "^4.7.8",
27
- "prettier": "^3.0.3"
21
+ "@dedot/api": "0.0.1-next.d9aff57d.57+d9aff57",
22
+ "@dedot/codecs": "0.0.1-next.d9aff57d.57+d9aff57",
23
+ "@dedot/providers": "0.0.1-next.d9aff57d.57+d9aff57",
24
+ "@dedot/runtime-specs": "0.0.1-next.d9aff57d.57+d9aff57",
25
+ "@dedot/shape": "0.0.1-next.d9aff57d.57+d9aff57",
26
+ "@dedot/types": "0.0.1-next.d9aff57d.57+d9aff57",
27
+ "@dedot/utils": "0.0.1-next.d9aff57d.57+d9aff57",
28
+ "handlebars": "^4.7.7",
29
+ "prettier": "^3.2.5"
28
30
  },
29
31
  "publishConfig": {
30
32
  "access": "public",
31
33
  "directory": "dist"
32
34
  },
33
35
  "license": "Apache-2.0",
34
- "gitHead": "c3d95ac146c0f96577f1984aac754b3f88d13d91",
36
+ "gitHead": "d9aff57dcbd8e15455da9627cbc43f07cb6c0afc",
35
37
  "module": "./index.js",
36
38
  "types": "./index.d.ts",
37
39
  "exports": {
38
40
  ".": {
39
- "import": {
40
- "types": "./index.d.ts",
41
- "default": "./index.js"
42
- },
43
- "require": {
44
- "types": "./index.d.ts",
45
- "default": "./cjs/index.js"
46
- }
41
+ "types": "./index.d.ts",
42
+ "import": "./index.js",
43
+ "require": "./cjs/index.js",
44
+ "default": "./index.js"
47
45
  }
48
46
  }
49
47
  }