@revenexx/app-sdk 1.6.0 → 2.0.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.
@@ -451,6 +451,65 @@ function refusalOf(err) {
451
451
  };
452
452
  }
453
453
 
454
+ // src/patch.ts
455
+ var EMPTY_UPDATE_MESSAGE = "request body must contain at least one field to update";
456
+ function updateFields(patch) {
457
+ if (!patch || typeof patch !== "object" || Array.isArray(patch)) return [];
458
+ return Object.keys(patch).filter(
459
+ (key) => patch[key] !== void 0
460
+ );
461
+ }
462
+ var TRASH_FLAG_COLUMNS = [
463
+ "deleted_at",
464
+ "deleted_by",
465
+ "deleted_by_id",
466
+ "trash_batch",
467
+ "purge_blocked_reason"
468
+ ];
469
+ var EXTERNAL_ID_FLAG_COLUMNS = ["external_id"];
470
+ function flagColumns(entity) {
471
+ return [
472
+ ...entity.trash === true ? TRASH_FLAG_COLUMNS : [],
473
+ ...entity.external_id === true ? EXTERNAL_ID_FLAG_COLUMNS : []
474
+ ];
475
+ }
476
+ var ONE_RECORD_MESSAGE = "a write takes one record, as a JSON object \u2014 not a list";
477
+ var INDEX = /^(?:0|[1-9]\d*)$/;
478
+ function isList(data) {
479
+ if (!data || typeof data !== "object") return false;
480
+ if (Array.isArray(data)) return data.length > 0;
481
+ const keys = Object.keys(data);
482
+ return keys.length > 0 && keys.every((k) => INDEX.test(k) && Number(k) < keys.length);
483
+ }
484
+ function trashFieldsIn(entity, body) {
485
+ if (entity.trash !== true || !body || typeof body !== "object") return [];
486
+ const row = body;
487
+ return TRASH_FLAG_COLUMNS.filter((k) => Object.prototype.hasOwnProperty.call(row, k) && row[k] !== void 0).sort();
488
+ }
489
+ function assertNoTrashFields(entity, body) {
490
+ const fields = trashFieldsIn(entity, body);
491
+ if (fields.length === 0) return;
492
+ const quoted = fields.map((k) => `'${k}'`).join(", ");
493
+ throw badRequest(
494
+ `field${fields.length > 1 ? "s" : ""} ${quoted} ${fields.length > 1 ? "are" : "is"} kept by the trash \u2014 put a record into the trash, restore or purge it through the trash routes.`,
495
+ { fields },
496
+ { code: "read_only_field" }
497
+ );
498
+ }
499
+ var ENTITY_DESCRIPTOR = /* @__PURE__ */ Symbol.for("revenexx.app-sdk.entity");
500
+ function guardsTrash(stated, described, columns) {
501
+ if (stated === true || described === true) return true;
502
+ if (stated === void 0 && described === void 0) return !!columns && Object.prototype.hasOwnProperty.call(columns, "trash_batch");
503
+ return false;
504
+ }
505
+ function describedEntity(repo) {
506
+ if (!repo || typeof repo !== "object") return void 0;
507
+ return repo[ENTITY_DESCRIPTOR];
508
+ }
509
+ function oneRecordRefusal() {
510
+ return badRequest(ONE_RECORD_MESSAGE, void 0, { code: "bad_request" });
511
+ }
512
+
454
513
  // src/backdating.ts
455
514
  var MAX_MONTHS_BACK = 24;
456
515
  var BACKDATING_SETTING = "allow_backdating";
