@revenexx/app-sdk 1.5.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.
@@ -118,26 +118,42 @@ var ACTOR_HEADERS = {
118
118
  trust: "x-revenexx-actor-trust",
119
119
  contact: "x-revenexx-contact",
120
120
  organization: "x-revenexx-organization",
121
- permissions: "x-revenexx-permissions"
121
+ permissions: "x-revenexx-permissions",
122
+ /** `contact` or `rep` — the kind of person the gateway resolved. */
123
+ caller: "x-revenexx-caller",
124
+ /** The rep's code, beside `caller: rep`, percent-encoded as UTF-8 (RFC 3986). */
125
+ rep: "x-revenexx-rep"
122
126
  };
123
127
  var nullIfEmpty = (v) => v.trim() === "" ? null : v.trim();
128
+ var decodeRep = (v) => {
129
+ if (v === null) return null;
130
+ try {
131
+ return nullIfEmpty(decodeURIComponent(v));
132
+ } catch {
133
+ return null;
134
+ }
135
+ };
124
136
  function actorFrom(header) {
125
137
  const read = (name) => {
126
138
  const v = header(name);
127
139
  return typeof v === "string" ? v : "";
128
140
  };
129
141
  const user = nullIfEmpty(read(ACTOR_HEADERS.user));
130
- const contact = nullIfEmpty(read(ACTOR_HEADERS.contact));
131
- const permissions = read(ACTOR_HEADERS.permissions).split(",").map((p) => p.trim()).filter(Boolean);
142
+ const isRepCall = read(ACTOR_HEADERS.caller).trim().toLowerCase() === "rep";
143
+ const rep = isRepCall ? decodeRep(nullIfEmpty(read(ACTOR_HEADERS.rep))) : null;
144
+ const contact = isRepCall ? null : nullIfEmpty(read(ACTOR_HEADERS.contact));
145
+ const organization = isRepCall ? null : nullIfEmpty(read(ACTOR_HEADERS.organization));
146
+ const permissions = isRepCall && rep === null ? [] : read(ACTOR_HEADERS.permissions).split(",").map((p) => p.trim()).filter(Boolean);
132
147
  const raw = read(ACTOR_HEADERS.trust).trim().toLowerCase();
133
- const trust = user === null && contact === null ? "none" : user !== null && raw === "authenticated" ? "authenticated" : "asserted";
134
- const plane = contact !== null ? "contact" : user !== null ? "operator" : "system";
148
+ const trust = user === null && contact === null && rep === null ? "none" : !isRepCall && user !== null && raw === "authenticated" ? "authenticated" : "asserted";
149
+ const plane = rep !== null ? "rep" : contact !== null ? "contact" : user !== null ? "operator" : "system";
135
150
  const held = new Set(permissions);
136
151
  return {
137
152
  user,
138
153
  email: nullIfEmpty(read(ACTOR_HEADERS.email)),
139
154
  contact,
140
- organization: nullIfEmpty(read(ACTOR_HEADERS.organization)),
155
+ organization,
156
+ rep,
141
157
  permissions,
142
158
  trust,
143
159
  plane,
@@ -435,6 +451,65 @@ function refusalOf(err) {
435
451
  };
436
452
  }
437
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
+
438
513
  // src/backdating.ts
439
514
  var MAX_MONTHS_BACK = 24;
440
515
  var BACKDATING_SETTING = "allow_backdating";
@@ -536,7 +611,7 @@ function backdatingDb(db, entities) {
536
611
  const member = Reflect.get(r, key, rr);
537
612
  if (key !== "create" || typeof member !== "function") return member;
538
613
  return (data, ...rest) => {
539
- if (!backdated() || !data || typeof data !== "object" || Array.isArray(data)) {
614
+ if (!backdated() || !data || typeof data !== "object" || Array.isArray(data) || isList(data)) {
540
615
  return member.call(r, data, ...rest);
541
616
  }
542
617
  const at = now();
@@ -751,13 +826,11 @@ function createApp(options = {}) {
751
826
  const fields = matched.route.backdating;
752
827
  if (!fields) return await matched.route.handler(c);
753
828
  const at = await requestedMoment(c, fields);
754
- if (at) {
755
- span.setAttribute("revenexx.backdated_at", at);
756
- if (strip) {
757
- const body = { ...c.body };
758
- for (const field of fields) delete body[field];
759
- c.body = body;
760
- }
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;
761
834
  }
762
835
  const route = matched.route;
763
836
  return await runAt(at, () => route.handler(c));
@@ -791,15 +864,6 @@ function createApp(options = {}) {
791
864
  return app;
792
865
  }
793
866
 
794
- // src/patch.ts
795
- var EMPTY_UPDATE_MESSAGE = "request body must contain at least one field to update";
796
- function updateFields(patch) {
797
- if (!patch || typeof patch !== "object" || Array.isArray(patch)) return [];
798
- return Object.keys(patch).filter(
799
- (key) => patch[key] !== void 0
800
- );
801
- }
802
-
803
867
  // src/query.ts
804
868
  var OPS = /* @__PURE__ */ new Set(["eq", "neq", "gt", "gte", "lt", "lte", "like", "ilike", "in", "is"]);
805
869
 
@@ -822,13 +886,12 @@ function assertJsonFilter(column, value) {
822
886
  );
823
887
  }
824
888
  }
825
- function assertKnownKeys(query, opts) {
826
- const columns = opts.columns ?? {};
889
+ function assertKnownKeys(query, opts, flags) {
827
890
  const extra = opts.queryParams ?? [];
828
- 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);
829
892
  const unknown = Object.keys(query).filter((k) => !known(k));
830
893
  if (unknown.length === 0) return;
831
- 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();
832
895
  const quoted = unknown.map((k) => `'${k}'`).join(", ");
833
896
  throw new HttpError(
834
897
  400,
@@ -837,6 +900,10 @@ function assertKnownKeys(query, opts) {
837
900
  { code: "unknown_filter" }
838
901
  );
839
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
+ }
840
907
  var OPERATOR_PREFIX = new RegExp(`^(?:not|${[...OPS].join("|")})\\.`);
841
908
  function assertNoOperator(column, value) {
842
909
  if (!OPERATOR_PREFIX.test(value)) return;
@@ -876,14 +943,19 @@ function mountCrud(app, repo, opts) {
876
943
  const enabled = new Set(opts.only ?? ["list", "get", "create", "update", "delete"]);
877
944
  const defaultLimit = opts.defaultLimit ?? 50;
878
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
+ };
879
951
  if (enabled.has("list")) {
880
952
  app.get(base, async (c) => {
881
- assertKnownKeys(c.query, opts);
953
+ assertKnownKeys(c.query, opts, flags);
882
954
  const where = {};
883
955
  for (const [k, v] of Object.entries(c.query)) {
884
- if (!opts.columns || !Object.prototype.hasOwnProperty.call(opts.columns, k)) continue;
956
+ if (!isFilterColumn(opts, flags, k)) continue;
885
957
  assertNoOperator(k, v);
886
- if (isJsonColumn(opts.columns[k])) assertJsonFilter(k, v);
958
+ if (isJsonColumn(opts.columns?.[k])) assertJsonFilter(k, v);
887
959
  where[k] = v;
888
960
  }
889
961
  await assertParentExists(c, opts.parent);
@@ -906,6 +978,8 @@ function mountCrud(app, repo, opts) {
906
978
  }
907
979
  if (enabled.has("create")) {
908
980
  app.post(base, async (c) => {
981
+ if (isList(c.body)) throw oneRecordRefusal();
982
+ assertNoTrashFields(flags, c.body);
909
983
  await assertParentExists(c, opts.parent);
910
984
  const data = { ...c.body, ...parentWhere(c, opts.parent) };
911
985
  return c.json(await repo.create(data), 201);
@@ -919,6 +993,8 @@ function mountCrud(app, repo, opts) {
919
993
  }
920
994
  if (enabled.has("update")) {
921
995
  app.put(idTemplate, async (c) => {
996
+ if (isList(c.body)) throw oneRecordRefusal();
997
+ assertNoTrashFields(flags, c.body);
922
998
  const { id } = await fetchOwned(repo, c, opts);
923
999
  if (updateFields(c.body).length === 0) throw new HttpError(400, EMPTY_UPDATE_MESSAGE, void 0, { code: "empty_update" });
924
1000
  const patch = { ...c.body, ...parentWhere(c, opts.parent) };