UNPKG

@sap-cloud-sdk/connectivity

Version:

SAP Cloud SDK for JavaScript connectivity

74 lines 3.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDestinationFromServiceBinding = getDestinationFromServiceBinding; exports.searchServiceBindingForDestination = searchServiceBindingForDestination; const util_1 = require("@sap-cloud-sdk/util"); const jwt_1 = require("../jwt"); const environment_accessor_1 = require("../environment-accessor"); const http_proxy_util_1 = require("./http-proxy-util"); const destination_service_types_1 = require("./destination-service-types"); const service_binding_to_destination_1 = require("./service-binding-to-destination"); const forward_auth_token_1 = require("./forward-auth-token"); const logger = (0, util_1.createLogger)({ package: 'connectivity', 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 options - Options to customize the behavior of this function. * @returns A destination. */ async function getDestinationFromServiceBinding(options) { const decodedJwt = options.iss ? { iss: options.iss } : options.jwt ? (0, jwt_1.decodeJwt)(options.jwt) : undefined; const retrievalOptions = { ...options, jwt: decodedJwt }; const destination = await retrieveDestination(retrievalOptions); const destWithProxy = destination && (0, destination_service_types_1.isHttpDestination)(destination) && ['internet', 'private-link'].includes((0, http_proxy_util_1.proxyStrategy)(destination)) ? (0, http_proxy_util_1.addProxyConfigurationInternet)(destination) : destination; if (destWithProxy) { (0, forward_auth_token_1.setForwardedAuthTokenIfNeeded)(destWithProxy, options.jwt); } return destWithProxy; } async function retrieveDestination({ useCache, jwt, destinationName, serviceBindingTransformFn }) { const service = (0, environment_accessor_1.getServiceBindingByInstanceName)(destinationName); const destination = await (serviceBindingTransformFn || transform)(service, { useCache, jwt }); return { name: destinationName, ...destination }; } async function transform(service, options) { if (!service_binding_to_destination_1.serviceToDestinationTransformers[service.label]) { throw serviceTypeNotSupportedError(service); } return service_binding_to_destination_1.serviceToDestinationTransformers[service.label](service, options); } function serviceTypeNotSupportedError(service) { return Error(`The service "${service.name}" is of type "${service.label}" which is not supported! Consider providing your own transformation function when calling \`getDestinationFromServiceBinding()\`, like this: destinationServiceForBinding(yourServiceName, { serviceBindingToDestination: yourTransformationFunction });`); } /** * @internal */ async function searchServiceBindingForDestination(options) { logger.debug('Attempting to retrieve destination from service binding.'); try { const destination = await getDestinationFromServiceBinding(options); logger.info('Successfully retrieved destination from service binding.'); return destination; } catch (error) { logger.debug(`Could not retrieve destination from service binding. If you are not using SAP Extension Factory, this information probably does not concern you. ${error.message}`); } return null; } //# sourceMappingURL=destination-from-vcap.js.map