@dedot/codegen 0.0.1-next.8f4b2c24.6 → 0.0.1-next.9c1d51fe.10

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 (128) hide show
  1. package/{generator → chaintypes/generator}/ApiGen.d.ts +4 -4
  2. package/{generator → chaintypes/generator}/ConstsGen.d.ts +1 -1
  3. package/{generator → chaintypes/generator}/ConstsGen.js +5 -5
  4. package/{generator → chaintypes/generator}/ErrorsGen.d.ts +1 -1
  5. package/{generator → chaintypes/generator}/ErrorsGen.js +8 -8
  6. package/{generator → chaintypes/generator}/EventsGen.d.ts +1 -1
  7. package/{generator → chaintypes/generator}/EventsGen.js +8 -8
  8. package/{generator → chaintypes/generator}/IndexGen.d.ts +1 -1
  9. package/chaintypes/generator/IndexGen.js +17 -0
  10. package/{generator → chaintypes/generator}/JsonRpcGen.d.ts +3 -2
  11. package/{generator → chaintypes/generator}/JsonRpcGen.js +8 -8
  12. package/{generator → chaintypes/generator}/QueryGen.d.ts +1 -1
  13. package/{generator → chaintypes/generator}/QueryGen.js +8 -8
  14. package/{generator → chaintypes/generator}/RuntimeApisGen.d.ts +2 -2
  15. package/{generator → chaintypes/generator}/RuntimeApisGen.js +11 -11
  16. package/{generator → chaintypes/generator}/TxGen.d.ts +1 -1
  17. package/{generator → chaintypes/generator}/TxGen.js +13 -11
  18. package/chaintypes/generator/TypesGen.d.ts +11 -0
  19. package/chaintypes/generator/TypesGen.js +55 -0
  20. package/chaintypes/index.d.ts +3 -0
  21. package/chaintypes/index.js +49 -0
  22. package/chaintypes/templates/consts.hbs +7 -0
  23. package/chaintypes/templates/errors.hbs +7 -0
  24. package/chaintypes/templates/events.hbs +7 -0
  25. package/chaintypes/templates/index.hbs +27 -0
  26. package/chaintypes/templates/json-rpc.hbs +5 -0
  27. package/chaintypes/templates/query.hbs +7 -0
  28. package/chaintypes/templates/runtime.hbs +8 -0
  29. package/{cjs → chaintypes}/templates/tx.hbs +1 -1
  30. package/cjs/{generator → chaintypes/generator}/ConstsGen.js +5 -5
  31. package/cjs/{generator → chaintypes/generator}/ErrorsGen.js +8 -8
  32. package/cjs/{generator → chaintypes/generator}/EventsGen.js +8 -8
  33. package/cjs/chaintypes/generator/IndexGen.js +21 -0
  34. package/cjs/{generator → chaintypes/generator}/JsonRpcGen.js +10 -10
  35. package/cjs/{generator → chaintypes/generator}/QueryGen.js +8 -8
  36. package/cjs/{generator → chaintypes/generator}/RuntimeApisGen.js +14 -14
  37. package/cjs/{generator → chaintypes/generator}/TxGen.js +13 -11
  38. package/cjs/chaintypes/generator/TypesGen.js +59 -0
  39. package/cjs/chaintypes/index.js +77 -0
  40. package/cjs/chaintypes/templates/consts.hbs +7 -0
  41. package/cjs/chaintypes/templates/errors.hbs +7 -0
  42. package/cjs/chaintypes/templates/events.hbs +7 -0
  43. package/cjs/chaintypes/templates/index.hbs +27 -0
  44. package/cjs/chaintypes/templates/json-rpc.hbs +5 -0
  45. package/cjs/chaintypes/templates/query.hbs +7 -0
  46. package/cjs/chaintypes/templates/runtime.hbs +8 -0
  47. package/{templates → cjs/chaintypes/templates}/tx.hbs +1 -1
  48. package/cjs/index.js +4 -62
  49. package/cjs/{generator/TypesGen.js → shared/BaseTypesGen.js} +88 -140
  50. package/cjs/{generator → shared}/TypeImports.js +19 -9
  51. package/cjs/shared/index.js +19 -0
  52. package/cjs/{generator → shared}/known-codecs.js +24 -5
  53. package/cjs/typink/generator/ConstructorQueryGen.js +23 -0
  54. package/cjs/typink/generator/ConstructorTxGen.js +23 -0
  55. package/cjs/typink/generator/IndexGen.js +23 -0
  56. package/cjs/typink/generator/QueryGen.js +71 -0
  57. package/cjs/typink/generator/TxGen.js +24 -0
  58. package/cjs/typink/generator/TypesGen.js +35 -0
  59. package/cjs/typink/generator/index.js +22 -0
  60. package/cjs/typink/index.js +37 -0
  61. package/cjs/typink/templates/constructor-query.hbs +8 -0
  62. package/cjs/typink/templates/constructor-tx.hbs +8 -0
  63. package/cjs/typink/templates/index.hbs +15 -0
  64. package/cjs/typink/templates/query.hbs +8 -0
  65. package/cjs/typink/templates/tx.hbs +8 -0
  66. package/cjs/typink/templates/types.hbs +5 -0
  67. package/cjs/{generator/utils.js → utils.js} +3 -3
  68. package/index.d.ts +2 -3
  69. package/index.js +2 -48
  70. package/package.json +16 -18
  71. package/{generator/TypesGen.d.ts → shared/BaseTypesGen.d.ts} +12 -11
  72. package/{generator/TypesGen.js → shared/BaseTypesGen.js} +85 -137
  73. package/{generator → shared}/TypeImports.d.ts +10 -3
  74. package/{generator → shared}/TypeImports.js +19 -9
  75. package/shared/index.d.ts +3 -0
  76. package/shared/index.js +3 -0
  77. package/{generator → shared}/known-codecs.d.ts +1 -1
  78. package/{generator → shared}/known-codecs.js +22 -3
  79. package/typink/generator/ConstructorQueryGen.d.ts +6 -0
  80. package/typink/generator/ConstructorQueryGen.js +19 -0
  81. package/typink/generator/ConstructorTxGen.d.ts +6 -0
  82. package/typink/generator/ConstructorTxGen.js +19 -0
  83. package/typink/generator/IndexGen.d.ts +6 -0
  84. package/typink/generator/IndexGen.js +19 -0
  85. package/typink/generator/QueryGen.d.ts +12 -0
  86. package/typink/generator/QueryGen.js +67 -0
  87. package/typink/generator/TxGen.d.ts +6 -0
  88. package/typink/generator/TxGen.js +20 -0
  89. package/typink/generator/TypesGen.d.ts +8 -0
  90. package/typink/generator/TypesGen.js +31 -0
  91. package/typink/generator/index.d.ts +6 -0
  92. package/typink/generator/index.js +6 -0
  93. package/typink/index.d.ts +2 -0
  94. package/typink/index.js +30 -0
  95. package/typink/templates/constructor-query.hbs +8 -0
  96. package/typink/templates/constructor-tx.hbs +8 -0
  97. package/typink/templates/index.hbs +15 -0
  98. package/typink/templates/query.hbs +8 -0
  99. package/typink/templates/tx.hbs +8 -0
  100. package/typink/templates/types.hbs +5 -0
  101. package/{generator/utils.d.ts → utils.d.ts} +1 -1
  102. package/{generator/utils.js → utils.js} +3 -3
  103. package/cjs/generator/IndexGen.js +0 -17
  104. package/cjs/templates/consts.hbs +0 -7
  105. package/cjs/templates/errors.hbs +0 -7
  106. package/cjs/templates/events.hbs +0 -7
  107. package/cjs/templates/index.hbs +0 -22
  108. package/cjs/templates/json-rpc.hbs +0 -5
  109. package/cjs/templates/query.hbs +0 -7
  110. package/cjs/templates/runtime.hbs +0 -8
  111. package/generator/IndexGen.js +0 -13
  112. package/templates/consts.hbs +0 -7
  113. package/templates/errors.hbs +0 -7
  114. package/templates/events.hbs +0 -7
  115. package/templates/index.hbs +0 -22
  116. package/templates/json-rpc.hbs +0 -5
  117. package/templates/query.hbs +0 -7
  118. package/templates/runtime.hbs +0 -8
  119. /package/{generator → chaintypes/generator}/ApiGen.js +0 -0
  120. /package/{generator → chaintypes/generator}/index.d.ts +0 -0
  121. /package/{generator → chaintypes/generator}/index.js +0 -0
  122. /package/{cjs → chaintypes}/templates/types.hbs +0 -0
  123. /package/cjs/{generator → chaintypes/generator}/ApiGen.js +0 -0
  124. /package/cjs/{generator → chaintypes/generator}/index.js +0 -0
  125. /package/{templates → cjs/chaintypes/templates}/types.hbs +0 -0
  126. /package/cjs/{generator/dirname.js → dirname.js} +0 -0
  127. /package/{generator/dirname.d.ts → dirname.d.ts} +0 -0
  128. /package/{generator/dirname.js → dirname.js} +0 -0
