UNPKG

@mikezimm/fps-core-v7

Version:

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

76 lines 3.78 kB
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'; /** * Delete a single list item from a list. If `recycleBin` is true (default), the item * will be sent to the recycle bin using the `recycle()` endpoint. If `recycleBin` is * false the item will be permanently deleted using the DELETE API. * * @param sourceProps - list/web info * @param item - number (item ID) or object containing an ID property * @param recycleBin - send to recycle bin unless explicitly false * @param alertMe * @param consoleLog * @returns */ export async function deleteSourceItemAPI(sourceProps, // eslint-disable-next-line @typescript-eslint/no-explicit-any item, // https://github.com/fps-solutions/Item-Ninja/issues/97 recycleBin = true, alertMe, consoleLog) { var _a, _b, _c; const { listTitle } = 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) { // 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); } // Determine the item ID let itemId; if (typeof item === 'number') itemId = item; else if (item && typeof item === 'object') itemId = (_c = (_b = (_a = item.Id) !== null && _a !== void 0 ? _a : item.ID) !== null && _b !== void 0 ? _b : item.id) !== null && _c !== void 0 ? _c : item.Id; if (!itemId && itemId !== 0) { if (alertMe === true) alert(`No item ID provided for ${listTitle}`); return createErrorFpsListReturn(useUrl, listTitle); } const useRecycle = recycleBin === undefined ? true : recycleBin; // Build API endpoint depending on recycle vs permanent delete const itemApiBase = `${useUrl}/_api/web/lists/getbytitle('${listTitle}')/items(${itemId})`; let deleteAPI = itemApiBase; let method = 'DELETE'; let headerContentType = ''; if (useRecycle === true) { // recycle() is a POST call to the recycle endpoint deleteAPI = `${itemApiBase}/recycle()`; method = 'POST'; headerContentType = 'application/json;odata=nometadata'; } else { // permanent delete - use DELETE (doSp helper converts to POST with IF-MATCH header) deleteAPI = itemApiBase; method = 'DELETE'; headerContentType = ''; } const result = await doSpHttpFetchOrPostAndCheck(deleteAPI, method, sourceProps.fpsSpService, headerContentType, alertMe, consoleLog, 'item', false, null); result.unifiedPerformanceOps.fetch.label = `delete ${listTitle} ${itemId}`; result.deleteOp = result.unifiedPerformanceOps.fetch; if (check4This(Check4.fpsShowFetchResults_Eq_true) === true) { console.log(`fps-core-v7 COMPLETE: deleteSourceItemAPI`, result); } return result; } //# sourceMappingURL=deleteSourceItemAPI.js.map