1 | "use strict";
|
2 | var __assign = (this && this.__assign) || function () {
|
3 | __assign = Object.assign || function(t) {
|
4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5 | s = arguments[i];
|
6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7 | t[p] = s[p];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | return __assign.apply(this, arguments);
|
12 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | exports.searchServiceBindingForDestination = exports.destinationForServiceBinding = void 0;
|
15 | var util_1 = require("@sap-cloud-sdk/util");
|
16 | var proxy_util_1 = require("../../../http-agent/proxy-util");
|
17 | var environment_accessor_1 = require("../environment-accessor");
|
18 | var logger = (0, util_1.createLogger)({
|
19 | package: 'core',
|
20 | messageContext: 'destination-accessor-vcap'
|
21 | });
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function destinationForServiceBinding(serviceInstanceName, options) {
|
31 | if (options === void 0) { options = {}; }
|
32 | var serviceBindings = loadServiceBindings();
|
33 | var selected = findServiceByName(serviceBindings, serviceInstanceName);
|
34 | var destination = options.transformationFn
|
35 | ? options.transformationFn(selected)
|
36 | : transform(selected);
|
37 | return destination &&
|
38 | ((0, proxy_util_1.proxyStrategy)(destination) === proxy_util_1.ProxyStrategy.INTERNET_PROXY ||
|
39 | (0, proxy_util_1.proxyStrategy)(destination) === proxy_util_1.ProxyStrategy.PRIVATELINK_PROXY)
|
40 | ? (0, proxy_util_1.addProxyConfigurationInternet)(destination)
|
41 | : destination;
|
42 | }
|
43 | exports.destinationForServiceBinding = destinationForServiceBinding;
|
44 | function loadServiceBindings() {
|
45 | var vcapServices = (0, environment_accessor_1.getVcapService)();
|
46 | if (!vcapServices) {
|
47 | throw noVcapServicesError();
|
48 | }
|
49 | return transformServiceBindings(vcapServices);
|
50 | }
|
51 | var transformServiceBindings = function (vcapService) {
|
52 | var serviceTypes = inlineServiceTypes(vcapService);
|
53 | var flattened = flattenServiceBindings(serviceTypes);
|
54 | return flattened;
|
55 | };
|
56 | function flattenServiceBindings(vcapServices) {
|
57 | return (0, util_1.flatten)(Object.values(vcapServices));
|
58 | }
|
59 | function inlineServiceTypes(vcapServices) {
|
60 | return Object.entries(vcapServices).reduce(function (vcap, _a) {
|
61 | var _b;
|
62 | var serviceType = _a[0], bindings = _a[1];
|
63 | return (__assign(__assign({}, vcap), (_b = {}, _b[serviceType] = bindings.map(function (b) { return (__assign(__assign({}, b), { type: serviceType })); }), _b)));
|
64 | }, {});
|
65 | }
|
66 | function findServiceByName(serviceBindings, serviceInstanceName) {
|
67 | var found = serviceBindings.find(function (s) { return s.name === serviceInstanceName; });
|
68 | if (!found) {
|
69 | throw noServiceBindingFoundError(serviceBindings, serviceInstanceName);
|
70 | }
|
71 | return found;
|
72 | }
|
73 | var serviceToDestinationTransformers = {
|
74 | 'business-logging': businessLoggingBindingToDestination,
|
75 | 's4-hana-cloud': xfS4hanaCloudBindingToDestination
|
76 | };
|
77 | function transform(serviceBinding) {
|
78 | if (!serviceToDestinationTransformers[serviceBinding.type]) {
|
79 | throw serviceTypeNotSupportedError(serviceBinding.type);
|
80 | }
|
81 | return serviceToDestinationTransformers[serviceBinding.type](serviceBinding);
|
82 | }
|
83 | function noVcapServicesError() {
|
84 | return Error('No services are bound to the application (environment variable VCAP_SERVICES is not defined)!');
|
85 | }
|
86 | function serviceTypeNotSupportedError(serviceType) {
|
87 | return Error("Service of type ".concat(serviceType, " is not supported! Consider providing your own transformation function when calling destinationForServiceBinding, like this:\n destinationServiceForBinding(yourServiceName, { serviceBindingToDestination: yourTransformationFunction });"));
|
88 | }
|
89 | function noServiceBindingFoundError(serviceBindings, serviceInstanceName) {
|
90 | return Error("Unable to find a service binding for given name \"".concat(serviceInstanceName, "\"! Found the following bindings: ").concat(serviceBindings
|
91 | .map(function (s) { return s.name; })
|
92 | .join(', '), ".\n "));
|
93 | }
|
94 | function businessLoggingBindingToDestination(serviceBinding) {
|
95 | return {
|
96 | url: serviceBinding.credentials.writeUrl,
|
97 | authentication: 'OAuth2ClientCredentials',
|
98 | username: serviceBinding.credentials.uaa.clientid,
|
99 | password: serviceBinding.credentials.uaa.clientsecret
|
100 | };
|
101 | }
|
102 | function xfS4hanaCloudBindingToDestination(serviceBinding) {
|
103 | return {
|
104 | url: serviceBinding.credentials.URL,
|
105 | authentication: 'BasicAuthentication',
|
106 | username: serviceBinding.credentials.User,
|
107 | password: serviceBinding.credentials.Password
|
108 | };
|
109 | }
|
110 |
|
111 |
|
112 |
|
113 | function searchServiceBindingForDestination(name) {
|
114 | logger.info('Attempting to retrieve destination from service binding.');
|
115 | try {
|
116 | var destination = destinationForServiceBinding(name);
|
117 | logger.info('Successfully retrieved destination from service binding.');
|
118 | return destination;
|
119 | }
|
120 | catch (error) {
|
121 | logger.info("Could not retrieve destination from service binding. If you are not using SAP Extension Factory, this information probably does not concern you. ".concat(error.message));
|
122 | }
|
123 | }
|
124 | exports.searchServiceBindingForDestination = searchServiceBindingForDestination;
|
125 |
|
\ | No newline at end of file |