@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.
@@ -1,5 +1,5 @@
1
- import { o as SettingsDefaults, l as ReadOptions, n as ResolvedSettings, r as SettingsSnapshot, u as WriteOptions, t as SettingsWriteResult, q as SettingsReader, p as SettingsOptions, e as BackdatingOptions, Q as Query, R as Row, P as Page, s as SettingsSource } from '../errors-CWT-wc8L.cjs';
2
- export { L as BACKDATING_FIELD, N as BACKDATING_INVALID, T as BACKDATING_SETTING, B as BackdatingEntity, U as BackdatingSource, D as DEFAULT_GATEWAY_ENDPOINT, f as DEFAULT_SETTINGS_ERROR_TTL_MS, g as DEFAULT_SETTINGS_TIMEOUT_MS, h as DEFAULT_SETTINGS_TTL_MS, H as HttpError, k as HttpErrorOptions, M as MAX_MONTHS_BACK, m as Refusal, v as backdated, V as backdatingAllowed, w as backdatingDb, X as badRequest, x as clock, Y as conflict, y as createSettings, Z as forbidden, z as isHttpError, _ as notFound, G as now, I as parseBackdate, J as refusalOf, K as runAt } from '../errors-CWT-wc8L.cjs';
1
+ import { o as SettingsDefaults, l as ReadOptions, n as ResolvedSettings, r as SettingsSnapshot, u as WriteOptions, t as SettingsWriteResult, q as SettingsReader, p as SettingsOptions, e as BackdatingOptions, Q as Query, R as Row, P as Page, s as SettingsSource } from '../errors-BsKwZ07u.cjs';
2
+ export { L as BACKDATING_FIELD, N as BACKDATING_INVALID, T as BACKDATING_SETTING, B as BackdatingEntity, U as BackdatingSource, D as DEFAULT_GATEWAY_ENDPOINT, f as DEFAULT_SETTINGS_ERROR_TTL_MS, g as DEFAULT_SETTINGS_TIMEOUT_MS, h as DEFAULT_SETTINGS_TTL_MS, H as HttpError, k as HttpErrorOptions, M as MAX_MONTHS_BACK, m as Refusal, v as backdated, V as backdatingAllowed, w as backdatingDb, X as badRequest, x as clock, Y as conflict, y as createSettings, Z as forbidden, z as isHttpError, _ as notFound, G as now, I as parseBackdate, J as refusalOf, K as runAt } from '../errors-BsKwZ07u.cjs';
3
3
 
4
4
  /**
5
5
  * Who is acting — the person behind a request, as the platform established
@@ -351,9 +351,25 @@ interface CrudOptions {
351
351
  * Column metadata (from the generated ENTITIES) — enables `?col=value` list
352
352
  * filters. A list query key that is neither a column, a reserved key
353
353
  * (`limit`, `offset`, `order`) nor declared in `queryParams` is refused with
354
- * `400 unknown_filter`. Without `columns`, only those keys are accepted.
354
+ * `400 unknown_filter`. Without `columns`, only those keys and the columns
355
+ * the entity's flags add are accepted.
355
356
  */
356
357
  columns?: Record<string, unknown>;
