@objectstack/rest 4.1.1 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -211,6 +211,18 @@ var RouteGroupBuilder = class {
211
211
  // src/rest-server.ts
212
212
  var logError = (...args) => globalThis.console?.error(...args);
213
213
  function mapDataError(error, object) {
214
+ if (error?.code === "CONCURRENT_UPDATE" || error?.name === "ConcurrentUpdateError") {
215
+ return {
216
+ status: 409,
217
+ body: {
218
+ error: error?.message ?? "Record was modified by another user",
219
+ code: "CONCURRENT_UPDATE",
220
+ ...error?.currentVersion ? { currentVersion: error.currentVersion } : {},
221
+ ...error?.currentRecord ? { currentRecord: error.currentRecord } : {},
222
+ ...object ? { object } : {}
223
+ }
224
+ };
225
+ }
214
226
  if (error?.code === "VALIDATION_FAILED" || error?.name === "ValidationError") {
215
227
  return {
216
228
  status: 400,
@@ -1381,10 +1393,19 @@ var RestServer = class {
1381
1393
  const p = await this.resolveProtocol(projectId, req);
1382
1394
  const context = await this.resolveExecCtx(projectId, req);
1383
1395
  if (this.enforceAuth(req, res, context)) return;
1396
+ const ifMatchHeader = req.headers?.["if-match"] ?? req.headers?.["If-Match"];
1397
+ const bodyVersion = req.body && typeof req.body === "object" ? req.body.expectedVersion : void 0;
1398
+ const expectedVersion = bodyVersion ?? ifMatchHeader;
1399
+ let data = req.body;
1400
+ if (data && typeof data === "object" && "expectedVersion" in data) {
1401
+ const { expectedVersion: _drop, ...rest } = data;
1402
+ data = rest;
1403
+ }
1384
1404
  const result = await p.updateData({
1385
1405
  object: req.params.object,
1386
1406
  id: req.params.id,
1387
- data: req.body,
1407
+ data,
1408
+ ...expectedVersion ? { expectedVersion: String(expectedVersion) } : {},
1388
1409
  ...projectId ? { projectId } : {},
1389
1410
  ...context ? { context } : {}
1390
1411
  });
@@ -1411,9 +1432,13 @@ var RestServer = class {
1411
1432
  const p = await this.resolveProtocol(projectId, req);
1412
1433
  const context = await this.resolveExecCtx(projectId, req);
1413
1434
  if (this.enforceAuth(req, res, context)) return;
1435
+ const ifMatchHeader = req.headers?.["if-match"] ?? req.headers?.["If-Match"];
1436
+ const queryVersion = req.query && typeof req.query === "object" ? req.query.expectedVersion : void 0;
1437
+ const expectedVersion = queryVersion ?? ifMatchHeader;
1414
1438
  const result = await p.deleteData({
1415
1439
  object: req.params.object,
1416
1440
  id: req.params.id,
1441
+ ...expectedVersion ? { expectedVersion: String(expectedVersion) } : {},
1417
1442
  ...projectId ? { projectId } : {},
1418
1443
  ...context ? { context } : {}
1419
1444
  });