@pezkuwi/typegen 16.5.5 → 16.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (193) hide show
  1. package/LICENSE +201 -0
  2. package/augment.js +2 -0
  3. package/bundle.js +2 -0
  4. package/cjs/augment.d.ts +2 -0
  5. package/cjs/augment.js +4 -0
  6. package/cjs/bundle.d.ts +2 -0
  7. package/cjs/bundle.js +7 -0
  8. package/cjs/extractChain.js +44 -0
  9. package/cjs/fromChain.js +95 -0
  10. package/cjs/fromDefs.js +82 -0
  11. package/cjs/generate/consts.js +91 -0
  12. package/cjs/generate/errors.js +59 -0
  13. package/cjs/generate/events.js +139 -0
  14. package/cjs/generate/index.js +23 -0
  15. package/cjs/generate/interfaceRegistry.js +64 -0
  16. package/cjs/generate/lookup.js +240 -0
  17. package/cjs/generate/query.js +140 -0
  18. package/cjs/generate/rpc.js +115 -0
  19. package/cjs/generate/runtime.js +233 -0
  20. package/{build → cjs}/generate/tsDef.d.ts +1 -1
  21. package/cjs/generate/tsDef.js +254 -0
  22. package/cjs/generate/tx.js +121 -0
  23. package/cjs/generate/types.js +16 -0
  24. package/cjs/index.js +5 -0
  25. package/cjs/interfacesTs.js +28 -0
  26. package/cjs/metadataMd.js +652 -0
  27. package/cjs/package.json +3 -0
  28. package/cjs/packageDetect.js +8 -0
  29. package/cjs/packageInfo.js +4 -0
  30. package/cjs/util/assert.js +15 -0
  31. package/cjs/util/derived.js +128 -0
  32. package/cjs/util/docs.js +10 -0
  33. package/cjs/util/file.js +33 -0
  34. package/{build → cjs}/util/formatting.d.ts +1 -1
  35. package/cjs/util/formatting.js +237 -0
  36. package/cjs/util/imports.js +126 -0
  37. package/{build → cjs}/util/index.d.ts +1 -0
  38. package/cjs/util/index.js +39 -0
  39. package/cjs/util/initMeta.js +22 -0
  40. package/cjs/util/register.js +10 -0
  41. package/cjs/util/wsMeta.js +60 -0
  42. package/extractChain.d.ts +1 -0
  43. package/extractChain.js +40 -0
  44. package/fromChain.d.ts +1 -0
  45. package/fromChain.js +91 -0
  46. package/fromDefs.d.ts +1 -0
  47. package/fromDefs.js +78 -0
  48. package/generate/consts.d.ts +5 -0
  49. package/generate/consts.js +87 -0
  50. package/generate/errors.d.ts +4 -0
  51. package/generate/errors.js +55 -0
  52. package/generate/events.d.ts +5 -0
  53. package/generate/events.js +135 -0
  54. package/{src/generate/index.ts → generate/index.d.ts} +0 -3
  55. package/generate/index.js +10 -0
  56. package/generate/interfaceRegistry.d.ts +4 -0
  57. package/generate/interfaceRegistry.js +59 -0
  58. package/generate/lookup.d.ts +4 -0
  59. package/generate/lookup.js +235 -0
  60. package/generate/query.d.ts +5 -0
  61. package/generate/query.js +136 -0
  62. package/generate/rpc.d.ts +6 -0
  63. package/generate/rpc.js +110 -0
  64. package/generate/runtime.d.ts +7 -0
  65. package/generate/runtime.js +228 -0
  66. package/generate/tsDef.d.ts +16 -0
  67. package/generate/tsDef.js +246 -0
  68. package/generate/tx.d.ts +5 -0
  69. package/generate/tx.js +117 -0
  70. package/generate/types.d.ts +12 -0
  71. package/generate/types.js +13 -0
  72. package/index.d.ts +2 -0
  73. package/index.js +2 -0
  74. package/interfacesTs.d.ts +1 -0
  75. package/interfacesTs.js +24 -0
  76. package/metadataMd.d.ts +1 -0
  77. package/metadataMd.js +648 -0
  78. package/package.json +520 -24
  79. package/packageDetect.d.ts +1 -0
  80. package/{src/packageDetect.ts → packageDetect.js} +0 -8
  81. package/packageInfo.d.ts +6 -0
  82. package/packageInfo.js +1 -0
  83. package/scripts/{polkadot-types-chain-info.mjs → pezkuwi-types-chain-info.mjs} +1 -1
  84. package/scripts/{polkadot-types-from-chain.mjs → pezkuwi-types-from-chain.mjs} +1 -1
  85. package/scripts/{polkadot-types-from-defs.mjs → pezkuwi-types-from-defs.mjs} +1 -1
  86. package/{build/scripts/polkadot-types-internal-interfaces.mjs → scripts/pezkuwi-types-internal-interfaces.mjs} +1 -1
  87. package/{build/scripts/polkadot-types-internal-metadata.mjs → scripts/pezkuwi-types-internal-metadata.mjs} +1 -1
  88. package/{src/templates → templates}/calls.hbs +2 -2
  89. package/{src/templates → templates}/consts.hbs +2 -2
  90. package/{src/templates → templates}/errors.hbs +2 -2
  91. package/{src/templates → templates}/events.hbs +2 -2
  92. package/{src/templates → templates}/interfaceRegistry.hbs +2 -2
  93. package/{src/templates → templates}/lookup/types.hbs +2 -2
  94. package/{src/templates → templates}/query.hbs +2 -2
  95. package/{src/templates → templates}/rpc.hbs +2 -2
  96. package/{src/templates → templates}/tx.hbs +2 -2
  97. package/util/assert.d.ts +2 -0
  98. package/util/assert.js +10 -0
  99. package/util/derived.d.ts +4 -0
  100. package/util/derived.js +125 -0
  101. package/util/docs.d.ts +1 -0
  102. package/util/docs.js +6 -0
  103. package/util/file.d.ts +2 -0
  104. package/util/file.js +28 -0
  105. package/util/formatting.d.ts +11 -0
  106. package/util/formatting.js +230 -0
  107. package/util/imports.d.ts +23 -0
  108. package/util/imports.js +121 -0
  109. package/{src/util/index.ts → util/index.d.ts} +6 -8
  110. package/util/index.js +34 -0
  111. package/util/initMeta.d.ts +12 -0
  112. package/util/initMeta.js +19 -0
  113. package/util/register.d.ts +4 -0
  114. package/util/register.js +7 -0
  115. package/util/wsMeta.d.ts +4 -0
  116. package/util/wsMeta.js +55 -0
  117. package/build/scripts/polkadot-types-chain-info.mjs +0 -7
  118. package/build/scripts/polkadot-types-from-chain.mjs +0 -7
  119. package/build/scripts/polkadot-types-from-defs.mjs +0 -7
  120. package/scripts/polkadot-types-internal-interfaces.mjs +0 -7
  121. package/scripts/polkadot-types-internal-metadata.mjs +0 -7
  122. package/src/augment.ts +0 -5
  123. package/src/bundle.ts +0 -5
  124. package/src/extractChain.ts +0 -54
  125. package/src/fromChain.ts +0 -123
  126. package/src/fromDefs.ts +0 -106
  127. package/src/generate/consts.ts +0 -112
  128. package/src/generate/errors.ts +0 -75
  129. package/src/generate/events.ts +0 -165
  130. package/src/generate/interfaceRegistry.ts +0 -85
  131. package/src/generate/lookup.ts +0 -294
  132. package/src/generate/query.ts +0 -169
  133. package/src/generate/rpc.ts +0 -158
  134. package/src/generate/runtime.ts +0 -284
  135. package/src/generate/tsDef.ts +0 -321
  136. package/src/generate/tx.ts +0 -152
  137. package/src/generate/types.ts +0 -26
  138. package/src/index.ts +0 -6
  139. package/src/interfacesTs.ts +0 -35
  140. package/src/metadataMd.ts +0 -844
  141. package/src/packageInfo.ts +0 -6
  142. package/src/util/assert.ts +0 -18
  143. package/src/util/derived.spec.ts +0 -58
  144. package/src/util/derived.ts +0 -133
  145. package/src/util/docs.ts +0 -13
  146. package/src/util/file.ts +0 -42
  147. package/src/util/formatting.spec.ts +0 -30
  148. package/src/util/formatting.ts +0 -295
  149. package/src/util/imports.ts +0 -164
  150. package/src/util/initMeta.ts +0 -37
  151. package/src/util/register.ts +0 -12
  152. package/src/util/wsMeta.ts +0 -70
  153. package/tsconfig.build.json +0 -28
  154. package/tsconfig.build.tsbuildinfo +0 -1
  155. package/tsconfig.scripts.json +0 -19
  156. package/tsconfig.spec.json +0 -17
  157. /package/{build/augment.d.ts → augment.d.ts} +0 -0
  158. /package/{build/bundle.d.ts → bundle.d.ts} +0 -0
  159. /package/{build → cjs}/extractChain.d.ts +0 -0
  160. /package/{build → cjs}/fromChain.d.ts +0 -0
  161. /package/{build → cjs}/fromDefs.d.ts +0 -0
  162. /package/{build → cjs}/generate/consts.d.ts +0 -0
  163. /package/{build → cjs}/generate/errors.d.ts +0 -0
  164. /package/{build → cjs}/generate/events.d.ts +0 -0
  165. /package/{build → cjs}/generate/index.d.ts +0 -0
  166. /package/{build → cjs}/generate/interfaceRegistry.d.ts +0 -0
  167. /package/{build → cjs}/generate/lookup.d.ts +0 -0
  168. /package/{build → cjs}/generate/query.d.ts +0 -0
  169. /package/{build → cjs}/generate/rpc.d.ts +0 -0
  170. /package/{build → cjs}/generate/runtime.d.ts +0 -0
  171. /package/{build → cjs}/generate/tx.d.ts +0 -0
  172. /package/{build → cjs}/generate/types.d.ts +0 -0
  173. /package/{build → cjs}/index.d.ts +0 -0
  174. /package/{build → cjs}/interfacesTs.d.ts +0 -0
  175. /package/{build → cjs}/metadataMd.d.ts +0 -0
  176. /package/{build → cjs}/packageDetect.d.ts +0 -0
  177. /package/{build → cjs}/packageInfo.d.ts +0 -0
  178. /package/{build → cjs}/util/assert.d.ts +0 -0
  179. /package/{build → cjs}/util/derived.d.ts +0 -0
  180. /package/{build → cjs}/util/docs.d.ts +0 -0
  181. /package/{build → cjs}/util/file.d.ts +0 -0
  182. /package/{build → cjs}/util/imports.d.ts +0 -0
  183. /package/{build → cjs}/util/initMeta.d.ts +0 -0
  184. /package/{build → cjs}/util/register.d.ts +0 -0
  185. /package/{build → cjs}/util/wsMeta.d.ts +0 -0
  186. /package/{src/templates → templates}/docs.hbs +0 -0
  187. /package/{src/templates → templates}/header.hbs +0 -0
  188. /package/{src/templates → templates}/lookup/defs-named.hbs +0 -0
  189. /package/{src/templates → templates}/lookup/defs.hbs +0 -0
  190. /package/{src/templates → templates}/lookup/index.hbs +0 -0
  191. /package/{src/templates → templates}/tsDef/index.hbs +0 -0
  192. /package/{src/templates → templates}/tsDef/moduleTypes.hbs +0 -0
  193. /package/{src/templates → templates}/tsDef/types.hbs +0 -0
