Skip to content

Commit 916641e

Browse files
author
Andrea Cosentino
committed
fix: patch requests handling
1 parent c01558d commit 916641e

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

src/utils/plugin.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,15 @@ async function handlingApiFsRequest(logger: ILogger, fullUrl: URL, request: Univ
298298
: dataFile.data
299299
? 1
300300
: 0;
301+
if (!HAS_PAG_OR_FILT && typeof dataFile.data === "string") {
302+
try {
303+
dataFile.data = JSON.parse(dataFile.data);
304+
result.data = dataFile.data;
305+
dataFile.total = Array.isArray(dataFile.data) ? dataFile.data.length : dataFile.data ? 1 : 0;
306+
} catch (_) {
307+
// INFO Not valid JSON — leave as string; byte length is still accurate
308+
}
309+
}
301310
result.headers.push(
302311
{ name: "content-type", value: dataFile.mimeType },
303312
{ name: "content-length", value: Utils.files.getByteLength(dataFile.data) },
@@ -361,7 +370,7 @@ async function handlingApiFsRequest(logger: ILogger, fullUrl: URL, request: Univ
361370
if (!["application/json", "application/json-patch+json", "application/merge-patch+json"].includes(request.headers["content-type"] || "")) {
362371
throw new UniversalApiError(`PATCH request content-type unsupported in ${IS_API_REST_FS ? "REST " : ""}File System API mode`, "ERROR", fullUrl.pathname, Constants.HTTP_STATUS_CODE.UNSUPPORTED_MEDIA_TYPE);
363372
}
364-
if (!IS_API_REST_FS) {
373+
if (request.body === null) {
365374
logger.debug("handlingApiFsRequest: parsing request");
366375
await Utils.request.parseRequest(request, res, fullUrl, parser, logger);
367376
}
@@ -371,6 +380,9 @@ async function handlingApiFsRequest(logger: ILogger, fullUrl: URL, request: Univ
371380
if (dataFile.mimeType !== MimeType[".json"]) {
372381
throw new UniversalApiError(`Only json file can be processing with PATCH http method`, "ERROR", fullUrl.pathname, Constants.HTTP_STATUS_CODE.BAD_REQUEST);
373382
}
383+
if (request.body === null) {
384+
throw new UniversalApiError("PATCH request body is missing", "ERROR", fullUrl.pathname, Constants.HTTP_STATUS_CODE.BAD_REQUEST);
385+
}
374386
result.status = Constants.HTTP_STATUS_CODE.OK;
375387
try {
376388
const TYPE_PATCH = ["application/json", "application/merge-patch+json"].includes(request.headers["content-type"]!) ? "merge" : "json";

0 commit comments

Comments
 (0)