Trate user-RPCs consistently alongside known (#4081)
This commit is contained in:
parent
add63b707d
commit
c70f82807e
@ -4,7 +4,7 @@
|
||||
import type { DefinitionRpc, DefinitionRpcParam } from '@polkadot/types/types';
|
||||
|
||||
export default function (): Record<string, Record<string, DefinitionRpc>> {
|
||||
function generateDescription (description:string, returnType:string, params:DefinitionRpcParam[] = []):DefinitionRpc {
|
||||
function generateDescription (description:string, returnType:string, params:DefinitionRpcParam[] = []): DefinitionRpc {
|
||||
return {
|
||||
description,
|
||||
params,
|
||||
|
||||
@ -4,11 +4,10 @@
|
||||
import type { DefinitionRpcExt } from '@polkadot/types/types';
|
||||
import type { DropdownOptions } from '../util/types';
|
||||
|
||||
import React from 'react';
|
||||
|
||||
import jsonrpc from '@polkadot/types/interfaces/jsonrpc';
|
||||
import React, { useCallback } from 'react';
|
||||
|
||||
import Dropdown from '../Dropdown';
|
||||
import rpcs from './rpcs';
|
||||
import { classes } from '../util';
|
||||
|
||||
interface Props {
|
||||
@ -19,14 +18,11 @@ interface Props {
|
||||
value: DefinitionRpcExt;
|
||||
}
|
||||
|
||||
function transform ({ value: { section } }: Props): (method: string) => DefinitionRpcExt {
|
||||
return function (method: string): DefinitionRpcExt {
|
||||
return jsonrpc[section][method];
|
||||
};
|
||||
}
|
||||
|
||||
function SelectMethod (props: Props): React.ReactElement<Props> | null {
|
||||
const { className = '', isError, onChange, options, value } = props;
|
||||
function SelectMethod ({ className = '', isError, onChange, options, value }: Props): React.ReactElement<Props> | null {
|
||||
const _transform = useCallback(
|
||||
() => rpcs[value.section][value.method],
|
||||
[value]
|
||||
);
|
||||
|
||||
if (!options.length) {
|
||||
return null;
|
||||
@ -38,7 +34,7 @@ function SelectMethod (props: Props): React.ReactElement<Props> | null {
|
||||
isError={isError}
|
||||
onChange={onChange}
|
||||
options={options}
|
||||
transform={transform(props)}
|
||||
transform={_transform}
|
||||
value={value.method}
|
||||
withLabel={false}
|
||||
/>
|
||||
|
||||
@ -8,13 +8,13 @@ import type { DropdownOptions } from '../util/types';
|
||||
|
||||
import React, { useCallback, useEffect, useState } from 'react';
|
||||
import { useApi } from '@polkadot/react-hooks';
|
||||
import jsonrpc from '@polkadot/types/interfaces/jsonrpc';
|
||||
|
||||
import LinkedWrapper from '../InputExtrinsic/LinkedWrapper';
|
||||
import SelectMethod from './SelectMethod';
|
||||
import SelectSection from './SelectSection';
|
||||
import methodOptions from './options/method';
|
||||
import sectionOptions from './options/section';
|
||||
import rpcs from './rpcs';
|
||||
|
||||
interface Props {
|
||||
className?: string;
|
||||
@ -43,7 +43,7 @@ function InputRpc ({ className = '', defaultValue, help, label, onChange, withLa
|
||||
}
|
||||
|
||||
// set via callback since the method is a function itself
|
||||
setValue((): DefinitionRpcExt => newValue);
|
||||
setValue(() => newValue);
|
||||
},
|
||||
[value]
|
||||
);
|
||||
@ -57,7 +57,7 @@ function InputRpc ({ className = '', defaultValue, help, label, onChange, withLa
|
||||
const optionsMethod = methodOptions(api, section);
|
||||
|
||||
setOptionsMethod(optionsMethod);
|
||||
_onMethodChange(jsonrpc[section][optionsMethod[0].value]);
|
||||
_onMethodChange(rpcs[section][optionsMethod[0].value]);
|
||||
},
|
||||
[_onMethodChange, api, value]
|
||||
);
|
||||
|
||||
@ -6,10 +6,11 @@ import type { DropdownOption, DropdownOptions } from '../../util/types';
|
||||
|
||||
import React from 'react';
|
||||
import { ApiPromise } from '@polkadot/api';
|
||||
import jsonrpc from '@polkadot/types/interfaces/jsonrpc';
|
||||
|
||||
import rpcs from '../rpcs';
|
||||
|
||||
export default function createOptions (api: ApiPromise, sectionName: string): DropdownOptions {
|
||||
const section = jsonrpc[sectionName];
|
||||
const section = rpcs[sectionName];
|
||||
|
||||
if (!section || Object.keys((api.rpc as Record<string, Record<string, unknown>>)[sectionName]).length === 0) {
|
||||
return [];
|
||||
@ -20,9 +21,9 @@ export default function createOptions (api: ApiPromise, sectionName: string): Dr
|
||||
.sort()
|
||||
.map((methodName) => section[methodName])
|
||||
.filter((ext): ext is DefinitionRpcExt => !!ext)
|
||||
.filter(({ isSubscription }): boolean => !isSubscription)
|
||||
.filter(({ isSubscription }) => !isSubscription)
|
||||
.map(({ description, method, params }): DropdownOption => {
|
||||
const inputs = params.map(({ name }): string => name).join(', ');
|
||||
const inputs = params.map(({ name }) => name).join(', ');
|
||||
|
||||
return {
|
||||
className: 'ui--DropdownLinked-Item',
|
||||
|
||||
@ -9,7 +9,7 @@ export default function createOptions (api: ApiPromise): DropdownOptions {
|
||||
return Object
|
||||
.keys(api.rpc)
|
||||
.sort()
|
||||
.filter((section): boolean => Object.keys((api.rpc as Record<string, Record<string, unknown>>)[section]).length !== 0)
|
||||
.filter((section) => Object.keys((api.rpc as Record<string, Record<string, unknown>>)[section]).length !== 0)
|
||||
.map((name): { text: string; value: string } => ({
|
||||
text: name,
|
||||
value: name
|
||||
|
||||
29
packages/react-components/src/InputRpc/rpcs.ts
Normal file
29
packages/react-components/src/InputRpc/rpcs.ts
Normal file
@ -0,0 +1,29 @@
|
||||
// Copyright 2017-2020 @polkadot/react-components authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { DefinitionRpc, DefinitionRpcExt } from '@polkadot/types/types';
|
||||
|
||||
import { typesRpc } from '@polkadot/apps-config';
|
||||
import jsonrpc from '@polkadot/types/interfaces/jsonrpc';
|
||||
|
||||
function toExt (section: string, input: Record<string, DefinitionRpc>): Record<string, DefinitionRpcExt> {
|
||||
return Object.entries(input).reduce((output: Record<string, DefinitionRpcExt>, [method, def]): Record<string, DefinitionRpcExt> => {
|
||||
output[method] = {
|
||||
isSubscription: false,
|
||||
jsonrpc: `${method}_${section}`,
|
||||
method,
|
||||
section,
|
||||
...def
|
||||
};
|
||||
|
||||
return output;
|
||||
}, {});
|
||||
}
|
||||
|
||||
const all = Object.entries(typesRpc).reduce((all: Record<string, Record<string, DefinitionRpcExt>>, [section, contents]): Record<string, Record<string, DefinitionRpcExt>> => {
|
||||
all[section] ??= toExt(section, contents);
|
||||
|
||||
return all;
|
||||
}, { ...jsonrpc });
|
||||
|
||||
export default all;
|
||||
Loading…
Reference in New Issue
Block a user