@@ -552,7 +611,7 @@ function backdatingDb(db, entities) {
552
611
  const member = Reflect.get(r, key, rr);
553
612
  if (key !== "create" || typeof member !== "function") return member;
554
613
  return (data, ...rest) => {
555
- if (!backdated() || !data || typeof data !== "object" || Array.isArray(data)) {
614
+ if (!backdated() || !data || typeof data !== "object" || Array.isArray(data) || isList(data)) {
556
615
  return member.call(r, data, ...rest);
557
616
  }
558
617
  const at = now();
@@ -767,13 +826,11 @@ function createApp(options = {}) {
767
826
  const fields = matched.route.backdating;
768
827
  if (!fields) return await matched.route.handler(c);
769
828
  const at = await requestedMoment(c, fields);
770
- if (at) {
771
- span.setAttribute("revenexx.backdated_at", at);
772
- if (strip) {
773
- const body = { ...c.body };
774
- for (const field of fields) delete body[field];
775
- c.body = body;
776
- }
829
+ if (at) span.setAttribute("revenexx.backdated_at", at);
830
+ if (strip && fields.some((field) => Object.prototype.hasOwnProperty.call(c.body ?? {}, field))) {
831
+ const body = { ...c.body };
832
+ for (const field of fields) delete body[field];
833
+ c.body = body;
777
834
  }
778
835
  const route = matched.route;
779
836
  return await runAt(at, () => route.handler(c));
@@ -807,15 +864,6 @@ function createApp(options = {}) {
807
864
  return app;
808
865
  }
809
866
 
810
- // src/patch.ts
811
- var EMPTY_UPDATE_MESSAGE = "request body must contain at least one field to update";
812
- function updateFields(patch) {
813
- if (!patch || typeof patch !== "object" || Array.isArray(patch)) return [];
814
- return Object.keys(patch).filter(
815
- (key) => patch[key] !== void 0
816
- );
817
- }
818
-
819
867
  // src/query.ts
820
868
  var OPS = /* @__PURE__ */ new Set(["eq", "neq", "gt", "gte", "lt", "lte", "like", "ilike", "in", "is"]);
821
869
 
@@ -838,13 +886,12 @@ function assertJsonFilter(column, value) {
838
886
  );
839
887
  }
840
888
  }
841
- function assertKnownKeys(query, opts) {
842
- const columns = opts.columns ?? {};
889
+ function assertKnownKeys(query, opts, flags) {
843
890
  const extra = opts.queryParams ?? [];
844
- const known = (k) => Object.prototype.hasOwnProperty.call(columns, k) || RESERVED_LIST_QUERY_KEYS.includes(k) || extra.includes(k);
891
+ const known = (k) => isFilterColumn(opts, flags, k) || RESERVED_LIST_QUERY_KEYS.includes(k) || extra.includes(k);
845
892
  const unknown = Object.keys(query).filter((k) => !known(k));
846
893
  if (unknown.length === 0) return;
847
- const allowed = [.../* @__PURE__ */ new Set([...Object.keys(columns), ...RESERVED_LIST_QUERY_KEYS, ...extra])].sort();
894
+ const allowed = [.../* @__PURE__ */ new Set([...Object.keys(opts.columns ?? {}), ...flagColumns(flags), ...RESERVED_LIST_QUERY_KEYS, ...extra])].sort();
848
895
  const quoted = unknown.map((k) => `'${k}'`).join(", ");
849
896
  throw new HttpError(
850
897
  400,
@@ -853,6 +900,10 @@ function assertKnownKeys(query, opts) {
853
900
  { code: "unknown_filter" }
854
901
  );
855
902
  }
903
+ function isFilterColumn(opts, flags, k) {
904
+ if (opts.columns && Object.prototype.hasOwnProperty.call(opts.columns, k)) return true;
905
+ return !(opts.queryParams ?? []).includes(k) && flagColumns(flags).includes(k);
906
+ }
856
907
  var OPERATOR_PREFIX = new RegExp(`^(?:not|${[...OPS].join("|")})\\.`);
857
908
  function assertNoOperator(column, value) {
858
909
  if (!OPERATOR_PREFIX.test(value)) return;
@@ -892,14 +943,19 @@ function mountCrud(app, repo, opts) {
892
943
  const enabled = new Set(opts.only ?? ["list", "get", "create", "update", "delete"]);
893
944
  const defaultLimit = opts.defaultLimit ?? 50;
894
945
  const maxLimit = opts.maxLimit ?? 200;
946
+ const described = describedEntity(repo);
947
+ const flags = {
948
+ trash: guardsTrash(opts.trash, described?.trash, opts.columns),
949
+ external_id: opts.external_id === true || described?.external_id === true
950
+ };
895
951
  if (enabled.has("list")) {
896
952
  app.get(base, async (c) => {
897
- assertKnownKeys(c.query, opts);
953
+ assertKnownKeys(c.query, opts, flags);
898
954
  const where = {};
899
955
  for (const [k, v] of Object.entries(c.query)) {
900
- if (!opts.columns || !Object.prototype.hasOwnProperty.call(opts.columns, k)) continue;
956
+ if (!isFilterColumn(opts, flags, k)) continue;
901
957
  assertNoOperator(k, v);
902
- if (isJsonColumn(opts.columns[k])) assertJsonFilter(k, v);
958
+ if (isJsonColumn(opts.columns?.[k])) assertJsonFilter(k, v);
903
959
  where[k] = v;
904
960
  }
905
961
  await assertParentExists(c, opts.parent);
@@ -922,6 +978,8 @@ function mountCrud(app, repo, opts) {
922
978
  }
923
979
  if (enabled.has("create")) {
924
980
  app.post(base, async (c) => {
981
+ if (isList(c.body)) throw oneRecordRefusal();
982
+ assertNoTrashFields(flags, c.body);
925
983
  await assertParentExists(c, opts.parent);
926
984
  const data = { ...c.body, ...parentWhere(c, opts.parent) };
927
985
  return c.json(await repo.create(data), 201);
@@ -935,6 +993,8 @@ function mountCrud(app, repo, opts) {
935
993
  }
936
994
  if (enabled.has("update")) {
937
995
  app.put(idTemplate, async (c) => {
996
+ if (isList(c.body)) throw oneRecordRefusal();
997
+ assertNoTrashFields(flags, c.body);
938
998
  const { id } = await fetchOwned(repo, c, opts);
939
999
  if (updateFields(c.body).length === 0) throw new HttpError(400, EMPTY_UPDATE_MESSAGE, void 0, { code: "empty_update" });
940
1000
  const patch = { ...c.body, ...parentWhere(c, opts.parent) };