@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.cjs CHANGED
@@ -250,6 +250,18 @@ var RouteGroupBuilder = class {
250
250
  // src/rest-server.ts
251
251
  var logError = (...args) => globalThis.console?.error(...args);
252
252
  function mapDataError(error, object) {
253
+ if (error?.code === "CONCURRENT_UPDATE" || error?.name === "ConcurrentUpdateError") {
254
+ return {
255
+ status: 409,
256
+ body: {
257
+ error: error?.message ?? "Record was modified by another user",
258
+ code: "CONCURRENT_UPDATE",
259
+ ...error?.currentVersion ? { currentVersion: error.currentVersion } : {},
260
+ ...error?.currentRecord ? { currentRecord: error.currentRecord } : {},
261
+ ...object ? { object } : {}
262
+ }
263
+ };
264
+ }
253
265
  if (error?.code === "VALIDATION_FAILED" || error?.name === "ValidationError") {
254
266
  return {
255
267
  status: 400,
@@ -1420,10 +1432,19 @@ var RestServer = class {
1420
1432
  const p = await this.resolveProtocol(projectId, req);
1421
1433
  const context = await this.resolveExecCtx(projectId, req);
1422
1434
  if (this.enforceAuth(req, res, context)) return;
1435
+ const ifMatchHeader = req.headers?.["if-match"] ?? req.headers?.["If-Match"];
1436
+ const bodyVersion = req.body && typeof req.body === "object" ? req.body.expectedVersion : void 0;
1437
+ const expectedVersion = bodyVersion ?? ifMatchHeader;
1438
+ let data = req.body;
1439
+ if (data && typeof data === "object" && "expectedVersion" in data) {
1440
+ const { expectedVersion: _drop, ...rest } = data;
1441
+ data = rest;
1442
+ }
1423
1443
  const result = await p.updateData({
1424
1444
  object: req.params.object,
1425
1445
  id: req.params.id,
1426
- data: req.body,
1446
+ data,
1447
+ ...expectedVersion ? { expectedVersion: String(expectedVersion) } : {},
1427
1448
  ...projectId ? { projectId } : {},
1428
1449
  ...context ? { context } : {}
1429
1450
  });
@@ -1450,9 +1471,13 @@ var RestServer = class {
1450
1471
  const p = await this.resolveProtocol(projectId, req);
1451
1472
  const context = await this.resolveExecCtx(projectId, req);
1452
1473
  if (this.enforceAuth(req, res, context)) return;
1474
+ const ifMatchHeader = req.headers?.["if-match"] ?? req.headers?.["If-Match"];
1475
+ const queryVersion = req.query && typeof req.query === "object" ? req.query.expectedVersion : void 0;
1476
+ const expectedVersion = queryVersion ?? ifMatchHeader;
1453
1477
  const result = await p.deleteData({
1454
1478
  object: req.params.object,
1455
1479
  id: req.params.id,
1480
+ ...expectedVersion ? { expectedVersion: String(expectedVersion) } : {},
1456
1481
  ...projectId ? { projectId } : {},
1457
1482
  ...context ? { context } : {}
1458
1483
  });