@sap/subaccount-destination-service-provider
Version:
Provide service consumption of SAP subaccount services
99 lines • 4.78 kB
JavaScript
;
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.SubaccountLocalConnectivity = void 0;
const logger_1 = require("@sap-ux/logger");
const store_1 = require("@sap-ux/store");
const utils_1 = require("./utils");
const messages_1 = require("../i18n/messages");
class SubaccountLocalConnectivity {
constructor() {
this.systemCache = new Map();
}
init(logPrefix, basePath) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.systemService) {
this.systemService = yield (0, store_1.getService)({
logger: new logger_1.ToolsLogger({ logPrefix }),
entityName: "system",
options: {
baseDirectory: basePath,
},
});
}
});
}
getSubAccountDestinations() {
return __awaiter(this, void 0, void 0, function* () {
const systems = yield this.systemService.getAll({
includeSensitiveData: true,
backendSystemFilter: {
connectionType: ["abap_catalog", "odata_service"],
},
});
const filteredSystems = systems.filter(utils_1.filterSupportedSystems);
filteredSystems.forEach((system) => this.setCacheKey(system));
const destinations = filteredSystems.map(utils_1.mapBackendSystemToProviderDestination);
return destinations;
});
}
sendRequestViaDestination(connProviderParams, destinationName) {
return __awaiter(this, void 0, void 0, function* () {
const { url, credentials, sapClient } = yield this.getDestinationByName(destinationName);
const destUrl = (0, utils_1.joinUrl)(url, connProviderParams.urlPath);
if (sapClient && !destUrl.searchParams.has("sap-client")) {
destUrl.searchParams.set("sap-client", sapClient);
}
connProviderParams.headers = (0, utils_1.addAuthorizationHeader)(credentials, connProviderParams.headers);
return (0, utils_1.buildParamsAndSendRequest)(connProviderParams, destUrl.toString());
});
}
sendRequestToPublicSystem(connProviderParams, baseSystemUrl) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${baseSystemUrl}${connProviderParams.urlPath}`;
return (0, utils_1.buildParamsAndSendRequest)(connProviderParams, url);
});
}
getDestinationByName(name) {
return __awaiter(this, void 0, void 0, function* () {
const key = this.systemCache.get(name);
if (!key) {
const allSystems = yield this.systemService.getAll({
includeSensitiveData: true,
backendSystemFilter: {
connectionType: ["abap_catalog", "odata_service"],
},
});
const system = allSystems.find((sys) => sys.name === name);
if (!system) {
throw new Error(messages_1.messages.DESTINATION_NOT_FOUND(name));
}
this.setCacheKey(system);
}
const system = yield this.systemService.read(key);
if (!system) {
throw new Error(messages_1.messages.DESTINATION_NOT_FOUND_BY_KEY(name));
}
const destination = (0, utils_1.mapBackendSystemToProviderDestination)(system);
const { username, password } = system;
destination.credentials.basicAuthentication =
username && password ? { userName: username, userPassword: password } : undefined;
return destination;
});
}
setCacheKey(system) {
const { url, client } = system;
const key = new store_1.BackendSystemKey({ url, client });
this.systemCache.set(system.name, key);
}
}
exports.SubaccountLocalConnectivity = SubaccountLocalConnectivity;
//# sourceMappingURL=subaccountLocalConnectivity.js.map