@@ -0,0 +1,31 @@
1
+ import { extractContractTypes } from '@dedot/contracts';
2
+ import { BaseTypesGen } from '../../shared/index.js';
3
+ import { beautifySourceCode, compileTemplate } from '../../utils.js';
4
+ const SKIP_TYPES = ['Result', 'Option'];
5
+ export class TypesGen extends BaseTypesGen {
6
+ contractMetadata;
7
+ constructor(contractMetadata) {
8
+ super(extractContractTypes(contractMetadata));
9
+ this.contractMetadata = contractMetadata;
10
+ this.skipTypes = SKIP_TYPES;
11
+ this.includedTypes = this.includeTypes();
12
+ }
13
+ generate(useSubPaths = false) {
14
+ let defTypeOut = '';
15
+ Object.values(this.includedTypes)
16
+ .filter(({ skip, knownType }) => !(skip || knownType))
17
+ .forEach(({ name, nameOut, id }) => {
18
+ defTypeOut += `export type ${nameOut} = ${this.generateType(id, 0, true)};\n\n`;
19
+ if (this.shouldGenerateTypeIn(id)) {
20
+ defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
21
+ }
22
+ });
23
+ const importTypes = this.typeImports.toImports({ excludeModules: ['./types'], useSubPaths });
24
+ const template = compileTemplate('typink/templates/types.hbs');
25
+ return beautifySourceCode(template({ importTypes, defTypeOut }));
26
+ }
27
+ shouldGenerateTypeIn(id) {
28
+ const { messages } = this.contractMetadata.spec;
29
+ return messages.some((message) => message.returnType.type === id);
30
+ }
31
+ }
@@ -0,0 +1,6 @@
1
+ export * from './IndexGen.js';
2
+ export * from './QueryGen.js';
3
+ export * from './TypesGen.js';
4
+ export * from './TxGen.js';
5
+ export * from './ConstructorTxGen.js';
6
+ export * from './ConstructorQueryGen.js';
@@ -0,0 +1,6 @@
1
+ export * from './IndexGen.js';
2
+ export * from './QueryGen.js';
3
+ export * from './TypesGen.js';
4
+ export * from './TxGen.js';
5
+ export * from './ConstructorTxGen.js';
6
+ export * from './ConstructorQueryGen.js';
@@ -0,0 +1,2 @@
1
+ import { ContractMetadata } from '@dedot/contracts';
2
+ export declare function generateContractTypesFromMetadata(metadata: ContractMetadata | string, contract?: string, outDir?: string, extension?: string, useSubPaths?: boolean): Promise<void>;
@@ -0,0 +1,30 @@
1
+ import { parseRawMetadata } from '@dedot/contracts';
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+ import { IndexGen, QueryGen, TxGen, TypesGen, ConstructorTxGen, ConstructorQueryGen } from './generator/index.js';
5
+ export async function generateContractTypesFromMetadata(metadata, contract, outDir = '.', extension = 'd.ts', useSubPaths = false) {
6
+ let contractMetadata = typeof metadata === 'string' ? parseRawMetadata(metadata) : metadata;
7
+ contract = contract || contractMetadata.contract.name;
8
+ const dirPath = path.resolve(outDir, contract);
9
+ const typesFileName = path.join(dirPath, `types.${extension}`);
10
+ const queryTypesFileName = path.join(dirPath, `query.${extension}`);
11
+ const txTypesFileName = path.join(dirPath, `tx.${extension}`);
12
+ const constructorTxTypesFileName = path.join(dirPath, `constructor-tx.${extension}`);
13
+ const constructorQueryTypesFileName = path.join(dirPath, `constructor-query.${extension}`);
14
+ const indexTypesFileName = path.join(dirPath, `index.${extension}`);
15
+ if (!fs.existsSync(dirPath)) {
16
+ fs.mkdirSync(dirPath, { recursive: true });
17
+ }
18
+ const typesGen = new TypesGen(contractMetadata);
19
+ const queryGen = new QueryGen(contractMetadata, typesGen);
20
+ const txGen = new TxGen(contractMetadata, typesGen);
21
+ const constructorTxGen = new ConstructorTxGen(contractMetadata, typesGen);
22
+ const constructorQueryGen = new ConstructorQueryGen(contractMetadata, typesGen);
23
+ const indexGen = new IndexGen(contractMetadata);
24
+ fs.writeFileSync(typesFileName, await typesGen.generate(useSubPaths));
25
+ fs.writeFileSync(queryTypesFileName, await queryGen.generate(useSubPaths));
26
+ fs.writeFileSync(txTypesFileName, await txGen.generate(useSubPaths));
27
+ fs.writeFileSync(constructorQueryTypesFileName, await constructorQueryGen.generate(useSubPaths));
28
+ fs.writeFileSync(constructorTxTypesFileName, await constructorTxGen.generate(useSubPaths));
29
+ fs.writeFileSync(indexTypesFileName, await indexGen.generate(useSubPaths));
30
+ }
@@ -0,0 +1,8 @@
1
+ // Generated by @dedot/codegen
2
+
3
+ import { GenericSubstrateApi } from '@dedot/types';
4
+ {{{ importTypes }}}
5
+
6
+ export interface ConstructorQuery<ChainApi extends GenericSubstrateApi> extends GenericConstructorQuery<ChainApi> {
7
+ {{{ constructorsOut }}}
8
+ }
@@ -0,0 +1,8 @@
1
+ // Generated by @dedot/codegen
2
+
3
+ import { GenericSubstrateApi } from '@dedot/types';
4
+ {{{ importTypes }}}
5
+
6
+ export interface ConstructorTx<ChainApi extends GenericSubstrateApi> extends GenericConstructorTx<ChainApi> {
7
+ {{{ constructorsOut }}}
8
+ }
@@ -0,0 +1,15 @@
1
+ // Generated by @dedot/codegen
2
+
3
+ {{{importTypes}}}
4
+ import { SubstrateApi } from 'dedot/chaintypes';
5
+ import { ContractQuery } from './query';
6
+ import { ContractTx } from './tx';
7
+ import { ConstructorQuery } from './constructor-query';
8
+ import { ConstructorTx } from './constructor-tx';
9
+
10
+ export interface {{{contractName}}}<ChainApi extends VersionedGenericSubstrateApi = SubstrateApi> extends GenericContractApi<ChainApi[RpcVersion]> {
11
+ query: ContractQuery<ChainApi[RpcVersion]>;
12
+ tx: ContractTx<ChainApi[RpcVersion]>;
13
+ constructorQuery: ConstructorQuery<ChainApi[RpcVersion]>;
14
+ constructorTx: ConstructorTx<ChainApi[RpcVersion]>;
15
+ }
@@ -0,0 +1,8 @@
1
+ // Generated by @dedot/codegen
2
+
3
+ import { GenericSubstrateApi } from '@dedot/types';
4
+ {{{ importTypes }}}
5
+
6
+ export interface ContractQuery<ChainApi extends GenericSubstrateApi> extends GenericContractQuery<ChainApi> {
7
+ {{{ queryCallsOut }}}
8
+ }
@@ -0,0 +1,8 @@
1
+ // Generated by @dedot/codegen
2
+
3
+ import { GenericSubstrateApi } from '@dedot/types';
4
+ {{{ importTypes }}}
5
+
6
+ export interface ContractTx<ChainApi extends GenericSubstrateApi> extends GenericContractTx<ChainApi> {
7
+ {{{ txCallsOut }}}
8
+ }
@@ -0,0 +1,5 @@
1
+ // Generated by @dedot/codegen
2
+
3
+ {{{ importTypes }}}
4
+
5
+ {{{ defTypeOut }}}
@@ -2,7 +2,7 @@ export declare const WRAPPER_TYPE_REGEX: RegExp;
2
2
  export declare const TUPLE_TYPE_REGEX: RegExp;
