@revenexx/app-sdk 1.3.0 → 1.4.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 { n as SettingsDefaults, k as ReadOptions, m as ResolvedSettings, q as SettingsSnapshot, p as SettingsReader, o as SettingsOptions, Q as Query, R as Row, P as Page, r as SettingsSource } from '../errors-CtrtCOoG.cjs';
2
- export { D as DEFAULT_GATEWAY_ENDPOINT, e as DEFAULT_SETTINGS_ERROR_TTL_MS, f as DEFAULT_SETTINGS_TIMEOUT_MS, g as DEFAULT_SETTINGS_TTL_MS, H as HttpError, j as HttpErrorOptions, l as Refusal, v as badRequest, w as conflict, s as createSettings, x as forbidden, t as isHttpError, y as notFound, u as refusalOf } from '../errors-CtrtCOoG.cjs';
1
+ import { o as SettingsDefaults, l as ReadOptions, n as ResolvedSettings, r as SettingsSnapshot, q as SettingsReader, p as SettingsOptions, e as BackdatingOptions, Q as Query, R as Row, P as Page, s as SettingsSource } from '../errors-DERK5sWS.cjs';
2
+ export { J as BACKDATING_FIELD, K as BACKDATING_INVALID, L as BACKDATING_SETTING, B as BackdatingEntity, N 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, t as backdated, T as backdatingAllowed, u as backdatingDb, U as badRequest, v as clock, V as conflict, w as createSettings, X as forbidden, x as isHttpError, Y as notFound, y as now, z as parseBackdate, G as refusalOf, I as runAt } from '../errors-DERK5sWS.cjs';
3
3
 
4
4
  /**
5
5
  * Who is acting — the person behind a request, as the platform established
@@ -220,6 +220,14 @@ interface RouteContext<D extends SettingsDefaults = SettingsDefaults> {
220
220
  settings(options?: ReadOptions): Promise<ResolvedSettings<D>>;
221
221
  /** The same read, plus market/masked/provenance. */
222
222
  settingsSnapshot(options?: ReadOptions): Promise<SettingsSnapshot<D>>;
223
+ /**
224
+ * The request's clock as an ISO string — the backdated moment while a
225
+ * backdated request runs, the wall clock otherwise (see `createApp`'s
226
+ * `backdating`). Same as the exported `now()`.
227
+ */
228
+ now(): string;
229
+ /** The request's clock as a Date. Same as the exported `clock()`. */
230
+ clock(): Date;
223
231
  }
224
232
  type Handler<D extends SettingsDefaults = SettingsDefaults> = (c: RouteContext<D>) => unknown | Promise<unknown>;
225
233
 
@@ -248,6 +256,16 @@ interface AppOptions<D extends SettingsDefaults = SettingsDefaults> {
248
256
  settings?: Omit<SettingsOptions<D>, 'app'> & {
249
257
  app?: string;
250
258
  } | SettingsReader<D>;
259
+ /**
260
+ * Let a test tenant backdate requests (RAD-311). Off by default. `true`: every
261
+ * POST/PUT/PATCH/DELETE route accepts a past moment as `at` in its body and
262
+ * runs on that clock (`c.now()`, `now()`, `clock()`). The object form adds
263
+ * per-route field aliases (`routes`), routes never backdated (`exclude`) and
264
+ * `strip`. Honoured only when the tenant header matches the identity token's
265
+ * `tenant_id` and the tenant's `allow_backdating` setting is on; otherwise
266
+ * the field is ignored. Stamp created rows with `backdatingDb`.
267
+ */
268
+ backdating?: boolean | BackdatingOptions;
251
269
  }
252
270
  interface App<D extends SettingsDefaults = SettingsDefaults> {
253
271
  get(template: string, handler: Handler<D>): App<D>;
@@ -586,4 +604,4 @@ declare function createGatewayCounter(options?: {
586
604
  }) => Promise<Blocker | null>;
587
605
  };
588
606
 
