@sap-cloud-sdk/core
Version:
SAP Cloud SDK for JavaScript core
125 lines • 5.81 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.searchServiceBindingForDestination = exports.destinationForServiceBinding = void 0;
var util_1 = require("@sap-cloud-sdk/util");
var proxy_util_1 = require("../../../http-agent/proxy-util");
var environment_accessor_1 = require("../environment-accessor");
var logger = (0, util_1.createLogger)({
package: 'core',
messageContext: 'destination-accessor-vcap'
});
/**
* Tries to build a destination from a service binding with the given name.
* 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.
* The last error can be circumvent by using the second parameter to provide a custom function that transforms a service binding to a destination.
* @param serviceInstanceName - The name of the service.
* @param options - Options to customize the behavior of this function.
* @returns A destination.
*/
function destinationForServiceBinding(serviceInstanceName, options) {
if (options === void 0) { options = {}; }
var serviceBindings = loadServiceBindings();
var selected = findServiceByName(serviceBindings, serviceInstanceName);
var destination = options.transformationFn
? options.transformationFn(selected)
: transform(selected);
return destination &&
((0, proxy_util_1.proxyStrategy)(destination) === proxy_util_1.ProxyStrategy.INTERNET_PROXY ||
(0, proxy_util_1.proxyStrategy)(destination) === proxy_util_1.ProxyStrategy.PRIVATELINK_PROXY)
? (0, proxy_util_1.addProxyConfigurationInternet)(destination)
: destination;
}
exports.destinationForServiceBinding = destinationForServiceBinding;
function loadServiceBindings() {
var vcapServices = (0, environment_accessor_1.getVcapService)();
if (!vcapServices) {
throw noVcapServicesError();
}
return transformServiceBindings(vcapServices);
}
var transformServiceBindings = function (vcapService) {
var serviceTypes = inlineServiceTypes(vcapService);
var flattened = flattenServiceBindings(serviceTypes);
return flattened;
};
function flattenServiceBindings(vcapServices) {
return (0, util_1.flatten)(Object.values(vcapServices));
}
function inlineServiceTypes(vcapServices) {
return Object.entries(vcapServices).reduce(function (vcap, _a) {
var _b;
var serviceType = _a[0], bindings = _a[1];
return (__assign(__assign({}, vcap), (_b = {}, _b[serviceType] = bindings.map(function (b) { return (__assign(__assign({}, b), { type: serviceType })); }), _b)));
}, {});
}
function findServiceByName(serviceBindings, serviceInstanceName) {
var found = serviceBindings.find(function (s) { return s.name === serviceInstanceName; });
if (!found) {
throw noServiceBindingFoundError(serviceBindings, serviceInstanceName);
}
return found;
}
var serviceToDestinationTransformers = {
'business-logging': businessLoggingBindingToDestination,
's4-hana-cloud': xfS4hanaCloudBindingToDestination
};
function transform(serviceBinding) {
if (!serviceToDestinationTransformers[serviceBinding.type]) {
throw serviceTypeNotSupportedError(serviceBinding.type);
}
return serviceToDestinationTransformers[serviceBinding.type](serviceBinding);
}
function noVcapServicesError() {
return Error('No services are bound to the application (environment variable VCAP_SERVICES is not defined)!');
}
function serviceTypeNotSupportedError(serviceType) {
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 });"));
}
function noServiceBindingFoundError(serviceBindings, serviceInstanceName) {
return Error("Unable to find a service binding for given name \"".concat(serviceInstanceName, "\"! Found the following bindings: ").concat(serviceBindings
.map(function (s) { return s.name; })
.join(', '), ".\n "));
}
function businessLoggingBindingToDestination(serviceBinding) {
return {
url: serviceBinding.credentials.writeUrl,
authentication: 'OAuth2ClientCredentials',
username: serviceBinding.credentials.uaa.clientid,
password: serviceBinding.credentials.uaa.clientsecret
};
}
function xfS4hanaCloudBindingToDestination(serviceBinding) {
return {
url: serviceBinding.credentials.URL,
authentication: 'BasicAuthentication',
username: serviceBinding.credentials.User,
password: serviceBinding.credentials.Password
};
}
/*
* @hidden
*/
function searchServiceBindingForDestination(name) {
logger.info('Attempting to retrieve destination from service binding.');
try {
var destination = destinationForServiceBinding(name);
logger.info('Successfully retrieved destination from service binding.');
return destination;
}
catch (error) {
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));
}
}
exports.searchServiceBindingForDestination = searchServiceBindingForDestination;
//# sourceMappingURL=destination-from-vcap.js.map
;