@classytic/primitives 0.20.0 → 0.22.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.
Files changed (51) hide show
  1. package/CHANGELOG.md +934 -906
  2. package/dist/approval.d.mts +3 -3
  3. package/dist/bank-transaction.d.mts +1 -2
  4. package/dist/{brand-LLXqoh5J.d.mts → brand-Lj_75Y_r.d.mts} +6 -6
  5. package/dist/brand.d.mts +1 -1
  6. package/dist/cadence.d.mts +1 -1
  7. package/dist/cadence.mjs +2 -2
  8. package/dist/calendar.d.mts +1 -1
  9. package/dist/calendar.mjs +2 -2
  10. package/dist/condition.mjs +2 -2
  11. package/dist/context.d.mts +0 -1
  12. package/dist/currency.d.mts +1 -1
  13. package/dist/environment.d.mts +90 -0
  14. package/dist/environment.mjs +53 -0
  15. package/dist/event-infra.d.mts +18 -1
  16. package/dist/idempotency.d.mts +11 -1
  17. package/dist/identity-verification.d.mts +0 -1
  18. package/dist/memory-outbox.d.mts +1 -2
  19. package/dist/memory-outbox.mjs +0 -8
  20. package/dist/mixin.d.mts +1 -1
  21. package/dist/monetization.d.mts +1 -2
  22. package/dist/{money-DPo9fLBi.d.mts → money-CfAvfNiM.d.mts} +1 -2
  23. package/dist/money.d.mts +1 -1
  24. package/dist/otp.mjs +1 -1
  25. package/dist/outbox.d.mts +0 -1
  26. package/dist/payment-allocation-status.d.mts +10 -4
  27. package/dist/payment-allocation-status.mjs +14 -0
  28. package/dist/payment-events.d.mts +2 -3
  29. package/dist/payment-events.mjs +12 -0
  30. package/dist/payment-gateway.d.mts +3 -3
  31. package/dist/payment-method-kind.d.mts +23 -11
  32. package/dist/payment-method-kind.mjs +17 -0
  33. package/dist/period.d.mts +39 -4
  34. package/dist/period.mjs +50 -1
  35. package/dist/person-parse.d.mts +0 -1
  36. package/dist/phone.d.mts +0 -1
  37. package/dist/phone.mjs +3 -3
  38. package/dist/proration.d.mts +1 -2
  39. package/dist/retention.mjs +3 -3
  40. package/dist/shelf-life.mjs +1 -1
  41. package/dist/sla-policy.d.mts +1 -2
  42. package/dist/state-diagram.d.mts +0 -1
  43. package/dist/state-diagram.mjs +1 -1
  44. package/dist/suspension.d.mts +6 -4
  45. package/dist/suspension.mjs +3 -2
  46. package/dist/{timezone-tlG7ygwx.d.mts → timezone-CxUjxmRU.d.mts} +3 -4
  47. package/dist/timezone.d.mts +1 -1
  48. package/dist/unit-cost-rate.d.mts +1 -2
  49. package/package.json +7 -2
  50. /package/dist/{cadence-BFQVC1Qp.d.mts → cadence-5X6TiD49.d.mts} +0 -0
  51. /package/dist/{calendar-B6fm-FbR.d.mts → calendar-DJWSbcgE.d.mts} +0 -0
package/dist/period.d.mts CHANGED
@@ -1,5 +1,4 @@
1
- import { t as CivilDate } from "./timezone-tlG7ygwx.mjs";
2
-
1
+ import { t as CivilDate } from "./timezone-CxUjxmRU.mjs";
3
2
  //#region src/scheduling/period.d.ts
