@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
95 lines (94 loc) • 5.03 kB
JavaScript
import { doSpHttpFetchOrPostAndCheck } from '../../../components/molecules/SpHttp/Sp/doSpHttpFetch';
import { SourcePropsNoListTitle, SourcePropsNoWebUrl } from '../../../components/molecules/source-props/ISourceProps';
import { check4This, Check4 } from '../../../logic/Links/CheckSearch';
import { createErrorFpsListReturn } from '../../../components/molecules/process-results/createErrorFpsListItemsReturn';
import { getAbsoluteWebUrlFromSourceProps } from '../../../logic/Strings/getAbssoluteWebUrlFromSourceProps';
import { isStringGuidOrTitle } from '../lists/isStringGuidOrTitle';
/**
* getSourceViewsAPI will get all the views information for a list
* import { getSourceViewsAPI, getSourceViewByTitleOrIdAPI } from '@mikezimm/fps-core-v7/lib/restAPIs/lists/views/getSourceViewsAPI';
*
* Tip: Use isStringGuidOrTitle to test if it's a guid or title
* import { isStringGuidOrTitle } from '@mikezimm/fps-core-v7/lib/restAPIs/lists/isStringGuidOrTitle';
*
* Replaces fetchFieldsD from pnp2
*
* @param sourceProps
* @param alertMe
* @param consoleLog
* @returns
*/
export async function getSourceViewsAPI(sourceProps, alertMe, consoleLog, viewTitleOrId = '') {
// const { performanceSettings } = sourceProps;
// 2024-09-15: Change to null as any to pass linting when migrating from fps-library-v2
// const fetchOp = performanceSettings ? startPerformOpV2( performanceSettings ) : null as any;
const { listTitle, orderBy, orderBy2, restFilter, } = sourceProps;
let { selectThese, } = 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);
if (!useUrl || !listTitle) {
// NO WebURL... Throw Alert
if (alertMe === true)
alert(`${listTitle ? listTitle : SourcePropsNoListTitle} ${SourcePropsNoWebUrl}`);
const results = createErrorFpsListReturn(useUrl, listTitle);
return results;
}
if (!selectThese || selectThese.length === 0)
selectThese = ['*'];
let fetchAPI = `${useUrl}/_api/web/lists`;
/**
* Added this logic for detecting correct selector for:
* https://github.com/mikezimm/drilldown7/issues/467
* https://github.com/mikezimm/drilldown7/issues/468
*/
const listTitleType = isStringGuidOrTitle(listTitle);
const viewTitleType = isStringGuidOrTitle(viewTitleOrId);
let selectorString = listTitleType === 'guid' ? `/getById('${listTitle}')/views` :
listTitleType === 'title' ? `/getByTitle('${listTitle}')/views` : '/views';
if (!viewTitleOrId) {
selectorString += '?';
}
else if (viewTitleType === 'guid') {
selectorString += `/getById(${viewTitleOrId})?`;
}
else if (viewTitleType === 'title') {
selectorString += `/getByTitle(${viewTitleOrId})?`;
}
else {
selectorString += '?';
}
fetchAPI += `${selectorString} `;
// let fetchAPI: string = `${useUrl}/_api/web/lists/getbytitle('${listTitle}')/views?`;
if (selectThese)
fetchAPI += `&$select= ${selectThese.join(',')}`;
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(',')}`;
if (viewTitleOrId) {
/**
* In SharePoint REST API queries, GUIDs must be explicitly cast as guid'xxxxx' because SharePoint needs to differentiate them from string values.
Why?
Type Enforcement: The Id field in SharePoint Views is of type Guid, not a string or integer. Unlike string fields (which use single quotes 'value'), GUID fields require the guid'...' format.
Syntax Requirement: SharePoint's OData implementation requires proper type casting for non-string values. If you don't include guid'...', SharePoint will treat it as an invalid filter type.
*/
const fieldFilter = `Title eq '${viewTitleOrId}' or Id eq guid'${viewTitleOrId}'`;
fetchAPI += `&$filter= ${fieldFilter}`;
}
else if (restFilter)
fetchAPI += `&$filter= ${restFilter}`;
const result = await doSpHttpFetchOrPostAndCheck(fetchAPI, 'GET', sourceProps.fpsSpService, '', alertMe, consoleLog, viewTitleOrId ? 'view' : 'views', false, null);
if (check4This(Check4.fpsShowFetchResults_Eq_true) === true) {
console.log(`fps-core-v7 COMPLETE: getSourceViewsAPI ~ 47`, result);
}
;
return result;
}
//# sourceMappingURL=getSourceViewsAPI.js.map