diff --git a/packages/api/src/util/augmentObject.ts b/packages/api/src/util/augmentObject.ts index 71f332e2fb..173c2190cf 100644 --- a/packages/api/src/util/augmentObject.ts +++ b/packages/api/src/util/augmentObject.ts @@ -4,6 +4,10 @@ import { lazyMethod } from '@polkadot/types'; import { logger } from '@polkadot/util'; +type Sections = Record>; + +type Methods = Record; + type StringsStrings = [string[], string[]]; const l = logger('api/augment'); @@ -43,7 +47,7 @@ function findSectionIncludes (a: string[], b: string[]): string[] { return a.filter((s) => b.includes(s)); } -function extractSections (src: Record>, dst: Record>): StringsStrings { +function extractSections (src: Sections, dst: Sections): StringsStrings { const srcSections = Object.keys(src); const dstSections = Object.keys(dst); @@ -53,7 +57,7 @@ function extractSections (src: Record>, dst: Rec ]; } -function findMethodExcludes (src: Record>, dst: Record>): string[] { +function findMethodExcludes (src: Sections, dst: Sections): string[] { const srcSections = Object.keys(src); const dstSections = findSectionIncludes(Object.keys(dst), srcSections); const excludes: string[] = []; @@ -73,24 +77,16 @@ function findMethodExcludes (src: Record>, dst: return excludes; } -function extractMethods (src: Record>, dst: Record>): StringsStrings { +function extractMethods (src: Sections, dst: Sections): StringsStrings { return [ findMethodExcludes(dst, src), findMethodExcludes(src, dst) ]; } -function lazySection (section: string, src: Record>, dst: Record>): void { - // NOTE: Unlike the other lazy decorations, here we actually do decorate the - // sections, just the internal form is not available (i.e. we skip lazyMethods) - // The initial issue is that we have a confict between "this already exists" and - // "this needs to start clean", so for a first round we skip... - if (!dst[section]) { - dst[section] = {}; - } - - const creator = (method: string) => src[section][method]; - const methods = Object.keys(src[section]); +function lazySection (src: Methods, dst: Methods): void { + const creator = (m: string) => src[m]; + const methods = Object.keys(src); for (let i = 0; i < methods.length; i++) { const method = methods[i]; @@ -98,8 +94,8 @@ function lazySection (section: string, src: Record>, dst: Record>, fromEmpty = false): Record> { +export function augmentObject (prefix: string | null, src: Sections, dst: Sections, fromEmpty = false): Sections { fromEmpty && clearObject(dst); // NOTE: This part is slightly problematic since it will get the @@ -120,10 +116,18 @@ export function augmentObject (prefix: string | null, src: Record