Move api-derive to correct location (#4343)

* Move api-derive to correct location

* Adjust (linting ok)

* No composite

* ApiDerive split

* Adjust

* Still WIP, getting closer

* It lints

* derive build issues sorted

* It builds :)

* Add system_health to mock

* api-augment usage

* Cleanups

* Adjust paths

* linting

* Some <'s

* Revert <>'s

* linting

* ordering

* Augmentation tweaks

* yarn.lock

* export
This commit is contained in:
Jaco
2021-12-18 09:18:55 +02:00
committed by GitHub
parent 7b8a6e89d9
commit 4913cf65fd
178 changed files with 720 additions and 685 deletions
+126 -2
View File
@@ -1,6 +1,130 @@
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
export * from '@polkadot/api/derive';
import type { AnyFunction } from '@polkadot/types/types';
import type { DeriveCustom, ExactDerive } from './derive';
import type { DeriveApi } from './types';
export { packageInfo } from './packageInfo';
import { lazyDeriveSection } from './util/lazy';
import { derive } from './derive';
export * from './derive';
export * from './type';
interface Avail {
instances: string[];
methods: string[];
withDetect?: boolean;
}
export { lazyDeriveSection };
// Enable derive only if some of these modules are available
const checks: Record<string, Avail> = {
contracts: {
instances: ['contracts'],
methods: []
},
council: {
instances: ['council'],
methods: [],
withDetect: true
},
crowdloan: {
instances: ['crowdloan'],
methods: []
},
democracy: {
instances: ['democracy'],
methods: []
},
elections: {
instances: ['phragmenElection', 'electionsPhragmen', 'elections', 'council'],
methods: [],
withDetect: true
},
imOnline: {
instances: ['imOnline'],
methods: []
},
membership: {
instances: ['membership'],
methods: []
},
parachains: {
instances: ['parachains', 'registrar'],
methods: []
},
session: {
instances: ['session'],
methods: []
},
society: {
instances: ['society'],
methods: []
},
staking: {
instances: ['staking'],
methods: ['erasRewardPoints']
},
technicalCommittee: {
instances: ['technicalCommittee'],
methods: [],
withDetect: true
},
treasury: {
instances: ['treasury'],
methods: []
}
};
function getModuleInstances (api: DeriveApi, specName: string, moduleName: string): string[] {
return api.registry.getModuleInstances(specName, moduleName) || [];
}
/**
* Returns an object that will inject `api` into all the functions inside
* `allSections`, and keep the object architecture of `allSections`.
*/
/** @internal */
function injectFunctions (instanceId: string, api: DeriveApi, derives: DeriveCustom): ExactDerive {
const result: Record<string, Record<string, AnyFunction>> = {};
const names = Object.keys(derives);
const keys = Object.keys(api.query);
const specName = api.runtimeVersion.specName.toString();
const filterKeys = (q: string) => keys.includes(q);
const filterInstances = (q: string) => getModuleInstances(api, specName, q).some(filterKeys);
const filterMethods = (all: string[]) => (m: string) => all.some((q) => keys.includes(q) && api.query[q][m]);
const getKeys = (s: string) => Object.keys(derives[s]);
const creator = (s: string, m: string) => derives[s][m](instanceId, api);
const isIncluded = (c: string) => (!checks[c] || (
(checks[c].instances.some(filterKeys) && (
!checks[c].methods.length ||
checks[c].methods.every(filterMethods(checks[c].instances))
)) ||
(
checks[c].withDetect &&
checks[c].instances.some(filterInstances)
)
));
for (let i = 0; i < names.length; i++) {
const name = names[i];
isIncluded(name) &&
lazyDeriveSection(result, name, getKeys, creator);
}
return result as ExactDerive;
}
// FIXME The return type of this function should be {...ExactDerive, ...DeriveCustom}
// For now we just drop the custom derive typings
/** @internal */
export function getAvailableDerives (instanceId: string, api: DeriveApi, custom: DeriveCustom = {}): ExactDerive {
return {
...injectFunctions(instanceId, api, derive as DeriveCustom),
...injectFunctions(instanceId, api, custom)
};
}