589
- export { ACTOR_HEADERS, AUDIT_SETTINGS_PATH, type Actor, type ActorPlane, type ActorTrust, type App, type AppOptions, type Blocker, type BlockerCheck, type CrudOptions, DEFAULT_TRASH_RETENTION_DAYS, type FnContext, type FnRequest, type FnResponse, type Handler, MARKET_HEADERS, type Market, PURGE_REASON_COLUMN, type PurgeResult, RESERVED_LIST_QUERY_KEYS, ReadOptions, type RepoLike, ResolvedSettings, type RetentionOptions, type RouteContext, SettingsDefaults, SettingsOptions, SettingsReader, SettingsSnapshot, SettingsSource, TRASH_COLUMN, type TrashCaller, type TrashChild, type TrashHandle, type TrashOptions, actorFrom, auditSettingsUrl, createApp, createGatewayCounter, marketFrom, mountCrud, mountTrash, readTrashRetention };
607
+ export { ACTOR_HEADERS, AUDIT_SETTINGS_PATH, type Actor, type ActorPlane, type ActorTrust, type App, type AppOptions, BackdatingOptions, type Blocker, type BlockerCheck, type CrudOptions, DEFAULT_TRASH_RETENTION_DAYS, type FnContext, type FnRequest, type FnResponse, type Handler, MARKET_HEADERS, type Market, PURGE_REASON_COLUMN, type PurgeResult, RESERVED_LIST_QUERY_KEYS, ReadOptions, type RepoLike, ResolvedSettings, type RetentionOptions, type RouteContext, SettingsDefaults, SettingsOptions, SettingsReader, SettingsSnapshot, SettingsSource, TRASH_COLUMN, type TrashCaller, type TrashChild, type TrashHandle, type TrashOptions, actorFrom, auditSettingsUrl, createApp, createGatewayCounter, marketFrom, mountCrud, mountTrash, readTrashRetention };
@@ -1,5 +1,5 @@
1
- import { n as SettingsDefaults, k as ReadOptions, m as ResolvedSettings, q as SettingsSnapshot, p as SettingsReader, o as SettingsOptions, Q as Query, R as Row, P as Page, r as SettingsSource } from '../errors-CtrtCOoG.js';
2
- export { D as DEFAULT_GATEWAY_ENDPOINT, e as DEFAULT_SETTINGS_ERROR_TTL_MS, f as DEFAULT_SETTINGS_TIMEOUT_MS, g as DEFAULT_SETTINGS_TTL_MS, H as HttpError, j as HttpErrorOptions, l as Refusal, v as badRequest, w as conflict, s as createSettings, x as forbidden, t as isHttpError, y as notFound, u as refusalOf } from '../errors-CtrtCOoG.js';
1
+ import { o as SettingsDefaults, l as ReadOptions, n as ResolvedSettings, r as SettingsSnapshot, q as SettingsReader, p as SettingsOptions, e as BackdatingOptions, Q as Query, R as Row, P as Page, s as SettingsSource } from '../errors-DERK5sWS.js';
2
+ export { J as BACKDATING_FIELD, K as BACKDATING_INVALID, L as BACKDATING_SETTING, B as BackdatingEntity, N 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, t as backdated, T as backdatingAllowed, u as backdatingDb, U as badRequest, v as clock, V as conflict, w as createSettings, X as forbidden, x as isHttpError, Y as notFound, y as now, z as parseBackdate, G as refusalOf, I as runAt } from '../errors-DERK5sWS.js';
3
3
 
4
4
  /**
5
5
  * Who is acting — the person behind a request, as the platform established
@@ -220,6 +220,14 @@ interface RouteContext<D extends SettingsDefaults = SettingsDefaults> {
220
220
  settings(options?: ReadOptions): Promise<ResolvedSettings<D>>;
221
221
  /** The same read, plus market/masked/provenance. */
222
222
  settingsSnapshot(options?: ReadOptions): Promise<SettingsSnapshot<D>>;
223
+ /**
224
+ * The request's clock as an ISO string — the backdated moment while a
225
+ * backdated request runs, the wall clock otherwise (see `createApp`'s
226
+ * `backdating`). Same as the exported `now()`.
227
+ */
228
+ now(): string;
229
+ /** The request's clock as a Date. Same as the exported `clock()`. */
230
+ clock(): Date;
223
231
  }
224
232
  type Handler<D extends SettingsDefaults = SettingsDefaults> = (c: RouteContext<D>) => unknown | Promise<unknown>;
225
233
 
@@ -248,6 +256,16 @@ interface AppOptions<D extends SettingsDefaults = SettingsDefaults> {
248
256
  settings?: Omit<SettingsOptions<D>, 'app'> & {
249
257
  app?: string;
250
258
  } | SettingsReader<D>;
259
+ /**
260
+ * Let a test tenant backdate requests (RAD-311). Off by default. `true`: every
261
+ * POST/PUT/PATCH/DELETE route accepts a past moment as `at` in its body and
262
+ * runs on that clock (`c.now()`, `now()`, `clock()`). The object form adds
263
+ * per-route field aliases (`routes`), routes never backdated (`exclude`) and
264
+ * `strip`. Honoured only when the tenant header matches the identity token's
265
+ * `tenant_id` and the tenant's `allow_backdating` setting is on; otherwise
266
+ * the field is ignored. Stamp created rows with `backdatingDb`.
267
+ */
268
+ backdating?: boolean | BackdatingOptions;
251
269
  }
252
270
  interface App<D extends SettingsDefaults = SettingsDefaults> {
253
271
  get(template: string, handler: Handler<D>): App<D>;
@@ -586,4 +604,4 @@ declare function createGatewayCounter(options?: {
586
604
  }) => Promise<Blocker | null>;
587
605
  };
