@revenexx/app-sdk 1.3.0 → 1.5.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, 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';
3
3
 
4
4
  /**
5
5
  * Who is acting — the person behind a request, as the platform established
@@ -218,8 +218,22 @@ interface RouteContext<D extends SettingsDefaults = SettingsDefaults> {
218
218
  * app; pass `settings` to `createApp` to declare defaults and get them typed.
219
219
  */
220
220
  settings(options?: ReadOptions): Promise<ResolvedSettings<D>>;
221
- /** The same read, plus market/masked/provenance. */
221
+ /** The same read, plus market/masked/stored/provenance. */
222
222
  settingsSnapshot(options?: ReadOptions): Promise<SettingsSnapshot<D>>;
223
+ /**
224
+ * Store this app's OWN tenant-level settings for the request's tenant —
225
+ * `{ ifAbsent: true }` for defaults, so a merchant's choice is never
226
+ * overwritten. Never throws; see `SettingsReader.set`.
227
+ */
228
+ setSettings(values: Record<string, unknown>, options?: WriteOptions): Promise<SettingsWriteResult>;
229
+ /**
230
+ * The request's clock as an ISO string — the backdated moment while a
231
+ * backdated request runs, the wall clock otherwise (see `createApp`'s
232
+ * `backdating`). Same as the exported `now()`.
233
+ */
234
+ now(): string;
235
+ /** The request's clock as a Date. Same as the exported `clock()`. */
236
+ clock(): Date;
223
237
  }
224
238
  type Handler<D extends SettingsDefaults = SettingsDefaults> = (c: RouteContext<D>) => unknown | Promise<unknown>;
225
239
 
@@ -248,6 +262,16 @@ interface AppOptions<D extends SettingsDefaults = SettingsDefaults> {
248
262
  settings?: Omit<SettingsOptions<D>, 'app'> & {
249
263
  app?: string;
250
264
  } | SettingsReader<D>;
265
+ /**
266
+ * Let a test tenant backdate requests (RAD-311). Off by default. `true`: every
267
+ * POST/PUT/PATCH/DELETE route accepts a past moment as `at` in its body and
268
+ * runs on that clock (`c.now()`, `now()`, `clock()`). The object form adds
269
+ * per-route field aliases (`routes`), routes never backdated (`exclude`) and
270
+ * `strip`. Honoured only when the tenant header matches the identity token's
271
+ * `tenant_id` and the tenant's `allow_backdating` setting is on; otherwise
272
+ * the field is ignored. Stamp created rows with `backdatingDb`.
273
+ */
274
+ backdating?: boolean | BackdatingOptions;
251
275
  }
252
276
  interface App<D extends SettingsDefaults = SettingsDefaults> {
253
277
  get(template: string, handler: Handler<D>): App<D>;
@@ -586,4 +610,4 @@ declare function createGatewayCounter(options?: {
586
610
  }) => Promise<Blocker | null>;
587
611
  };
588
612
 
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 };
613
+ 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, SettingsWriteResult, TRASH_COLUMN, type TrashCaller, type TrashChild, type TrashHandle, type TrashOptions, WriteOptions, 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, 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';
3
3
 
4
4
  /**
5
5
  * Who is acting — the person behind a request, as the platform established
@@ -218,8 +218,22 @@ interface RouteContext<D extends SettingsDefaults = SettingsDefaults> {
218
218
  * app; pass `settings` to `createApp` to declare defaults and get them typed.
219
219
  */
220
220
  settings(options?: ReadOptions): Promise<ResolvedSettings<D>>;
221
- /** The same read, plus market/masked/provenance. */
221
+ /** The same read, plus market/masked/stored/provenance. */
222
222
  settingsSnapshot(options?: ReadOptions): Promise<SettingsSnapshot<D>>;
223
+ /**
224
+ * Store this app's OWN tenant-level settings for the request's tenant —
225
+ * `{ ifAbsent: true }` for defaults, so a merchant's choice is never
226
+ * overwritten. Never throws; see `SettingsReader.set`.
227
+ */
228
+ setSettings(values: Record<string, unknown>, options?: WriteOptions): Promise<SettingsWriteResult>;
229
+ /**
230
+ * The request's clock as an ISO string — the backdated moment while a
231
+ * backdated request runs, the wall clock otherwise (see `createApp`'s
232
+ * `backdating`). Same as the exported `now()`.
233
+ */
234
+ now(): string;
235
+ /** The request's clock as a Date. Same as the exported `clock()`. */
236
+ clock(): Date;
223
237
  }
224
238
  type Handler<D extends SettingsDefaults = SettingsDefaults> = (c: RouteContext<D>) => unknown | Promise<unknown>;
225
239
 
@@ -248,6 +262,16 @@ interface AppOptions<D extends SettingsDefaults = SettingsDefaults> {
248
262
  settings?: Omit<SettingsOptions<D>, 'app'> & {
249
263
  app?: string;
250
264
  } | SettingsReader<D>;
265
+ /**
266
+ * Let a test tenant backdate requests (RAD-311). Off by default. `true`: every
267
+ * POST/PUT/PATCH/DELETE route accepts a past moment as `at` in its body and
268
+ * runs on that clock (`c.now()`, `now()`, `clock()`). The object form adds
269
+ * per-route field aliases (`routes`), routes never backdated (`exclude`) and
270
+ * `strip`. Honoured only when the tenant header matches the identity token's
271
+ * `tenant_id` and the tenant's `allow_backdating` setting is on; otherwise
272
+ * the field is ignored. Stamp created rows with `backdatingDb`.
273
+ */
274
+ backdating?: boolean | BackdatingOptions;
251
275
  }
