Build metadata with rpc aliasses (#3013)
* Build metadata with rpc aliasses * links
This commit is contained in:
@@ -16,17 +16,21 @@ import { Text } from '@polkadot/types/primitive';
|
||||
import { unwrapStorageType } from '@polkadot/types/primitive/StorageKey';
|
||||
import { stringCamelCase, stringLowerFirst } from '@polkadot/util';
|
||||
|
||||
interface Section {
|
||||
link?: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
items: {
|
||||
link?: string;
|
||||
name: string;
|
||||
[bullet: string]: undefined | string | Vec<Text>;
|
||||
}[];
|
||||
}
|
||||
|
||||
interface Page {
|
||||
title: string;
|
||||
description: string;
|
||||
sections: {
|
||||
name: string;
|
||||
description?: string;
|
||||
items: {
|
||||
name: string;
|
||||
[bullet: string]: string | Vec<Text>;
|
||||
}[];
|
||||
}[];
|
||||
sections: Section[];
|
||||
}
|
||||
|
||||
const STATIC_TEXT = '\n\n(NOTE: These were generated from a static/snapshot view of a recent Substrate master node. Some items may not be available in older nodes, or in any customized implementations.)';
|
||||
@@ -71,16 +75,22 @@ function renderPage (page: Page): string {
|
||||
|
||||
// contents
|
||||
page.sections.forEach((section) => {
|
||||
md += `\n___\n\n\n## ${section.name}\n`;
|
||||
md += '\n___\n\n\n';
|
||||
md += section.link
|
||||
? `<h2 id="#${section.link}">${section.name}</h2>\n`
|
||||
: `## ${section.name}\n`;
|
||||
|
||||
if (section.description) {
|
||||
md += `\n_${section.description}_\n`;
|
||||
}
|
||||
|
||||
section.items.forEach((item) => {
|
||||
md += ` \n### ${item.name}`;
|
||||
md += ' \n';
|
||||
md += item.link
|
||||
? `<h3 id="#${item.link}">${item.name}</h3>`
|
||||
: `### ${item.name}`;
|
||||
|
||||
Object.keys(item).filter((i) => i !== 'name').forEach((bullet) => {
|
||||
Object.keys(item).filter((key) => !['link', 'name'].includes(key)).forEach((bullet) => {
|
||||
md += `\n- **${bullet}**: ${
|
||||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
||||
item[bullet] instanceof Vec
|
||||
@@ -112,32 +122,41 @@ function addRpc (): string {
|
||||
description: DESC_RPC,
|
||||
sections: sections
|
||||
.sort()
|
||||
.map((_sectionName) => {
|
||||
.reduce((all: Section[], _sectionName): Section[] => {
|
||||
const section = definitions[_sectionName as 'babe'];
|
||||
|
||||
return {
|
||||
// description: section.description,
|
||||
items: Object.keys(section.rpc)
|
||||
.sort()
|
||||
.map((methodName) => {
|
||||
const method = section.rpc[methodName];
|
||||
const sectionName = method.aliasSection || _sectionName;
|
||||
const args = method.params.map(({ isOptional, name, type }: DefinitionRpcParam): string => {
|
||||
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
||||
return name + (isOptional ? '?' : '') + ': `' + type + '`';
|
||||
}).join(', ');
|
||||
const type = '`' + method.type + '`';
|
||||
Object.keys(section.rpc)
|
||||
.sort()
|
||||
.forEach((methodName) => {
|
||||
const method = section.rpc[methodName];
|
||||
const sectionName = method.aliasSection || _sectionName;
|
||||
const topName = method.aliasSection ? `${_sectionName}/${method.aliasSection}` : _sectionName;
|
||||
let container = all.find(({ name }) => name === topName);
|
||||
|
||||
return {
|
||||
interface: '`' + `api.rpc.${sectionName}.${methodName}` + '`',
|
||||
jsonrpc: '`' + (method.endpoint || `${sectionName}_${methodName}`) + '`',
|
||||
name: `${methodName}(${args}): ${type}`,
|
||||
...(method.description && { summary: method.description })
|
||||
};
|
||||
}),
|
||||
name: _sectionName
|
||||
};
|
||||
}),
|
||||
if (!container) {
|
||||
container = { items: [], name: topName };
|
||||
|
||||
all.push(container);
|
||||
}
|
||||
|
||||
const args = method.params.map(({ isOptional, name, type }: DefinitionRpcParam): string => {
|
||||
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
||||
return name + (isOptional ? '?' : '') + ': `' + type + '`';
|
||||
}).join(', ');
|
||||
const type = '`' + method.type + '`';
|
||||
const jsonrpc = (method.endpoint || `${sectionName}_${methodName}`);
|
||||
|
||||
container.items.push({
|
||||
interface: '`' + `api.rpc.${sectionName}.${methodName}` + '`',
|
||||
jsonrpc: '`' + jsonrpc + '`',
|
||||
// link: jsonrpc,
|
||||
name: `${methodName}(${args}): ${type}`,
|
||||
...(method.description && { summary: method.description })
|
||||
});
|
||||
});
|
||||
|
||||
return all;
|
||||
}, []).sort((a, b) => a.name.localeCompare(b.name)),
|
||||
title: 'JSON-RPC'
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user