// Copyright 2017-2019 @polkadot/api-derive authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. import { Observable } from 'rxjs'; import { ApiInterface$Rx } from '@polkadot/api/types'; import * as accounts from './accounts'; import * as balances from './balances'; import * as chain from './chain'; import * as contract from './contract'; import * as democracy from './democracy'; import * as session from './session'; import * as staking from './staking'; // Put all derived functions in an object, for easier Object.keys()-ing. const functions = { accounts, balances, chain, democracy, session, staking }; /** * T represents the section here (chain, balances...), and P represents * the function name (bestNumber, sessionProgress...). */ type ReturnTypes any>> = { [P in keyof T]: ReturnType }; export interface DeriveCustomMethod { (api: ApiInterface$Rx): (...args: Array) => Observable; } export interface DeriveCustom { [index: string]: { [index: string]: DeriveCustomMethod }; } export interface Derive { accounts: ReturnTypes; balances: ReturnTypes; chain: ReturnTypes; contract: ReturnTypes; democracy: ReturnTypes; session: ReturnTypes; staking: ReturnTypes; } function injectFunctions (api: ApiInterface$Rx, derive: Derive, functions: DeriveCustom): Derive { Object.keys(functions).forEach((sectionName: string) => { const section = functions[sectionName as keyof Derive]; const result = derive[sectionName as keyof Derive] = derive[sectionName as keyof Derive] || {}; Object.keys(section).forEach((methodName) => { // @ts-ignore No idea how to make this work... result[methodName as keyof section] = section[methodName as keyof section](api); }); }); return derive; } export default function decorateDerive (api: ApiInterface$Rx, custom: DeriveCustom = {}): Derive { const derive = {} as Derive; injectFunctions(api, derive, functions); injectFunctions(api, derive, custom); return derive; }