@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
125 lines (123 loc) • 6.02 kB
JavaScript
const GracefullyRemoved = `Gracefully removed `;
const GracefullyRemovedService = `${GracefullyRemoved}fpsSpService`;
export function stringifyFpsSpServiceObj(obj, leaveNote = true) {
// Step 0: FIRST check if sourceProps is not null :)
// https://github.com/fps-solutions/SP-API-Tester/issues/18
if (!obj)
return ``;
let newOtherProps = ``;
if (obj.fpsSpService) {
// Step 1: Temporarily remove fpsSpService
const { fpsSpService, ...otherProps } = obj;
// Step 2: Return only the item that does not have a class
newOtherProps = otherProps;
}
else if (obj.minSourceFetchProps) {
// Step 1: Temporarily remove fpsSpService
const { minSourceFetchProps, ...otherProps } = obj;
// Step 2: Reprocess minSourceFetchProps to stringify it
otherProps.minSourceFetchProps = stringifyFpsSpServiceObj(minSourceFetchProps, leaveNote);
// Step 3: Add a note in place of the class so it's visible that it existed
if (leaveNote === true)
otherProps.minSourceFetchProps.fpsSpServiceX = GracefullyRemovedService;
newOtherProps = otherProps;
}
newOtherProps = JSON.stringify(newOtherProps);
// https://github.com/fps-solutions/PagePal/issues/36
return newOtherProps;
}
/**
2024-12-23: trying to proactively fix issue with stringifying sourceProps with fpsSpService on it
per ChatGPT, this is the way to properly stringify sourceProps without impacting fpsSpService
2025-01-20: Also adding logic to remove standard react.elements on the input
* @param sourceProps
* @returns
*/
export function clonePropsWithFpsSpService(sourceProps) {
// Step 0: FIRST check if sourceProps is not null :)
// https://github.com/fps-solutions/SP-API-Tester/issues/18
if (!sourceProps)
return sourceProps;
// Step 1: Temporarily remove fpsSpService
if (sourceProps.fpsSpService) {
const { fpsSpService, ...otherSourceProps } = sourceProps;
// Step 2: Clone the rest of the object
const newSourceProps = JSON.parse(JSON.stringify(otherSourceProps));
// Step 3: Reattach fpsSpService
newSourceProps.fpsSpService = fpsSpService;
return newSourceProps;
}
else if (sourceProps.minSourceFetchProps) {
const { minSourceFetchProps, ...otherSourceProps } = sourceProps;
// Step 2: Clone the rest of the object
const newSourceProps = JSON.parse(JSON.stringify(otherSourceProps));
// Step 3: Reattach fpsSpService
newSourceProps.minSourceFetchProps = clonePropsWithFpsSpService(minSourceFetchProps);
return newSourceProps;
}
return sourceProps;
}
/**
* removeFpsSpService is used for when you want to remove things for adding to the ReactJSON component
* It will remove fpsSpService class and also known bannerProps and webpart props JSX.Elements
*
* @param sourceProps
* @param leaveNote
* @returns
*/
export function removeFpsSpService(sourceProps, leaveNote = true) {
// Step 0: FIRST check if sourceProps is not null :)
// https://github.com/fps-solutions/SP-API-Tester/issues/18
if (!sourceProps)
return sourceProps;
// Step 1: Temporarily remove fpsSpService
if (sourceProps.fpsSpService && typeof sourceProps.fpsSpService !== 'string') {
let { fpsSpService, ...otherSourceProps } = sourceProps;
// 2025-01-20: Changed to add fpsSpServiceX on otherSourceProps instead of sourceProps since it is the one returned
if (leaveNote === true)
otherSourceProps.fpsSpServiceX = GracefullyRemovedService;
otherSourceProps = removeSuspectedReactElements(otherSourceProps);
return otherSourceProps;
}
else if (sourceProps.minSourceFetchProps && typeof sourceProps.minSourceFetchProps !== 'string') {
let { minSourceFetchProps, ...otherSourceProps } = sourceProps;
otherSourceProps.minSourceFetchProps = removeFpsSpService(minSourceFetchProps);
// 2025-01-20: Changed to add fpsSpServiceX on otherSourceProps instead of sourceProps since it is the one returned
if (leaveNote === true)
otherSourceProps.minSourceFetchProps.fpsSpServiceX = GracefullyRemovedService;
otherSourceProps = removeSuspectedReactElements(otherSourceProps);
return otherSourceProps;
}
else if (sourceProps.bannerProps && typeof sourceProps.bannerProps !== 'string') {
let { bannerProps, ...otherSourceProps } = sourceProps;
otherSourceProps.bannerProps = removeFpsSpService(bannerProps);
// 2025-01-20: Changed to add fpsSpServiceX on otherSourceProps instead of sourceProps since it is the one returned
if (leaveNote === true)
otherSourceProps.bannerProps.fpsSpServiceX = GracefullyRemovedService;
otherSourceProps = removeSuspectedReactElements(otherSourceProps);
return otherSourceProps;
}
return sourceProps;
}
// https://github.com/mikezimm/pivottiles7/issues/437
const HtmlELementsToRemove = ['WebpartElement', 'domElement', 'replacePanelHTML', 'bonusHTML1', 'bonusHTML2'];
/**
* 2025-01-20: added this function to handle this issue where ReactJSONViewer was crashing when given items with React Elements
* https://github.com/mikezimm/pivottiles7/issues/437
* @param otherSourceProps
* @returns
*/
function removeSuspectedReactElements(otherSourceProps) {
HtmlELementsToRemove.map(key => {
if (otherSourceProps[key])
otherSourceProps[key] = `${GracefullyRemoved}${key}`;
});
if (otherSourceProps.bannerProps) {
HtmlELementsToRemove.map(key => {
if (otherSourceProps.bannerProps[key])
otherSourceProps.bannerProps[key] = `${GracefullyRemoved}${key}`;
});
}
return otherSourceProps;
}
//# sourceMappingURL=cloneSourceProps.js.map