@@ -2,13 +2,13 @@
2
2
 
3
3
  // import type lookup before we augment - in some environments
4
4
  // this is required to allow for ambient/previous definitions
5
- import '@polkadot/api-base/types/errors';
5
+ import '@pezkuwi/api-base/types/errors';
6
6
 
7
7
  {{{ importsAll }}}
8
8
 
9
9
  export type __AugmentedError<ApiType extends ApiTypes> = AugmentedError<ApiType>;
10
10
 
11
- declare module '@polkadot/api-base/types/errors' {
11
+ declare module '@pezkuwi/api-base/types/errors' {
12
12
  interface AugmentedErrors<ApiType extends ApiTypes> {
13
13
  {{#each modules}}
14
14
  {{{name}}}: {
@@ -2,13 +2,13 @@
2
2
 
3
3
  // import type lookup before we augment - in some environments
4
4
  // this is required to allow for ambient/previous definitions
5
- import '@polkadot/api-base/types/events';
5
+ import '@pezkuwi/api-base/types/events';
6
6
 
7
7
  {{{ importsAll }}}
8
8
 
9
9
  export type __AugmentedEvent<ApiType extends ApiTypes> = AugmentedEvent<ApiType>;
10
10
 
11
- declare module '@polkadot/api-base/types/events' {
11
+ declare module '@pezkuwi/api-base/types/events' {
12
12
  interface AugmentedEvents<ApiType extends ApiTypes> {
13
13
  {{#each modules}}
14
14
  {{{name}}}: {
@@ -2,11 +2,11 @@
2
2
 
3
3
  // import type lookup before we augment - in some environments
4
4
  // this is required to allow for ambient/previous definitions
5
- import '@polkadot/types/types/registry';
5
+ import '@pezkuwi/types/types/registry';
6
6
 
7
7
  {{{ importsAll }}}
8
8
 
9
- declare module '@polkadot/types/types/registry' {
9
+ declare module '@pezkuwi/types/types/registry' {
10
10
  interface InterfaceTypes {
11
11
  {{#each items}}
12
12
  {{{this}}}: {{{this}}};
@@ -2,11 +2,11 @@
2
2
 
3
3
  // import type lookup before we augment - in some environments
4
4
  // this is required to allow for ambient/previous definitions
5
- import '@polkadot/types/lookup';
5
+ import '@pezkuwi/types/lookup';
6
6
 
7
7
  {{{ importsAll }}}
8
8
 
9
- declare module '@polkadot/types/lookup' {
9
+ declare module '@pezkuwi/types/lookup' {
10
10
  {{#each items}}
11
11
  {{{this}}}
12
12
 
@@ -2,14 +2,14 @@
2
2
 
3
3
  // import type lookup before we augment - in some environments
4
4
  // this is required to allow for ambient/previous definitions
5
- import '@polkadot/api-base/types/storage';
5
+ import '@pezkuwi/api-base/types/storage';
6
6
 
7
7
  {{{ importsAll }}}
8
8
 
9
9
  export type __AugmentedQuery<ApiType extends ApiTypes> = AugmentedQuery<ApiType, () => unknown>;
10
10
  export type __QueryableStorageEntry<ApiType extends ApiTypes> = QueryableStorageEntry<ApiType>;
11
11
 
12
- declare module '@polkadot/api-base/types/storage' {
12
+ declare module '@pezkuwi/api-base/types/storage' {
13
13
  interface AugmentedQueries<ApiType extends ApiTypes> {
14
14
  {{#each modules}}
15
15
  {{{name}}}: {
@@ -2,13 +2,13 @@
2
2
 
3
3
  // import type lookup before we augment - in some environments
4
4
  // this is required to allow for ambient/previous definitions
5
- import '@polkadot/rpc-core/types/jsonrpc';
5
+ import '@pezkuwi/rpc-core/types/jsonrpc';
6
6
 
7
7
  {{{ importsAll }}}
8
8
 
9
9
  export type __AugmentedRpc = AugmentedRpc<() => unknown>;
10
10
 
11
- declare module '@polkadot/rpc-core/types/jsonrpc' {
11
+ declare module '@pezkuwi/rpc-core/types/jsonrpc' {
12
12
  interface RpcInterface {
13
13
  {{#each modules}}
14
14
  {{{name}}}: {
@@ -2,7 +2,7 @@
2
2
 
3
3
  // import type lookup before we augment - in some environments
4
4
  // this is required to allow for ambient/previous definitions
5
- import '@polkadot/api-base/types/submittable';
5
+ import '@pezkuwi/api-base/types/submittable';
6
6
 
7
7
  {{{ importsAll }}}
8
8
 
@@ -10,7 +10,7 @@ export type __AugmentedSubmittable = AugmentedSubmittable<() => unknown>;
10
10
  export type __SubmittableExtrinsic<ApiType extends ApiTypes> = SubmittableExtrinsic<ApiType>;
11
11
  export type __SubmittableExtrinsicFunction<ApiType extends ApiTypes> = SubmittableExtrinsicFunction<ApiType>;
12
12
 
13
- declare module '@polkadot/api-base/types/submittable' {
13
+ declare module '@pezkuwi/api-base/types/submittable' {
14
14
  interface AugmentedSubmittables<ApiType extends ApiTypes> {
15
15
  {{#each modules}}
16
16
  {{{name}}}: {
@@ -0,0 +1,2 @@
1
+ export declare function assertDir(path: string): string;
2
+ export declare function assertFile(path: string): string;
package/util/assert.js ADDED
@@ -0,0 +1,10 @@
1
+ import fs from 'node:fs';
2
+ import { assert } from '@pezkuwi/util';
3
+ export function assertDir(path) {
4
+ assert(fs.existsSync(path) && fs.lstatSync(path).isDirectory(), `${path} is not a directory`);
5
+ return path;
6
+ }
7
+ export function assertFile(path) {
8
+ assert(fs.existsSync(path) && fs.lstatSync(path).isFile(), `${path} is not a file`);
9
+ return path;
10
+ }
@@ -0,0 +1,4 @@
1
+ import type { Registry } from '@pezkuwi/types/types';
2
+ import type { ModuleTypes, TypeImports } from './imports.js';
3
+ /** @internal */
4
+ export declare function getSimilarTypes(registry: Registry, definitions: Record<string, ModuleTypes>, _type: string, imports: TypeImports): string[];
@@ -0,0 +1,125 @@
1
+ import { GenericAccountId, GenericCall, GenericLookupSource, GenericVote } from '@pezkuwi/types/generic';
2
+ import { AllConvictions } from '@pezkuwi/types/interfaces/democracy/definitions';
3
+ import { AbstractInt, bool, Compact, Enum, Null, Option, Struct, Tuple, Vec, WrapperKeepOpaque, WrapperOpaque } from '@pezkuwi/types-codec';
4
+ import { getTypeDef, TypeDefInfo } from '@pezkuwi/types-create';
5
+ import { isChildClass, stringify } from '@pezkuwi/util';
6
+ import { formatType } from './formatting.js';
7
+ import { setImports } from './imports.js';
8
+ function arrayToStrType(arr) {
9
+ return `${arr.map((c) => `'${c}'`).join(' | ')}`;
10
+ }
11
+ const voteConvictions = arrayToStrType(AllConvictions);
12
+ /** @internal */
13
+ export function getSimilarTypes(registry, definitions, _type, imports) {
14
+ const typeParts = _type.split('::');
15
+ const type = typeParts[typeParts.length - 1];
16
+ const possibleTypes = [formatType(registry, definitions, type, imports)];
17
+ if (type === 'Extrinsic') {
18
+ setImports(definitions, imports, ['IExtrinsic']);
19
+ return ['Extrinsic', 'IExtrinsic', 'string', 'Uint8Array'];
20
+ }
21
+ else if (type === 'Keys') {
22
+ // This one is weird... surely it should popup as a Tuple? (but either way better as defined hex)
23
+ return ['Keys', 'string', 'Uint8Array'];
24
+ }
25
+ else if (type === 'StorageKey') {
26
+ // TODO We can do better
27
+ return ['StorageKey', 'string', 'Uint8Array', 'any'];
28
+ }
29
+ else if (type === '()') {
30
+ return ['null'];
31
+ }
32
+ const Clazz = registry.createClass(type);
33
+ if (isChildClass(Vec, Clazz)) {
34
+ const vecDef = getTypeDef(type);
35
+ const subDef = (vecDef.sub);
36
+ // this could be that we define a Vec type and refer to it by name
37
+ if (subDef) {
38
+ if (subDef.info === TypeDefInfo.Plain) {
39
+ possibleTypes.push(`(${getSimilarTypes(registry, definitions, subDef.type, imports).join(' | ')})[]`);
40
+ }
41
+ else if (subDef.info === TypeDefInfo.Tuple) {
42
+ const subs = subDef.sub.map(({ type }) => getSimilarTypes(registry, definitions, type, imports).join(' | '));
43
+ possibleTypes.push(`([${subs.join(', ')}])[]`);
44
+ }
45
+ else if (subDef.info === TypeDefInfo.Option || subDef.info === TypeDefInfo.Vec || subDef.info === TypeDefInfo.VecFixed) {
46
+ // TODO: Add possibleTypes so imports work
47
+ }
48
+ else if (subDef.info === TypeDefInfo.Struct) {
49
+ // TODO: Add possibleTypes so imports work
50
+ }
51
+ else {
52
+ throw new Error(`Unhandled subtype in Vec, ${stringify(subDef)}`);
53
+ }
54
+ }
55
+ }
56
+ else if (isChildClass(Enum, Clazz)) {
57
+ const { defKeys, isBasic } = new Clazz(registry);
58
+ const keys = defKeys.filter((v) => !v.startsWith('__Unused'));
59
+ if (isBasic) {
60
+ possibleTypes.push(arrayToStrType(keys), 'number');
61
+ }
62
+ else {
63
+ // TODO We don't really want any here, these should be expanded
64
+ possibleTypes.push(...keys.map((k) => `{ ${k}: any }`), 'string');
65
+ }
66
+ possibleTypes.push('Uint8Array');
67
+ }
68
+ else if (isChildClass(AbstractInt, Clazz) || isChildClass(Compact, Clazz)) {
69
+ possibleTypes.push('AnyNumber', 'Uint8Array');
70
+ }
71
+ else if (isChildClass(GenericLookupSource, Clazz)) {
72
+ possibleTypes.push('Address', 'AccountId', 'AccountIndex', 'LookupSource', 'string', 'Uint8Array');
73
+ }
74
+ else if (isChildClass(GenericAccountId, Clazz)) {
75
+ possibleTypes.push('string', 'Uint8Array');
76
+ }
77
+ else if (isChildClass(GenericCall, Clazz)) {
78
+ possibleTypes.push('IMethod', 'string', 'Uint8Array');
79
+ }
80
+ else if (isChildClass(bool, Clazz)) {
81
+ possibleTypes.push('boolean', 'Uint8Array');
82
+ }
83
+ else if (isChildClass(Null, Clazz)) {
84
+ possibleTypes.push('null');
85
+ }
86
+ else if (isChildClass(Struct, Clazz)) {
87
+ const s = new Clazz(registry);
88
+ const obj = s.defKeys.map((key) => `${key}?: any`).join('; ');
89
+ possibleTypes.push(`{ ${obj} }`, 'string', 'Uint8Array');
90
+ }
91
+ else if (isChildClass(Option, Clazz)) {
92
+ possibleTypes.push('null', 'Uint8Array');
93
+ const optDef = getTypeDef(type);
94
+ const subDef = (optDef.sub);
95
+ if (subDef) {
96
+ possibleTypes.push(...getSimilarTypes(registry, definitions, subDef.type, imports));
97
+ }
98
+ else {
99
+ possibleTypes.push('object', 'string');
100
+ }
101
+ }
102
+ else if (isChildClass(GenericVote, Clazz)) {
103
+ possibleTypes.push(`{ aye: boolean; conviction?: ${voteConvictions} | number }`, 'boolean', 'string', 'Uint8Array');
104
+ }
105
+ else if (isChildClass(WrapperKeepOpaque, Clazz) || isChildClass(WrapperOpaque, Clazz)) {
106
+ // TODO inspect container
107
+ possibleTypes.push('object', 'string', 'Uint8Array');
108
+ }
109
+ else if (isChildClass(Uint8Array, Clazz)) {
110
+ possibleTypes.push('string', 'Uint8Array');
111
+ }
112
+ else if (isChildClass(String, Clazz)) {
113
+ possibleTypes.push('string');
114
+ }
115
+ else if (isChildClass(Tuple, Clazz)) {
116
+ const tupDef = getTypeDef(type);
117
+ const subDef = tupDef.sub;
118
+ // this could be that we define a Tuple type and refer to it by name
119
+ if (Array.isArray(subDef)) {
120
+ const subs = subDef.map(({ type }) => getSimilarTypes(registry, definitions, type, imports).join(' | '));
121
+ possibleTypes.push(`[${subs.join(', ')}]`);
122
+ }
123
+ }
124
+ return [...new Set(possibleTypes)];
125
+ }
package/util/docs.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const __TYPEGEN_DUMMY_DOCS = "DUMMY_DOCS";
package/util/docs.js ADDED
@@ -0,0 +1,6 @@
1
+ import Handlebars from 'handlebars';
2
+ import { readTemplate } from './file.js';
3
+ Handlebars.registerPartial({
4
+ docs: Handlebars.compile(readTemplate('docs'))
5
+ });
6
+ export const __TYPEGEN_DUMMY_DOCS = 'DUMMY_DOCS';
package/util/file.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare function writeFile(dest: string, generator: () => string, noLog?: boolean): void;
2
+ export declare function readTemplate(template: string): string;
package/util/file.js ADDED
@@ -0,0 +1,28 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import process from 'node:process';
4
+ import { packageInfo } from '../packageInfo.js';
5
+ export function writeFile(dest, generator, noLog) {
6
+ !noLog && console.log(`${dest}\n\tGenerating`);
7
+ let generated = generator();
8
+ while (generated.includes('\n\n\n')) {
9
+ generated = generated.replace(/\n\n\n/g, '\n\n');
10
+ }
11
+ !noLog && console.log('\tWriting');
12
+ fs.writeFileSync(dest, generated, { flag: 'w' });
13
+ !noLog && console.log('');
14
+ }
15
+ export function readTemplate(template) {
16
+ // Inside the api repo itself, it will be 'auto'
17
+ const rootDir = packageInfo.path === 'auto'
18
+ ? path.join(process.cwd(), 'packages/typegen/src')
19
+ : packageInfo.path;
20
+ // NOTE With cjs in a subdir, search one lower as well
21
+ const file = ['./templates', '../templates']
22
+ .map((p) => path.join(rootDir, p, `${template}.hbs`))
23
+ .find((p) => fs.existsSync(p));
24
+ if (!file) {
25
+ throw new Error(`Unable to locate ${template}.hbs from ${rootDir}`);
26
+ }
27
+ return fs.readFileSync(file).toString();
28
+ }
@@ -0,0 +1,11 @@
1
+ import type { AnyString, Registry } from '@pezkuwi/types/types';
2
+ import type { TypeDef } from '@pezkuwi/types-create/types';
3
+ import type { ModuleTypes, TypeImports } from './imports.js';
4
+ export declare const HEADER: (type: "chain" | "defs") => string;
5
+ /** @internal */
6
+ export declare function exportInterface(lookupIndex: number | undefined, name: string | undefined, base: string, body?: string, withShortcut?: boolean): string;
7
+ /**
8
+ * Correctly format a given type
9
+ */
10
+ /** @internal */
11
+ export declare function formatType(registry: Registry, definitions: Record<string, ModuleTypes>, type: AnyString | TypeDef, imports: TypeImports, withShortcut?: boolean): string;
@@ -0,0 +1,230 @@
1
+ import Handlebars from 'handlebars';
2
+ import * as typesCodec from '@pezkuwi/types-codec';
3
+ import { getTypeDef, paramsNotation, TypeDefInfo } from '@pezkuwi/types-create';
4
+ import { isString, stringify } from '@pezkuwi/util';
5
+ import { readTemplate } from './file.js';
6
+ import { setImports } from './imports.js';
7
+ function rebrandTypeName(typeName) {
8
+ return typeName
9
+ // Main projects
10
+ .replace(/Polkadot/g, 'Pezkuwi')
11
+ .replace(/Substrate/g, 'Bizinikiwi')
12
+ .replace(/Cumulus/g, 'Pezcumulus')
13
+ // Networks
14
+ .replace(/Kusama/g, 'Dicle')
15
+ .replace(/Westend/g, 'Zagros')
16
+ .replace(/Rococo/g, 'Pezkuwichain')
17
+ // Chain types
18
+ .replace(/Parachain/g, 'Teyrchain')
19
+ // Module prefixes (in PascalCase type names)
20
+ .replace(/Frame(?=[A-Z])/g, 'Pezframe')
21
+ .replace(/Pallet(?=[A-Z])/g, 'Pezpallet')
22
+ // Primitives prefixes (sp_ becomes Sp in type names)
23
+ .replace(/^Sp(?=[A-Z])/g, 'Pezsp')
24
+ .replace(/([^a-zA-Z])Sp(?=[A-Z])/g, '$1Pezsp')
25
+ // Client prefixes (sc_ becomes Sc in type names)
26
+ .replace(/^Sc(?=[A-Z])/g, 'Pezsc')
27
+ .replace(/([^a-zA-Z])Sc(?=[A-Z])/g, '$1Pezsc');
28
+ }
29
+ const NO_CODEC = ['Tuple', 'VecFixed'];
30
+ const ON_CODEC = Object.keys(typesCodec);
31
+ const ON_CODEC_TYPES = ['Codec', 'AnyJson', 'AnyFunction', 'AnyNumber', 'AnyString', 'AnyTuple', 'AnyU8a', 'ICompact', 'IEnum', 'IMap', 'INumber', 'IOption', 'IResult', 'ISet', 'IStruct', 'ITuple', 'IU8a', 'IVec', 'IMethod'];
32
+ export const HEADER = (type) => `// Auto-generated via \`yarn pezkuwi-types-from-${type}\`, do not edit\n/* eslint-disable */\n\n`;
33
+ function extractImports({ imports, types }) {
34
+ const toplevel = [
35
+ ...Object.keys(imports.codecTypes),
36
+ ...Object.keys(imports.extrinsicTypes),
37
+ ...Object.keys(imports.genericTypes),
38
+ ...Object.keys(imports.metadataTypes),
39
+ ...Object.keys(imports.primitiveTypes)
40
+ ];
41
+ return [
42
+ {
43
+ file: '@pezkuwi/types',
44
+ types: toplevel.filter((n) => !NO_CODEC.includes(n) && !ON_CODEC.includes(n))
45
+ },
46
+ {
47
+ file: '@pezkuwi/types/lookup',
48
+ types: Object.keys(imports.lookupTypes)
49
+ },
50
+ {
51
+ file: '@pezkuwi/types/types',
52
+ types: Object.keys(imports.typesTypes).filter((n) => !ON_CODEC_TYPES.includes(n))
53
+ },
54
+ {
55
+ file: '@pezkuwi/types-codec',
56
+ types: toplevel.filter((n) => !NO_CODEC.includes(n) && ON_CODEC.includes(n))
57
+ },
58
+ {
59
+ file: '@pezkuwi/types-codec/types',
60
+ types: Object.keys(imports.typesTypes).filter((n) => ON_CODEC_TYPES.includes(n))
61
+ },
62
+ ...types
63
+ ]
64
+ .filter(({ types }) => types.length)
65
+ .sort(({ file }, b) => file.localeCompare(b.file))
66
+ .map(({ file, types }) => `import type { ${types.sort().join(', ')} } from '${file}';`);
67
+ }
68
+ Handlebars.registerPartial({
69
+ header: Handlebars.compile(readTemplate('header'))
70
+ });
71
+ Handlebars.registerHelper({
72
+ importsAll() {
73
+ return extractImports(this)
74
+ .join('\n');
75
+ },
76
+ importsPackage() {
77
+ return extractImports(this)
78
+ .filter((l) => !l.includes("from '."))
79
+ .join('\n ');
80
+ },
81
+ importsRelative() {
82
+ return extractImports(this)
83
+ .filter((l) => l.includes("from '."))
84
+ .join('\n');
85
+ },
86
+ trim(options) {
87
+ return options.fn(this).trim();
88
+ },
89
+ upper(options) {
90
+ return options.fn(this).toUpperCase();
91
+ }
92
+ });
93
+ /** @internal */
94
+ export function exportInterface(lookupIndex = -1, name = '', base, body = '', withShortcut = false) {
95
+ // * @description extends [[${base}]]
96
+ const doc = withShortcut
97
+ ? ''
98
+ : `/** @name ${name}${lookupIndex !== -1 ? ` (${lookupIndex})` : ''} */\n`;
99
+ return `${doc}${withShortcut ? '' : `export interface ${name} extends ${base} `}{${body.length ? '\n' : ''}${body}${withShortcut ? ' ' : ''}}`;
100
+ }
101
+ function singleParamNotation(registry, wrapper, typeDef, definitions, imports, withShortcut) {
102
+ const sub = typeDef.sub;
103
+ const lookupName = rebrandTypeName(sub.lookupName || '');
104
+ setImports(definitions, imports, [wrapper, lookupName]);
105
+ return paramsNotation(wrapper, lookupName || formatType(registry, definitions, sub.type, imports, withShortcut));
106
+ }
107
+ function dualParamsNotation(registry, wrapper, typeDef, definitions, imports, withShortcut) {
108
+ const [a, b] = typeDef.sub;
109
+ const aLookupName = rebrandTypeName(a.lookupName || '');
110
+ const bLookupName = rebrandTypeName(b.lookupName || '');
111
+ setImports(definitions, imports, [wrapper, aLookupName, bLookupName]);
112
+ return paramsNotation(wrapper, [
113
+ aLookupName || formatType(registry, definitions, a.type, imports, withShortcut),
114
+ bLookupName || formatType(registry, definitions, b.type, imports, withShortcut)
115
+ ]);
116
+ }
117
+ const formatters = {
118
+ [TypeDefInfo.Compact]: (registry, typeDef, definitions, imports, withShortcut) => {
119
+ return singleParamNotation(registry, 'Compact', typeDef, definitions, imports, withShortcut);
120
+ },
121
+ [TypeDefInfo.DoNotConstruct]: (_registry, _typeDef, definitions, imports, _withShortcut) => {
122
+ setImports(definitions, imports, ['DoNotConstruct']);
123
+ return 'DoNotConstruct';
124
+ },
125
+ [TypeDefInfo.Enum]: (_registry, typeDef, _definitions, _imports, _withShortcut) => {
126
+ if (typeDef.lookupName) {
127
+ return rebrandTypeName(typeDef.lookupName);
128
+ }
129
+ throw new Error(`TypeDefInfo.Enum: Parameter formatting not implemented on ${stringify(typeDef)}`);
130
+ },
131
+ [TypeDefInfo.Int]: (_registry, typeDef, _definitions, _imports, _withShortcut) => {
132
+ throw new Error(`TypeDefInfo.Int: Parameter formatting not implemented on ${stringify(typeDef)}`);
133
+ },
134
+ [TypeDefInfo.UInt]: (_registry, typeDef, _definitions, _imports, _withShortcut) => {
135
+ throw new Error(`TypeDefInfo.UInt: Parameter formatting not implemented on ${stringify(typeDef)}`);
136
+ },
137
+ [TypeDefInfo.Null]: (_registry, _typeDef, definitions, imports, _withShortcut) => {
138
+ setImports(definitions, imports, ['Null']);
139
+ return 'Null';
140
+ },
141
+ [TypeDefInfo.Option]: (registry, typeDef, definitions, imports, withShortcut) => {
142
+ return singleParamNotation(registry, 'Option', typeDef, definitions, imports, withShortcut);
143
+ },
144
+ [TypeDefInfo.Plain]: (_registry, typeDef, definitions, imports, _withShortcut) => {
145
+ setImports(definitions, imports, [rebrandTypeName(typeDef.type)]);
146
+ return rebrandTypeName(typeDef.type);
147
+ },
148
+ [TypeDefInfo.Range]: (registry, typeDef, definitions, imports, withShortcut) => {
149
+ return singleParamNotation(registry, 'Range', typeDef, definitions, imports, withShortcut);
150
+ },
151
+ [TypeDefInfo.RangeInclusive]: (registry, typeDef, definitions, imports, withShortcut) => {
152
+ return singleParamNotation(registry, 'RangeInclusive', typeDef, definitions, imports, withShortcut);
153
+ },
154
+ [TypeDefInfo.Set]: (_registry, typeDef, _definitions, _imports, _withShortcut) => {
155
+ throw new Error(`TypeDefInfo.Set: Parameter formatting not implemented on ${stringify(typeDef)}`);
156
+ },
157
+ [TypeDefInfo.Si]: (registry, typeDef, definitions, imports, withShortcut) => {
158
+ return formatType(registry, definitions, registry.lookup.getTypeDef(typeDef.type), imports, withShortcut);
159
+ },
160
+ [TypeDefInfo.Struct]: (registry, typeDef, definitions, imports, withShortcut) => {
161
+ if (typeDef.lookupName) {
162
+ return rebrandTypeName(typeDef.lookupName);
163
+ }
164
+ const sub = typeDef.sub;
165
+ setImports(definitions, imports, ['Struct', ...sub.map(({ lookupName }) => rebrandTypeName(lookupName || ''))]);
166
+ return `{${withShortcut ? ' ' : '\n'}${sub.map(({ lookupName, name, type }, index) => [
167
+ name || `unknown${index}`,
168
+ rebrandTypeName(lookupName || '') || formatType(registry, definitions, type, imports, withShortcut)
169
+ ]).map(([k, t]) => `${withShortcut ? '' : ' readonly '}${k}: ${t};`).join(withShortcut ? ' ' : '\n')}${withShortcut ? ' ' : '\n '}} & Struct`;
170
+ },
171
+ [TypeDefInfo.Tuple]: (registry, typeDef, definitions, imports, withShortcut) => {
172
+ const sub = typeDef.sub;
173
+ setImports(definitions, imports, ['ITuple', ...sub.map(({ lookupName }) => rebrandTypeName(lookupName || ''))]);
174
+ // `(a,b)` gets transformed into `ITuple<[a, b]>`
175
+ return paramsNotation('ITuple', `[${sub.map(({ lookupName, type }) => rebrandTypeName(lookupName || '') || formatType(registry, definitions, type, imports, withShortcut)).join(', ')}]`);
176
+ },
177
+ [TypeDefInfo.Vec]: (registry, typeDef, definitions, imports, withShortcut) => {
178
+ return singleParamNotation(registry, 'Vec', typeDef, definitions, imports, withShortcut);
179
+ },
180
+ [TypeDefInfo.VecFixed]: (registry, typeDef, definitions, imports, withShortcut) => {
181
+ const sub = typeDef.sub;
182
+ if (sub.type === 'u8') {
183
+ setImports(definitions, imports, ['U8aFixed']);
184
+ return 'U8aFixed';
185
+ }
186
+ return singleParamNotation(registry, 'Vec', typeDef, definitions, imports, withShortcut);
187
+ },
188
+ [TypeDefInfo.BTreeMap]: (registry, typeDef, definitions, imports, withShortcut) => {
189
+ return dualParamsNotation(registry, 'BTreeMap', typeDef, definitions, imports, withShortcut);
190
+ },
191
+ [TypeDefInfo.BTreeSet]: (registry, typeDef, definitions, imports, withShortcut) => {
192
+ return singleParamNotation(registry, 'BTreeSet', typeDef, definitions, imports, withShortcut);
193
+ },
194
+ [TypeDefInfo.HashMap]: (registry, typeDef, definitions, imports, withShortcut) => {
195
+ return dualParamsNotation(registry, 'HashMap', typeDef, definitions, imports, withShortcut);
196
+ },
197
+ [TypeDefInfo.Linkage]: (registry, typeDef, definitions, imports, withShortcut) => {
198
+ return singleParamNotation(registry, 'Linkage', typeDef, definitions, imports, withShortcut);
199
+ },
200
+ [TypeDefInfo.Result]: (registry, typeDef, definitions, imports, withShortcut) => {
201
+ return dualParamsNotation(registry, 'Result', typeDef, definitions, imports, withShortcut);
202
+ },
203
+ [TypeDefInfo.WrapperKeepOpaque]: (registry, typeDef, definitions, imports, withShortcut) => {
204
+ return singleParamNotation(registry, 'WrapperKeepOpaque', typeDef, definitions, imports, withShortcut);
205
+ },
206
+ [TypeDefInfo.WrapperOpaque]: (registry, typeDef, definitions, imports, withShortcut) => {
207
+ return singleParamNotation(registry, 'WrapperOpaque', typeDef, definitions, imports, withShortcut);
208
+ }
209
+ };
210
+ /**
211
+ * Correctly format a given type
212
+ */
213
+ /** @internal */
214
+ export function formatType(registry, definitions, type, imports, withShortcut = false) {
215
+ let typeDef;
216
+ if (isString(type)) {
217
+ const _type = type.toString();
218
+ // If type is "unorthodox" (i.e. `{ something: any }` for an Enum input or `[a | b | c, d | e | f]` for a Tuple's similar types),
219
+ // we return it as-is
220
+ if (withShortcut && /(^{.+:.+})|^\([^,]+\)|^\(.+\)\[\]|^\[.+\]/.exec(_type) && !/\[\w+;\w+\]/.exec(_type)) {
221
+ return rebrandTypeName(_type);
222
+ }
223
+ typeDef = getTypeDef(type);
224
+ }
225
+ else {
226
+ typeDef = type;
227
+ }
228
+ setImports(definitions, imports, [rebrandTypeName(typeDef.lookupName || typeDef.type)]);
229
+ return rebrandTypeName(formatters[typeDef.info](registry, typeDef, definitions, imports, withShortcut));
230
+ }
@@ -0,0 +1,23 @@
1
+ export interface ModuleTypes {
2
+ types: Record<string, unknown>;
3
+ }
4
+ type TypeExist = Record<string, boolean>;
5
+ type TypeExistMap = Record<string, TypeExist>;
6
+ export interface TypeImports {
7
+ codecTypes: TypeExist;
8
+ extrinsicTypes: TypeExist;
9
+ genericTypes: TypeExist;
10
+ ignoredTypes: string[];
11
+ localTypes: TypeExistMap;
12
+ lookupTypes: TypeExistMap;
13
+ primitiveTypes: TypeExist;
14
+ metadataTypes: TypeExist;
15
+ typesTypes: TypeExist;
16
+ definitions: Record<string, ModuleTypes>;
17
+ typeToModule: Record<string, string>;
18
+ }
19
+ /** @internal */
20
+ export declare function setImports(allDefs: Record<string, ModuleTypes>, imports: TypeImports, types: (string | null | undefined)[]): void;
21
+ /** @internal */
22
+ export declare function createImports(importDefinitions: Record<string, Record<string, ModuleTypes>>, { types }?: ModuleTypes): TypeImports;
23
+ export {};