UNPKG

5.81 kBJavaScriptView Raw
1"use strict";
2var __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};
13Object.defineProperty(exports, "__esModule", { value: true });
14exports.searchServiceBindingForDestination = exports.destinationForServiceBinding = void 0;
15var util_1 = require("@sap-cloud-sdk/util");
16var proxy_util_1 = require("../../../http-agent/proxy-util");
17var environment_accessor_1 = require("../environment-accessor");
18var logger = (0, util_1.createLogger)({
19 package: 'core',
20 messageContext: 'destination-accessor-vcap'
21});
22/**
23 * Tries to build a destination from a service binding with the given name.
24 * Throws an error if no services are bound at all, no service with the given name can be found, or the service type is not supported.
25 * The last error can be circumvent by using the second parameter to provide a custom function that transforms a service binding to a destination.
26 * @param serviceInstanceName - The name of the service.
27 * @param options - Options to customize the behavior of this function.
28 * @returns A destination.
29 */
30function 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}
43exports.destinationForServiceBinding = destinationForServiceBinding;
44function loadServiceBindings() {
45 var vcapServices = (0, environment_accessor_1.getVcapService)();
46 if (!vcapServices) {
47 throw noVcapServicesError();
48 }
49 return transformServiceBindings(vcapServices);
50}
51var transformServiceBindings = function (vcapService) {
52 var serviceTypes = inlineServiceTypes(vcapService);
53 var flattened = flattenServiceBindings(serviceTypes);
54 return flattened;
55};
56function flattenServiceBindings(vcapServices) {
57 return (0, util_1.flatten)(Object.values(vcapServices));
58}
59function 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}
66function 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}
73var serviceToDestinationTransformers = {
74 'business-logging': businessLoggingBindingToDestination,
75 's4-hana-cloud': xfS4hanaCloudBindingToDestination
76};
77function transform(serviceBinding) {
78 if (!serviceToDestinationTransformers[serviceBinding.type]) {
79 throw serviceTypeNotSupportedError(serviceBinding.type);
80 }
81 return serviceToDestinationTransformers[serviceBinding.type](serviceBinding);
82}
83function noVcapServicesError() {
84 return Error('No services are bound to the application (environment variable VCAP_SERVICES is not defined)!');
85}
86function 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}
89function 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}
94function 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}
102function 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 * @hidden
112 */
113function 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}
124exports.searchServiceBindingForDestination = searchServiceBindingForDestination;
125//# sourceMappingURL=destination-from-vcap.js.map
\No newline at end of file