Skip to content

Commit 49ede07

Browse files
author
Andrea Cosentino
committed
fix: POST handling requests
1 parent 6f3bd32 commit 49ede07

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

src/utils/plugin.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,30 @@ async function handlingApiFsRequest(logger: ILogger, fullUrl: URL, request: Univ
292292
}
293293
}
294294
if (HAS_DATA) {
295-
if (!IS_JSON_CONTENT || !HAS_PAG_OR_FILT) {
296-
throw new UniversalApiError(`File at ${fullUrl.pathname} already exists`, "ERROR", fullUrl.pathname, Constants.HTTP_STATUS_CODE.CONFLICT);
295+
if (!IS_JSON_CONTENT) {
296+
throw new UniversalApiError(
297+
`Content-Type must be application/json for POST requests to an existing file in ${IS_API_REST_FS ? "REST " : ""}File System API mode`,
298+
"ERROR",
299+
fullUrl.pathname,
300+
Constants.HTTP_STATUS_CODE.UNSUPPORTED_MEDIA_TYPE
301+
);
302+
}
303+
if (!HAS_PAG_OR_FILT) {
304+
throw new UniversalApiError(
305+
`File at ${fullUrl.pathname} already exists. Use PUT to replace it or PATCH to partially update it`,
306+
"ERROR",
307+
fullUrl.pathname,
308+
Constants.HTTP_STATUS_CODE.CONFLICT
309+
);
297310
}
298311
const bodyClean = Utils.request.getCleanBody(request.method, currentContent, paginationHandler, filtersHandler, paginationPlugin, filtersPlugin);
299312
if (bodyClean !== null) {
300-
throw new UniversalApiError(`File at ${fullUrl.pathname} already exists`, "ERROR", fullUrl.pathname, Constants.HTTP_STATUS_CODE.CONFLICT);
313+
throw new UniversalApiError(
314+
`File at ${fullUrl.pathname} already exists. The request body must contain only pagination/filter parameters`,
315+
"ERROR",
316+
fullUrl.pathname,
317+
Constants.HTTP_STATUS_CODE.CONFLICT
318+
);
301319
}
302320
}
303321
result.data = dataFile.data;
@@ -325,7 +343,7 @@ async function handlingApiFsRequest(logger: ILogger, fullUrl: URL, request: Univ
325343
throw new UniversalApiError(`No data provided`, "ERROR", fullUrl.pathname, Constants.HTTP_STATUS_CODE.BAD_REQUEST);
326344
}
327345
if (HAS_PAG_OR_FILT) {
328-
throw new UniversalApiError(`No data to filter or to paginate`, "ERROR", fullUrl.pathname, Constants.HTTP_STATUS_CODE.BAD_REQUEST);
346+
throw new UniversalApiError(`Cannot apply pagination or filters: resource at ${fullUrl.pathname} does not exist yet.`, "ERROR", fullUrl.pathname, Constants.HTTP_STATUS_CODE.BAD_REQUEST);
329347
}
330348
result.status = Constants.HTTP_STATUS_CODE.CREATED;
331349
writeFile = currentContent;

0 commit comments

Comments
 (0)