-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathdestination-from-env.ts
165 lines (152 loc) · 5.08 KB
/
destination-from-env.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { createLogger, ErrorWithCause } from '@sap-cloud-sdk/util';
import {
proxyStrategy,
ProxyStrategy,
addProxyConfigurationInternet
} from '../proxy-util';
import {
sanitizeDestination,
isDestinationConfiguration,
parseDestination
} from './destination';
import { Destination } from './destination-service-types';
const logger = createLogger({
package: 'core',
messageContext: 'env-destination-accessor'
});
/**
* Get all destinations from the environment variable "destinations".
* This is discouraged for productive use! Use [[useOrFetchDestination]] for fetching destinations from the Cloud Foundry destination service.
*
* @returns A list of destinations
*/
export function getDestinationsFromEnv(): Destination[] {
const destinationsEnv = getDestinationsEnvVariable();
logger.debug(
`The value for the destination environment variable is: ${destinationsEnv}`
);
if (destinationsEnv) {
let destinations;
try {
destinations = JSON.parse(destinationsEnv);
} catch (err) {
throw new ErrorWithCause(
'Error in parsing the destinations from the environment variable.',
err
);
}
validateDestinations(destinations);
return destinations.map(destination =>
isDestinationConfiguration(destination)
? parseDestination(destination)
: sanitizeDestination(destination)
);
}
return [];
}
/**
* @deprecated Since v1.4.2. Use [[getDestinationsFromEnv]] instead.
*
* Get all destinations from the environment variable "destinations".
* This is discouraged for productive use! Use destination-accessor/useOrFetchDestination for fetching destinations
* from the Cloud Foundry destination service.
*
* @returns A list of destinations
*/
export function getDestinations(): Destination[] {
return getDestinationsFromEnv();
}
/**
* Get a destination from the environment variables by name. If there are multiple destinations with the same name the first one will be used.
* This is discouraged for productive use! Use destination-accessor/useOrFetchDestination for fetching destinations
* from the Cloud Foundry destination service.
*
* @param name - Name of the destination
* @returns The requested destination if existent, otherwise `null`
*/
export function getDestinationFromEnvByName(name: string): Destination | null {
const matchingDestinations = getDestinationsFromEnv().filter(
dest => dest.name === name
);
if (!matchingDestinations.length) {
return null;
}
if (matchingDestinations.length > 1) {
logger.warn(
`The 'destinations' env variable contains multiple destinations with the name '${name}'. Only the first entry will be respected.`
);
}
const destination = matchingDestinations[0];
return proxyStrategy(destination) === ProxyStrategy.INTERNET_PROXY
? addProxyConfigurationInternet(destination)
: destination;
}
/**
* @deprecated Since v1.4.2. Use [[getDestinationFromEnvByName]] instead.
*
* Get a destination from the environment variables by name. Throws an error if there are multiple destinations with the same name.
* This is discouraged for productive use! Use destination-accessor/useOrFetchDestination for fetching destinations
* from the Cloud Foundry destination service.
*
* @param name - Name of the destination
* @returns The requested destination if existent, otherwise `null`
*/
export function getDestinationByName(name: string): Destination | null {
return getDestinationFromEnvByName(name);
}
/* eslint-disable valid-jsdoc */
/**
* @hidden
*/
export function getDestinationConfig(
dest: string | Destination = 'ErpQueryEndpoint'
): Destination | null {
return typeof dest === 'string' ? getDestinationFromEnvByName(dest) : dest;
}
/**
* @hidden
*/
export function getDestinationsEnvVariable(): string | undefined {
return process.env['destinations'];
}
function validateDestinations(destinations: any[]) {
destinations.forEach(destination => {
if (
typeof destination.name === 'undefined' &&
typeof destination.Name === 'undefined'
) {
logger.warn(
"Destination from 'destinations' env variable is missing 'name' or 'Name' property."
);
}
});
}
/**
* @hidden
*/
export function searchEnvVariablesForDestination(
name: string
): Destination | undefined {
logger.info('Attempting to retrieve destination from environment variable.');
if (getDestinationsEnvVariable()) {
logger.warn(
"Environment variable 'destinations' is set. Destinations will be read from this variable. " +
'This is discouraged for a productive application! ' +
'Unset the variable to read destinations from the destination service on SAP Cloud Platform.'
);
try {
const destination = getDestinationFromEnvByName(name);
if (destination) {
logger.info(
'Successfully retrieved destination from environment variable.'
);
return destination;
}
} catch (error) {
logger.error(
`Error in reading the given destinations from the environment variable ${error.message}.`
);
}
}
logger.info('No environment variable set.');
}