class AjaxUtil { constructor(opts) { // Define the base URLs for different environments const AUTHOR_BASE_URL = { nonProd: "https://msonecloudapifd-nonprod-adg7arhndebhe8hd.z01.azurefd.net", prod: "https://msonecloudapifd-c7cndkdmc9c3d7e3.z01.azurefd.net" }; // Flag indicating if it's an author or non-author environment const isAuthor = opts.isAuthor; // Flag indicating if it's a production or non-production environment const isProd = opts.isProd; // If the 'host' option is provided, use it as the authorHost value; otherwise, select the appropriate base URL based on the isProd flag const authorHost = AUTHOR_BASE_URL[isProd ? 'prod' : 'nonProd']; // Set the nonAuthorHost value as the origin of the current window const nonAuthorHost = window.location.origin; // Set the baseUrl value based on whether it's an author environment or not. If isAuthor is true, use authorHost; otherwise, use nonAuthorHost. this.baseUrl = isAuthor ? authorHost : nonAuthorHost; } /** * Invoke the ajax request with the provided request information * @param {Object} requestInfo - The request information including Url, method, headers, contentType, and body. * requestInfo.url {string} - The API Url for the request * requestInfo.method {string} - The request method type (e.g., "GET", "POST", etc.) * requestInfo.headers {Object} - Additional request headers (e.g., {"Authorization": "Bearer token"}) * requestInfo.contentType {string} - Content type of the request (e.g., "application/json") * requestInfo.body {any} - Request data (e.g., JSON payload or form data) * @returns {Promise} - A promise that resolves to the response from the request */ invokeRequest = async (requestInfo) => { if (!requestInfo.url) { console.log("The request information does not contain the API URL."); return; } const finalUrl = this.baseUrl + requestInfo.url; const headers = requestInfo.headers || {}; const requestOptions = { method: requestInfo.method || "GET", headers: { ...headers, "Content-Type": requestInfo.contentType || "application/json", }, body: requestInfo.body || undefined, }; return await fetch(finalUrl, requestOptions); } }