4
3
  /**
5
4
  * A date range `[start, end]`.
@@ -55,7 +54,19 @@ type PeriodGranularity = 'year' | 'quarter' | 'month' | 'custom';
55
54
  declare class DateRangeError extends Error {
56
55
  readonly name = "DateRangeError";
57
56
  }
58
- type PeriodErrorCode = /** Two resolution shapes at once (`month` + `quarter`, or either + `start`/`end`). */'AMBIGUOUS_PERIOD' /** `start` without `end`, or vice versa. */ | 'INCOMPLETE_RANGE' /** `year` / `month` / `quarter` outside its legal domain, or non-integer. */ | 'INVALID_FIELD' /** No zone on the period and none supplied by the caller. */ | 'MISSING_TIMEZONE' /** The period's own zone and the caller's zone disagree. */ | 'TIMEZONE_CONFLICT' /** A `'YYYY'` / `'YYYY-MM'` / `'YYYY-Qn'` label that could not be read. */ | 'INVALID_LABEL';
57
+ type PeriodErrorCode =
58
+ /** Two resolution shapes at once (`month` + `quarter`, or either + `start`/`end`). */
59
+ 'AMBIGUOUS_PERIOD' |
60
+ /** `start` without `end`, or vice versa. */
61
+ 'INCOMPLETE_RANGE' |
62
+ /** `year` / `month` / `quarter` outside its legal domain, or non-integer. */
63
+ 'INVALID_FIELD' |
64
+ /** No zone on the period and none supplied by the caller. */
65
+ 'MISSING_TIMEZONE' |
66
+ /** The period's own zone and the caller's zone disagree. */
67
+ 'TIMEZONE_CONFLICT' |
68
+ /** A `'YYYY'` / `'YYYY-MM'` / `'YYYY-Qn'` label that could not be read. */
69
+ 'INVALID_LABEL';
59
70
  declare class PeriodError extends Error {
60
71
  readonly name = "PeriodError";
61
72
  readonly code: PeriodErrorCode;
@@ -149,6 +160,30 @@ declare function resolvePeriod(period: Period, timezone?: string): DateRange;
149
160
  * For the `$lte` call sites that predate half-open ranges. Derived from the
150
161
  * resolved end rather than `start + 24h - 1`, so it stays DST-exact.
151
162
  */
163
+ /**
164
+ * Shift a MONTH or QUARTER period by `n` of its own units (negative = back).
165
+ *
166
+ * The arithmetic a caller would otherwise write inline, and the reason this is
167
+ * here rather than in each of them: "two filing months ago" is a `Period`
168
+ * question, and the shapes reachable without it are both wrong in the same way.
169
+ *
170
+ * `new Date(Date.UTC(y, m - 1 - n, 1))` — a UTC instant used to answer a
171
+ * BUSINESS-calendar question. Six hours off in Asia/Dhaka at both ends; the
172
+ * five silent defects in this module's header are all that shape.
173
+ *
174
+ * hand-rolled integer math (`y * 12 + (m - 1) - n`) — correct, but it is a
175
+ * third copy of a rule the `Period` type already owns, and the year rollover
176
+ * is exactly where a copy goes wrong.
177
+ *
178
+ * NO Date is constructed and no zone is consulted: shifting a month is pure
179
+ * calendar arithmetic on `(year, month)`. The `timezone` rides along unchanged,
180
+ * so the shifted period still resolves against the calendar it was defined in —
181
+ * `resolvePeriod` / {@link resolveMonth} remain the only places instants appear.
182
+ *
183
+ * A YEAR period shifts by years; a `custom` (ad-hoc start/end) period has no
184
+ * unit to shift and throws rather than guessing one.
185
+ */
186
+ declare function shiftPeriod(period: Period, n: number): Period;
152
187
  declare function inclusiveEnd(range: DateRange): Date;
153
188
  /**
154
189
  * The period an INSTANT falls in, on the local calendar.
@@ -174,4 +209,4 @@ declare function parsePeriod(label: string, timezone?: string): Period;
174
209
  */
175
210
  declare function formatPeriod(period: Period): string;
176
211
  //#endregion
177
- export { DateRange, DateRangeError, Period, PeriodError, PeriodErrorCode, PeriodGranularity, dayStart, formatPeriod, granularityOf, inclusiveEnd, isDateRange, isWithin, parsePeriod, periodOf, periodTimeZone, rangeDurationMs, rangesOverlap, resolveDateSpan, resolveDay, resolveMonth, resolvePeriod, resolveQuarter, resolveYear };
212
+ export { DateRange, DateRangeError, Period, PeriodError, PeriodErrorCode, PeriodGranularity, dayStart, formatPeriod, granularityOf, inclusiveEnd, isDateRange, isWithin, parsePeriod, periodOf, periodTimeZone, rangeDurationMs, rangesOverlap, resolveDateSpan, resolveDay, resolveMonth, resolvePeriod, resolveQuarter, resolveYear, shiftPeriod };
package/dist/period.mjs CHANGED
@@ -261,6 +261,55 @@ function resolvePeriod(period, timezone) {
261
261
  * For the `$lte` call sites that predate half-open ranges. Derived from the
262
262
  * resolved end rather than `start + 24h - 1`, so it stays DST-exact.
263
263
  */
264
+ /**
265
+ * Shift a MONTH or QUARTER period by `n` of its own units (negative = back).
266
+ *
267
+ * The arithmetic a caller would otherwise write inline, and the reason this is
268
+ * here rather than in each of them: "two filing months ago" is a `Period`
269
+ * question, and the shapes reachable without it are both wrong in the same way.
270
+ *
271
+ * `new Date(Date.UTC(y, m - 1 - n, 1))` — a UTC instant used to answer a
272
+ * BUSINESS-calendar question. Six hours off in Asia/Dhaka at both ends; the
273
+ * five silent defects in this module's header are all that shape.
274
+ *
275
+ * hand-rolled integer math (`y * 12 + (m - 1) - n`) — correct, but it is a
276
+ * third copy of a rule the `Period` type already owns, and the year rollover
277
+ * is exactly where a copy goes wrong.
278
+ *
279
+ * NO Date is constructed and no zone is consulted: shifting a month is pure
280
+ * calendar arithmetic on `(year, month)`. The `timezone` rides along unchanged,
281
+ * so the shifted period still resolves against the calendar it was defined in —
282
+ * `resolvePeriod` / {@link resolveMonth} remain the only places instants appear.
283
+ *
284
+ * A YEAR period shifts by years; a `custom` (ad-hoc start/end) period has no
285
+ * unit to shift and throws rather than guessing one.
286
+ */
287
+ function shiftPeriod(period, n) {
288
+ if (!Number.isInteger(n)) throw new PeriodError("INVALID_FIELD", `shiftPeriod expects an integer offset, received ${String(n)}.`);
289
+ const granularity = granularityOf(period);
290
+ const zone = period.timezone !== void 0 ? { timezone: period.timezone } : {};
291
+ if (granularity === "month") {
292
+ const zeroBased = period.year * 12 + (period.month - 1) + n;
293
+ return {
294
+ year: Math.floor(zeroBased / 12),
295
+ month: zeroBased % 12 + 1,
296
+ ...zone
297
+ };
298
+ }
299
+ if (granularity === "quarter") {
300
+ const zeroBased = period.year * 4 + (period.quarter - 1) + n;
301
+ return {
302
+ year: Math.floor(zeroBased / 4),
303
+ quarter: zeroBased % 4 + 1,
304
+ ...zone
305
+ };
306
+ }
307
+ if (granularity === "year") return {
308
+ year: period.year + n,
309
+ ...zone
310
+ };
311
+ throw new PeriodError("AMBIGUOUS_PERIOD", "shiftPeriod has no unit to shift for an ad-hoc start/end period — shift the instants, or use a calendar period.");
312
+ }
264
313
  function inclusiveEnd(range) {
265
314
  assertUsableRange(range, "range");
266
315
  return /* @__PURE__ */ new Date(range.end.getTime() - 1);
@@ -339,4 +388,4 @@ function formatPeriod(period) {
339
388
  throw new PeriodError("INVALID_LABEL", "An ad-hoc Period (start + end) has no canonical label.");
340
389
  }
341
390
  //#endregion
342
- export { DateRangeError, PeriodError, dayStart, formatPeriod, granularityOf, inclusiveEnd, isDateRange, isWithin, parsePeriod, periodOf, periodTimeZone, rangeDurationMs, rangesOverlap, resolveDateSpan, resolveDay, resolveMonth, resolvePeriod, resolveQuarter, resolveYear };
391
+ export { DateRangeError, PeriodError, dayStart, formatPeriod, granularityOf, inclusiveEnd, isDateRange, isWithin, parsePeriod, periodOf, periodTimeZone, rangeDurationMs, rangesOverlap, resolveDateSpan, resolveDay, resolveMonth, resolvePeriod, resolveQuarter, resolveYear, shiftPeriod };
@@ -1,5 +1,4 @@
1
1
  import { PersonName } from "./person.mjs";
2
-
3
2
  //#region src/identity/person-parse.d.ts
4
3
  /**
5
4
  * Does this `PersonName` carry at least one usable, non-empty name part?
package/dist/phone.d.mts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { Result } from "./result.mjs";
2
-
3
2
  //#region src/identity/phone.d.ts
4
3
  type PhoneErrorCode = 'EMPTY' | 'MISSING_PLUS' | 'NOT_E164' | 'TOO_SHORT' | 'TOO_LONG';
5
4
  declare class PhoneError extends Error {
package/dist/phone.mjs CHANGED
@@ -42,7 +42,7 @@ var PhoneError = class extends Error {
42
42
  * Kept inline (not exported) because it's an implementation detail of the
43
43
  * parser. Length is the only thing we actually need for the split.
44
44
  */
45
- const E164_PREFIXES_3 = new Set([
45
+ const E164_PREFIXES_3 = /* @__PURE__ */ new Set([
46
46
  "210",
47
47
  "211",
48
48
  "212",
@@ -215,7 +215,7 @@ const E164_PREFIXES_3 = new Set([
215
215
  "996",
216
216
  "998"
217
217
  ]);
218
- const E164_PREFIXES_2 = new Set([
218
+ const E164_PREFIXES_2 = /* @__PURE__ */ new Set([
219
219
  "20",
220
220
  "27",
221
221
  "30",
@@ -261,7 +261,7 @@ const E164_PREFIXES_2 = new Set([
261
261
  "95",
262
262
  "98"
263
263
  ]);
264
- const E164_PREFIXES_1 = new Set(["1", "7"]);
264
+ const E164_PREFIXES_1 = /* @__PURE__ */ new Set(["1", "7"]);
265
265
  /**
266
266
  * Parse free-form phone input into a normalized `PhoneNumber`. Accepts:
267
267
  * - `'+14155550182'` → e164 unchanged
@@ -1,5 +1,4 @@
1
- import { n as Money } from "./money-DPo9fLBi.mjs";
2
-
1
+ import { n as Money } from "./money-CfAvfNiM.mjs";
3
2
  //#region src/money/proration.d.ts
4
3
  /**
5
4
  * How to count days within a period.
@@ -1,15 +1,15 @@
1
1
  //#region src/retention/purge-evidence.ts
2
- const STRATEGY_KINDS = new Set([
2
+ const STRATEGY_KINDS = /* @__PURE__ */ new Set([
3
3
  "hard",
4
4
  "soft",
5
5
  "anonymize"
6
6
  ]);
7
- const ACTOR_KINDS = new Set([
7
+ const ACTOR_KINDS = /* @__PURE__ */ new Set([
8
8
  "user",
9
9
  "system",
10
10
  "service"
11
11
  ]);
12
- const STATUS_KINDS = new Set([
12
+ const STATUS_KINDS = /* @__PURE__ */ new Set([
13
13
  "completed",
14
14
  "partial",
15
15
  "failed"
@@ -43,7 +43,7 @@ var ShelfLifeError = class extends Error {
43
43
  this.code = code;
44
44
  }
45
45
  };
46
- const DAY_MS = 1440 * 60 * 1e3;
46
+ const DAY_MS = 864e5;
47
47
  const TRACKING_MODES = [
48
48
  "none",
49
49
  "lot",
@@ -1,6 +1,5 @@
1
- import { s as IsoWeekday } from "./cadence-BFQVC1Qp.mjs";
1
+ import { s as IsoWeekday } from "./cadence-5X6TiD49.mjs";
2
2
  import { SLA } from "./sla.mjs";
3
-
4
3
  //#region src/scheduling/sla-policy.d.ts
5
4
  /**
6
5
  * Tier of severity. Hosts decide the keys ('urgent' / 'p0' / 'gold' / etc.) —
@@ -1,5 +1,4 @@
1
1
  import { StateMachine } from "./state-machine.mjs";
2
-
3
2
  //#region src/workflow/state-diagram.d.ts
4
3
  interface StateDiagramOptions<TStatus extends string> {
5
4
  /**
@@ -54,7 +54,7 @@ function describeStateMachine(machine) {
54
54
  * that can never leave the state it is stuck in.
55
55
  */
56
56
  function unreachableStates(machine, initial) {
57
- const seen = new Set([initial]);
57
+ const seen = /* @__PURE__ */ new Set([initial]);
58
58
  const queue = [initial];
59
59
  while (queue.length > 0) for (const next of machine.validTargets(queue.pop())) if (!seen.has(next)) {
60
60
  seen.add(next);
@@ -1,7 +1,6 @@
1
- import { t as UtcOffsetMinutes } from "./calendar-B6fm-FbR.mjs";
1
+ import { t as UtcOffsetMinutes } from "./calendar-DJWSbcgE.mjs";
2
2
  import { DateRange } from "./period.mjs";
3
3
  import { StatusChangeEntry } from "./status-history.mjs";
4
-
5
4
  //#region src/workflow/suspension.d.ts
6
5
  /**
7
6
  * One suspension, past or current.
@@ -84,7 +83,9 @@ declare function allowancePeriod(at: Date, periodMonths?: number, offsetMinutes?
84
83
  * December. Ignoring that double-counts a straddling pause in both periods and
85
84
  * refuses a member who is within their allowance.
86
85
  */
87
- declare function suspendedDaysInPeriod(history: readonly SuspensionSpan[], period: DateRange, /** Now — bounds still-open spans. Defaults to the period end. */at?: Date): number;
86
+ declare function suspendedDaysInPeriod(history: readonly SuspensionSpan[], period: DateRange,
87
+ /** Now — bounds still-open spans. Defaults to the period end. */
88
+ at?: Date): number;
88
89
  /** Is any span still open at `at`? */
89
90
  declare function openSpans(history: readonly SuspensionSpan[], at: Date): readonly SuspensionSpan[];
90
91
  /**
@@ -144,7 +145,8 @@ declare function spansFromStatusHistory<TStatus extends string>(history: readonl
144
145
  * resume yet (a crash between the write and the append). Leaving it open would
145
146
  * charge the member allowance for time they were not frozen.
146
147
  */
147
- readonly currentStatus?: TStatus | undefined; /** Closes a genuinely-open trailing span for counting. Defaults to open. */
148
+ readonly currentStatus?: TStatus | undefined;
149
+ /** Closes a genuinely-open trailing span for counting. Defaults to open. */
148
150
  readonly openEndedAt?: Date | undefined;
149
151
  }): SuspensionSpan[];
150
152
  //#endregion
@@ -39,11 +39,12 @@ import { addMonths, startOfDay } from "./calendar.mjs";
39
39
  * PURE: no clock reads beyond the `at` you pass, no I/O. The same evaluation runs
40
40
  * on the server, in an admin preview, and in a test.
41
41
  */
42
- const MS_PER_DAY = 1440 * 60 * 1e3;
42
+ const MS_PER_DAY = 864e5;
43
43
  /** The allowance period containing `at`, as a half-open range. */
44
44
  function allowancePeriod(at, periodMonths = 12, offsetMinutes = 0) {
45
+ const anchor = startOfDay(at, offsetMinutes);
45
46
  return {
46
- start: addMonths(startOfDay(at, offsetMinutes), -periodMonths),
47
+ start: addMonths(anchor, -periodMonths),
47
48
  end: at
48
49
  };
49
50
  }
@@ -1,7 +1,6 @@
1
- import { t as Brand } from "./brand-LLXqoh5J.mjs";
2
- import { s as IsoWeekday } from "./cadence-BFQVC1Qp.mjs";
3
- import { t as UtcOffsetMinutes } from "./calendar-B6fm-FbR.mjs";
4
-
1
+ import { t as Brand } from "./brand-Lj_75Y_r.mjs";
2
+ import { s as IsoWeekday } from "./cadence-5X6TiD49.mjs";
3
+ import { t as UtcOffsetMinutes } from "./calendar-DJWSbcgE.mjs";
5
4
  //#region src/scheduling/timezone.d.ts
6
5
  declare class TimeZoneError extends Error {
7
6
  constructor(message: string);
@@ -1,2 +1,2 @@
1
- import { a as civilDate, c as civilDaysBetween, d as isoWeekOfCivilDate, f as listTimeZones, h as zoneOffsetMinutes, i as addCivilDays, l as isCivilDate, m as zoneOffsetLabel, n as LocalTimeParts, o as civilDateOf, p as localTimeParts, r as TimeZoneError, s as civilDateToInstant, t as CivilDate, u as isValidTimeZone } from "./timezone-tlG7ygwx.mjs";
1
+ import { a as civilDate, c as civilDaysBetween, d as isoWeekOfCivilDate, f as listTimeZones, h as zoneOffsetMinutes, i as addCivilDays, l as isCivilDate, m as zoneOffsetLabel, n as LocalTimeParts, o as civilDateOf, p as localTimeParts, r as TimeZoneError, s as civilDateToInstant, t as CivilDate, u as isValidTimeZone } from "./timezone-CxUjxmRU.mjs";
2
2
  export { CivilDate, LocalTimeParts, TimeZoneError, addCivilDays, civilDate, civilDateOf, civilDateToInstant, civilDaysBetween, isCivilDate, isValidTimeZone, isoWeekOfCivilDate, listTimeZones, localTimeParts, zoneOffsetLabel, zoneOffsetMinutes };
@@ -1,5 +1,4 @@
1
- import { t as MinorUnits, w as CurrencyCode } from "./money-DPo9fLBi.mjs";
2
-
1
+ import { t as MinorUnits, w as CurrencyCode } from "./money-CfAvfNiM.mjs";
3
2
  //#region src/money/unit-cost-rate.d.ts
4
3
  /** Fixed, ecosystem-wide rate scale: 6 decimal places below the minor unit. */
5
4
  declare const RATE_SCALE = 1000000;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classytic/primitives",
3
- "version": "0.20.0",
3
+ "version": "0.22.0",
4
4
  "description": "Framework-agnostic TypeScript primitives shared across Classytic packages — Money, events, pagination, tenant config, and other building blocks.",
5
5
  "author": "Classytic",
6
6
  "license": "MIT",
@@ -57,6 +57,10 @@
57
57
  "types": "./dist/context.d.mts",
58
58
  "default": "./dist/context.mjs"
59
59
  },
60
+ "./environment": {
61
+ "types": "./dist/environment.d.mts",
62
+ "default": "./dist/environment.mjs"
63
+ },
60
64
  "./events": {
61
65
  "types": "./dist/events.d.mts",
62
66
  "default": "./dist/events.mjs"
@@ -225,7 +229,8 @@
225
229
  "@biomejs/biome": "^2.4.9",
226
230
  "@classytic/dev-tools": "^0.2.0",
227
231
  "@types/node": "^25.5.0",
228
- "tsdown": "^0.21.7",
232
+ "publint": "^0.3.23",
233
+ "tsdown": "^0.22.14",
229
234
  "typescript": "^5.7.0",
230
235
  "vitest": "^3.0.0"
231
236
  },