UNPKG

@mikezimm/fps-core-v7

Version:

Library of reusable core interfaces, types and constants migrated from fps-library-v2

119 lines (116 loc) 5.57 kB
import { doSpHttpFetchOrPostAndCheck } from '../../../components/molecules/SpHttp/Sp/doSpHttpFetch'; import { check4This, Check4 } from '../../../logic/Links/CheckSearch'; import { createErrorFpsListReturn } from '../../../components/molecules/process-results/createErrorFpsListItemsReturn'; import { SourcePropsNoWebUrl } from '../../../components/molecules/source-props/ISourceProps'; import { getAbsoluteWebUrlFromSourceProps, } from '../../../logic/Strings/getAbssoluteWebUrlFromSourceProps'; import { getCollectionUrl } from '../../../logic/Links/getSPOUrl'; /** * To get a parent node, this is the filter structure you want to include (without including the $filter=) $filter= `ParentNodeId eq ${ParentNodeId}` NOTE: This api currently needs to use Odata V4. At least I think it does based on pnpjs v2 * 2024-12-29: Has been moved to fps-core-v7 * import { getHubSiteDataAPI, } from "@mikezimm/fps-core-v7/lib/restAPIs/sites/nav/getHubSiteDataAPI"; * @param sourceProps * @param alertMe * @param consoleLog * @param body * @returns */ export async function getHubSiteDataAPI(sourceProps, alertMe, consoleLog) { let useUrl = getAbsoluteWebUrlFromSourceProps(sourceProps); if (!useUrl) { // NO WebURL... Throw Alert if (alertMe === true) alert(`getHubSiteDataAPI: ${SourcePropsNoWebUrl}`); const results = createErrorFpsListReturn(useUrl, ''); return results; } /** SAMPLE Full SharePoint Items Rest call: ``${this.props.SettingsSource.useUrl}/ _api/web/sitegroups? $filter=startswith(Title,'EULA')& $select=Title,Id`; */ let fetchAPI = `${useUrl}/_api/web/HubSiteData(true)`; // As of 2024-12-29: Navigation does not currently have any expand, orderby, select or filtering // fetchAPI += `$orderby=Title asc&`; // fetchAPI+= `$select=${ selectThese.length > 0 ? selectThese.join(',') : '*'}&`; // const expandThese: string[] = getExpandColumns( selectThese ); // if ( expandThese.length > 0 ) fetchAPI+= `$expand=${ expandThese.join(',') }&`; // if ( restFilter ) fetchAPI+= `$filter=${ restFilter }`; const result = await doSpHttpFetchOrPostAndCheck(fetchAPI, 'GET', sourceProps.fpsSpService, '', alertMe, consoleLog, 'hub', false, null); // try { // const baseData = result.items? result.items : result.item ? result.item : null; // const isString: boolean = typeof baseData === 'string' ? true : false; // result.hub = isString === true ? JSON.parse( baseData ) : baseData; // } catch(e) { // alert( `Unexpected error parsing HubSiteData ~ 63 - see console.`); // console.log( `getHubSiteDataAPI: `, result ); // } /** * Migrated from fetchMyHubNav in fps-library-v2\components\molecules\SpHttp\HubSiteData\fetchMyHubNav.ts */ // eslint-disable-next-line @typescript-eslint/no-explicit-any if (typeof result.items === 'string') { // eslint-disable-next-line @typescript-eslint/no-explicit-any result.hub = JSON.parse(result.items); result[`nav.navigation`] = result.hub.navigation; /** * 2025-01-01: verified through testing that the return for this is actually on .item * https://github.com/fps-solutions/HubCon/issues/121 * * To prevent any issues in the future, I'm going to just always check both item and items object and set the correct one. * After this change, I can see nav in HubCon! */ } else if (typeof result.item === 'string') { // eslint-disable-next-line @typescript-eslint/no-explicit-any result.hub = JSON.parse(result.item); result[`nav.navigation`] = result.hub.navigation; } else { // This is of unknown origin.... result.hub = null; result.ok = false; result.statusNo = 404; result.statusText = 'Unknown nav object'; } result.itemsN = []; result.metaN = []; // Have to do this to get the count of nav nodes... // although since the return result is just 1 string, maybe just c = 1 is more accurate representation. if (!result.fetchOp) result.fetchOp = {}; if (!result.fetchOp.ms) result.fetchOp.ms = 779; // Manually added this to appease TS gods // https://github.com/fps-solutions/HubCon/issues/151 result.fetchOp.c = result.hub ? countNavNodes(result.hub.navigation) : 0; result.fetchOp.a = Math.round((result.fetchOp.ms / result.fetchOp.c) * 10) / 10; // Do some short post-processing work here to create an 'Item' with the object properties from the returned result // Should be modeled after IHubSiteDataResponse interface result.unifiedPerformanceOps.fetch.label = `${getCollectionUrl(useUrl)} HubSiteData`; if (check4This(Check4.fpsShowFetchResults_Eq_true) === true) { console.log(`fps-core-v7 COMPLETE: getHubSiteDataAPI ~ 72`, result); } ; return result; } function countNavNodes(navigation) { let count = 0; navigation.map(child1 => { count++; if (child1.Children) { child1.Children.map(child2 => { count++; if (child2.Children) { child2.Children.map(child3 => { count++; }); } }); } }); return count; } //# sourceMappingURL=getHubSiteDataAPI.js.map