UNPKG

@mikezimm/fps-core-v7

Version:

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

128 lines (126 loc) 7.19 kB
/** * 2024-09-29: Migrated from src\pnpjs\SourceItems\getSourceItems.ts */ import { SourcePropsNoListTitle, SourcePropsNoWebUrl } from '../../../components/molecules/source-props/ISourceProps'; ///ISourceProps'; import { check4This, Check4 } from "../../../logic/Links/CheckSearch"; import { doSpHttpFetchOrPostAndCheck } from "../../../components/molecules/SpHttp/Sp/doSpHttpFetch"; import { createErrorFpsListReturn } from '../../../components/molecules/process-results/createErrorFpsListItemsReturn'; import { getAbsoluteWebUrlFromSourceProps } from '../../../logic/Strings/getAbssoluteWebUrlFromSourceProps'; import { getTodayRestFilter } from './getTodayRestFilter'; import { clonePropsWithFpsSpService } from '../../../components/molecules/source-props/createSources/cloneSourceProps'; import { getExpandColumns } from '../../../components/molecules/source-props/Lists/getVX/getExpandV2'; export const UniquePermsFilter = 'HasUniqueRoleAssignments eq true'; export const UniquePermsHiddenFilter = `${UniquePermsFilter} and Hidden eq false`; /** * 2024-12-23: This may need to be refactored to NOT parse and stringify sourceProps because it now has fpsSpService * @param sourceProps * @param autoExapnd = true is for normal sourceProps, use false if you have custom logic for expands such as Drilldown * @param alertMe * @param consoleLog * @param expandFormats = optional - use FieldValuesAsText and FieldValuesAsHtml to add to the expands for return format regardless of autoExpand * @returns */ export async function getSourceItemsUniquePermissionsAPI(sourceProps, autoExapnd, alertMe, consoleLog, expandFormats = []) { // const newSourceProps: ISourceProps = JSON.parse(JSON.stringify( sourceProps )); const newSourceProps = clonePropsWithFpsSpService(sourceProps); newSourceProps.restFilter = `${newSourceProps.restFilter ? newSourceProps.restFilter : ''}`; newSourceProps.restFilter += `${newSourceProps.restFilter ? ' and ' : ''}${UniquePermsHiddenFilter}`; const results = await getSourceItemsAPI(sourceProps, autoExapnd, alertMe, consoleLog, expandFormats); return results; } /** * getSourceItems calls the Pnp function to get the results which returns the raw error. * This function then will convert the error into the helpful error and return the standard IItemsError object. * const UniquePermsFilter = 'HasUniqueRoleAssignments eq true'; * const UniquePermsHiddenFilter = `${ UniquePermsFilter } and Hidden eq false`; * * import { getSourceItemsAPI } from "@mikezimm/fps-core-v7/lib/restAPIs/lists/items/getSourceItemsAPI"; * * @param sourceProps * @param autoExapnd = true is for normal sourceProps, use false if you have custom logic for expands such as Drilldown * @param alertMe * @param consoleLog * @param expandFormats = optional - use FieldValuesAsText and FieldValuesAsHtml to add to the expands for return format regardless of autoExpand * @param ItemId -- optional to add a number Id greater than -1 to use the .getById filter, over-rides any restFilter on sourceProps * @returns */ export async function getSourceItemsAPI(sourceProps, autoExapnd, alertMe, consoleLog, expandFormats = [], ItemId = -1) { const { listTitle, orderBy, orderBy2, fetchCount, restFilter, selectThese, expandThese, skip } = sourceProps; // 2024-12-05: Added this because some places like PivotTiles had absoluteWebUrl in webUrl prop... so just taking care of differences const useUrl = getAbsoluteWebUrlFromSourceProps(sourceProps); let fetchAPI = `${useUrl}/_api/web/lists/getbytitle('${listTitle}')/items`; if (!useUrl) { // NO WebURL... Throw Alert if (alertMe === true) alert(`${listTitle} ${SourcePropsNoWebUrl}`); return createErrorFpsListReturn(useUrl, listTitle); } if (!listTitle) { // NO WebURL... Throw Alert if (alertMe === true) alert(`${''} ${SourcePropsNoListTitle}`); return createErrorFpsListReturn(useUrl, listTitle); } // Added just so it's always here for cases like getOnlySharedItems // sourceProps = await addFPSDigestToSourceProps( sourceProps ); /** SAMPLE Full SharePoint Items Rest call: https://example.sharepoint.com/sites/MySite/_api/web/lists/getbytitle('MyList')/items? $select=Id,Title,EULAText,ExpandedField1/Id,ExpandedField1/Title,ExpandedField2/Id,ExpandedField2/Title& $expand=ExpandedField1,ExpandedField2& $filter=Title eq 'SomeTitle'& $orderby=Id desc */ // https://github.com/fps-solutions/HubCon/issues/112 if (ItemId > -1) { // add the getById selector fetchAPI += `/getById(${ItemId})?`; } else { fetchAPI += `?`; } if (selectThese && selectThese.length > 0) fetchAPI += `&$select=${selectThese.join(',')}`; // https://github.com/fps-solutions/HubCon/issues/111 let useExpands = expandFormats && expandFormats.length > 0 ? expandFormats : []; if (autoExapnd === false && expandThese && expandThese.length > 0) useExpands = expandThese; if (autoExapnd !== false) { useExpands = getExpandColumns(selectThese); } if (useExpands.length > 0) fetchAPI += `&$expand=${useExpands.join(',')}`; // 2024-12-05: Added getTodayRestFilter from fps-Pnp2 fetchAnyItems if (restFilter && ItemId < 0) fetchAPI += `&$filter=${getTodayRestFilter(restFilter)}`; const orderBys = []; /** * 2025-02-09: Updated the orderBy for when there is no orderBy.prop or .order * https://github.com/mikezimm/PageInfo/issues/178 */ if (orderBy && orderBy.prop) orderBys.push(`${orderBy.prop} ${orderBy.order ? orderBy.order : 'asc'}`); if (orderBy2 && orderBy2.prop) orderBys.push(`${orderBy2.prop} ${orderBy2.order ? orderBy2.order : 'asc'}`); if (orderBys.length > 0) fetchAPI += `&$orderby=${orderBys.join(',')}`; // 2024-12-05: changed to fetchCount > 0 because it was doing top=null which caused an error if (fetchCount > 0) fetchAPI += `&$top=${fetchCount}`; if (skip) fetchAPI += `&$skip=${skip}`; const result = await doSpHttpFetchOrPostAndCheck(fetchAPI, 'GET', sourceProps.fpsSpService, '', alertMe, consoleLog, 'item', false, null); // Add any required custom logic here // if ( resultHubSites.ok ) { // resultHubSites.items = cleanSearchedWebs( resultHubSites.items ); // } result.unifiedPerformanceOps.fetch.label = `fetch${listTitle}`; // result.fpsContentType = !sourceProps.fpsContentType ? [ 'item' ] : sourceProps.fpsContentType; // 2024-12-05: changed if to test result, not sourceProps... was getting error reading indexOf // if ( result.fpsContentType.indexOf( 'item' ) < 0 ) result.fpsContentType.push( 'item' ); if (check4This(Check4.fpsShowFetchResults_Eq_true) === true) { console.log(`fps-core-v7 COMPLETE: getSourceItemsAPI ~ 90`, result); } ; return result; } //# sourceMappingURL=getSourceItemsAPI.js.map