@sap/adp-common
Version:
common logic for all yeoman generators
185 lines • 8.12 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.EndpointsManager = void 0;
const EnvironmentUtils_1 = require("./EnvironmentUtils");
const system_access_1 = require("@sap-ux/system-access");
const environment_check_1 = require("@sap-ux/environment-check");
const vscode_1 = require("../vscode");
const logger_1 = require("../logger");
/**
* Determines if authentication is not required for accessing a specified system.
*
* @param {string} system - The name of the system to check.
* @param {Endpoint[]} endpoints Array of system endpoints.
* @returns {boolean} True if no authentication is required, false otherwise.
*/
function getDestinationRequiresAuth(system, endpoints) {
const found = endpoints.find((endpoint) => endpoint.Name === system);
return (found === null || found === void 0 ? void 0 : found.Authentication) === "NoAuthentication";
}
/**
* Checks if a specified system requires authentication based on the endpoint information and installation status.
*
* @param {string} system - The name of the system to check.
* @param {Endpoint[]} endpoints Array of system endpoints.
* @returns {boolean} True if the system requires authentication, false otherwise.
*/
function getSystemRequiresAuthentication(system, endpoints) {
const isInstalled = vscode_1.VSCodeUtils.isExtensionInstalled("sapse.sap-ux-application-modeler-extension");
if (!isInstalled || endpoints.length === 0) {
return true;
}
return !(endpoints.filter((endpoint) => endpoint.Url.trim() === system.trim()).length > 0 ||
endpoints.filter((endpoint) => endpoint.Name.trim() === system.trim()).length > 0);
}
/**
* Retrieves the names of all stored endpoints and sorted alphabetically.
*
* @param {Endpoint[]} endpoints Array of system endpoints.
* @returns {string[]} An array of endpoint names and sorted order.
*/
function getEndpointNames(endpoints) {
return endpoints.map((endpoint) => endpoint.Name).sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase(), "en", { sensitivity: "base" }));
}
/**
* Service class to manage and retrieve information about system endpoints,
* including their names, authentication requirements, and specific details.
*/
class EndpointsManager {
/**
* Creates an instance of EndpointsManager.
*/
constructor() {
this.endpoints = [];
}
/**
* Creates an instance of EndpointsManager.
*
* @returns {EndpointsManager} instance of endpoints manager
*/
static getInstance() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.instance) {
this.instance = new EndpointsManager();
yield this.instance.loadEndpoints();
}
return this.instance;
});
}
/**
* Fetches endpoints from a predefined source and stores them in the service.
*
* @returns {Endpoint[]} Array of system endpoints.
*/
getEndpoints() {
return this.endpoints;
}
/**
* Fetches endpoint names from a predefined source and stores them in the service.
*
* @returns {string[]} Array of endpoint names.
*/
getEndpointNames() {
return getEndpointNames(this.endpoints);
}
/**
* Checks if there are set endpoints.
*
* @returns {boolean} true if there are set endpoints otherwise false.
*/
hasEndpoints() {
return this.endpoints.length > 0;
}
/**
* Fetches endpoints from a predefined source and stores them in the service.
*
* @returns {Promise<void>} A promise that resolves when endpoints are fetched and stored.
*/
loadEndpoints() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
const { endpoints } = yield (0, environment_check_1.checkEndpoints)();
this.endpoints = EnvironmentUtils_1.EnvironmentUtils.isRunningInBAS() ? endpoints : endpoints.filter((endpoint) => !endpoint.Scp);
}
catch (e) {
(_a = logger_1.Logger === null || logger_1.Logger === void 0 ? void 0 : logger_1.Logger.getLogger) === null || _a === void 0 ? void 0 : _a.error(`Failed to fetch endpoints list. Reason: ${e.message}`);
throw new Error(e.message);
}
});
}
/**
* Determines whether local system details should be retrieved based on the environment and installation status.
*
* @returns {boolean} True if the application is running in VS Code native and there are set systems,
* indicating that it's appropriate to fetch local system details; otherwise, false.
*/
shouldGetLocalSystemDetails() {
return !EnvironmentUtils_1.EnvironmentUtils.isRunningInBAS() && !this.hasEndpoints();
}
/**
* Retrieves destination info by name.
*
* @param {string} system - The name of the system to check.
* @returns {Endpoint | undefined} The destination info for the specific system.
*/
getDestinationInfoByName(system) {
return this.endpoints.find((endpoint) => endpoint.Name === system);
}
/**
* Determines whether a system requires authentication based on the environment of the application.
*
* @param {string} systemName The name of the system to check.
* @returns {boolean} True if the system requires authentication, false otherwise.
*/
getSystemRequiresAuth(systemName) {
return EnvironmentUtils_1.EnvironmentUtils.isRunningInBAS()
? getDestinationRequiresAuth(systemName, this.endpoints)
: getSystemRequiresAuthentication(systemName, this.endpoints);
}
/**
* Retrieves authentication details for a specified system if available.
*
* @param {string} system - The name or URL of the system to find.
* @returns {SystemDetails | undefined} Authentication details if the system is found, undefined otherwise.
*/
getSystemDetails(system) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
const endpoint = this.endpoints.find((e) => e.Name.trim() === system || e.Url.trim() === system);
if (!endpoint) {
logger_1.Logger.getLogger.info(`No endpoint found for system: ${system}`);
return undefined;
}
const details = {
client: (_a = endpoint.Client) !== null && _a !== void 0 ? _a : "",
url: (_b = endpoint.Url) !== null && _b !== void 0 ? _b : ""
};
try {
const storedSystem = yield (0, system_access_1.getCredentialsFromStore)({ url: details.url, client: details.client }, undefined);
if (storedSystem) {
details.authenticationType = storedSystem.authenticationType;
details.username = storedSystem.username;
details.password = storedSystem.password;
}
}
catch (e) {
logger_1.Logger.getLogger.error(`Error fetching credentials from store for system: ${system}`);
logger_1.Logger.getLogger.error(e.message);
return undefined;
}
return details;
});
}
}
exports.EndpointsManager = EndpointsManager;
//# sourceMappingURL=EndpointsManager.js.map