588
606
 
589
- export { ACTOR_HEADERS, AUDIT_SETTINGS_PATH, type Actor, type ActorPlane, type ActorTrust, type App, type AppOptions, type Blocker, type BlockerCheck, type CrudOptions, DEFAULT_TRASH_RETENTION_DAYS, type FnContext, type FnRequest, type FnResponse, type Handler, MARKET_HEADERS, type Market, PURGE_REASON_COLUMN, type PurgeResult, RESERVED_LIST_QUERY_KEYS, ReadOptions, type RepoLike, ResolvedSettings, type RetentionOptions, type RouteContext, SettingsDefaults, SettingsOptions, SettingsReader, SettingsSnapshot, SettingsSource, TRASH_COLUMN, type TrashCaller, type TrashChild, type TrashHandle, type TrashOptions, actorFrom, auditSettingsUrl, createApp, createGatewayCounter, marketFrom, mountCrud, mountTrash, readTrashRetention };
607
+ export { ACTOR_HEADERS, AUDIT_SETTINGS_PATH, type Actor, type ActorPlane, type ActorTrust, type App, type AppOptions, BackdatingOptions, type Blocker, type BlockerCheck, type CrudOptions, DEFAULT_TRASH_RETENTION_DAYS, type FnContext, type FnRequest, type FnResponse, type Handler, MARKET_HEADERS, type Market, PURGE_REASON_COLUMN, type PurgeResult, RESERVED_LIST_QUERY_KEYS, ReadOptions, type RepoLike, ResolvedSettings, type RetentionOptions, type RouteContext, SettingsDefaults, SettingsOptions, SettingsReader, SettingsSnapshot, SettingsSource, TRASH_COLUMN, type TrashCaller, type TrashChild, type TrashHandle, type TrashOptions, actorFrom, auditSettingsUrl, createApp, createGatewayCounter, marketFrom, mountCrud, mountTrash, readTrashRetention };
@@ -1,21 +1,34 @@
1
1
  import {
2
+ BACKDATING_FIELD,
3
+ BACKDATING_INVALID,
4
+ BACKDATING_SETTING,
2
5
  DEFAULT_GATEWAY_ENDPOINT,
3
6
  DEFAULT_SETTINGS_ERROR_TTL_MS,
4
7
  DEFAULT_SETTINGS_TIMEOUT_MS,
5
8
  DEFAULT_SETTINGS_TTL_MS,
6
9
  EMPTY_UPDATE_MESSAGE,
7
10
  HttpError,
11
+ MAX_MONTHS_BACK,
8
12
  OPS,
13
+ backdated,
14
+ backdatingAllowed,
15
+ backdatingDb,
16
+ backdatingFields,
9
17
  badRequest,
18
+ clock,
10
19
  conflict,
11
20
  createSettings,
12
21
  forbidden,
13
22
  isHttpError,
14
23
  notFound,
24
+ now,
25
+ parseBackdate,
15
26
  refusalBody,
16
27
  refusalOf,
28
+ requestedMoment,
29
+ runAt,
17
30
  updateFields
18
- } from "../chunk-P6ZEOMKS.js";
31
+ } from "../chunk-D6QST6NV.js";
19
32
 
20
33
  // src/instrument.ts
21
34
  var started = false;
