UNPKG

@sap-cloud-sdk/connectivity

Version:

SAP Cloud SDK for JavaScript connectivity

114 lines 5.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.serviceToDestinationTransformers = void 0; exports.transformServiceBindingToDestination = transformServiceBindingToDestination; exports.transformServiceBindingToClientCredentialsDestination = transformServiceBindingToClientCredentialsDestination; const token_accessor_1 = require("../token-accessor"); const build_ias_destination_1 = require("./build-ias-destination"); /** * @internal */ exports.serviceToDestinationTransformers = { 'business-logging': businessLoggingBindingToDestination, 's4-hana-cloud': xfS4hanaCloudBindingToDestination, destination: destinationBindingToDestination, 'saas-registry': saasRegistryBindingToDestination, workflow: workflowBindingToDestination, 'service-manager': serviceManagerBindingToDestination, xsuaa: xsuaaToDestination, aicore: aicoreToDestination, identity: transformIasBindingToDestination }; /** * Convenience function to create a destination from the provided service binding. * If a JWT is provided as part of options, the tenant in the JWT is used for client credentials grant, else the provider tenant is used, wherever applicable. * Supported service types are: * - business-logging (OAuth2ClientCredentials) * - destination (OAuth2ClientCredentials) * - s4-hana-cloud (BasicAuthentication) * - saas-registry (OAuth2ClientCredentials) * - workflow (OAuth2ClientCredentials) * - service-manager (OAuth2ClientCredentials) * - xsuaa (OAuth2ClientCredentials) * - aicore (OAuth2ClientCredentials) * - identity (OAuth2ClientCredentials with mTLS or client secret) * Throws an error if the provided service binding is not supported. * @param serviceBinding - The service binding to transform. * @param options - Options used for fetching the destination. * @returns A promise returning the transformed destination on success. */ async function transformServiceBindingToDestination(serviceBinding, options) { if (exports.serviceToDestinationTransformers[serviceBinding.label]) { return exports.serviceToDestinationTransformers[serviceBinding.label](serviceBinding, options); } throw new Error(`The provided service binding of type ${serviceBinding.label} is not supported out of the box for destination transformation.`); } /** * Convenience function to create a destination from the provided service binding. * Transforms a service binding to a destination of type OAuth2ClientCredentials. * If a JWT is provided as part of the options, the tenant in the JWT is used for the client credentials grant, else the provider tenant is used, wherever applicable. * @param service - The service binding to transform. * @param options - Options used to transform the service binding. * @returns A promise returning the transformed destination on success. */ async function transformServiceBindingToClientCredentialsDestination(service, options) { const token = await (0, token_accessor_1.serviceToken)(service, options); return (0, build_ias_destination_1.buildDestination)(token, options?.url ?? service.url, service.name); } async function aicoreToDestination(service, options) { const token = await (0, token_accessor_1.serviceToken)(service, options); return (0, build_ias_destination_1.buildDestination)(token, service.credentials.serviceurls.AI_API_URL, service.name); } async function xsuaaToDestination(service, options) { const token = await (0, token_accessor_1.serviceToken)(service, options); return (0, build_ias_destination_1.buildDestination)(token, service.credentials.apiurl, service.name); } async function serviceManagerBindingToDestination(service, options) { const token = await (0, token_accessor_1.serviceToken)(service, options); return (0, build_ias_destination_1.buildDestination)(token, service.credentials.sm_url, service.name); } async function destinationBindingToDestination(service, options) { const token = await (0, token_accessor_1.serviceToken)(service, options); return (0, build_ias_destination_1.buildDestination)(token, service.credentials.uri, service.name); } async function saasRegistryBindingToDestination(service, options) { const token = await (0, token_accessor_1.serviceToken)(service, options); return (0, build_ias_destination_1.buildDestination)(token, service.credentials['saas_registry_url'], service.name); } async function businessLoggingBindingToDestination(service, options) { const transformedService = { ...service, credentials: { ...service.credentials.uaa } }; const token = await (0, token_accessor_1.serviceToken)(transformedService, options); return (0, build_ias_destination_1.buildDestination)(token, service.credentials.writeUrl, service.name); } async function workflowBindingToDestination(service, options) { const transformedService = { ...service, credentials: { ...service.credentials.uaa } }; const token = await (0, token_accessor_1.serviceToken)(transformedService, options); return (0, build_ias_destination_1.buildDestination)(token, service.credentials.endpoints.workflow_odata_url, service.name); } async function xfS4hanaCloudBindingToDestination(service) { return { url: service.credentials.URL, authentication: 'BasicAuthentication', username: service.credentials.User, password: service.credentials.Password }; } async function transformIasBindingToDestination(service, options) { const iasOptions = { authenticationType: 'OAuth2ClientCredentials', ...(options?.iasOptions || {}) }; const { token } = await (0, token_accessor_1.getIasToken)(service, { jwt: options?.jwt, useCache: options?.useCache !== false, ...iasOptions }); return (0, build_ias_destination_1.buildIasDestination)(token, service, iasOptions); } //# sourceMappingURL=service-binding-to-destination.js.map