import LrepConnector from 'sap/ui/fl/LrepConnector'; import FakeLrepConnector from 'sap/ui/fl/FakeLrepConnector'; import { getAdditionalChangeInfo } from '../utils/additional-change-info.js'; import type { FlexChange } from './common.js'; import { CHANGES_API_PATH as CHANGES_API_PATH_STATIC, getFlexSettings } from './common.js'; interface FetchedChanges { [key: string]: FlexChange; } interface LoadChangesResult { changes: { loadModules: boolean; changes: FlexChange[]; settings: { [key: string]: string | boolean | undefined; }; }; componentClassName: string; etag: string; loadModules: boolean; messagebundle: string | undefined; } const baseUrl = document.getElementById('sap-ui-bootstrap')?.dataset.openUxPreviewBaseUrl ?? ''; const changesApiPath = `${baseUrl}${CHANGES_API_PATH_STATIC}`; /** * Processes an array of FlexChange objects. * It updates each change object with settings and sends them to a API endpoint. * * @param {FlexChange | FlexChange[]} changes - Array of FlexChange objects to be processed. * @returns {Promise} A promise that resolves when all changes are processed. */ export async function create(changes: FlexChange | FlexChange[]): Promise { const settings = getFlexSettings(); await Promise.all( (Array.isArray(changes) ? changes : [changes]).map((change) => { if (settings) { change.support ??= {}; change.support.generator = settings.generator; } const additionalChangeInfo = getAdditionalChangeInfo(change); if (typeof FakeLrepConnector.fileChangeRequestNotifier === 'function' && change.fileName) { try { FakeLrepConnector.fileChangeRequestNotifier(change.fileName, 'create', change, additionalChangeInfo); } catch { // exceptions in the listener call are ignored } } const body = { change, additionalChangeInfo }; return fetch(changesApiPath, { method: 'POST', body: JSON.stringify(body, null, 2), headers: { 'content-type': 'application/json' } }); }) ); } /** * Loads changes from a given path and processes them using an LrepConnector instance. * The changes are then formatted and returned in a specified structure. * * @returns {Promise} A promise that resolves to an object of type LoadChangesResult. */ export async function loadChanges(...args: []): Promise { const lrep = new LrepConnector(); const response = await fetch(changesApiPath, { method: 'GET', headers: { 'content-type': 'application/json' } }); const changes = (await response.json()) as FetchedChanges; return LrepConnector.prototype.loadChanges.apply(lrep, args).then((res: LoadChangesResult) => { res.changes.changes = Object.values(changes); return res; }); } /** * Configures and enables the FakeLrepConnector based on the SAP UI5 version. * If the minor version of the SAP UI5 is less than 72, this function extends * the FakeLrepConnector's prototype with specific methods and enables the fake connector. * * Assumes the existence of a global 'sap' object with an 'ui.version' property, * and global jQuery object with 'extend' method. * * @returns {void} */ export default function (): void { // eslint-disable-next-line no-undef jQuery.extend(FakeLrepConnector.prototype, { create, loadChanges, loadSettings: () => Promise.resolve() }); FakeLrepConnector.enableFakeConnector(); }