From 2a5c5ce3fe65852db8752ff772a708864db4eb2d Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 28 Jan 2020 11:07:02 +0100 Subject: [PATCH] Only include derives with parent query (#1755) * Only include derives with parent query * Update CHANGELOG --- CHANGELOG.md | 1 + packages/api-derive/src/index.ts | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31c60d8099..bbbba07993 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 1.0.0-beta-x - **Breaking change** Drop support for Substrate v1 chain in all derives +- Only decorate derives where relevant parent `api.query.*` is available - Support `.entries(arg?: any)` lookups on DoubleMaps (in addition to previously supported maps) - Remove un-deployed support for v11 metadata (this was decided against on Substrate) - Allow v9 metadata to parse even in cases where it was wrongly deployed pre-v10 diff --git a/packages/api-derive/src/index.ts b/packages/api-derive/src/index.ts index 55c8fa10cd..9347e47787 100644 --- a/packages/api-derive/src/index.ts +++ b/packages/api-derive/src/index.ts @@ -23,6 +23,8 @@ import * as treasury from './treasury'; export * from './type'; +export const derive = { accounts, balances, chain, contracts, council, democracy, elections, imOnline, session, society, staking, technicalCommittee, treasury }; + type DeriveSection
= { [Method in keyof Section]: Section[Method] extends AnyFunction ? ReturnType // ReturnType will be the inner function, i.e. without (api) argument @@ -34,14 +36,35 @@ type DeriveAllSections = { export type DeriveCustom = Record (...args: any[]) => Observable>>; +export type ExactDerive = DeriveAllSections; + +// Enable derive only if some of these modules are available +const deriveAvail: Record = { + contracts: ['contracts'], + council: ['council'], + democracy: ['democracy'], + elections: ['electionsPhragmen', 'elections'], + imOnline: ['imOnline'], + session: ['session'], + society: ['society'], + staking: ['staking'], + technicalCommittee: ['technicalCommittee'], + treasury: ['treasury'] +}; + /** * Returns an object that will inject `api` into all the functions inside * `allSections`, and keep the object architecture of `allSections`. */ /** @internal */ function injectFunctions (api: ApiInterfaceRx, allSections: AllSections): DeriveAllSections { + const queryKeys = Object.keys(api.query); + return Object .keys(allSections) + .filter((sectionName): boolean => + !deriveAvail[sectionName] || deriveAvail[sectionName].some((query): boolean => queryKeys.includes(query)) + ) .reduce((deriveAcc, sectionName): DeriveAllSections => { const section = allSections[sectionName as keyof AllSections]; @@ -61,9 +84,6 @@ function injectFunctions (api: ApiInterfaceRx, allSections: AllSect }, {} as DeriveAllSections); } -export const derive = { accounts, balances, chain, contracts, council, democracy, elections, imOnline, session, society, staking, technicalCommittee, treasury }; -export type ExactDerive = DeriveAllSections; - // FIXME The return type of this function should be {...ExactDerive, ...DeriveCustom} // For now we just drop the custom derive typings /** @internal */