Skip to content

Commit a7164c1

Browse files
author
Andrea Cosentino
committed
fix: partial delete for json objects not supported
1 parent 8645538 commit a7164c1

1 file changed

Lines changed: 15 additions & 22 deletions

File tree

src/utils/plugin.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -421,30 +421,23 @@ async function handlingApiFsRequest(logger: ILogger, fullUrl: URL, request: Univ
421421
logger.debug("handlingApiFsRequest: ERROR parsing json content file ", file!, error);
422422
throw new UniversalApiError(`Error parsing json content file ${file}`, "ERROR", fullUrl.pathname, Constants.HTTP_STATUS_CODE.BAD_REQUEST);
423423
}
424-
let newData: any[] | Record<string, any>;
424+
/**
425+
* INFO
426+
* Pagination/filters only apply to JSON arrays (documented).
427+
* For JSON objects the filter acts as a match condition: if the
428+
* object passes, the whole file is deleted (removeFile stays true).
429+
*/
425430
if (Array.isArray(dataFile.originalData)) {
426431
const toDeleteStrings = new Set((dataFile.data as Array<any>).map(el => JSON.stringify(el)));
427-
newData = dataFile.originalData.filter(el => !toDeleteStrings.has(JSON.stringify(el)));
428-
} else {
429-
newData = structuredClone(dataFile.originalData);
430-
Object.keys(dataFile.data).forEach(key => {
431-
JSON.stringify(dataFile.originalData[key]) === JSON.stringify(dataFile.data[key]) && delete (newData as Record<string, any>)[key];
432-
})
433-
}
434-
if (Array.isArray(newData) && newData.length > 0) {
435-
removeFile = false;
436-
await Utils.files.writingFile(file, fileFound, newData, MimeType[".json"], true);
437-
result.headers.push({
438-
name: Constants.DELETED_ELEMENTS_HEADER,
439-
value: dataFile.originalData.length - newData.length
440-
});
441-
} else if (!Array.isArray(newData) && Object.keys(newData).length > 0) {
442-
removeFile = false;
443-
await Utils.files.writingFile(file, fileFound, newData, MimeType[".json"], true);
444-
result.headers.push({
445-
name: Constants.DELETED_ELEMENTS_HEADER,
446-
value: 0
447-
});
432+
const newData = dataFile.originalData.filter(el => !toDeleteStrings.has(JSON.stringify(el)));
433+
if (newData.length > 0) {
434+
removeFile = false;
435+
await Utils.files.writingFile(file, fileFound, newData, MimeType[".json"], true);
436+
result.headers.push({
437+
name: Constants.DELETED_ELEMENTS_HEADER,
438+
value: dataFile.originalData.length - newData.length
439+
});
440+
}
448441
}
449442
}
450443
if (removeFile) {

0 commit comments

Comments
 (0)