358
+ /**
359
+ * The entity's schema flags. Read from the repo when it is an entity client
360
+ * of this SDK (`db.notes`), as PHP's `Crud::mount` reads them from the
361
+ * `Entity`. A repo of your own — a wrapper that rebuilds the client's methods
362
+ * — carries no descriptor: pass `trash: true` here, or its trash columns are
363
+ * guarded only when `columns` names `trash_batch`. A flag is only ever added:
364
+ * `trash: false` does not switch off what the descriptor says. The columns they add —
365
+ * `trash: true` the trash columns, `external_id: true` `external_id` — filter
366
+ * a list like a declared column, whether or not `columns` names them, unless
367
+ * `queryParams` claims the key. With `trash: true`, `POST` / `PUT` refuse a
368
+ * body that sets a trash column (`400 read_only_field`): only the trash
369
+ * routes write them.
370
+ */
371
+ trash?: boolean;
372
+ external_id?: boolean;
357
373
  /**
358
374
  * Extra list query parameters the App handles itself (e.g. read by
359
375
  * middleware, a wrapping handler, or simply tolerated). The list route
@@ -1,5 +1,5 @@
1
- import { o as SettingsDefaults, l as ReadOptions, n as ResolvedSettings, r as SettingsSnapshot, u as WriteOptions, t as SettingsWriteResult, q as SettingsReader, p as SettingsOptions, e as BackdatingOptions, Q as Query, R as Row, P as Page, s as SettingsSource } from '../errors-CWT-wc8L.js';
2
- export { L as BACKDATING_FIELD, N as BACKDATING_INVALID, T as BACKDATING_SETTING, B as BackdatingEntity, U as BackdatingSource, D as DEFAULT_GATEWAY_ENDPOINT, f as DEFAULT_SETTINGS_ERROR_TTL_MS, g as DEFAULT_SETTINGS_TIMEOUT_MS, h as DEFAULT_SETTINGS_TTL_MS, H as HttpError, k as HttpErrorOptions, M as MAX_MONTHS_BACK, m as Refusal, v as backdated, V as backdatingAllowed, w as backdatingDb, X as badRequest, x as clock, Y as conflict, y as createSettings, Z as forbidden, z as isHttpError, _ as notFound, G as now, I as parseBackdate, J as refusalOf, K as runAt } from '../errors-CWT-wc8L.js';
1
+ import { o as SettingsDefaults, l as ReadOptions, n as ResolvedSettings, r as SettingsSnapshot, u as WriteOptions, t as SettingsWriteResult, q as SettingsReader, p as SettingsOptions, e as BackdatingOptions, Q as Query, R as Row, P as Page, s as SettingsSource } from '../errors-BsKwZ07u.js';
2
+ export { L as BACKDATING_FIELD, N as BACKDATING_INVALID, T as BACKDATING_SETTING, B as BackdatingEntity, U as BackdatingSource, D as DEFAULT_GATEWAY_ENDPOINT, f as DEFAULT_SETTINGS_ERROR_TTL_MS, g as DEFAULT_SETTINGS_TIMEOUT_MS, h as DEFAULT_SETTINGS_TTL_MS, H as HttpError, k as HttpErrorOptions, M as MAX_MONTHS_BACK, m as Refusal, v as backdated, V as backdatingAllowed, w as backdatingDb, X as badRequest, x as clock, Y as conflict, y as createSettings, Z as forbidden, z as isHttpError, _ as notFound, G as now, I as parseBackdate, J as refusalOf, K as runAt } from '../errors-BsKwZ07u.js';
3
3
 
4
4
  /**
5
5
  * Who is acting — the person behind a request, as the platform established
@@ -351,9 +351,25 @@ interface CrudOptions {
351
351
  * Column metadata (from the generated ENTITIES) — enables `?col=value` list
352
352
  * filters. A list query key that is neither a column, a reserved key
353
353
  * (`limit`, `offset`, `order`) nor declared in `queryParams` is refused with
354
- * `400 unknown_filter`. Without `columns`, only those keys are accepted.
354
+ * `400 unknown_filter`. Without `columns`, only those keys and the columns
355
+ * the entity's flags add are accepted.
355
356
  */
356
357
  columns?: Record<string, unknown>;
358
+ /**
359
+ * The entity's schema flags. Read from the repo when it is an entity client
360
+ * of this SDK (`db.notes`), as PHP's `Crud::mount` reads them from the
361
+ * `Entity`. A repo of your own — a wrapper that rebuilds the client's methods
362
+ * — carries no descriptor: pass `trash: true` here, or its trash columns are
363
+ * guarded only when `columns` names `trash_batch`. A flag is only ever added:
364
+ * `trash: false` does not switch off what the descriptor says. The columns they add —
365
+ * `trash: true` the trash columns, `external_id: true` `external_id` — filter
366
+ * a list like a declared column, whether or not `columns` names them, unless
367
+ * `queryParams` claims the key. With `trash: true`, `POST` / `PUT` refuse a
368
+ * body that sets a trash column (`400 read_only_field`): only the trash
369
+ * routes write them.
370
+ */
371
+ trash?: boolean;
372
+ external_id?: boolean;
357
373
  /**
358
374
  * Extra list query parameters the App handles itself (e.g. read by
359
375
  * middleware, a wrapping handler, or simply tolerated). The list route
@@ -10,6 +10,7 @@ import {
10
10
  HttpError,
11
11
  MAX_MONTHS_BACK,
12
12
  OPS,
13
+ assertNoTrashFields,
13
14
  backdated,
14
15
  backdatingAllowed,
15
16
  backdatingDb,
@@ -18,17 +19,22 @@ import {
18
19
  clock,
19
20
  conflict,
20
21
  createSettings,
22
+ describedEntity,
23
+ flagColumns,
21
24
  forbidden,
25
+ guardsTrash,
22
26
  isHttpError,
27
+ isList,
23
28
  notFound,
24
29
  now,
30
+ oneRecordRefusal,
25
31
  parseBackdate,
26
32
  refusalBody,
27
33
  refusalOf,
28
34
  requestedMoment,
29
35
  runAt,
30
36
  updateFields
31
- } from "../chunk-FMHCS3ZH.js";
37
+ } from "../chunk-AVWPY5D2.js";
32
38
 
33
39
  // src/instrument.ts
34
40
  var started = false;
@@ -337,13 +343,11 @@ function createApp(options = {}) {
337
343
  const fields = matched.route.backdating;
338
344
  if (!fields) return await matched.route.handler(c);
339
345
  const at = await requestedMoment(c, fields);
340
- if (at) {
341
- span.setAttribute("revenexx.backdated_at", at);
342
- if (strip) {
343
- const body = { ...c.body };
344
- for (const field of fields) delete body[field];
345
- c.body = body;
346
- }
346
+ if (at) span.setAttribute("revenexx.backdated_at", at);
347
+ if (strip && fields.some((field) => Object.prototype.hasOwnProperty.call(c.body ?? {}, field))) {
348
+ const body = { ...c.body };
349
+ for (const field of fields) delete body[field];
350
+ c.body = body;
347
351
  }
348
352
  const route = matched.route;
349
353
  return await runAt(at, () => route.handler(c));
@@ -396,13 +400,12 @@ function assertJsonFilter(column, value) {
396
400
  );
397
401
  }
398
402
  }
399
- function assertKnownKeys(query, opts) {
400
- const columns = opts.columns ?? {};
403
+ function assertKnownKeys(query, opts, flags) {
401
404
  const extra = opts.queryParams ?? [];
402
- const known = (k) => Object.prototype.hasOwnProperty.call(columns, k) || RESERVED_LIST_QUERY_KEYS.includes(k) || extra.includes(k);
405
+ const known = (k) => isFilterColumn(opts, flags, k) || RESERVED_LIST_QUERY_KEYS.includes(k) || extra.includes(k);
403
406
  const unknown = Object.keys(query).filter((k) => !known(k));
404
407
  if (unknown.length === 0) return;
405
- const allowed = [.../* @__PURE__ */ new Set([...Object.keys(columns), ...RESERVED_LIST_QUERY_KEYS, ...extra])].sort();
408
+ const allowed = [.../* @__PURE__ */ new Set([...Object.keys(opts.columns ?? {}), ...flagColumns(flags), ...RESERVED_LIST_QUERY_KEYS, ...extra])].sort();
406
409
  const quoted = unknown.map((k) => `'${k}'`).join(", ");