252
276
  interface App<D extends SettingsDefaults = SettingsDefaults> {
253
277
  get(template: string, handler: Handler<D>): App<D>;
@@ -586,4 +610,4 @@ declare function createGatewayCounter(options?: {
586
610
  }) => Promise<Blocker | null>;
587
611
  };
588
612
 
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 };
613
+ 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, SettingsWriteResult, TRASH_COLUMN, type TrashCaller, type TrashChild, type TrashHandle, type TrashOptions, WriteOptions, 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-PXQA7E4F.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,24 @@ 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
+ setSettings: (values, writeOptions) => settings.set(c, values, writeOptions),
318
+ now,
319
+ clock
302
320
  };
303
- return await matched.route.handler(c);
321
+ const fields = matched.route.backdating;
322
+ if (!fields) return await matched.route.handler(c);
323
+ const at = await requestedMoment(c, fields);
324
+ if (at) {
325
+ span.setAttribute("revenexx.backdated_at", at);
326
+ if (strip) {
327
+ const body = { ...c.body };
328
+ for (const field of fields) delete body[field];
329
+ c.body = body;
330
+ }
331
+ }
332
+ const route = matched.route;
333
+ return await runAt(at, () => route.handler(c));
304
334
  } catch (err) {
305
335
  if (isHttpError(err)) {
306
336
  span.setAttribute("http.response.status_code", err.status);
@@ -534,7 +564,7 @@ function mountTrash(app, repo, opts) {
534
564
  const base = opts.path.replace(/\/+$/, "");
535
565
  const idParam = opts.idParam ?? "id";
536
566
  const children = opts.children ?? [];
537
- const now = opts.now ?? (() => /* @__PURE__ */ new Date());
567
+ const now2 = opts.now ?? (() => /* @__PURE__ */ new Date());
538
568
  const reasonColumn = opts.purgeReasonColumn === false ? null : opts.purgeReasonColumn ?? PURGE_REASON_COLUMN;
539
569
  const load = async (id) => {
540
570
  if (!id) throw refuse(400, `missing path parameter '${idParam}'`, "missing_parameter");
@@ -555,7 +585,7 @@ function mountTrash(app, repo, opts) {
555
585
  }
556
586
  const batch = randomUUID();
557
587
  const fields = {
558
- [TRASH_COLUMN]: now().toISOString(),
588
+ [TRASH_COLUMN]: now2().toISOString(),
559
589
  deleted_by: caller.actor?.email ?? caller.actor?.user ?? null,
560
590
  deleted_by_id: caller.actor?.user ?? caller.actor?.contact ?? null,
561
591
  trash_batch: batch
@@ -628,7 +658,7 @@ function mountTrash(app, repo, opts) {
628
658
  } catch (err) {
629
659
  return { skipped: true, reason: err instanceof Error ? err.message : String(err), purged: [], blocked: [] };
630
660
  }
631
- const cutoff = new Date(now().getTime() - retentionDays * 864e5).toISOString();
661
+ const cutoff = new Date(now2().getTime() - retentionDays * 864e5).toISOString();
632
662
  const max = options.max ?? 500;
633
663
  const due = (await listAll(repo, { [TRASH_COLUMN]: { op: "lt", value: cutoff } })).slice(0, max);
634
664
  const result = { skipped: false, retentionDays, cutoff, purged: [], blocked: [] };
@@ -712,6 +742,9 @@ function createGatewayCounter(options = {}) {
712
742
  export {
713
743
  ACTOR_HEADERS,
714
744
  AUDIT_SETTINGS_PATH,
745
+ BACKDATING_FIELD,
746
+ BACKDATING_INVALID,
747
+ BACKDATING_SETTING,
715
748
  DEFAULT_GATEWAY_ENDPOINT,
716
749
  DEFAULT_SETTINGS_ERROR_TTL_MS,
717
750
  DEFAULT_SETTINGS_TIMEOUT_MS,
@@ -719,12 +752,17 @@ export {
719
752
  DEFAULT_TRASH_RETENTION_DAYS,
720
753
  HttpError,
721
754
  MARKET_HEADERS,
755
+ MAX_MONTHS_BACK,
722
756
  PURGE_REASON_COLUMN,
723
757
  RESERVED_LIST_QUERY_KEYS,
724
758
  TRASH_COLUMN,
725
759
  actorFrom,
726
760
  auditSettingsUrl,
761
+ backdated,
762
+ backdatingAllowed,
763
+ backdatingDb,
727
764
  badRequest,
765
+ clock,
728
766
  conflict,
729
767
  createApp,
730
768
  createGatewayCounter,
@@ -735,7 +773,10 @@ export {
735
773
  mountCrud,
736
774
  mountTrash,
737
775
  notFound,
776
+ now,
777
+ parseBackdate,
738
778
  readTrashRetention,
739
- refusalOf
779
+ refusalOf,
780
+ runAt
740
781
  };
741
782
  //# sourceMappingURL=index.js.map