@sap/adp-abap
Version:
abap service for all yeoman generators
115 lines • 5.29 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.AbapProvider = void 0;
const adp_common_1 = require("@sap/adp-common");
const system_access_1 = require("@sap-ux/system-access");
/**
* Service for managing and providing access to an ABAP service provider.
*/
class AbapProvider {
/**
* Constructs an instance of AbapProvider.
*
* @param {EndpointsManager} endpointsManager - The endpoints service for retrieving system details.
*/
constructor(endpointsManager) {
this.endpointsManager = endpointsManager;
this.connected = false;
this.system = undefined;
}
/**
* Retrieves the configured ABAP service provider if set, otherwise throws an error.
*
* @returns {AbapServiceProvider} - The configured ABAP service provider.
*/
getProvider() {
if (!this.provider) {
throw new Error("Provider was not set!");
}
return this.provider;
}
/**
* Retrieves wheter the configured ABAP service provider is connected to ABAP system.
*
* @returns {boolean} - if provider is connected to ABAP system.
*/
isConnected() {
return this.connected;
}
/**
* Retrieves ABAP service provider connected ABAP system.
*
* @returns {string | undefined} - the connected system.
*/
getSystem() {
return this.system;
}
/**
* Configures the ABAP service provider using the specified system details and credentials.
*
* @param {string} system - The system identifier.
* @param {string} [client] - The client, if applicable.
* @param {string} [username] - The username for authentication.
* @param {string} [password] - The password for authentication.
*/
setProvider(system, client, username, password) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
const requestOptions = {
ignoreCertErrors: false
};
const target = yield this.determineTarget(requestOptions, system, client);
if (username && password) {
requestOptions.auth = { username, password };
}
this.provider = yield (0, system_access_1.createAbapServiceProvider)(target, requestOptions, false, undefined);
this.connected = true;
this.system = system;
}
catch (e) {
(_a = adp_common_1.Logger === null || adp_common_1.Logger === void 0 ? void 0 : adp_common_1.Logger.getLogger) === null || _a === void 0 ? void 0 : _a.error(`Failed to instantiate provider for system: ${system}. Reason: ${e.message}`);
throw new Error(e.message);
}
});
}
/**
* Determines the target configuration for the ABAP service provider based on whether the application
* is running within SAP App Studio or outside of it.
*
* @param {RequestOptions} requestOptions - The request options to be configured during this setup.
* @param {string} system - The system identifier, which could be a URL or a system name.
* @param {string} [client] - Optional client number, used in systems where multiple clients exist.
* @returns {Promise<AbapTarget>} - The configuration object for the ABAP service provider, tailored based on the running environment.
*/
determineTarget(requestOptions, system, client) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
let target;
if (adp_common_1.EnvironmentUtils.isRunningInBAS()) {
target = {
destination: system
};
}
else {
const details = yield this.endpointsManager.getSystemDetails(system);
target = Object.assign(Object.assign({}, details), { client: (_a = details === null || details === void 0 ? void 0 : details.client) !== null && _a !== void 0 ? _a : client });
if ((details === null || details === void 0 ? void 0 : details.username) && (details === null || details === void 0 ? void 0 : details.password)) {
requestOptions.auth = { username: details === null || details === void 0 ? void 0 : details.username, password: details === null || details === void 0 ? void 0 : details.password };
}
}
return target;
});
}
}
exports.AbapProvider = AbapProvider;
//# sourceMappingURL=AbapProvider.js.map