UNPKG

@sap-ux/system-access

Version:

Reusable module allowing to access systems using the store or prompts.

192 lines 8.03 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isUrlTarget = isUrlTarget; exports.isDestinationTarget = isDestinationTarget; exports.createAbapServiceProvider = createAbapServiceProvider; const axios_extension_1 = require("@sap-ux/axios-extension"); const credentials_1 = require("./credentials"); const btp_utils_1 = require("@sap-ux/btp-utils"); const prompts_1 = require("./prompts"); const prompts_2 = __importDefault(require("prompts")); const fs_1 = require("fs"); const store_1 = require("@sap-ux/store"); /** * Check if it is a url target. * * @param target target configuration * @returns true if it is a UrlAbapTarget */ function isUrlTarget(target) { return target.url !== undefined; } /** * Check if it is a destination target. * * @param target target configuration * @returns true if it is a DestinationAbapTarget */ function isDestinationTarget(target) { return target.destination !== undefined; } /** * Enhance axios options and create a service provider instance for an ABAP Cloud system. * * @param options - predefined axios options * @param target - url target configuration * @param prompt - prompt the user for missing information * @param logger - reference to the logger instance * @returns an abap service provider */ async function createAbapCloudServiceProvider(options, target, prompt, logger) { const providerConfig = { ...options, environment: axios_extension_1.AbapCloudEnvironment.Standalone, service: target.serviceKey }; if (!providerConfig.service) { // first try reading the keys from the store const storedOpts = await (0, credentials_1.getCredentialsFromStore)(target, logger); if ((0, credentials_1.isServiceAuth)(storedOpts)) { providerConfig.service = storedOpts.serviceKeys; providerConfig.refreshToken = storedOpts.refreshToken; logger.info(`Using system [${storedOpts.name}] from System store`); } if (!providerConfig.service && prompt) { const { path } = await (0, prompts_2.default)(prompts_1.questions.serviceKeysPath); providerConfig.service = JSON.parse((0, fs_1.readFileSync)(path, 'utf-8')); } } // if no keys are available throw and error if (providerConfig.service) { return (0, axios_extension_1.createForAbapOnCloud)(providerConfig); } else { throw new Error('Service keys required for ABAP Cloud environment.'); } } /** * Enhance axios options and create a service provider instance for an on-premise ABAP system. * * @param options predefined axios options * @param target url target configuration * @param prompt - prompt the user for missing information * @param logger reference to the logger instance * @returns an ABAPServiceProvider instance */ async function createAbapOnPremServiceProvider(options, target, prompt, logger) { if (!options.auth) { const storedOpts = await (0, credentials_1.getCredentialsFromStore)(target, logger); if ((0, credentials_1.isBasicAuth)(storedOpts)) { options.auth = { username: storedOpts.username, password: storedOpts.password }; } else { if ((0, credentials_1.isServiceAuth)(storedOpts)) { throw new Error('This is an ABAP Cloud system, please correct your configuration.'); } options.auth ??= (0, credentials_1.getCredentialsFromEnvVariables)(); if (!options.auth && prompt) { const { authType } = await (0, prompts_2.default)([prompts_1.questions.authType]); if (authType === store_1.AuthenticationType.ReentranceTicket) { target.authenticationType = store_1.AuthenticationType.ReentranceTicket; } else { const credentials = await (0, credentials_1.getCredentialsWithPrompts)(storedOpts?.username); options.auth = credentials; process.env.FIORI_TOOLS_USER = credentials.username; process.env.FIORI_TOOLS_PASSWORD = credentials.password; } } } } return target.authenticationType === store_1.AuthenticationType.ReentranceTicket ? (0, axios_extension_1.createForAbapOnCloud)({ ...options, ...target, environment: axios_extension_1.AbapCloudEnvironment.EmbeddedSteampunk }) : (0, axios_extension_1.createForAbap)(options); } /** * Enhance axios options and create a service provider instance for a destination. * * @param options predefined axios options * @param target url target configuration * @param prompt - prompt the user for missing information * @returns an ABAPServiceProvider instance */ async function createAbapDestinationServiceProvider(options, target, prompt) { // Need additional properties to determine the type of destination we are dealing with const destinations = await (0, btp_utils_1.listDestinations)(); const destination = destinations?.[target.destination]; if (!destination) { throw new Error(`Destination ${target.destination} not found on subaccount`); } const provider = (0, axios_extension_1.createForDestination)(options, destination); // if prompting is enabled, check if the destination works or basic auth is required if (prompt) { const id = provider.interceptors.response.use(undefined, async (error) => { provider.interceptors.response.eject(id); if (error.response?.status === 401) { const credentials = await (0, credentials_1.getCredentialsWithPrompts)(); provider.defaults.auth = credentials; process.env.FIORI_TOOLS_USER = credentials.username; process.env.FIORI_TOOLS_PASSWORD = credentials.password; return provider.request(error.config); } else { throw error; } }); } return provider; } /** * Create an instance of an ABAP service provider connected to the given target configuration. * * @param target - target configuration * @param requestOptions - additional AxiosRequestOptions * @param prompt - prompt the user for missing information * @param logger - optional reference to the logger instance * @returns service instance */ async function createAbapServiceProvider(target, requestOptions, prompt, logger) { let provider; const options = { params: target.params ?? {}, ...requestOptions }; // Destination only supported in Business Application Studio if ((0, btp_utils_1.isAppStudio)() && isDestinationTarget(target)) { provider = await createAbapDestinationServiceProvider(options, target, prompt); } else if (isUrlTarget(target)) { if (target.scp) { provider = await createAbapCloudServiceProvider(options, target, prompt, logger); } else if (target.authenticationType === store_1.AuthenticationType.ReentranceTicket) { provider = (0, axios_extension_1.createForAbapOnCloud)({ ignoreCertErrors: options.ignoreCertErrors, environment: axios_extension_1.AbapCloudEnvironment.EmbeddedSteampunk, ...target }); } else { options.baseURL = target.url; if (target.client) { options.params['sap-client'] = target.client; } provider = await createAbapOnPremServiceProvider(options, target, prompt, logger); } } else { throw new Error('Unable to handle the configuration in the current environment.'); } return provider; } //# sourceMappingURL=connect.js.map