UNPKG

@sap/subaccount-destination-service-provider

Version:

Provide service consumption of SAP subaccount services

113 lines 4.85 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.mapBackendSystemToProviderDestination = mapBackendSystemToProviderDestination; exports.mapFioriAuthToAuthentication = mapFioriAuthToAuthentication; exports.filterSupportedSystems = filterSupportedSystems; exports.mapSystemTypeToUsage = mapSystemTypeToUsage; exports.addHeaders = addHeaders; exports.addAuthorizationHeader = addAuthorizationHeader; exports.buildParamsAndSendRequest = buildParamsAndSendRequest; exports.joinUrl = joinUrl; const service_provider_apis_1 = require("@sap/service-provider-apis"); const axios_1 = require("axios"); const messages_1 = require("../i18n/messages"); const systemUtils_1 = require("../utils/systemUtils"); function mapBackendSystemToProviderDestination(system) { const { name, url, authenticationType, client, systemType, connectionType } = system; const credentials = { authentication: authenticationType ? mapFioriAuthToAuthentication(authenticationType) : service_provider_apis_1.AuthenticationType.NoAuthentication, }; const destUrl = new URL(url); const destination = { name, type: "HTTP", credentials, proxyType: "Internet", sapClient: client, description: "", url: destUrl, usage: mapSystemTypeToUsage(systemType, connectionType), host: destUrl.toString(), }; return destination; } function mapFioriAuthToAuthentication(value) { const authType = systemUtils_1.fioriAuthenticationTypeMap.get(value); if (authType) { return authType; } else { return service_provider_apis_1.AuthenticationType.NoAuthentication; } } function filterSupportedSystems(system) { const supportedSystemTypes = Object.values(systemUtils_1.SupportedFioriSystemType); const supportedConnectionTypes = Object.values(systemUtils_1.SupportedFioriConnectionType); return (supportedSystemTypes.includes(system.systemType) || supportedConnectionTypes.includes(system.connectionType)); } function mapSystemTypeToUsage(systemType, connectionType) { if (Object.values(systemUtils_1.SupportedFioriConnectionType).includes(connectionType)) { return systemUtils_1.fioriConnectionUsageMap.get(connectionType); } if (Object.values(systemUtils_1.SupportedFioriSystemType).includes(systemType)) { return systemUtils_1.fioriSystemUsageMap.get(systemType); } throw new Error(messages_1.messages.SYSTEM_TYPE_NOT_SUPPORTED(systemType)); } function addHeaders(responseType, headers) { if (!headers) { headers = {}; } headers["User-Agent"] = "subaccount-destination-service-provider"; if (responseType === "json") { headers.Accept = "application/json"; } return headers; } function addAuthorizationHeader(credentials, headers) { if (!headers) { headers = {}; } if (headers.Authorization) { return headers; } if (credentials.basicAuthentication) { const { userName, userPassword } = credentials.basicAuthentication; headers.Authorization = new service_provider_apis_1.Authentication(userName, userPassword).getEncodedBasicAuthentication(); } return headers; } function buildParamsAndSendRequest(connProviderParams, url) { return __awaiter(this, void 0, void 0, function* () { const options = { url, method: connProviderParams.method, responseType: connProviderParams.responseType, headers: addHeaders(connProviderParams.responseType, connProviderParams.headers), data: connProviderParams.body, beforeRedirect: (opts) => { opts.headers = Object.assign({}, connProviderParams.headers); }, }; const response = yield axios_1.default.request(options); return response.data; }); } function joinUrl(baseUrl, extraPath) { const cleanExtra = extraPath.replace(/^\/+/, ""); const joinedUrl = baseUrl.pathname.replace(/\/+$/, "") + "/" + cleanExtra; return new URL(joinedUrl, baseUrl.origin); } //# sourceMappingURL=utils.js.map