3
3
  export declare const commentBlock: (...docs: (string | string[])[]) => string;
4
4
  export declare const beautifySourceCode: (source: string) => Promise<string>;
5
- export declare const compileTemplate: (templateFileName: string) => HandlebarsTemplateDelegate<any>;
5
+ export declare const compileTemplate: (templateFile: string) => HandlebarsTemplateDelegate<any>;
6
6
  /**
7
7
  * Check if a word is TypeScript/JavaScript reserved
8
8
  * @param word
@@ -7,7 +7,7 @@ export const WRAPPER_TYPE_REGEX = /^(\w+)<(.*)>$/;
7
7
  export const TUPLE_TYPE_REGEX = /^\[(.*)]$/;
8
8
  export const commentBlock = (...docs) => {
9
9
  const flatLines = docs.flat();
10
- if (flatLines.length === 0) {
10
+ if (flatLines.length === 0 || !flatLines.find((o) => !!o)) {
11
11
  return '';
12
12
  }
13
13
  else {
@@ -22,8 +22,8 @@ export const beautifySourceCode = async (source) => {
22
22
  const prettierOptions = await prettier.resolveConfig(path.resolve(currentDirname(), '../../../../.prettierrc.js'));
23
23
  return prettier.format(source, { parser: 'babel-ts', ...prettierOptions });
24
24
  };
25
- export const compileTemplate = (templateFileName) => {
26
- const templateFilePath = path.resolve(currentDirname(), `../templates/${templateFileName}`);
25
+ export const compileTemplate = (templateFile) => {
26
+ const templateFilePath = path.resolve(currentDirname(), templateFile);
27
27
  return handlebars.compile(fs.readFileSync(templateFilePath, 'utf8'));
28
28
  };
29
29
  // TODO add more reserved words
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.IndexGen = void 0;
4
- const utils_js_1 = require("./utils.js");
5
- const utils_1 = require("@dedot/utils");
6
- class IndexGen {
7
- chain;
8
- constructor(chain) {
9
- this.chain = chain;
10
- }
11
- async generate() {
12
- const interfaceName = (0, utils_1.stringPascalCase)(this.chain);
13
- const template = (0, utils_js_1.compileTemplate)('index.hbs');
14
- return (0, utils_js_1.beautifySourceCode)(template({ interfaceName }));
15
- }
16
- }
17
- exports.IndexGen = IndexGen;
@@ -1,7 +0,0 @@
1
- // Generated by @dedot/codegen
2
-
3
- {{{ importTypes }}}
4
-
5
- export interface ChainConsts extends GenericChainConsts {
6
- {{{ defTypeOut }}}
7
- }
@@ -1,7 +0,0 @@
1
- // Generated by @dedot/codegen
2
-
3
- {{{ importTypes }}}
4
-
5
- export interface ChainErrors extends GenericChainErrors {
6
- {{{ defTypeOut }}}
7
- }
@@ -1,7 +0,0 @@
1
- // Generated by @dedot/codegen
2
-
3
- {{{ importTypes }}}
4
-
5
- export interface ChainEvents extends GenericChainEvents {
6
- {{{ defTypeOut }}}
7
- }
@@ -1,22 +0,0 @@
1
- // Generated by @dedot/codegen
2
-
3
- import { GenericSubstrateApi } from '@dedot/types';
4
- import { ChainConsts } from './consts';
5
- import { ChainStorage } from './query';
6
- import { ChainJsonRpcApis } from './json-rpc';
7
- import { ChainErrors } from './errors';
8
- import { ChainEvents } from './events';
9
- import { RuntimeApis } from './runtime';
10
- import { ChainTx } from './tx';
11
-
12
- export * from './types';
13
-
14
- export interface {{{interfaceName}}}Api extends GenericSubstrateApi {
15
- rpc: ChainJsonRpcApis;
16
- consts: ChainConsts;
17
- query: ChainStorage;
18
- errors: ChainErrors;
19
- events: ChainEvents;
20
- call: RuntimeApis;
21
- tx: ChainTx;
22
- }
@@ -1,5 +0,0 @@
1
- // Generated by @dedot/codegen
2
-
3
- {{{ importTypes }}}
4
-
5
- export type ChainJsonRpcApis = Pick<JsonRpcApis, {{{jsonRpcMethods}}}> & GenericJsonRpcApis;
@@ -1,7 +0,0 @@
1
- // Generated by @dedot/codegen
2
-
3
- {{{ importTypes }}}
4
-
5
- export interface ChainStorage extends GenericChainStorage {
6
- {{{ defTypeOut }}}
7
- }
@@ -1,8 +0,0 @@
1
- // Generated by @dedot/codegen
2
-
3
- {{{ importTypes }}}
4
-
5
- export interface RuntimeApis extends GenericRuntimeApis {
6
- {{{ runtimeCallsOut }}}
7
- }
8
-
@@ -1,13 +0,0 @@
1
- import { beautifySourceCode, compileTemplate } from './utils.js';
2
- import { stringPascalCase } from '@dedot/utils';
3
- export class IndexGen {
4
- chain;
5
- constructor(chain) {
6
- this.chain = chain;
7
- }
8
- async generate() {
9
- const interfaceName = stringPascalCase(this.chain);
10
- const template = compileTemplate('index.hbs');
11
- return beautifySourceCode(template({ interfaceName }));
12
- }
13
- }
@@ -1,7 +0,0 @@
1
- // Generated by @dedot/codegen
2
-
3
- {{{ importTypes }}}
4
-
5
- export interface ChainConsts extends GenericChainConsts {
6
- {{{ defTypeOut }}}
7
- }
@@ -1,7 +0,0 @@
1
- // Generated by @dedot/codegen
2
-
3
- {{{ importTypes }}}
4
-
5
- export interface ChainErrors extends GenericChainErrors {
6
- {{{ defTypeOut }}}
7
- }
@@ -1,7 +0,0 @@
1
- // Generated by @dedot/codegen
2
-
3
- {{{ importTypes }}}
4
-
5
- export interface ChainEvents extends GenericChainEvents {
6
- {{{ defTypeOut }}}
7
- }
@@ -1,22 +0,0 @@
1
- // Generated by @dedot/codegen
2
-
3
- import { GenericSubstrateApi } from '@dedot/types';
4
- import { ChainConsts } from './consts';
5
- import { ChainStorage } from './query';
6
- import { ChainJsonRpcApis } from './json-rpc';
7
- import { ChainErrors } from './errors';
8
- import { ChainEvents } from './events';
9
- import { RuntimeApis } from './runtime';
10
- import { ChainTx } from './tx';
11
-
12
- export * from './types';
13
-
14
- export interface {{{interfaceName}}}Api extends GenericSubstrateApi {
15
- rpc: ChainJsonRpcApis;
16
- consts: ChainConsts;
17
- query: ChainStorage;
18
- errors: ChainErrors;
19
- events: ChainEvents;
20
- call: RuntimeApis;
21
- tx: ChainTx;
22
- }
@@ -1,5 +0,0 @@
1
- // Generated by @dedot/codegen
2
-
3
- {{{ importTypes }}}
4
-
5
- export type ChainJsonRpcApis = Pick<JsonRpcApis, {{{jsonRpcMethods}}}> & GenericJsonRpcApis;
@@ -1,7 +0,0 @@
1
- // Generated by @dedot/codegen
2
-
3
- {{{ importTypes }}}
4
-
5
- export interface ChainStorage extends GenericChainStorage {
6
- {{{ defTypeOut }}}
7
- }
@@ -1,8 +0,0 @@
1
- // Generated by @dedot/codegen
2
-
3
- {{{ importTypes }}}
4
-
5
- export interface RuntimeApis extends GenericRuntimeApis {
6
- {{{ runtimeCallsOut }}}
7
- }
8
-
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes