UNPKG

3.25 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.augmentObject = void 0;
4const util_1 = require("@polkadot/util");
5const l = (0, util_1.logger)('api/augment');
6function logLength(type, values, and = []) {
7 return values.length
8 ? ` ${values.length} ${type}${and.length ? ' and' : ''}`
9 : '';
10}
11function logValues(type, values) {
12 return values.length
13 ? `\n\t${type.padStart(7)}: ${values.sort().join(', ')}`
14 : '';
15}
16function warn(prefix, type, [added, removed]) {
17 if (added.length || removed.length) {
18 l.warn(`api.${prefix}: Found${logLength('added', added, removed)}${logLength('removed', removed)} ${type}:${logValues('added', added)}${logValues('removed', removed)}`);
19 }
20}
21function findSectionExcludes(a, b) {
22 return a.filter((s) => !b.includes(s));
23}
24function findSectionIncludes(a, b) {
25 return a.filter((s) => b.includes(s));
26}
27function extractSections(src, dst) {
28 const srcSections = Object.keys(src);
29 const dstSections = Object.keys(dst);
30 return [
31 findSectionExcludes(srcSections, dstSections),
32 findSectionExcludes(dstSections, srcSections)
33 ];
34}
35function findMethodExcludes(src, dst) {
36 const srcSections = Object.keys(src);
37 const dstSections = findSectionIncludes(Object.keys(dst), srcSections);
38 const excludes = [];
39 for (let s = 0, scount = dstSections.length; s < scount; s++) {
40 const section = dstSections[s];
41 const srcMethods = Object.keys(src[section]);
42 const dstMethods = Object.keys(dst[section]);
43 for (let d = 0, mcount = dstMethods.length; d < mcount; d++) {
44 const method = dstMethods[d];
45 if (!srcMethods.includes(method)) {
46 excludes.push(`${section}.${method}`);
47 }
48 }
49 }
50 return excludes;
51}
52function extractMethods(src, dst) {
53 return [
54 findMethodExcludes(dst, src),
55 findMethodExcludes(src, dst)
56 ];
57}
58/**
59 * @description Takes a decorated api section (e.g. api.tx) and augment it with the details. It does not override what is
60 * already available, but rather just adds new missing items into the result object.
61 * @internal
62 */
63function augmentObject(prefix, src, dst, fromEmpty = false) {
64 fromEmpty && (0, util_1.objectClear)(dst);
65 // NOTE: This part is slightly problematic since it will get the
66 // values for at least all the sections and the names of the methods
67 // (Since methods won't be decorated before lazy, this _may_ be ok)
68 if (prefix && Object.keys(dst).length) {
69 warn(prefix, 'modules', extractSections(src, dst));
70 warn(prefix, 'calls', extractMethods(src, dst));
71 }
72 const sections = Object.keys(src);
73 for (let i = 0, count = sections.length; i < count; i++) {
74 const section = sections[i];
75 const methods = src[section];
76 // We don't set here with a lazy interface, we decorate based
77 // on the top-level structure (this bypasses adding lazy onto lazy)
78 if (!dst[section]) {
79 dst[section] = {};
80 }
81 (0, util_1.lazyMethods)(dst[section], Object.keys(methods), (m) => methods[m]);
82 }
83 return dst;
84}
85exports.augmentObject = augmentObject;