Augment user type registry (#1926)
* Augment user type registry * Augment InterfaceRegistry * Addition api.createType test * Import '@polkadot/types/interfaceAugment' * Skip CC on generated * Move augmentation to full path imports * Output to augment-*
This commit is contained in:
@@ -8,3 +8,4 @@ exclude_patterns:
|
||||
- "packages/typegen/scripts"
|
||||
- "packages/typegen/src"
|
||||
- "packages/types/src/interfaces/"
|
||||
- "packages/types/src/interfaceAugment.ts"
|
||||
|
||||
@@ -75,7 +75,7 @@ async function rpc (api: ApiPromise): Promise<void> {
|
||||
});
|
||||
}
|
||||
|
||||
function types (): void {
|
||||
function types (api: ApiPromise): void {
|
||||
// check correct types with `createType`
|
||||
const balance = createType(registry, 'Balance', 2);
|
||||
const gas = createType(registry, 'Gas', 2);
|
||||
@@ -85,7 +85,7 @@ function types (): void {
|
||||
const gasUnsafe = createTypeUnsafe(registry, 'Gas', [2]);
|
||||
const overriddenUnsafe = createTypeUnsafe<Header>(registry, 'Gas', [2]);
|
||||
|
||||
console.log(balance, gas, compact, gasUnsafe, overriddenUnsafe);
|
||||
console.log(balance, gas, compact, gasUnsafe, overriddenUnsafe, api.createType('AccountData'));
|
||||
}
|
||||
|
||||
async function tx (api: ApiPromise, keyring: TestKeyringMap): Promise<void> {
|
||||
@@ -139,7 +139,7 @@ async function main (): Promise<void> {
|
||||
derive(api);
|
||||
query(api, keyring);
|
||||
rpc(api);
|
||||
types();
|
||||
types(api);
|
||||
tx(api, keyring);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
// Module augmentation so that `api.{consts,query,rpc,tx}.*.*` work
|
||||
import './augment';
|
||||
import '@polkadot/api/types/augment';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { DeriveCustom, ExactDerive } from '@polkadot/api-derive';
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import yargs from 'yargs';
|
||||
import getWSClass from '@polkadot/rpc-provider/ws/getWSClass';
|
||||
@@ -20,15 +21,19 @@ function generate (metaHex: string, pkg: string | undefined, output: string, isS
|
||||
const extraTypes = pkg
|
||||
? { [pkg]: require(path.join(process.cwd(), output, 'definitions')) }
|
||||
: {};
|
||||
const hasRegistry = fs.existsSync(path.join(process.cwd(), output, 'augment-registry.ts'));
|
||||
|
||||
generateConst(path.join(process.cwd(), output, 'augment.consts.ts'), metaHex, extraTypes, isStrict);
|
||||
generateQuery(path.join(process.cwd(), output, 'augment.query.ts'), metaHex, extraTypes, isStrict);
|
||||
generateTx(path.join(process.cwd(), output, 'augment.tx.ts'), metaHex, extraTypes, isStrict);
|
||||
generateConst(path.join(process.cwd(), output, 'augment-consts.ts'), metaHex, extraTypes, isStrict);
|
||||
generateQuery(path.join(process.cwd(), output, 'augment-query.ts'), metaHex, extraTypes, isStrict);
|
||||
generateTx(path.join(process.cwd(), output, 'augment-tx.ts'), metaHex, extraTypes, isStrict);
|
||||
|
||||
writeFile(path.join(process.cwd(), output, 'augment.ts'), (): string =>
|
||||
[
|
||||
HEADER,
|
||||
...['./augment.consts', './augment.query', './augment.tx', '@polkadot/api/types/augment/rpc'].map((path) => `import '${path}';\n`)
|
||||
...[
|
||||
'@polkadot/api/types/augment/rpc',
|
||||
...['consts', 'query', hasRegistry ? 'registry' : '', 'tx'].filter((key) => !!key).map((key) => `./augment-${key}`)
|
||||
].map((path) => `import '${path}';\n`)
|
||||
].join('')
|
||||
);
|
||||
|
||||
|
||||
@@ -49,5 +49,5 @@ export default function main (): void {
|
||||
};
|
||||
|
||||
generateTsDef(allDefs, path.join(process.cwd(), input), pkg);
|
||||
generateInterfaceRegistry(allDefs, path.join(process.cwd(), input, 'interfaceRegistry.ts'));
|
||||
generateInterfaceRegistry({ [pkg]: userDefs }, path.join(process.cwd(), input, 'augment-registry.ts'), true);
|
||||
}
|
||||
|
||||
@@ -14,14 +14,13 @@ const primitiveClasses = {
|
||||
};
|
||||
|
||||
/** @internal */
|
||||
export function generateInterfaceRegistry (importDefinitions: { [importPath: string]: object }, dest: string): void {
|
||||
export function generateInterfaceRegistry (importDefinitions: { [importPath: string]: object }, dest: string, skipPrimitives?: boolean): void {
|
||||
writeFile(dest, (): string => {
|
||||
Object.entries(importDefinitions).reduce((acc, def) => Object.assign(acc, def), {} as object);
|
||||
|
||||
const imports = createImports(importDefinitions);
|
||||
const definitions = imports.definitions;
|
||||
|
||||
const primitives = Object
|
||||
const primitives = skipPrimitives ? '' : Object
|
||||
.keys(primitiveClasses)
|
||||
.filter((name): boolean => !!name.indexOf('Generic'))
|
||||
.reduce((accumulator, primitiveName): string => {
|
||||
@@ -29,7 +28,7 @@ export function generateInterfaceRegistry (importDefinitions: { [importPath: str
|
||||
|
||||
return [
|
||||
accumulator,
|
||||
getDerivedTypes(definitions, primitiveName, primitiveName, imports).map(indent(2)).join('\n')
|
||||
getDerivedTypes(definitions, primitiveName, primitiveName, imports).map(indent(4)).join('\n')
|
||||
].join('\n');
|
||||
}, '');
|
||||
|
||||
@@ -40,7 +39,7 @@ export function generateInterfaceRegistry (importDefinitions: { [importPath: str
|
||||
return [
|
||||
accumulator,
|
||||
...Object.keys(types).map((type): string =>
|
||||
getDerivedTypes(definitions, type, types[type], imports).map(indent(2)).join('\n')
|
||||
getDerivedTypes(definitions, type, types[type], imports).map(indent(4)).join('\n')
|
||||
)
|
||||
].join('\n');
|
||||
}, '');
|
||||
@@ -52,8 +51,8 @@ export function generateInterfaceRegistry (importDefinitions: { [importPath: str
|
||||
}))
|
||||
]);
|
||||
|
||||
const interfaceStart = 'export interface InterfaceRegistry {';
|
||||
const interfaceEnd = '\n}';
|
||||
const interfaceStart = "declare module '@polkadot/types/interfaceRegistry' {\n export interface InterfaceRegistry {";
|
||||
const interfaceEnd = '\n }\n}';
|
||||
|
||||
return header
|
||||
.concat(interfaceStart)
|
||||
@@ -68,6 +67,6 @@ export function generateInterfaceRegistry (importDefinitions: { [importPath: str
|
||||
export default function generateDefaultInterfaceRegistry (): void {
|
||||
generateInterfaceRegistry(
|
||||
{ '@polkadot/types/interfaces': defaultDefinitions },
|
||||
'packages/types/src/interfaceRegistry.ts'
|
||||
'packages/types/src/interfaceAugment.ts'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { InterfaceRegistry } from '@polkadot/types/interfaceRegistry';
|
||||
import { Codec, Constructor, InterfaceTypes, Registry } from '../types';
|
||||
import { FromReg, TypeDef, TypeDefInfo } from './types';
|
||||
|
||||
@@ -22,7 +23,6 @@ import U8aFixed, { BitLength as U8aFixedBitLength } from '../codec/U8aFixed';
|
||||
import UInt from '../codec/UInt';
|
||||
import Vec from '../codec/Vec';
|
||||
import VecFixed from '../codec/VecFixed';
|
||||
import { InterfaceRegistry } from '../interfaceRegistry';
|
||||
import { getTypeDef } from './getTypeDef';
|
||||
|
||||
export function createClass<T extends Codec = Codec, K extends string = string> (registry: Registry, type: K): Constructor<FromReg<T, K>> {
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { InterfaceRegistry } from '@polkadot/types/interfaceRegistry';
|
||||
import { Codec, Constructor, InterfaceTypes, Registry } from '../types';
|
||||
import { FromReg } from './types';
|
||||
|
||||
import { isU8a, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { InterfaceRegistry } from '../interfaceRegistry';
|
||||
import { createClass } from './createClass';
|
||||
|
||||
function u8aHasValue (value: Uint8Array): boolean {
|
||||
|
||||
@@ -2,10 +2,9 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { InterfaceRegistry } from '@polkadot/types/interfaceRegistry';
|
||||
import { Codec, InterfaceTypes } from '../types';
|
||||
|
||||
import { InterfaceRegistry } from '../interfaceRegistry';
|
||||
|
||||
// Type which says: if `K` is in the InterfaceRegistry, then return InterfaceRegistry[K], else fallback to T
|
||||
export type FromReg<T extends Codec, K extends string> = K extends InterfaceTypes
|
||||
? InterfaceRegistry[K]
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -2,9 +2,11 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import BN from 'bn.js';
|
||||
// augment the registry
|
||||
import '@polkadot/types/interfaceAugment';
|
||||
import { InterfaceRegistry } from '@polkadot/types/interfaceRegistry';
|
||||
|
||||
import { InterfaceRegistry } from '../interfaceRegistry';
|
||||
import BN from 'bn.js';
|
||||
|
||||
export type InterfaceTypes = keyof InterfaceRegistry;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user