forked from microsoft/BotFramework-WebChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchCapabilitiesFromAdapter.ts
More file actions
59 lines (50 loc) · 1.84 KB
/
Copy pathfetchCapabilitiesFromAdapter.ts
File metadata and controls
59 lines (50 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { DirectLineJSBotConnection, isForbiddenPropertyName } from 'botframework-webchat-core';
import type { Capabilities } from '../types/Capabilities';
import CAPABILITY_REGISTRY from './capabilityRegistry';
import shallowEqual from './shallowEqual';
type FetchResult = {
capabilities: Capabilities;
hasChanged: boolean;
};
/**
* Fetches all capabilities from the adapter based on the registry.
* Returns a new capabilities object with values fetched from the adapter.
*/
export default function fetchCapabilitiesFromAdapter(
directLine: DirectLineJSBotConnection,
prevCapabilities: Capabilities
): FetchResult {
let hasChanged = false;
const entries: [string, unknown][] = [];
for (const descriptor of CAPABILITY_REGISTRY) {
const { key, getterName, isEqual = shallowEqual } = descriptor;
if (isForbiddenPropertyName(key) || isForbiddenPropertyName(getterName)) {
continue;
}
// eslint-disable-next-line security/detect-object-injection
const getter = directLine?.[getterName];
if (typeof getter === 'function') {
try {
const fetchedValue = getter.call(directLine);
// eslint-disable-next-line security/detect-object-injection
const prevValue = prevCapabilities[key];
if (fetchedValue) {
if (typeof prevValue !== 'undefined' && isEqual(prevValue, fetchedValue)) {
entries.push([key, prevValue]);
} else {
entries.push([key, Object.freeze({ ...fetchedValue })]);
hasChanged = true;
}
} else if (typeof prevValue !== 'undefined') {
hasChanged = true;
}
} catch (error) {
console.warn(`botframework-webchat: Error calling capability ${getterName}:`, error);
}
}
}
return {
capabilities: Object.freeze(Object.fromEntries(entries)) as Capabilities,
hasChanged
};
}