-
Notifications
You must be signed in to change notification settings - Fork 45
/
destination-selection-strategies.ts
88 lines (80 loc) · 2.82 KB
/
destination-selection-strategies.ts
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { createLogger } from '@sap-cloud-sdk/util';
import {
AllDestinations,
DestinationsByType
} from './destination-accessor-types';
import { Destination } from './destination-service-types';
const logger = createLogger({
package: 'core',
messageContext: 'destination-selection-strategies'
});
export type DestinationSelectionStrategy = (
allDestinations: AllDestinations,
destinationName: string
) => Destination | null;
/**
* Constraints the selection to provider destinations.
*
* @param allDestinations - Retrieved destinations
* @param destinationName - Name of the destination to retrieve
* @returns the destination to retrieve, returns null if no matched provider destination is found
*/
export function alwaysProvider(
allDestinations: AllDestinations,
destinationName: string
): Destination | null {
return findDestination(allDestinations.provider, destinationName) || null;
}
/**
* Constraints the selection to subscriber destinations.
*
* @param allDestinations - Retrieved destinations
* @param destinationName - Name of the destination to retrieve
* @returns the destination to retrieve, returns null if no matched subscriber destination is found
*/
export function alwaysSubscriber(
allDestinations: AllDestinations,
destinationName: string
): Destination | null {
return findDestination(allDestinations.subscriber, destinationName) || null;
}
/**
* Prioritizes the selection of subscriber destinations.
*
* @param allDestinations - Retrieved destinations
* @param destinationName - Name of the destination to retrieve
* @returns the destination to retrieve, returns null if no matched destination is found
*/
export function subscriberFirst(
allDestinations: AllDestinations,
destinationName: string
): Destination | null {
return (
findDestination(allDestinations.subscriber, destinationName) ||
findDestination(allDestinations.provider, destinationName) ||
null
);
}
/**
* Selector of destination selection strategies. See [[alwaysProvider]], [[alwaysSubscriber]] and [[subscriberFirst]] for more information available selection strategies.
*/
export const DestinationSelectionStrategies = {
alwaysProvider,
alwaysSubscriber,
subscriberFirst
};
function findDestination(
destinations: DestinationsByType,
destinationName: string
): Destination | undefined {
const isRequestedDestination = (destination: Destination) =>
destination.name === destinationName;
const instanceDest = destinations.instance.find(isRequestedDestination);
const subAccountDest = destinations.subaccount.find(isRequestedDestination);
if (instanceDest && subAccountDest) {
logger.warn(
`A destination with name ${destinationName} has been found for the destination serivce instance and subaccount. The instance destination will be used.`
);
}
return instanceDest || subAccountDest;
}