@dedot/codegen 0.14.1 → 0.15.1

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.
@@ -154,7 +154,7 @@ export declare abstract class ApiGen {
154
154
  docs: string[];
155
155
  }[];
156
156
  viewFunctions: {
157
- id: readonly [number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number];
157
+ id: Uint8Array;
158
158
  name: string;
159
159
  inputs: {
160
160
  name: string;
@@ -191,8 +191,8 @@ export declare abstract class ApiGen {
191
191
  extrinsic: {
192
192
  versions: number[];
193
193
  addressTypeId: number;
194
- signatureTypeId: number;
195
194
  callTypeId: number;
195
+ signatureTypeId: number;
196
196
  signedExtensionsByVersion: ReadonlyMap<number, number[]>;
197
197
  signedExtensions: {
198
198
  ident: string;
@@ -1,5 +1,6 @@
1
1
  import { normalizeName, stringCamelCase } from '@dedot/utils';
2
2
  import { beautifySourceCode, commentBlock, compileTemplate } from '../../utils.js';
3
+ import { getDeprecationComment } from '../../utils.js';
3
4
  import { ApiGen } from './ApiGen.js';
4
5
  export class ConstsGen extends ApiGen {
5
6
  generate(useSubPaths = false) {
@@ -8,11 +9,18 @@ export class ConstsGen extends ApiGen {
8
9
  this.typesGen.typeImports.addKnownType('GenericChainConsts', 'RpcVersion');
9
10
  let defTypeOut = '';
10
11
  for (let pallet of pallets) {
11
- const typedConstants = pallet.constants.map((one) => ({
12
- name: normalizeName(one.name),
13
- type: this.typesGen.generateType(one.typeId, 1, true),
14
- docs: one.docs,
15
- }));
12
+ const typedConstants = pallet.constants.map((one) => {
13
+ const { deprecationInfo, docs } = one;
14
+ const deprecationComments = getDeprecationComment(deprecationInfo);
15
+ if (deprecationComments.length > 0) {
16
+ docs.push('\n', ...deprecationComments);
17
+ }
18
+ return {
19
+ name: normalizeName(one.name),
20
+ type: this.typesGen.generateType(one.typeId, 1, true),
21
+ docs,
22
+ };
23
+ });
16
24
  defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s constants`);
17
25
  defTypeOut += `${stringCamelCase(pallet.name)}: {
18
26
  ${typedConstants.map(({ name, type, docs }) => `${commentBlock(docs)}${name}: ${type}`).join(',\n')}
@@ -1,5 +1,6 @@
1
1
  import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
2
2
  import { beautifySourceCode, commentBlock, compileTemplate } from '../../utils.js';
3
+ import { getVariantDeprecationComment } from '../../utils.js';
3
4
  import { ApiGen } from './ApiGen.js';
4
5
  export class ErrorsGen extends ApiGen {
5
6
  generate(useSubPaths = false) {
@@ -16,6 +17,14 @@ export class ErrorsGen extends ApiGen {
16
17
  defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s errors`);
17
18
  defTypeOut += `${stringCamelCase(pallet.name)}: {
18
19
  ${errorDefs
20
+ .map((def, index) => {
21
+ const { docs } = def;
22
+ const deprecationComments = getVariantDeprecationComment(errorTypeId.deprecationInfo, index);
23
+ if (deprecationComments.length > 0) {
24
+ docs.push('\n', ...deprecationComments);
25
+ }
26
+ return { ...def, docs };
27
+ })
19
28
  .map(({ name, docs }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletError<Rv>`)
20
29
  .join(',\n')}
21
30
 
@@ -1,5 +1,6 @@
1
1
  import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
2
2
  import { beautifySourceCode, commentBlock, compileTemplate } from '../../utils.js';
3
+ import { getVariantDeprecationComment } from '../../utils.js';
3
4
  import { ApiGen } from './ApiGen.js';
4
5
  export class EventsGen extends ApiGen {
5
6
  generate(useSubPaths = false) {
@@ -17,7 +18,7 @@ export class EventsGen extends ApiGen {
17
18
  defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s events`);
18
19
  defTypeOut += `${stringCamelCase(pallet.name)}: {
19
20
  ${eventDefs
20
- .map((def) => {
21
+ .map((def, index) => {
21
22
  const genericParts = [`'${pallet.name}'`, `'${def.name}'`];
22
23
  const eventDataPart = this.#generateMemberDefType(def.fields);
23
24
  if (eventDataPart) {
@@ -26,9 +27,14 @@ export class EventsGen extends ApiGen {
26
27
  else {
27
28
  genericParts.push(flatMembers ? 'undefined' : 'null');
28
29
  }
29
- return { ...def, genericParts };
30
+ const { docs } = def;
31
+ const deprecationComments = getVariantDeprecationComment(eventTypeId.deprecationInfo, index);
32
+ if (deprecationComments.length > 0) {
33
+ docs.push('\n', ...deprecationComments);
34
+ }
35
+ return { ...def, genericParts, docs };
30
36
  })
31
- .map(({ name, docs, fields, genericParts }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletEvent<Rv, ${genericParts.join(', ')}>`)
37
+ .map(({ name, docs, genericParts }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletEvent<Rv, ${genericParts.join(', ')}>`)
32
38
  .join(',\n')}
33
39
 
34
40
  ${commentBlock('Generic pallet event')}[prop: string]: GenericPalletEvent<Rv>,
@@ -1,5 +1,6 @@
1
1
  import { normalizeName, stringCamelCase } from '@dedot/utils';
2
2
  import { beautifySourceCode, commentBlock, compileTemplate } from '../../utils.js';
3
+ import { getDeprecationComment } from '../../utils.js';
3
4
  import { ApiGen } from './ApiGen.js';
4
5
  export class QueryGen extends ApiGen {
5
6
  generate(useSubPaths = false) {
@@ -32,7 +33,7 @@ export class QueryGen extends ApiGen {
32
33
  return beautifySourceCode(template({ importTypes, defTypeOut }));
33
34
  }
34
35
  #generateEntry(entry) {
35
- const { name, storageType, docs, modifier } = entry;
36
+ const { name, storageType, docs, modifier, deprecationInfo } = entry;
36
37
  let valueType, keyTypeOut, keyTypeIn;
37
38
  if (storageType.type === 'Plain') {
38
39
  valueType = this.typesGen.generateType(storageType.value.valueTypeId, 1, true);
@@ -52,6 +53,10 @@ export class QueryGen extends ApiGen {
52
53
  docs.push(`@param {${keyTypeIn}} arg`);
53
54
  }
54
55
  docs.push(`@param {Callback<${valueType}> =} callback`);
56
+ const deprecationComments = getDeprecationComment(deprecationInfo);
57
+ if (deprecationComments.length > 0) {
58
+ docs.push('\n', ...deprecationComments);
59
+ }
55
60
  return {
56
61
  name: normalizeName(name),
57
62
  valueType,
@@ -2,6 +2,7 @@ import { getRuntimeApiNames, getRuntimeApiSpecs } from '@dedot/runtime-specs';
2
2
  import { calcRuntimeApiHash, stringSnakeCase, stringCamelCase } from '@dedot/utils';
3
3
  import { findKnownCodecType } from '../../shared/index.js';
4
4
  import { beautifySourceCode, commentBlock, compileTemplate, isNativeType, TUPLE_TYPE_REGEX, WRAPPER_TYPE_REGEX, } from '../../utils.js';
5
+ import { getDeprecationComment } from '../../utils.js';
5
6
  import { ApiGen } from './ApiGen.js';
6
7
  export class RuntimeApisGen extends ApiGen {
7
8
  typesGen;
@@ -71,13 +72,17 @@ export class RuntimeApisGen extends ApiGen {
71
72
  return `${commentBlock(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<Rv, (${paramsOut}) => Promise<${typeOut}>>`;
72
73
  }
73
74
  #generateMethodDef(runtimeApiName, methodDef) {
74
- const { name: methodName, inputs, output, docs } = methodDef;
75
+ const { name: methodName, inputs, output, docs, deprecationInfo } = methodDef;
75
76
  const callName = `${runtimeApiName}_${stringSnakeCase(methodName)}`;
76
77
  const defaultDocs = [`@callname: ${callName}`];
78
+ const deprecationComments = getDeprecationComment(deprecationInfo);
79
+ if (deprecationComments.length > 0) {
80
+ deprecationComments.unshift('\n');
81
+ }
77
82
  const typeOut = this.typesGen.generateType(output, 1, true);
78
83
  this.#addTypeImport(typeOut, false);
79
84
  const typedInputs = inputs
80
- .map((input, idx) => ({
85
+ .map((input) => ({
81
86
  ...input,
82
87
  type: this.typesGen.generateType(input.typeId, 1),
83
88
  }))
@@ -89,7 +94,7 @@ export class RuntimeApisGen extends ApiGen {
89
94
  const paramsOut = typedInputs
90
95
  .map(({ name, type, isOptional }) => `${stringCamelCase(name)}${isOptional ? '?' : ''}: ${type}`)
91
96
  .join(', ');
92
- return `${commentBlock(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`))}${stringCamelCase(methodName)}: GenericRuntimeApiMethod<Rv, (${paramsOut}) => Promise<${typeOut}>>`;
97
+ return `${commentBlock(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`), deprecationComments)}${stringCamelCase(methodName)}: GenericRuntimeApiMethod<Rv, (${paramsOut}) => Promise<${typeOut}>>`;
93
98
  }
94
99
  #targetRuntimeApiSpecs() {
95
100
  const specs = Object.entries(this.runtimeApis).map(([runtimeApiHash, version]) => {
@@ -1,5 +1,5 @@
1
1
  import { stringCamelCase, stringPascalCase } from '@dedot/utils';
2
- import { beautifySourceCode, commentBlock, compileTemplate, isReservedWord } from '../../utils.js';
2
+ import { beautifySourceCode, commentBlock, compileTemplate, isReservedWord, getVariantDeprecationComment, } from '../../utils.js';
3
3
  import { ApiGen } from '../generator/index.js';
4
4
  export class TxGen extends ApiGen {
5
5
  generate(useSubPaths = false) {
@@ -22,7 +22,7 @@ export class TxGen extends ApiGen {
22
22
  continue;
23
23
  const isFlatEnum = typeDef.value.members.every((m) => m.fields.length === 0);
24
24
  const typedTxs = typeDef.value.members
25
- .map((one) => ({
25
+ .map((one, index) => ({
26
26
  functionName: stringCamelCase(one.name),
27
27
  params: one.fields.map((f) => ({
28
28
  name: stringCamelCase(f.name),
@@ -31,6 +31,7 @@ export class TxGen extends ApiGen {
31
31
  docs: f.docs,
32
32
  })),
33
33
  docs: one.docs,
34
+ index,
34
35
  }))
35
36
  .map((f) => {
36
37
  return {
@@ -41,7 +42,13 @@ export class TxGen extends ApiGen {
41
42
  txDefsOut += commentBlock(`Pallet \`${pallet.name}\`'s transaction calls`);
42
43
  txDefsOut += `${stringCamelCase(pallet.name)}: {
43
44
  ${typedTxs
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
+ .map(({ functionName, params, docs, callInput }, idx) => {
46
+ const deprecationComments = getVariantDeprecationComment(pallet.calls?.deprecationInfo, idx);
47
+ if (deprecationComments.length > 0) {
48
+ deprecationComments.unshift('\n');
49
+ }
50
+ return `${commentBlock(docs, '\n', params.map((p) => `@param {${p.type}} ${p.normalizedName} ${p.docs}`), deprecationComments)}${functionName}: GenericTxCall<Rv, (${params.map((p) => `${p.normalizedName}: ${p.type}`).join(', ')}) => ChainSubmittableExtrinsic<Rv, ${callInput}>>`;
51
+ })
45
52
  .join(',\n')}
46
53
 
47
54
  ${commentBlock('Generic pallet tx call')}[callName: string]: GenericTxCall<Rv, TxCall<Rv>>,
@@ -0,0 +1,4 @@
1
+ import { ApiGen } from './ApiGen.js';
2
+ export declare class ViewFunctionsGen extends ApiGen {
3
+ generate(useSubPaths?: boolean): Promise<string>;
4
+ }
@@ -0,0 +1,40 @@
1
+ import { stringCamelCase } from '@dedot/utils';
2
+ import { beautifySourceCode, commentBlock, compileTemplate } from '../../utils.js';
3
+ import { getDeprecationComment } from '../../utils.js';
4
+ import { ApiGen } from './ApiGen.js';
5
+ export class ViewFunctionsGen extends ApiGen {
6
+ generate(useSubPaths = false) {
7
+ const { pallets } = this.metadata;
8
+ this.typesGen.clearCache();
9
+ this.typesGen.typeImports.addKnownType('GenericChainViewFunctions', // --
10
+ 'GenericViewFunction', 'RpcVersion');
11
+ let defTypeOut = '';
12
+ for (let pallet of pallets) {
13
+ if (!pallet.viewFunctions || pallet.viewFunctions.length === 0)
14
+ continue;
15
+ const viewFns = pallet.viewFunctions.map((vf) => {
16
+ const { docs, deprecationInfo } = vf;
17
+ const paramsDoc = vf.inputs.map((input) => `@param {${this.typesGen.generateType(input.typeId, 1)}} ${stringCamelCase(input.name)}`);
18
+ if (paramsDoc.length > 0) {
19
+ docs.push('\n', ...paramsDoc);
20
+ }
21
+ const deprecationComments = getDeprecationComment(deprecationInfo);
22
+ if (deprecationComments.length > 0) {
23
+ docs.push('\n', ...deprecationComments);
24
+ }
25
+ const params = vf.inputs.map((input) => `${stringCamelCase(input.name)}: ${this.typesGen.generateType(input.typeId, 1)}`);
26
+ const outputType = this.typesGen.generateType(vf.output, 1, true);
27
+ return `${commentBlock(docs)}${stringCamelCase(vf.name)}: GenericViewFunction<Rv, (${params.join(', ')}) => Promise<${outputType}>>`;
28
+ });
29
+ defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s view functions`);
30
+ defTypeOut += `${stringCamelCase(pallet.name)}: {
31
+ ${viewFns.join(',\n')}
32
+
33
+ ${commentBlock('Generic pallet view function')}[name: string]: GenericViewFunction<Rv>;
34
+ },`;
35
+ }
36
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
37
+ const template = compileTemplate('chaintypes/templates/view-functions.hbs');
38
+ return beautifySourceCode(template({ importTypes, defTypeOut }));
39
+ }
40
+ }
@@ -8,3 +8,4 @@ export * from './ErrorsGen.js';
8
8
  export * from './EventsGen.js';
9
9
  export * from './TxGen.js';
10
10
  export * from './RuntimeApisGen.js';
11
+ export * from './ViewFunctionsGen.js';
@@ -8,3 +8,4 @@ export * from './ErrorsGen.js';
8
8
  export * from './EventsGen.js';
9
9
  export * from './TxGen.js';
10
10
  export * from './RuntimeApisGen.js';
11
+ export * from './ViewFunctionsGen.js';
@@ -3,7 +3,7 @@ import { WsProvider } from '@dedot/providers';
3
3
  import { stringDashCase, stringPascalCase } from '@dedot/utils';
4
4
  import * as fs from 'fs';
5
5
  import * as path from 'path';
6
- import { ConstsGen, ErrorsGen, EventsGen, IndexGen, JsonRpcGen, QueryGen, RuntimeApisGen, TxGen, TypesGen, } from './generator/index.js';
6
+ import { ConstsGen, ErrorsGen, EventsGen, IndexGen, JsonRpcGen, QueryGen, RuntimeApisGen, TxGen, ViewFunctionsGen, TypesGen, } from './generator/index.js';
7
7
  export async function generateTypesFromEndpoint(chain, endpoint, outDir, extension = 'd.ts', useSubPaths = false) {
8
8
  // Immediately throw error if cannot connect to provider for the first time.
9
9
  const client = await LegacyClient.new(new WsProvider({ endpoint, retryDelayMs: 0, timeout: 0 }));
@@ -24,6 +24,7 @@ export async function generateTypes(chain, metadata, rpcMethods, runtimeVersion,
24
24
  const eventsFileName = path.join(dirPath, `events.${extension}`);
25
25
  const runtimeApisFileName = path.join(dirPath, `runtime.${extension}`);
26
26
  const txFileName = path.join(dirPath, `tx.${extension}`);
27
+ const viewFunctionsFileName = path.join(dirPath, `view-functions.${extension}`);
27
28
  if (!fs.existsSync(dirPath)) {
28
29
  fs.mkdirSync(dirPath, { recursive: true });
29
30
  }
@@ -37,6 +38,7 @@ export async function generateTypes(chain, metadata, rpcMethods, runtimeVersion,
37
38
  const eventsGen = new EventsGen(typesGen);
38
39
  const runtimeApisGen = new RuntimeApisGen(typesGen, runtimeVersion.apis);
39
40
  const txGen = new TxGen(typesGen);
41
+ const viewFunctionGen = new ViewFunctionsGen(typesGen);
40
42
  fs.writeFileSync(defTypesFileName, await typesGen.generate(useSubPaths));
41
43
  fs.writeFileSync(errorsFileName, await errorsGen.generate(useSubPaths));
42
44
  fs.writeFileSync(eventsFileName, await eventsGen.generate(useSubPaths));
@@ -46,5 +48,6 @@ export async function generateTypes(chain, metadata, rpcMethods, runtimeVersion,
46
48
  fs.writeFileSync(txFileName, await txGen.generate(useSubPaths));
47
49
  fs.writeFileSync(indexFileName, await indexGen.generate(useSubPaths));
48
50
  fs.writeFileSync(runtimeApisFileName, await runtimeApisGen.generate(useSubPaths));
51
+ fs.writeFileSync(viewFunctionsFileName, await viewFunctionGen.generate(useSubPaths));
49
52
  return { interfaceName, outputFolder: dirPath };
50
53
  }
@@ -7,6 +7,7 @@ import { ChainJsonRpcApis } from './json-rpc.js';
7
7
  import { ChainErrors } from './errors.js';
8
8
  import { ChainEvents } from './events.js';
9
9
  import { RuntimeApis } from './runtime.js';
10
+ import { ChainViewFunctions } from './view-functions.js';
10
11
  import { ChainTx } from './tx.js';
11
12
 
12
13
  export * from './types.js';
@@ -18,6 +19,7 @@ export interface Versioned{{{interfaceName}}}<Rv extends RpcVersion> extends Gen
18
19
  errors: ChainErrors<Rv>;
19
20
  events: ChainEvents<Rv>;
20
21
  call: RuntimeApis<Rv>;
22
+ view: ChainViewFunctions<Rv>;
21
23
  tx: ChainTx<Rv>;
22
24
  }
23
25
 
@@ -0,0 +1,7 @@
1
+ // Generated by dedot cli
2
+
3
+ {{{ importTypes }}}
4
+
5
+ export interface ChainViewFunctions<Rv extends RpcVersion> extends GenericChainViewFunctions<Rv> {
6
+ {{{ defTypeOut }}}
7
+ }
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ConstsGen = void 0;
4
4
  const utils_1 = require("@dedot/utils");
5
5
  const utils_js_1 = require("../../utils.js");
6
+ const utils_js_2 = require("../../utils.js");
6
7
  const ApiGen_js_1 = require("./ApiGen.js");
7
8
  class ConstsGen extends ApiGen_js_1.ApiGen {
8
9
  generate(useSubPaths = false) {
@@ -11,11 +12,18 @@ class ConstsGen extends ApiGen_js_1.ApiGen {
11
12
  this.typesGen.typeImports.addKnownType('GenericChainConsts', 'RpcVersion');
12
13
  let defTypeOut = '';
13
14
  for (let pallet of pallets) {
14
- const typedConstants = pallet.constants.map((one) => ({
15
- name: (0, utils_1.normalizeName)(one.name),
16
- type: this.typesGen.generateType(one.typeId, 1, true),
17
- docs: one.docs,
18
- }));
15
+ const typedConstants = pallet.constants.map((one) => {
16
+ const { deprecationInfo, docs } = one;
17
+ const deprecationComments = (0, utils_js_2.getDeprecationComment)(deprecationInfo);
18
+ if (deprecationComments.length > 0) {
19
+ docs.push('\n', ...deprecationComments);
20
+ }
21
+ return {
22
+ name: (0, utils_1.normalizeName)(one.name),
23
+ type: this.typesGen.generateType(one.typeId, 1, true),
24
+ docs,
25
+ };
26
+ });
19
27
  defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s constants`);
20
28
  defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
21
29
  ${typedConstants.map(({ name, type, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${name}: ${type}`).join(',\n')}
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ErrorsGen = void 0;
4
4
  const utils_1 = require("@dedot/utils");
5
5
  const utils_js_1 = require("../../utils.js");
6
+ const utils_js_2 = require("../../utils.js");
6
7
  const ApiGen_js_1 = require("./ApiGen.js");
7
8
  class ErrorsGen extends ApiGen_js_1.ApiGen {
8
9
  generate(useSubPaths = false) {
@@ -19,6 +20,14 @@ class ErrorsGen extends ApiGen_js_1.ApiGen {
19
20
  defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s errors`);
20
21
  defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
21
22
  ${errorDefs
23
+ .map((def, index) => {
24
+ const { docs } = def;
25
+ const deprecationComments = (0, utils_js_2.getVariantDeprecationComment)(errorTypeId.deprecationInfo, index);
26
+ if (deprecationComments.length > 0) {
27
+ docs.push('\n', ...deprecationComments);
28
+ }
29
+ return { ...def, docs };
30
+ })
22
31
  .map(({ name, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${(0, utils_1.stringPascalCase)(name)}: GenericPalletError<Rv>`)
23
32
  .join(',\n')}
24
33
 
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.EventsGen = void 0;
4
4
  const utils_1 = require("@dedot/utils");
5
5
  const utils_js_1 = require("../../utils.js");
6
+ const utils_js_2 = require("../../utils.js");
6
7
  const ApiGen_js_1 = require("./ApiGen.js");
7
8
  class EventsGen extends ApiGen_js_1.ApiGen {
8
9
  generate(useSubPaths = false) {
@@ -20,7 +21,7 @@ class EventsGen extends ApiGen_js_1.ApiGen {
20
21
  defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s events`);
21
22
  defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
22
23
  ${eventDefs
23
- .map((def) => {
24
+ .map((def, index) => {
24
25
  const genericParts = [`'${pallet.name}'`, `'${def.name}'`];
25
26
  const eventDataPart = this.#generateMemberDefType(def.fields);
26
27
  if (eventDataPart) {
@@ -29,9 +30,14 @@ class EventsGen extends ApiGen_js_1.ApiGen {
29
30
  else {
30
31
  genericParts.push(flatMembers ? 'undefined' : 'null');
31
32
  }
32
- return { ...def, genericParts };
33
+ const { docs } = def;
34
+ const deprecationComments = (0, utils_js_2.getVariantDeprecationComment)(eventTypeId.deprecationInfo, index);
35
+ if (deprecationComments.length > 0) {
36
+ docs.push('\n', ...deprecationComments);
37
+ }
38
+ return { ...def, genericParts, docs };
33
39
  })
34
- .map(({ name, docs, fields, genericParts }) => `${(0, utils_js_1.commentBlock)(docs)}${(0, utils_1.stringPascalCase)(name)}: GenericPalletEvent<Rv, ${genericParts.join(', ')}>`)
40
+ .map(({ name, docs, genericParts }) => `${(0, utils_js_1.commentBlock)(docs)}${(0, utils_1.stringPascalCase)(name)}: GenericPalletEvent<Rv, ${genericParts.join(', ')}>`)
35
41
  .join(',\n')}
36
42
 
37
43
  ${(0, utils_js_1.commentBlock)('Generic pallet event')}[prop: string]: GenericPalletEvent<Rv>,
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.QueryGen = void 0;
4
4
  const utils_1 = require("@dedot/utils");
5
5
  const utils_js_1 = require("../../utils.js");
6
+ const utils_js_2 = require("../../utils.js");
6
7
  const ApiGen_js_1 = require("./ApiGen.js");
7
8
  class QueryGen extends ApiGen_js_1.ApiGen {
8
9
  generate(useSubPaths = false) {
@@ -35,7 +36,7 @@ class QueryGen extends ApiGen_js_1.ApiGen {
35
36
  return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
36
37
  }
37
38
  #generateEntry(entry) {
38
- const { name, storageType, docs, modifier } = entry;
39
+ const { name, storageType, docs, modifier, deprecationInfo } = entry;
39
40
  let valueType, keyTypeOut, keyTypeIn;
40
41
  if (storageType.type === 'Plain') {
41
42
  valueType = this.typesGen.generateType(storageType.value.valueTypeId, 1, true);
@@ -55,6 +56,10 @@ class QueryGen extends ApiGen_js_1.ApiGen {
55
56
  docs.push(`@param {${keyTypeIn}} arg`);
56
57
  }
57
58
  docs.push(`@param {Callback<${valueType}> =} callback`);
59
+ const deprecationComments = (0, utils_js_2.getDeprecationComment)(deprecationInfo);
60
+ if (deprecationComments.length > 0) {
61
+ docs.push('\n', ...deprecationComments);
62
+ }
58
63
  return {
59
64
  name: (0, utils_1.normalizeName)(name),
60
65
  valueType,
@@ -5,6 +5,7 @@ const runtime_specs_1 = require("@dedot/runtime-specs");
5
5
  const utils_1 = require("@dedot/utils");
6
6
  const index_js_1 = require("../../shared/index.js");
7
7
  const utils_js_1 = require("../../utils.js");
8
+ const utils_js_2 = require("../../utils.js");
8
9
  const ApiGen_js_1 = require("./ApiGen.js");
9
10
  class RuntimeApisGen extends ApiGen_js_1.ApiGen {
10
11
  typesGen;
@@ -74,13 +75,17 @@ class RuntimeApisGen extends ApiGen_js_1.ApiGen {
74
75
  return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<Rv, (${paramsOut}) => Promise<${typeOut}>>`;
75
76
  }
76
77
  #generateMethodDef(runtimeApiName, methodDef) {
77
- const { name: methodName, inputs, output, docs } = methodDef;
78
+ const { name: methodName, inputs, output, docs, deprecationInfo } = methodDef;
78
79
  const callName = `${runtimeApiName}_${(0, utils_1.stringSnakeCase)(methodName)}`;
79
80
  const defaultDocs = [`@callname: ${callName}`];
81
+ const deprecationComments = (0, utils_js_2.getDeprecationComment)(deprecationInfo);
82
+ if (deprecationComments.length > 0) {
83
+ deprecationComments.unshift('\n');
84
+ }
80
85
  const typeOut = this.typesGen.generateType(output, 1, true);
81
86
  this.#addTypeImport(typeOut, false);
82
87
  const typedInputs = inputs
83
- .map((input, idx) => ({
88
+ .map((input) => ({
84
89
  ...input,
85
90
  type: this.typesGen.generateType(input.typeId, 1),
86
91
  }))
@@ -92,7 +97,7 @@ class RuntimeApisGen extends ApiGen_js_1.ApiGen {
92
97
  const paramsOut = typedInputs
93
98
  .map(({ name, type, isOptional }) => `${(0, utils_1.stringCamelCase)(name)}${isOptional ? '?' : ''}: ${type}`)
94
99
  .join(', ');
95
- return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`))}${(0, utils_1.stringCamelCase)(methodName)}: GenericRuntimeApiMethod<Rv, (${paramsOut}) => Promise<${typeOut}>>`;
100
+ return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`), deprecationComments)}${(0, utils_1.stringCamelCase)(methodName)}: GenericRuntimeApiMethod<Rv, (${paramsOut}) => Promise<${typeOut}>>`;
96
101
  }
97
102
  #targetRuntimeApiSpecs() {
98
103
  const specs = Object.entries(this.runtimeApis).map(([runtimeApiHash, version]) => {
@@ -25,7 +25,7 @@ class TxGen extends index_js_1.ApiGen {
25
25
  continue;
26
26
  const isFlatEnum = typeDef.value.members.every((m) => m.fields.length === 0);
27
27
  const typedTxs = typeDef.value.members
28
- .map((one) => ({
28
+ .map((one, index) => ({
29
29
  functionName: (0, utils_1.stringCamelCase)(one.name),
30
30
  params: one.fields.map((f) => ({
31
31
  name: (0, utils_1.stringCamelCase)(f.name),
@@ -34,6 +34,7 @@ class TxGen extends index_js_1.ApiGen {
34
34
  docs: f.docs,
35
35
  })),
36
36
  docs: one.docs,
37
+ index,
37
38
  }))
38
39
  .map((f) => {
39
40
  return {
@@ -44,7 +45,13 @@ class TxGen extends index_js_1.ApiGen {
44
45
  txDefsOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s transaction calls`);
45
46
  txDefsOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
46
47
  ${typedTxs
47
- .map(({ functionName, params, docs, callInput }) => `${(0, utils_js_1.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}>>`)
48
+ .map(({ functionName, params, docs, callInput }, idx) => {
49
+ const deprecationComments = (0, utils_js_1.getVariantDeprecationComment)(pallet.calls?.deprecationInfo, idx);
50
+ if (deprecationComments.length > 0) {
51
+ deprecationComments.unshift('\n');
52
+ }
53
+ return `${(0, utils_js_1.commentBlock)(docs, '\n', params.map((p) => `@param {${p.type}} ${p.normalizedName} ${p.docs}`), deprecationComments)}${functionName}: GenericTxCall<Rv, (${params.map((p) => `${p.normalizedName}: ${p.type}`).join(', ')}) => ChainSubmittableExtrinsic<Rv, ${callInput}>>`;
54
+ })
48
55
  .join(',\n')}
49
56
 
50
57
  ${(0, utils_js_1.commentBlock)('Generic pallet tx call')}[callName: string]: GenericTxCall<Rv, TxCall<Rv>>,
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ViewFunctionsGen = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ const utils_js_1 = require("../../utils.js");
6
+ const utils_js_2 = require("../../utils.js");
7
+ const ApiGen_js_1 = require("./ApiGen.js");
8
+ class ViewFunctionsGen extends ApiGen_js_1.ApiGen {
9
+ generate(useSubPaths = false) {
10
+ const { pallets } = this.metadata;
11
+ this.typesGen.clearCache();
12
+ this.typesGen.typeImports.addKnownType('GenericChainViewFunctions', // --
13
+ 'GenericViewFunction', 'RpcVersion');
14
+ let defTypeOut = '';
15
+ for (let pallet of pallets) {
16
+ if (!pallet.viewFunctions || pallet.viewFunctions.length === 0)
17
+ continue;
18
+ const viewFns = pallet.viewFunctions.map((vf) => {
19
+ const { docs, deprecationInfo } = vf;
20
+ const paramsDoc = vf.inputs.map((input) => `@param {${this.typesGen.generateType(input.typeId, 1)}} ${(0, utils_1.stringCamelCase)(input.name)}`);
21
+ if (paramsDoc.length > 0) {
22
+ docs.push('\n', ...paramsDoc);
23
+ }
24
+ const deprecationComments = (0, utils_js_2.getDeprecationComment)(deprecationInfo);
25
+ if (deprecationComments.length > 0) {
26
+ docs.push('\n', ...deprecationComments);
27
+ }
28
+ const params = vf.inputs.map((input) => `${(0, utils_1.stringCamelCase)(input.name)}: ${this.typesGen.generateType(input.typeId, 1)}`);
29
+ const outputType = this.typesGen.generateType(vf.output, 1, true);
30
+ return `${(0, utils_js_1.commentBlock)(docs)}${(0, utils_1.stringCamelCase)(vf.name)}: GenericViewFunction<Rv, (${params.join(', ')}) => Promise<${outputType}>>`;
31
+ });
32
+ defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s view functions`);
33
+ defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
34
+ ${viewFns.join(',\n')}
35
+
36
+ ${(0, utils_js_1.commentBlock)('Generic pallet view function')}[name: string]: GenericViewFunction<Rv>;
37
+ },`;
38
+ }
39
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
40
+ const template = (0, utils_js_1.compileTemplate)('chaintypes/templates/view-functions.hbs');
41
+ return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
42
+ }
43
+ }
44
+ exports.ViewFunctionsGen = ViewFunctionsGen;
@@ -24,3 +24,4 @@ __exportStar(require("./ErrorsGen.js"), exports);
24
24
  __exportStar(require("./EventsGen.js"), exports);
25
25
  __exportStar(require("./TxGen.js"), exports);
26
26
  __exportStar(require("./RuntimeApisGen.js"), exports);
27
+ __exportStar(require("./ViewFunctionsGen.js"), exports);
@@ -51,6 +51,7 @@ async function generateTypes(chain, metadata, rpcMethods, runtimeVersion, outDir
51
51
  const eventsFileName = path.join(dirPath, `events.${extension}`);
52
52
  const runtimeApisFileName = path.join(dirPath, `runtime.${extension}`);
53
53
  const txFileName = path.join(dirPath, `tx.${extension}`);
54
+ const viewFunctionsFileName = path.join(dirPath, `view-functions.${extension}`);
54
55
  if (!fs.existsSync(dirPath)) {
55
56
  fs.mkdirSync(dirPath, { recursive: true });
56
57
  }
@@ -64,6 +65,7 @@ async function generateTypes(chain, metadata, rpcMethods, runtimeVersion, outDir
64
65
  const eventsGen = new index_js_1.EventsGen(typesGen);
65
66
  const runtimeApisGen = new index_js_1.RuntimeApisGen(typesGen, runtimeVersion.apis);
66
67
  const txGen = new index_js_1.TxGen(typesGen);
68
+ const viewFunctionGen = new index_js_1.ViewFunctionsGen(typesGen);
67
69
  fs.writeFileSync(defTypesFileName, await typesGen.generate(useSubPaths));
68
70
  fs.writeFileSync(errorsFileName, await errorsGen.generate(useSubPaths));
69
71
  fs.writeFileSync(eventsFileName, await eventsGen.generate(useSubPaths));
@@ -73,6 +75,7 @@ async function generateTypes(chain, metadata, rpcMethods, runtimeVersion, outDir
73
75
  fs.writeFileSync(txFileName, await txGen.generate(useSubPaths));
74
76
  fs.writeFileSync(indexFileName, await indexGen.generate(useSubPaths));
75
77
  fs.writeFileSync(runtimeApisFileName, await runtimeApisGen.generate(useSubPaths));
78
+ fs.writeFileSync(viewFunctionsFileName, await viewFunctionGen.generate(useSubPaths));
76
79
  return { interfaceName, outputFolder: dirPath };
77
80
  }
78
81
  exports.generateTypes = generateTypes;
@@ -7,6 +7,7 @@ import { ChainJsonRpcApis } from './json-rpc.js';
7
7
  import { ChainErrors } from './errors.js';
8
8
  import { ChainEvents } from './events.js';
9
9
  import { RuntimeApis } from './runtime.js';
10
+ import { ChainViewFunctions } from './view-functions.js';
10
11
  import { ChainTx } from './tx.js';
11
12
 
12
13
  export * from './types.js';
@@ -18,6 +19,7 @@ export interface Versioned{{{interfaceName}}}<Rv extends RpcVersion> extends Gen
18
19
  errors: ChainErrors<Rv>;
19
20
  events: ChainEvents<Rv>;
20
21
  call: RuntimeApis<Rv>;
22
+ view: ChainViewFunctions<Rv>;
21
23
  tx: ChainTx<Rv>;
22
24
  }
23
25
 
@@ -0,0 +1,7 @@
1
+ // Generated by dedot cli
2
+
3
+ {{{ importTypes }}}
4
+
5
+ export interface ChainViewFunctions<Rv extends RpcVersion> extends GenericChainViewFunctions<Rv> {
6
+ {{{ defTypeOut }}}
7
+ }
package/cjs/utils.js CHANGED
@@ -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.isNativeType = exports.isReservedWord = exports.compileTemplate = exports.beautifySourceCode = exports.commentBlock = exports.TUPLE_TYPE_REGEX = exports.WRAPPER_TYPE_REGEX = void 0;
29
+ exports.getVariantDeprecationComment = exports.getDeprecationComment = 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"));
@@ -95,3 +95,49 @@ const isNativeType = (type) => {
95
95
  });
96
96
  };
97
97
  exports.isNativeType = isNativeType;
98
+ /**
99
+ * Generate deprecation comment from ItemDeprecationInfoDefV16
100
+ *
101
+ * @param deprecationInfo - The deprecation information for the item
102
+ */
103
+ const getDeprecationComment = (deprecationInfo) => {
104
+ if (!deprecationInfo) {
105
+ return [];
106
+ }
107
+ switch (deprecationInfo.type) {
108
+ case 'DeprecatedWithoutNote':
109
+ return ['@deprecated'];
110
+ case 'Deprecated': {
111
+ const { note, since } = deprecationInfo.value;
112
+ return [`@deprecated ${note}${since ? ` (since ${since})` : ''}`];
113
+ }
114
+ case 'NotDeprecated':
115
+ default:
116
+ return [];
117
+ }
118
+ };
119
+ exports.getDeprecationComment = getDeprecationComment;
120
+ /**
121
+ * Generate deprecation comment for enum variant from EnumDeprecationInfoDefV16
122
+ *
123
+ * @param deprecationInfo - The deprecation information for the enum
124
+ * @param variantIndex
125
+ */
126
+ const getVariantDeprecationComment = (deprecationInfo, variantIndex) => {
127
+ if (!deprecationInfo || !deprecationInfo[0])
128
+ return [];
129
+ const variantDeprecation = deprecationInfo[0].get(variantIndex);
130
+ if (!variantDeprecation)
131
+ return [];
132
+ switch (variantDeprecation.type) {
133
+ case 'DeprecatedWithoutNote':
134
+ return ['@deprecated'];
135
+ case 'Deprecated': {
136
+ const { note, since } = variantDeprecation.value;
137
+ return [`@deprecated ${note}${since ? ` (since ${since})` : ''}`];
138
+ }
139
+ default:
140
+ return [];
141
+ }
142
+ };
143
+ exports.getVariantDeprecationComment = getVariantDeprecationComment;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dedot/codegen",
3
- "version": "0.14.1",
3
+ "version": "0.15.1",
4
4
  "description": "Generate types",
5
5
  "author": "Thang X. Vu <thang@dedot.dev>",
6
6
  "homepage": "https://dedot.dev",
@@ -18,14 +18,14 @@
18
18
  "copy": "cp -R ./src/chaintypes/templates ./dist/chaintypes && cp -R ./src/chaintypes/templates ./dist/cjs/chaintypes && cp -R ./src/typink/templates ./dist/typink && cp -R ./src/typink/templates ./dist/cjs/typink"
19
19
  },
20
20
  "dependencies": {
21
- "@dedot/api": "0.14.1",
22
- "@dedot/codecs": "0.14.1",
23
- "@dedot/contracts": "0.14.1",
24
- "@dedot/providers": "0.14.1",
25
- "@dedot/runtime-specs": "0.14.1",
26
- "@dedot/shape": "0.14.1",
27
- "@dedot/types": "0.14.1",
28
- "@dedot/utils": "0.14.1",
21
+ "@dedot/api": "0.15.1",
22
+ "@dedot/codecs": "0.15.1",
23
+ "@dedot/contracts": "0.15.1",
24
+ "@dedot/providers": "0.15.1",
25
+ "@dedot/runtime-specs": "0.15.1",
26
+ "@dedot/shape": "0.15.1",
27
+ "@dedot/types": "0.15.1",
28
+ "@dedot/utils": "0.15.1",
29
29
  "handlebars": "^4.7.8",
30
30
  "prettier": "^3.5.3"
31
31
  },
@@ -37,7 +37,7 @@
37
37
  "node": ">=18"
38
38
  },
39
39
  "license": "Apache-2.0",
40
- "gitHead": "d5adec49781a6e0b45910055dcaf9bf472a39a6b",
40
+ "gitHead": "fdac8072dc7ea6698f19f11604904b665ec7ba89",
41
41
  "module": "./index.js",
42
42
  "types": "./index.d.ts",
43
43
  "exports": {
package/utils.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { ItemDeprecationInfoDefV16, EnumDeprecationInfoDefV16 } from '@dedot/codecs';
1
2
  export declare const WRAPPER_TYPE_REGEX: RegExp;
2
3
  export declare const TUPLE_TYPE_REGEX: RegExp;
3
4
  export declare const commentBlock: (...docs: (string | string[])[]) => string;
@@ -13,3 +14,16 @@ export declare const isReservedWord: (word: string) => boolean;
13
14
  * @param type
14
15
  */
15
16
  export declare const isNativeType: (type: string) => boolean;
17
+ /**
18
+ * Generate deprecation comment from ItemDeprecationInfoDefV16
19
+ *
20
+ * @param deprecationInfo - The deprecation information for the item
21
+ */
22
+ export declare const getDeprecationComment: (deprecationInfo: ItemDeprecationInfoDefV16 | undefined) => string[];
23
+ /**
24
+ * Generate deprecation comment for enum variant from EnumDeprecationInfoDefV16
25
+ *
26
+ * @param deprecationInfo - The deprecation information for the enum
27
+ * @param variantIndex
28
+ */
29
+ export declare const getVariantDeprecationComment: (deprecationInfo: EnumDeprecationInfoDefV16 | undefined, variantIndex: number) => string[];
package/utils.js CHANGED
@@ -61,3 +61,47 @@ export const isNativeType = (type) => {
61
61
  }
62
62
  });
63
63
  };
64
+ /**
65
+ * Generate deprecation comment from ItemDeprecationInfoDefV16
66
+ *
67
+ * @param deprecationInfo - The deprecation information for the item
68
+ */
69
+ export const getDeprecationComment = (deprecationInfo) => {
70
+ if (!deprecationInfo) {
71
+ return [];
72
+ }
73
+ switch (deprecationInfo.type) {
74
+ case 'DeprecatedWithoutNote':
75
+ return ['@deprecated'];
76
+ case 'Deprecated': {
77
+ const { note, since } = deprecationInfo.value;
78
+ return [`@deprecated ${note}${since ? ` (since ${since})` : ''}`];
79
+ }
80
+ case 'NotDeprecated':
81
+ default:
82
+ return [];
83
+ }
84
+ };
85
+ /**
86
+ * Generate deprecation comment for enum variant from EnumDeprecationInfoDefV16
87
+ *
88
+ * @param deprecationInfo - The deprecation information for the enum
89
+ * @param variantIndex
90
+ */
91
+ export const getVariantDeprecationComment = (deprecationInfo, variantIndex) => {
92
+ if (!deprecationInfo || !deprecationInfo[0])
93
+ return [];
94
+ const variantDeprecation = deprecationInfo[0].get(variantIndex);
95
+ if (!variantDeprecation)
96
+ return [];
97
+ switch (variantDeprecation.type) {
98
+ case 'DeprecatedWithoutNote':
99
+ return ['@deprecated'];
100
+ case 'Deprecated': {
101
+ const { note, since } = variantDeprecation.value;
102
+ return [`@deprecated ${note}${since ? ` (since ${since})` : ''}`];
103
+ }
104
+ default:
105
+ return [];
106
+ }
107
+ };