Files
api/packages/api/src/util/decorate.ts
T
Amaury Martiny fadb1ffdd8 Make derive types work, and a bit more readable (#1430)
* Fix typings for derive

* Fix typings

* Spacing
2019-10-01 14:30:53 +02:00

62 lines
2.2 KiB
TypeScript

// Copyright 2017-2019 @polkadot/api 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 { AnyFunction } from '@polkadot/types/types';
import { DecorateMethod, MethodResult } from '../types';
// Most generic typings for `api.derive.*.*`
type AnyDerive = Record<string, Record<string, AnyFunction>>;
// Exact typings for a particular section `api.derive.section.*`
type DeriveSection<ApiType, Section extends Record<string, AnyFunction>> = { [MethodName in keyof Section]: MethodResult<ApiType, Section[MethodName]> };
// Exact typings for all sections `api.derive.*.*`
export type DeriveAllSections<ApiType, AllSections extends AnyDerive> = { [SectionName in keyof AllSections]: DeriveSection<ApiType, AllSections[SectionName]> };
// A technically unsafe version of Object.keys(obj) that assumes that
// obj only has known properties of T
function keys<T extends object> (obj: T): (keyof T)[] {
return Object.keys(obj) as (keyof T)[];
}
/**
* This is a methods decorator which keeps all type information.
*/
function decorateMethods<ApiType, Section extends Record<string, AnyFunction>> (
section: Section,
decorateMethod: DecorateMethod<ApiType>
): DeriveSection<ApiType, Section> {
return keys(section).reduce(
<MethodName extends keyof Section>(
acc: DeriveSection<ApiType, Section>,
methodName: MethodName
): DeriveSection<ApiType, Section> => {
const method = section[methodName];
acc[methodName] = decorateMethod(method);
return acc;
},
{} as DeriveSection<ApiType, Section>
);
}
/**
* This is a section decorator which keeps all type information.
*/
export function decorateSections<ApiType, AllSections extends AnyDerive> (
allSections: AllSections,
decorateMethod: DecorateMethod<ApiType>
): DeriveAllSections<ApiType, AllSections> {
return keys(allSections).reduce(
<MethodName extends keyof AllSections>(
acc: DeriveAllSections<ApiType, AllSections>,
sectionName: MethodName
): DeriveAllSections<ApiType, AllSections> => {
acc[sectionName] = decorateMethods(allSections[sectionName], decorateMethod);
return acc;
},
{} as DeriveAllSections<ApiType, AllSections>
);
}