407
410
  throw new HttpError(
408
411
  400,
@@ -411,6 +414,10 @@ function assertKnownKeys(query, opts) {
411
414
  { code: "unknown_filter" }
412
415
  );
413
416
  }
417
+ function isFilterColumn(opts, flags, k) {
418
+ if (opts.columns && Object.prototype.hasOwnProperty.call(opts.columns, k)) return true;
419
+ return !(opts.queryParams ?? []).includes(k) && flagColumns(flags).includes(k);
420
+ }
414
421
  var OPERATOR_PREFIX = new RegExp(`^(?:not|${[...OPS].join("|")})\\.`);
415
422
  function assertNoOperator(column, value) {
416
423
  if (!OPERATOR_PREFIX.test(value)) return;
@@ -450,14 +457,19 @@ function mountCrud(app, repo, opts) {
450
457
  const enabled = new Set(opts.only ?? ["list", "get", "create", "update", "delete"]);
451
458
  const defaultLimit = opts.defaultLimit ?? 50;
452
459
  const maxLimit = opts.maxLimit ?? 200;
460
+ const described = describedEntity(repo);
461
+ const flags = {
462
+ trash: guardsTrash(opts.trash, described?.trash, opts.columns),
463
+ external_id: opts.external_id === true || described?.external_id === true
464
+ };
453
465
  if (enabled.has("list")) {
454
466
  app.get(base, async (c) => {
455
- assertKnownKeys(c.query, opts);
467
+ assertKnownKeys(c.query, opts, flags);
456
468
  const where = {};
457
469
  for (const [k, v] of Object.entries(c.query)) {
458
- if (!opts.columns || !Object.prototype.hasOwnProperty.call(opts.columns, k)) continue;
470
+ if (!isFilterColumn(opts, flags, k)) continue;
459
471
  assertNoOperator(k, v);
460
- if (isJsonColumn(opts.columns[k])) assertJsonFilter(k, v);
472
+ if (isJsonColumn(opts.columns?.[k])) assertJsonFilter(k, v);
461
473
  where[k] = v;
462
474
  }
463
475
  await assertParentExists(c, opts.parent);
@@ -480,6 +492,8 @@ function mountCrud(app, repo, opts) {
480
492
  }
481
493
  if (enabled.has("create")) {
482
494
  app.post(base, async (c) => {
495
+ if (isList(c.body)) throw oneRecordRefusal();
496
+ assertNoTrashFields(flags, c.body);
483
497
  await assertParentExists(c, opts.parent);
484
498
  const data = { ...c.body, ...parentWhere(c, opts.parent) };
485
499
  return c.json(await repo.create(data), 201);
@@ -493,6 +507,8 @@ function mountCrud(app, repo, opts) {
493
507
  }
494
508
  if (enabled.has("update")) {
495
509
  app.put(idTemplate, async (c) => {
510
+ if (isList(c.body)) throw oneRecordRefusal();
511
+ assertNoTrashFields(flags, c.body);
496
512
  const { id } = await fetchOwned(repo, c, opts);
497
513
  if (updateFields(c.body).length === 0) throw new HttpError(400, EMPTY_UPDATE_MESSAGE, void 0, { code: "empty_update" });
498
514
  const patch = { ...c.body, ...parentWhere(c, opts.parent) };