fix: Generate custom type definitions (#4224)
* fix: Generate custom type definitions * Generate separate lookup types * fix: Custom imports * fix linting errors Co-authored-by: Github Actions <action@github.com>
This commit is contained in:
co-authored by
Github Actions
parent
194f6aece1
commit
e5a67d82fa
@@ -4,6 +4,7 @@
|
||||
import path from 'path';
|
||||
import yargs from 'yargs';
|
||||
|
||||
import { Definitions } from '@polkadot/types/types';
|
||||
import { formatNumber } from '@polkadot/util';
|
||||
import { WebSocket } from '@polkadot/x-ws';
|
||||
|
||||
@@ -18,12 +19,24 @@ function generate (metaHex: string, pkg: string | undefined, output: string, isS
|
||||
? { [pkg]: require(path.join(process.cwd(), output, 'definitions')) as Record<string, any> }
|
||||
: {};
|
||||
|
||||
generateDefaultConsts(path.join(process.cwd(), output, 'augment-api-consts.ts'), metaHex, extraTypes, isStrict);
|
||||
let customLookupDefinitions;
|
||||
|
||||
try {
|
||||
customLookupDefinitions = {
|
||||
rpc: {},
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires
|
||||
types: require(path.join(process.cwd(), output, 'lookup.ts')).default
|
||||
} as Definitions;
|
||||
} catch (error) {
|
||||
console.log('No custom definitions found.');
|
||||
}
|
||||
|
||||
generateDefaultConsts(path.join(process.cwd(), output, 'augment-api-consts.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions);
|
||||
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);
|
||||
generateDefaultEvents(path.join(process.cwd(), output, 'augment-api-events.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions);
|
||||
generateDefaultQuery(path.join(process.cwd(), output, 'augment-api-query.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions);
|
||||
generateDefaultRpc(path.join(process.cwd(), output, 'augment-api-rpc.ts'), extraTypes);
|
||||
generateDefaultTx(path.join(process.cwd(), output, 'augment-api-tx.ts'), metaHex, extraTypes, isStrict);
|
||||
generateDefaultTx(path.join(process.cwd(), output, 'augment-api-tx.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions);
|
||||
|
||||
writeFile(path.join(process.cwd(), output, 'augment-api.ts'), (): string =>
|
||||
[
|
||||
|
||||
@@ -8,11 +8,16 @@ import * as substrateDefs from '@polkadot/types/interfaces/definitions';
|
||||
|
||||
import { generateInterfaceTypes } from './generate/interfaceRegistry';
|
||||
import { generateTsDef } from './generate/tsDef';
|
||||
import { generateDefaultLookup } from './generate';
|
||||
|
||||
type ArgV = { input: string; package: string };
|
||||
type ArgV = { input: string; package: string; endpoint?: string; };
|
||||
|
||||
export function main (): void {
|
||||
const { input, package: pkg } = yargs.strict().options({
|
||||
const { endpoint, input, package: pkg } = yargs.strict().options({
|
||||
endpoint: {
|
||||
description: 'The endpoint to connect to (e.g. wss://kusama-rpc.polkadot.io) or relative path to a file containing the JSON output of an RPC state_getMetadata call',
|
||||
type: 'string'
|
||||
},
|
||||
input: {
|
||||
description: 'The directory to use for the user definitions',
|
||||
required: true,
|
||||
@@ -25,6 +30,13 @@ export function main (): void {
|
||||
}
|
||||
}).argv as ArgV;
|
||||
|
||||
if (endpoint) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const metaHex = (require(path.join(process.cwd(), endpoint)) as Record<string, string>).result;
|
||||
|
||||
generateDefaultLookup(path.join(process.cwd(), input), metaHex);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const userDefs = require(path.join(process.cwd(), input, 'definitions.ts')) as Record<string, any>;
|
||||
const userKeys = Object.keys(userDefs);
|
||||
|
||||
@@ -8,6 +8,7 @@ import Handlebars from 'handlebars';
|
||||
|
||||
import lookupDefinitions from '@polkadot/types/augment/lookup/definitions';
|
||||
import * as defaultDefs from '@polkadot/types/interfaces/definitions';
|
||||
import { Definitions } from '@polkadot/types/types';
|
||||
import { stringCamelCase } from '@polkadot/util';
|
||||
|
||||
import { compareName, createImports, formatType, initMeta, readTemplate, setImports, writeFile } from '../util';
|
||||
@@ -16,10 +17,15 @@ const template = readTemplate('consts');
|
||||
const generateForMetaTemplate = Handlebars.compile(template);
|
||||
|
||||
/** @internal */
|
||||
function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean): void {
|
||||
function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean, customLookupDefinitions?: Definitions): void {
|
||||
writeFile(dest, (): string => {
|
||||
const allTypes = {
|
||||
'@polkadot/types/augment': { lookup: lookupDefinitions },
|
||||
'@polkadot/types/augment': {
|
||||
lookup: {
|
||||
...lookupDefinitions,
|
||||
...customLookupDefinitions
|
||||
}
|
||||
},
|
||||
'@polkadot/types/interfaces': defaultDefs,
|
||||
...extraTypes
|
||||
};
|
||||
@@ -79,8 +85,8 @@ function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes,
|
||||
|
||||
// Call `generateForMeta()` with current static metadata
|
||||
/** @internal */
|
||||
export function generateDefaultConsts (dest = 'packages/api/src/augment/consts.ts', data?: string, extraTypes: ExtraTypes = {}, isStrict = false): void {
|
||||
export function generateDefaultConsts (dest = 'packages/api/src/augment/consts.ts', data?: string, extraTypes: ExtraTypes = {}, isStrict = false, customLookupDefinitions?: Definitions): void {
|
||||
const { metadata } = initMeta(data, extraTypes);
|
||||
|
||||
return generateForMeta(metadata, dest, extraTypes, isStrict);
|
||||
return generateForMeta(metadata, dest, extraTypes, isStrict, customLookupDefinitions);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import Handlebars from 'handlebars';
|
||||
|
||||
import lookupDefinitions from '@polkadot/types/augment/lookup/definitions';
|
||||
import * as defaultDefs from '@polkadot/types/interfaces/definitions';
|
||||
import { Definitions } from '@polkadot/types/types';
|
||||
import { stringCamelCase } from '@polkadot/util';
|
||||
|
||||
import { compareName, createImports, formatType, initMeta, readTemplate, setImports, writeFile } from '../util';
|
||||
@@ -16,10 +17,15 @@ const template = readTemplate('events');
|
||||
const generateForMetaTemplate = Handlebars.compile(template);
|
||||
|
||||
/** @internal */
|
||||
function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean): void {
|
||||
function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean, customLookupDefinitions?: Definitions): void {
|
||||
writeFile(dest, (): string => {
|
||||
const allTypes = {
|
||||
'@polkadot/types/augment': { lookup: lookupDefinitions },
|
||||
'@polkadot/types/augment': {
|
||||
lookup: {
|
||||
...lookupDefinitions,
|
||||
...customLookupDefinitions
|
||||
}
|
||||
},
|
||||
'@polkadot/types/interfaces': defaultDefs,
|
||||
...extraTypes
|
||||
};
|
||||
@@ -71,8 +77,8 @@ function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes,
|
||||
|
||||
// Call `generateForMeta()` with current static metadata
|
||||
/** @internal */
|
||||
export function generateDefaultEvents (dest = 'packages/api/src/augment/events.ts', data?: string, extraTypes: ExtraTypes = {}, isStrict = false): void {
|
||||
export function generateDefaultEvents (dest = 'packages/api/src/augment/events.ts', data?: string, extraTypes: ExtraTypes = {}, isStrict = false, customLookupDefinitions?: Definitions): void {
|
||||
const { metadata } = initMeta(data, extraTypes);
|
||||
|
||||
return generateForMeta(metadata, dest, extraTypes, isStrict);
|
||||
return generateForMeta(metadata, dest, extraTypes, isStrict, customLookupDefinitions);
|
||||
}
|
||||
|
||||
@@ -234,7 +234,7 @@ export function generateDefaultLookup (destDir = 'packages/types/src/augment/loo
|
||||
generateLookup(
|
||||
destDir,
|
||||
staticData
|
||||
? [[undefined, staticData]]
|
||||
? [['lookup', staticData]]
|
||||
: [
|
||||
['substrate', staticSubstrate],
|
||||
['polkadot', staticPolkadot],
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import type { StorageEntryMetadataLatest } from '@polkadot/types/interfaces';
|
||||
import type { Metadata, PortableRegistry } from '@polkadot/types/metadata';
|
||||
import type { Registry } from '@polkadot/types/types';
|
||||
import type { Definitions, Registry } from '@polkadot/types/types';
|
||||
import type { ExtraTypes } from './types';
|
||||
|
||||
import Handlebars from 'handlebars';
|
||||
@@ -66,10 +66,15 @@ const template = readTemplate('query');
|
||||
const generateForMetaTemplate = Handlebars.compile(template);
|
||||
|
||||
/** @internal */
|
||||
function generateForMeta (registry: Registry, meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean): void {
|
||||
function generateForMeta (registry: Registry, meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean, customLookupDefinitions?: Definitions): void {
|
||||
writeFile(dest, (): string => {
|
||||
const allTypes: ExtraTypes = {
|
||||
'@polkadot/types/augment': { lookup: lookupDefinitions },
|
||||
'@polkadot/types/augment': {
|
||||
lookup: {
|
||||
...lookupDefinitions,
|
||||
...customLookupDefinitions
|
||||
}
|
||||
},
|
||||
'@polkadot/types/interfaces': defaultDefs,
|
||||
...extraTypes
|
||||
};
|
||||
@@ -129,8 +134,8 @@ 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?: string, extraTypes: ExtraTypes = {}, isStrict = false): void {
|
||||
export function generateDefaultQuery (dest = 'packages/api/src/augment/query.ts', data?: string, extraTypes: ExtraTypes = {}, isStrict = false, customLookupDefinitions?: Definitions): void {
|
||||
const { metadata, registry } = initMeta(data, extraTypes);
|
||||
|
||||
return generateForMeta(registry, metadata, dest, extraTypes, isStrict);
|
||||
return generateForMeta(registry, metadata, dest, extraTypes, isStrict, customLookupDefinitions);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Metadata } from '@polkadot/types/metadata/Metadata';
|
||||
import type { Registry } from '@polkadot/types/types';
|
||||
import type { Definitions, Registry } from '@polkadot/types/types';
|
||||
import type { ExtraTypes } from './types';
|
||||
|
||||
import Handlebars from 'handlebars';
|
||||
@@ -29,10 +29,15 @@ const template = readTemplate('tx');
|
||||
const generateForMetaTemplate = Handlebars.compile(template);
|
||||
|
||||
/** @internal */
|
||||
function generateForMeta (registry: Registry, meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean): void {
|
||||
function generateForMeta (registry: Registry, meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean, customLookupDefinitions?: Definitions): void {
|
||||
writeFile(dest, (): string => {
|
||||
const allTypes: ExtraTypes = {
|
||||
'@polkadot/types/augment': { lookup: lookupDefinitions },
|
||||
'@polkadot/types/augment': {
|
||||
lookup: {
|
||||
...lookupDefinitions,
|
||||
...customLookupDefinitions
|
||||
}
|
||||
},
|
||||
'@polkadot/types/interfaces': defaultDefs,
|
||||
...extraTypes
|
||||
};
|
||||
@@ -114,8 +119,8 @@ 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?: string, extraTypes: ExtraTypes = {}, isStrict = false): void {
|
||||
export function generateDefaultTx (dest = 'packages/api/src/augment/tx.ts', data?: string, extraTypes: ExtraTypes = {}, isStrict = false, customLookupDefinitions?: Definitions): void {
|
||||
const { metadata, registry } = initMeta(data, extraTypes);
|
||||
|
||||
return generateForMeta(registry, metadata, dest, extraTypes, isStrict);
|
||||
return generateForMeta(registry, metadata, dest, extraTypes, isStrict, customLookupDefinitions);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
/* eslint-disable sort-keys */
|
||||
|
||||
import type { DefinitionsTypes } from '../../types';
|
||||
|
||||
export default {
|
||||
{{#each defs}}
|
||||
{{> docs}}
|
||||
@@ -11,5 +9,5 @@ export default {
|
||||
{{{this}}}
|
||||
{{/each}}
|
||||
{{/each}}
|
||||
} as DefinitionsTypes;
|
||||
};
|
||||
{{> footer }}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
/* eslint-disable sort-keys */
|
||||
|
||||
import type { Definitions } from '../../types';
|
||||
|
||||
export default {
|
||||
rpc: {},
|
||||
types: {
|
||||
@@ -14,5 +12,5 @@ export default {
|
||||
{{/each}}
|
||||
{{/each}}
|
||||
}
|
||||
} as Definitions;
|
||||
};
|
||||
{{> footer }}
|
||||
|
||||
Reference in New Issue
Block a user