@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
81 lines (80 loc) • 5.73 kB
JavaScript
/**
* CodeAnalizerComment: Updated 13 imports on 2024-09-21 23:07:24
* Update:: import { IJSFetchReturn } to '@mikezimm/fps-core-v7/lib/components/molecules/SpHttp/doSpJsFetch;'
* Update:: import { createEmptyFetchReturn } to '@mikezimm/fps-core-v7/lib/components/molecules/SpHttp/doSpJsFetch;'
* Update:: import { doSpJsFetch } to '@mikezimm/fps-core-v7/lib/components/molecules/SpHttp/doSpJsFetch;'
* Update:: import { IPerformanceSettings } to '@mikezimm/fps-core-v7/lib/components/molecules/Performance/IPerformanceSettings;'
* Update:: import { startPerformOpV2 } to '@mikezimm/fps-core-v7/lib/components/molecules/Performance/functions;'
* Update:: import { unifiedPerformanceEnd } to '@mikezimm/fps-core-v7/lib/components/molecules/Performance/functions;'
* Update:: import { IFpsErrorObject } to '@mikezimm/fps-core-v7/lib/types/fps-returns/common/IFpsErrorObject;'
* Update:: import { IFpsItemsReturn } to '@mikezimm/fps-core-v7/lib/components/molecules/process-results/CheckItemsResults;'
* Update:: import { checkItemsResults } to '@mikezimm/fps-core-v7/lib/components/molecules/process-results/CheckItemsResults;'
* Update:: import { WebPartContextCopy_15_2 } to '@mikezimm/fps-core-v7/lib/types/@msft/1.15.2/WebPartContext;'
* Update:: import { IFPSListItemPropPaneDropDownOption } to '@mikezimm/fps-core-v7/lib/banner/components/ItemPicker/interfaces/IFPSListItemPropPaneDropDownOption;'
* Update:: import { sortObjectArrayByStringKey } to '@mikezimm/fps-core-v7/lib/logic/Arrays/sorting/objects;'
* Update:: import { IPropPaneFetchFileProps } to '@mikezimm/fps-core-v7/lib/banner/components/ItemPicker/interfaces/IPropPaneFetchFileProps;'
*/
import { doSpHttpFetchOrPostAndCheck } from '../../../../components/molecules/SpHttp/Sp/doSpHttpFetch';
import { createEmptyFetchReturn } from "../../../../components/molecules/SpHttp/interfaces/IJSFetchReturn";
import { sortObjectArrayByStringKey } from '../../../../logic/Arrays/sorting/objects';
import { CurrentOrigin } from '../../../../components/molecules/source-props/WindowLocationConstants';
// import { CurrentOrigin } from "../../../../logic/Strings/getSiteCollectionUrlFromLink";
export const DefaultCustomFileSelect = "select=FileLeafRef,FileRef,FileSystemObjectType";
// THIS SHOULD COME FROM doSpHttpFetchOrPostAndCheck file
/**
* This was originally used for onListPickerChanged call... added fetchFileListUniqueIds option for easier PagePal use
* @param digestValue: string - only if needed
* @param filesLocation
* @param postFilters : would be string array of valid extensions to return
* @param customSelect : pass in custom select/filter statement....
* It's used like this: /Items?$${customSelect}` so there is no intial $ needed at the beginning
* @returns
*/
// export async function getPickerFiles ( context: WebPartContextCopy_15_2, filesLocation: undefined | IFPSListItemPropPaneDropDownOption, postFilters: string[] ): Promise<IPropPaneFetchFileProps[]> {
export async function fetchFileList(fpsSpService, filesLocation, postFilters, customSelect = DefaultCustomFileSelect) {
// const performanceSettings: IPerformanceSettings = { label: 'myHubs', updateMiliseconds: true, includeMsStr: true, op: 'fetch' };
// const fetchOp = performanceSettings ? startPerformOpV2( performanceSettings ) : null;
const apiFetchFilesQuery = filesLocation ? `${CurrentOrigin}${filesLocation === null || filesLocation === void 0 ? void 0 : filesLocation.siteRelativeURL}/_api/web/lists/getbytitle('${filesLocation === null || filesLocation === void 0 ? void 0 : filesLocation.text}')/Items?$${customSelect}` : '';
if (filesLocation === undefined)
return createEmptyFetchReturn(apiFetchFilesQuery, 'GET');
const result = await doSpHttpFetchOrPostAndCheck(apiFetchFilesQuery, 'GET', fpsSpService, '', false, true, 'file', false, null);
if (result.ok)
result.items = cleanWebFileList(result.items, postFilters);
// let result : IJSFetchReturn = unifiedPerformanceEnd( resultFileList as IFpsErrorObject, performanceSettings, fetchOp as IPerformanceOp, 'fetch', 'items' ) as IJSFetchReturn;
// result.fpsContentType = [ 'file' ];
// result = checkItemsResults( result , `fps-core-v7: fetchFileList ~ 69`, false, true ) as IJSFetchReturn;
return result;
}
export function cleanWebFileList(FilePickerInfo, postFilters) {
// Do postFilters here
//Issue #6 & #7
let FilteredItems = [];
if (!postFilters || postFilters.length === 0 || postFilters.indexOf('*') > -1) {
FilePickerInfo.map((item) => {
// Only push files, not folders
if (item.FileSystemObjectType !== 1) {
FilteredItems.push(item);
}
});
}
else {
FilePickerInfo.map((item) => {
// const extension = item.key.substr(item.key.lastIndexOf(".") + 1).toLowerCase() as IApprovedFileType;
const extension = item.FileRef ? item.FileRef.substring(item.FileRef.lastIndexOf('.') + 1).toLowerCase() : 'unknown';
if (extension && extension.length > 0 && postFilters.indexOf(extension) > -1) {
FilteredItems.push(item);
}
});
}
// Moved after because it's more efficient with potentially less items
FilteredItems.map(item => {
item.Name = item.FileLeafRef;
item.id = item['@odata.id'];
item.text = item.FileLeafRef;
item.type = item['@odata.id'];
item.key = item.FileRef;
});
const SortedItems = sortObjectArrayByStringKey(FilteredItems, 'asc', 'text');
return SortedItems;
}
//# sourceMappingURL=fetchFileList.js.map