@@ -195,6 +208,7 @@ var headerGetter = {
195
208
  function createApp(options = {}) {
196
209
  const routes = [];
197
210
  const health = options.health !== false;
211
+ const strip = typeof options.backdating === "object" && options.backdating.strip === true;
198
212
  const settings = options.settings && isSettingsReader(options.settings) ? options.settings : createSettings({ app: options.name, ...options.settings ?? {} });
199
213
  const app = {
200
214
  settings,
@@ -210,7 +224,8 @@ function createApp(options = {}) {
210
224
  template,
211
225
  segments,
212
226
  literals: segments.filter((s) => s.lit !== void 0).length,
213
- handler
227
+ handler,
228
+ backdating: backdatingFields(options.backdating, method, template)
214
229
  });
215
230
  routes.sort((a, b) => b.literals - a.literals);
216
231
  return app;
@@ -254,12 +269,12 @@ function createApp(options = {}) {
254
269
  const reqSegs = splitPath(path);
255
270
  let matched = null;
256
271
  let pathExists = false;
257
- for (const route of routes) {
258
- const params = matchSegments(route.segments, reqSegs);
272
+ for (const route2 of routes) {
273
+ const params = matchSegments(route2.segments, reqSegs);
259
274
  if (!params) continue;
260
275
  pathExists = true;
261
- if (route.method === method) {
262
- matched = { route, params };
276
+ if (route2.method === method) {
277
+ matched = { route: route2, params };
263
278
  break;
264
279
  }
265
280
  }
@@ -298,9 +313,23 @@ function createApp(options = {}) {
298
313
  // The market rides on the request, so a market-scoped setting
299
314
  // resolves for the market this call is for — no argument needed.
300
315
  settings: (readOptions) => settings.get(c, readOptions),
301
- settingsSnapshot: (readOptions) => settings.load(c, readOptions)
316
+ settingsSnapshot: (readOptions) => settings.load(c, readOptions),
317
+ now,
318
+ clock
302
319
  };
303
- return await matched.route.handler(c);
320
+ const fields = matched.route.backdating;
321
+ if (!fields) return await matched.route.handler(c);
322
+ const at = await requestedMoment(c, fields);
323
+ if (at) {
324
+ span.setAttribute("revenexx.backdated_at", at);
325
+ if (strip) {
326
+ const body = { ...c.body };
327
+ for (const field of fields) delete body[field];
328
+ c.body = body;
329
+ }
330
+ }
331
+ const route = matched.route;
332
+ return await runAt(at, () => route.handler(c));
304
333
  } catch (err) {
305
334
  if (isHttpError(err)) {
306
335
  span.setAttribute("http.response.status_code", err.status);
@@ -534,7 +563,7 @@ function mountTrash(app, repo, opts) {
534
563
  const base = opts.path.replace(/\/+$/, "");
535
564
  const idParam = opts.idParam ?? "id";
536
565
  const children = opts.children ?? [];
537
- const now = opts.now ?? (() => /* @__PURE__ */ new Date());
566
+ const now2 = opts.now ?? (() => /* @__PURE__ */ new Date());
538
567
  const reasonColumn = opts.purgeReasonColumn === false ? null : opts.purgeReasonColumn ?? PURGE_REASON_COLUMN;
539
568
  const load = async (id) => {
540
569
  if (!id) throw refuse(400, `missing path parameter '${idParam}'`, "missing_parameter");
@@ -555,7 +584,7 @@ function mountTrash(app, repo, opts) {
555
584
  }
556
585
  const batch = randomUUID();
557
586
  const fields = {
558
- [TRASH_COLUMN]: now().toISOString(),
587
+ [TRASH_COLUMN]: now2().toISOString(),
559
588
  deleted_by: caller.actor?.email ?? caller.actor?.user ?? null,
560
589
  deleted_by_id: caller.actor?.user ?? caller.actor?.contact ?? null,
561
590
  trash_batch: batch
@@ -628,7 +657,7 @@ function mountTrash(app, repo, opts) {
628
657
  } catch (err) {
629
658
  return { skipped: true, reason: err instanceof Error ? err.message : String(err), purged: [], blocked: [] };
630
659
  }
631
- const cutoff = new Date(now().getTime() - retentionDays * 864e5).toISOString();
660
+ const cutoff = new Date(now2().getTime() - retentionDays * 864e5).toISOString();
632
661
  const max = options.max ?? 500;
633
662
  const due = (await listAll(repo, { [TRASH_COLUMN]: { op: "lt", value: cutoff } })).slice(0, max);
634
663
  const result = { skipped: false, retentionDays, cutoff, purged: [], blocked: [] };
@@ -712,6 +741,9 @@ function createGatewayCounter(options = {}) {
712
741
  export {
713
742
  ACTOR_HEADERS,
714
743
  AUDIT_SETTINGS_PATH,
744
+ BACKDATING_FIELD,
745
+ BACKDATING_INVALID,
746
+ BACKDATING_SETTING,
715
747
  DEFAULT_GATEWAY_ENDPOINT,
716
748
  DEFAULT_SETTINGS_ERROR_TTL_MS,
717
749
  DEFAULT_SETTINGS_TIMEOUT_MS,
@@ -719,12 +751,17 @@ export {
719
751
  DEFAULT_TRASH_RETENTION_DAYS,
720
752
  HttpError,
721
753
  MARKET_HEADERS,
754
+ MAX_MONTHS_BACK,
722
755
  PURGE_REASON_COLUMN,
723
756
  RESERVED_LIST_QUERY_KEYS,
724
757
  TRASH_COLUMN,
725
758
  actorFrom,
726
759
  auditSettingsUrl,
760
+ backdated,
761
+ backdatingAllowed,
762
+ backdatingDb,
727
763
  badRequest,
764
+ clock,
728
765
  conflict,
729
766
  createApp,
730
767
  createGatewayCounter,
@@ -735,7 +772,10 @@ export {
735
772
  mountCrud,
736
773
  mountTrash,
737
774
  notFound,
775
+ now,
776
+ parseBackdate,
738
777
  readTrashRetention,
739
- refusalOf
778
+ refusalOf,
779
+ runAt
740
780
  };
741
781
  //# sourceMappingURL=index.js.map