Register all types for error generation (#3088)
* Register all types for error generation * type for ExtraTypes
This commit is contained in:
@@ -19,7 +19,7 @@ function generate (metaHex: string, pkg: string | undefined, output: string, isS
|
||||
: {};
|
||||
|
||||
generateDefaultConsts(path.join(process.cwd(), output, 'augment-api-consts.ts'), metaHex, extraTypes, isStrict);
|
||||
generateDefaultErrors(path.join(process.cwd(), output, 'augment-api-errors.ts'), metaHex, isStrict);
|
||||
generateDefaultErrors(path.join(process.cwd(), output, 'augment-api-errors.ts'), metaHex, extraTypes, isStrict);
|
||||
generateDefaultEvents(path.join(process.cwd(), output, 'augment-api-events.ts'), metaHex, extraTypes, isStrict);
|
||||
generateDefaultQuery(path.join(process.cwd(), output, 'augment-api-query.ts'), metaHex, extraTypes, isStrict);
|
||||
generateDefaultRpc(path.join(process.cwd(), output, 'augment-api-rpc.ts'), extraTypes);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/typegen authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ExtraTypes } from './types';
|
||||
|
||||
import Handlebars from 'handlebars';
|
||||
|
||||
import { Metadata } from '@polkadot/metadata/Metadata';
|
||||
@@ -15,7 +17,7 @@ const template = readTemplate('consts');
|
||||
const generateForMetaTemplate = Handlebars.compile(template);
|
||||
|
||||
/** @internal */
|
||||
function generateForMeta (meta: Metadata, dest: string, extraTypes: Record<string, Record<string, { types: Record<string, any> }>>, isStrict: boolean): void {
|
||||
function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean): void {
|
||||
writeFile(dest, (): string => {
|
||||
const allTypes: Record<string, Record<string, { types: Record<string, unknown> }>> = { '@polkadot/types/interfaces': defaultDefs, ...extraTypes };
|
||||
const imports = createImports(allTypes);
|
||||
@@ -74,7 +76,7 @@ function generateForMeta (meta: Metadata, dest: string, extraTypes: Record<strin
|
||||
|
||||
// Call `generateForMeta()` with current static metadata
|
||||
/** @internal */
|
||||
export function generateDefaultConsts (dest = 'packages/api/src/augment/consts.ts', data = staticData, extraTypes: Record<string, Record<string, { types: Record<string, any> }>> = {}, isStrict = false): void {
|
||||
export function generateDefaultConsts (dest = 'packages/api/src/augment/consts.ts', data = staticData, extraTypes: ExtraTypes = {}, isStrict = false): void {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
registerDefinitions(registry, extraTypes);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/typegen authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ExtraTypes } from './types';
|
||||
|
||||
import Handlebars from 'handlebars';
|
||||
|
||||
import { Metadata } from '@polkadot/metadata/Metadata';
|
||||
@@ -8,7 +10,7 @@ import staticData from '@polkadot/metadata/static';
|
||||
import { TypeRegistry } from '@polkadot/types/create';
|
||||
import { stringCamelCase } from '@polkadot/util';
|
||||
|
||||
import { compareName, createImports, readTemplate, writeFile } from '../util';
|
||||
import { compareName, createImports, readTemplate, registerDefinitions, writeFile } from '../util';
|
||||
|
||||
const template = readTemplate('errors');
|
||||
const generateForMetaTemplate = Handlebars.compile(template);
|
||||
@@ -50,8 +52,11 @@ function generateForMeta (meta: Metadata, dest: string, isStrict: boolean): void
|
||||
|
||||
// Call `generateForMeta()` with current static metadata
|
||||
/** @internal */
|
||||
export function generateDefaultErrors (dest = 'packages/api/src/augment/errors.ts', data = staticData, isStrict = false): void {
|
||||
export function generateDefaultErrors (dest = 'packages/api/src/augment/errors.ts', data = staticData, extraTypes: ExtraTypes = {}, isStrict = false): void {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
registerDefinitions(registry, extraTypes);
|
||||
|
||||
const metadata = new Metadata(registry, data);
|
||||
|
||||
registry.setMetadata(metadata);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/typegen authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ExtraTypes } from './types';
|
||||
|
||||
import Handlebars from 'handlebars';
|
||||
|
||||
import { Metadata } from '@polkadot/metadata/Metadata';
|
||||
@@ -15,7 +17,7 @@ const template = readTemplate('events');
|
||||
const generateForMetaTemplate = Handlebars.compile(template);
|
||||
|
||||
/** @internal */
|
||||
function generateForMeta (meta: Metadata, dest: string, extraTypes: Record<string, Record<string, { types: Record<string, any> }>>, isStrict: boolean): void {
|
||||
function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean): void {
|
||||
writeFile(dest, (): string => {
|
||||
const allTypes: Record<string, Record<string, { types: Record<string, unknown> }>> = { '@polkadot/types/interfaces': defaultDefs, ...extraTypes };
|
||||
const imports = createImports(allTypes);
|
||||
@@ -67,7 +69,7 @@ function generateForMeta (meta: Metadata, dest: string, extraTypes: Record<strin
|
||||
|
||||
// Call `generateForMeta()` with current static metadata
|
||||
/** @internal */
|
||||
export function generateDefaultEvents (dest = 'packages/api/src/augment/events.ts', data = staticData, extraTypes: Record<string, Record<string, { types: Record<string, any> }>> = {}, isStrict = false): void {
|
||||
export function generateDefaultEvents (dest = 'packages/api/src/augment/events.ts', data = staticData, extraTypes: ExtraTypes = {}, isStrict = false): void {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
registerDefinitions(registry, extraTypes);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
import type { StorageEntryMetadataLatest } from '@polkadot/types/interfaces/metadata';
|
||||
import type { Registry } from '@polkadot/types/types';
|
||||
import type { ExtraTypes } from './types';
|
||||
|
||||
import Handlebars from 'handlebars';
|
||||
|
||||
@@ -67,9 +68,9 @@ const template = readTemplate('query');
|
||||
const generateForMetaTemplate = Handlebars.compile(template);
|
||||
|
||||
/** @internal */
|
||||
function generateForMeta (registry: Registry, meta: Metadata, dest: string, extraTypes: Record<string, Record<string, { types: Record<string, any> }>>, isStrict: boolean): void {
|
||||
function generateForMeta (registry: Registry, meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean): void {
|
||||
writeFile(dest, (): string => {
|
||||
const allTypes: Record<string, Record<string, { types: Record<string, any> }>> = { '@polkadot/types/interfaces': defaultDefs, ...extraTypes };
|
||||
const allTypes: ExtraTypes = { '@polkadot/types/interfaces': defaultDefs, ...extraTypes };
|
||||
const imports = createImports(allTypes);
|
||||
const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => {
|
||||
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs);
|
||||
@@ -130,7 +131,7 @@ function generateForMeta (registry: Registry, meta: Metadata, dest: string, extr
|
||||
|
||||
// Call `generateForMeta()` with current static metadata
|
||||
/** @internal */
|
||||
export function generateDefaultQuery (dest = 'packages/api/src/augment/query.ts', data = staticData, extraTypes: Record<string, Record<string, { types: Record<string, any> }>> = {}, isStrict = false): void {
|
||||
export function generateDefaultQuery (dest = 'packages/api/src/augment/query.ts', data = staticData, extraTypes: ExtraTypes = {}, isStrict = false): void {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
registerDefinitions(registry, extraTypes);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Definitions } from '@polkadot/types/types';
|
||||
import type { ExtraTypes } from './types';
|
||||
|
||||
import Handlebars from 'handlebars';
|
||||
|
||||
@@ -29,9 +30,9 @@ const template = readTemplate('rpc');
|
||||
const generateRpcTypesTemplate = Handlebars.compile(template);
|
||||
|
||||
/** @internal */
|
||||
export function generateRpcTypes (registry: TypeRegistry, importDefinitions: Record<string, Definitions>, dest: string, extraTypes: Record<string, Record<string, { types: Record<string, any> }>> = {}): void {
|
||||
export function generateRpcTypes (registry: TypeRegistry, importDefinitions: Record<string, Definitions>, dest: string, extraTypes: ExtraTypes = {}): void {
|
||||
writeFile(dest, (): string => {
|
||||
const allTypes: Record<string, Record<string, { types: Record<string, any> }>> = { '@polkadot/types/interfaces': importDefinitions, ...extraTypes };
|
||||
const allTypes: ExtraTypes = { '@polkadot/types/interfaces': importDefinitions, ...extraTypes };
|
||||
const imports = createImports(allTypes);
|
||||
const definitions = imports.definitions as Record<string, Definitions>;
|
||||
const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => {
|
||||
@@ -141,7 +142,7 @@ export function generateRpcTypes (registry: TypeRegistry, importDefinitions: Rec
|
||||
});
|
||||
}
|
||||
|
||||
export function generateDefaultRpc (dest = 'packages/api/src/augment/rpc.ts', extraTypes: Record<string, Record<string, { types: Record<string, any> }>> = {}): void {
|
||||
export function generateDefaultRpc (dest = 'packages/api/src/augment/rpc.ts', extraTypes: ExtraTypes = {}): void {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
registerDefinitions(registry, extraTypes);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Registry } from '@polkadot/types/types';
|
||||
import type { ExtraTypes } from './types';
|
||||
|
||||
import Handlebars from 'handlebars';
|
||||
|
||||
@@ -28,9 +29,9 @@ const template = readTemplate('tx');
|
||||
const generateForMetaTemplate = Handlebars.compile(template);
|
||||
|
||||
/** @internal */
|
||||
function generateForMeta (registry: Registry, meta: Metadata, dest: string, extraTypes: Record<string, Record<string, { types: Record<string, any> }>>, isStrict: boolean): void {
|
||||
function generateForMeta (registry: Registry, meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean): void {
|
||||
writeFile(dest, (): string => {
|
||||
const allTypes: Record<string, Record<string, { types: Record<string, any> }>> = { '@polkadot/types/interfaces': defaultDefs, ...extraTypes };
|
||||
const allTypes: ExtraTypes = { '@polkadot/types/interfaces': defaultDefs, ...extraTypes };
|
||||
const imports = createImports(allTypes);
|
||||
const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => {
|
||||
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs);
|
||||
@@ -93,7 +94,7 @@ function generateForMeta (registry: Registry, meta: Metadata, dest: string, extr
|
||||
|
||||
// Call `generateForMeta()` with current static metadata
|
||||
/** @internal */
|
||||
export function generateDefaultTx (dest = 'packages/api/src/augment/tx.ts', data = staticData, extraTypes: Record<string, Record<string, { types: Record<string, any> }>> = {}, isStrict = false): void {
|
||||
export function generateDefaultTx (dest = 'packages/api/src/augment/tx.ts', data = staticData, extraTypes: ExtraTypes = {}, isStrict = false): void {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
registerDefinitions(registry, extraTypes);
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
// Copyright 2017-2021 @polkadot/typegen authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export type ExtraTypes = Record<string, Record<string, { types: Record<string, any> }>>;
|
||||
Reference in New Issue
Block a user