@beignet/core 0.0.1 → 0.0.3

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 (287) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +202 -8
  3. package/dist/application/index.d.ts +93 -9
  4. package/dist/application/index.d.ts.map +1 -1
  5. package/dist/application/index.js +11 -11
  6. package/dist/application/index.js.map +1 -1
  7. package/dist/client/client.d.ts +73 -12
  8. package/dist/client/client.d.ts.map +1 -1
  9. package/dist/client/client.js +37 -12
  10. package/dist/client/client.js.map +1 -1
  11. package/dist/client/index.d.ts +12 -0
  12. package/dist/client/index.d.ts.map +1 -1
  13. package/dist/client/index.js +6 -0
  14. package/dist/client/index.js.map +1 -1
  15. package/dist/client/types.d.ts +69 -8
  16. package/dist/client/types.d.ts.map +1 -1
  17. package/dist/config/index.d.ts +84 -0
  18. package/dist/config/index.d.ts.map +1 -1
  19. package/dist/config/index.js +36 -0
  20. package/dist/config/index.js.map +1 -1
  21. package/dist/contracts/contract-builder.d.ts +49 -22
  22. package/dist/contracts/contract-builder.d.ts.map +1 -1
  23. package/dist/contracts/contract-builder.js +48 -21
  24. package/dist/contracts/contract-builder.js.map +1 -1
  25. package/dist/contracts/contract-group.d.ts +35 -19
  26. package/dist/contracts/contract-group.d.ts.map +1 -1
  27. package/dist/contracts/contract-group.js +35 -19
  28. package/dist/contracts/contract-group.js.map +1 -1
  29. package/dist/contracts/contract-like.d.ts +4 -4
  30. package/dist/contracts/contract-like.d.ts.map +1 -1
  31. package/dist/contracts/contract-like.js +2 -1
  32. package/dist/contracts/contract-like.js.map +1 -1
  33. package/dist/contracts/index.d.ts +28 -0
  34. package/dist/contracts/index.d.ts.map +1 -1
  35. package/dist/contracts/index.js +12 -0
  36. package/dist/contracts/index.js.map +1 -1
  37. package/dist/contracts/openapi-meta.d.ts +8 -8
  38. package/dist/contracts/openapi-meta.d.ts.map +1 -1
  39. package/dist/contracts/path-template.d.ts +27 -0
  40. package/dist/contracts/path-template.d.ts.map +1 -1
  41. package/dist/contracts/path-template.js +6 -0
  42. package/dist/contracts/path-template.js.map +1 -1
  43. package/dist/contracts/types.d.ts +104 -10
  44. package/dist/contracts/types.d.ts.map +1 -1
  45. package/dist/contracts/types.js +15 -0
  46. package/dist/contracts/types.js.map +1 -1
  47. package/dist/contracts/utils.d.ts +6 -0
  48. package/dist/contracts/utils.d.ts.map +1 -1
  49. package/dist/contracts/utils.js +6 -0
  50. package/dist/contracts/utils.js.map +1 -1
  51. package/dist/domain/entity.d.ts +22 -11
  52. package/dist/domain/entity.d.ts.map +1 -1
  53. package/dist/domain/entity.js +5 -1
  54. package/dist/domain/entity.js.map +1 -1
  55. package/dist/domain/events.d.ts +5 -2
  56. package/dist/domain/events.d.ts.map +1 -1
  57. package/dist/domain/events.js +4 -1
  58. package/dist/domain/events.js.map +1 -1
  59. package/dist/domain/value-object.d.ts +19 -9
  60. package/dist/domain/value-object.d.ts.map +1 -1
  61. package/dist/domain/value-object.js +5 -1
  62. package/dist/domain/value-object.js.map +1 -1
  63. package/dist/errors/catalog.d.ts +40 -16
  64. package/dist/errors/catalog.d.ts.map +1 -1
  65. package/dist/errors/catalog.js +18 -7
  66. package/dist/errors/catalog.js.map +1 -1
  67. package/dist/errors/response.d.ts +16 -4
  68. package/dist/errors/response.d.ts.map +1 -1
  69. package/dist/errors/response.js +3 -3
  70. package/dist/errors/response.js.map +1 -1
  71. package/dist/errors/validation.d.ts +10 -1
  72. package/dist/errors/validation.d.ts.map +1 -1
  73. package/dist/errors/validation.js +3 -0
  74. package/dist/errors/validation.js.map +1 -1
  75. package/dist/events/index.d.ts +133 -0
  76. package/dist/events/index.d.ts.map +1 -1
  77. package/dist/events/index.js +30 -0
  78. package/dist/events/index.js.map +1 -1
  79. package/dist/idempotency/index.d.ts +355 -0
  80. package/dist/idempotency/index.d.ts.map +1 -0
  81. package/dist/idempotency/index.js +360 -0
  82. package/dist/idempotency/index.js.map +1 -0
  83. package/dist/jobs/index.d.ts +248 -4
  84. package/dist/jobs/index.d.ts.map +1 -1
  85. package/dist/jobs/index.js +183 -1
  86. package/dist/jobs/index.js.map +1 -1
  87. package/dist/mail/index.d.ts +149 -0
  88. package/dist/mail/index.d.ts.map +1 -1
  89. package/dist/mail/index.js +30 -0
  90. package/dist/mail/index.js.map +1 -1
  91. package/dist/notifications/index.d.ts +369 -0
  92. package/dist/notifications/index.d.ts.map +1 -0
  93. package/dist/notifications/index.js +310 -0
  94. package/dist/notifications/index.js.map +1 -0
  95. package/dist/openapi/index.d.ts +132 -16
  96. package/dist/openapi/index.d.ts.map +1 -1
  97. package/dist/openapi/index.js +1 -1
  98. package/dist/openapi/index.js.map +1 -1
  99. package/dist/outbox/index.d.ts +474 -0
  100. package/dist/outbox/index.d.ts.map +1 -0
  101. package/dist/outbox/index.js +538 -0
  102. package/dist/outbox/index.js.map +1 -0
  103. package/dist/pagination/index.d.ts +166 -0
  104. package/dist/pagination/index.d.ts.map +1 -0
  105. package/dist/pagination/index.js +96 -0
  106. package/dist/pagination/index.js.map +1 -0
  107. package/dist/ports/audit.d.ts +271 -0
  108. package/dist/ports/audit.d.ts.map +1 -1
  109. package/dist/ports/audit.js +128 -0
  110. package/dist/ports/audit.js.map +1 -1
  111. package/dist/ports/auth.d.ts +70 -0
  112. package/dist/ports/auth.d.ts.map +1 -1
  113. package/dist/ports/auth.js +30 -0
  114. package/dist/ports/auth.js.map +1 -1
  115. package/dist/ports/cache.d.ts +41 -0
  116. package/dist/ports/cache.d.ts.map +1 -1
  117. package/dist/ports/cache.js +10 -0
  118. package/dist/ports/cache.js.map +1 -1
  119. package/dist/ports/clock.d.ts +38 -0
  120. package/dist/ports/clock.d.ts.map +1 -1
  121. package/dist/ports/clock.js +20 -0
  122. package/dist/ports/clock.js.map +1 -1
  123. package/dist/ports/id-generator.d.ts +37 -0
  124. package/dist/ports/id-generator.d.ts.map +1 -1
  125. package/dist/ports/id-generator.js +22 -0
  126. package/dist/ports/id-generator.js.map +1 -1
  127. package/dist/ports/index.d.ts +83 -0
  128. package/dist/ports/index.d.ts.map +1 -1
  129. package/dist/ports/index.js +41 -5
  130. package/dist/ports/index.js.map +1 -1
  131. package/dist/ports/logger.d.ts +56 -0
  132. package/dist/ports/logger.d.ts.map +1 -1
  133. package/dist/ports/logger.js +17 -0
  134. package/dist/ports/logger.js.map +1 -1
  135. package/dist/ports/policy.d.ts +132 -0
  136. package/dist/ports/policy.d.ts.map +1 -1
  137. package/dist/ports/policy.js +45 -0
  138. package/dist/ports/policy.js.map +1 -1
  139. package/dist/ports/rate-limit.d.ts +25 -0
  140. package/dist/ports/rate-limit.d.ts.map +1 -1
  141. package/dist/ports/rate-limit.js +10 -0
  142. package/dist/ports/rate-limit.js.map +1 -1
  143. package/dist/ports/redaction.d.ts +101 -0
  144. package/dist/ports/redaction.d.ts.map +1 -1
  145. package/dist/ports/redaction.js +59 -0
  146. package/dist/ports/redaction.js.map +1 -1
  147. package/dist/ports/storage.d.ts +100 -0
  148. package/dist/ports/storage.d.ts.map +1 -1
  149. package/dist/ports/storage.js +10 -0
  150. package/dist/ports/storage.js.map +1 -1
  151. package/dist/ports/testing.d.ts +47 -0
  152. package/dist/ports/testing.d.ts.map +1 -1
  153. package/dist/ports/testing.js +23 -0
  154. package/dist/ports/testing.js.map +1 -1
  155. package/dist/ports/unit-of-work.d.ts +60 -3
  156. package/dist/ports/unit-of-work.d.ts.map +1 -1
  157. package/dist/ports/unit-of-work.js +11 -2
  158. package/dist/ports/unit-of-work.js.map +1 -1
  159. package/dist/providers/instrumentation.d.ts +205 -1
  160. package/dist/providers/instrumentation.d.ts.map +1 -1
  161. package/dist/providers/instrumentation.js +14 -0
  162. package/dist/providers/instrumentation.js.map +1 -1
  163. package/dist/providers/provider.d.ts +14 -1
  164. package/dist/providers/provider.d.ts.map +1 -1
  165. package/dist/providers/provider.js.map +1 -1
  166. package/dist/schedules/index.d.ts +246 -0
  167. package/dist/schedules/index.d.ts.map +1 -1
  168. package/dist/schedules/index.js +27 -0
  169. package/dist/schedules/index.js.map +1 -1
  170. package/dist/server/health.d.ts +14 -5
  171. package/dist/server/health.d.ts.map +1 -1
  172. package/dist/server/health.js +5 -2
  173. package/dist/server/health.js.map +1 -1
  174. package/dist/server/hooks/auth.d.ts +68 -26
  175. package/dist/server/hooks/auth.d.ts.map +1 -1
  176. package/dist/server/hooks/auth.js +44 -55
  177. package/dist/server/hooks/auth.js.map +1 -1
  178. package/dist/server/hooks/cors.d.ts +27 -0
  179. package/dist/server/hooks/cors.d.ts.map +1 -1
  180. package/dist/server/hooks/cors.js +12 -0
  181. package/dist/server/hooks/cors.js.map +1 -1
  182. package/dist/server/hooks/errors.d.ts +15 -6
  183. package/dist/server/hooks/errors.d.ts.map +1 -1
  184. package/dist/server/hooks/errors.js.map +1 -1
  185. package/dist/server/hooks/index.d.ts +4 -1
  186. package/dist/server/hooks/index.d.ts.map +1 -1
  187. package/dist/server/hooks/index.js +3 -0
  188. package/dist/server/hooks/index.js.map +1 -1
  189. package/dist/server/hooks/logging.d.ts +36 -0
  190. package/dist/server/hooks/logging.d.ts.map +1 -1
  191. package/dist/server/hooks/logging.js +6 -0
  192. package/dist/server/hooks/logging.js.map +1 -1
  193. package/dist/server/hooks/rate-limit.d.ts +33 -0
  194. package/dist/server/hooks/rate-limit.d.ts.map +1 -1
  195. package/dist/server/hooks/rate-limit.js +11 -0
  196. package/dist/server/hooks/rate-limit.js.map +1 -1
  197. package/dist/server/http.d.ts +222 -0
  198. package/dist/server/http.d.ts.map +1 -1
  199. package/dist/server/http.js +20 -1
  200. package/dist/server/http.js.map +1 -1
  201. package/dist/server/index.d.ts +19 -1
  202. package/dist/server/index.d.ts.map +1 -1
  203. package/dist/server/index.js +7 -1
  204. package/dist/server/index.js.map +1 -1
  205. package/dist/server/openapi.d.ts +5 -3
  206. package/dist/server/openapi.d.ts.map +1 -1
  207. package/dist/server/openapi.js +4 -2
  208. package/dist/server/openapi.js.map +1 -1
  209. package/dist/server/providers/loadProviderConfig.d.ts +9 -0
  210. package/dist/server/providers/loadProviderConfig.d.ts.map +1 -1
  211. package/dist/server/providers/loadProviderConfig.js +9 -0
  212. package/dist/server/providers/loadProviderConfig.js.map +1 -1
  213. package/dist/server/server.d.ts +159 -19
  214. package/dist/server/server.d.ts.map +1 -1
  215. package/dist/server/server.js +72 -31
  216. package/dist/server/server.js.map +1 -1
  217. package/dist/testing/index.d.ts +171 -0
  218. package/dist/testing/index.d.ts.map +1 -0
  219. package/dist/testing/index.js +127 -0
  220. package/dist/testing/index.js.map +1 -0
  221. package/dist/uploads/client.d.ts +278 -0
  222. package/dist/uploads/client.d.ts.map +1 -0
  223. package/dist/uploads/client.js +428 -0
  224. package/dist/uploads/client.js.map +1 -0
  225. package/dist/uploads/index.d.ts +361 -0
  226. package/dist/uploads/index.d.ts.map +1 -0
  227. package/dist/uploads/index.js +543 -0
  228. package/dist/uploads/index.js.map +1 -0
  229. package/package.json +31 -2
  230. package/src/application/index.ts +85 -22
  231. package/src/client/client.ts +73 -12
  232. package/src/client/index.ts +12 -0
  233. package/src/client/types.ts +70 -9
  234. package/src/config/index.ts +86 -0
  235. package/src/contracts/contract-builder.ts +49 -22
  236. package/src/contracts/contract-group.ts +35 -19
  237. package/src/contracts/contract-like.ts +4 -4
  238. package/src/contracts/index.ts +28 -1
  239. package/src/contracts/openapi-meta.ts +8 -8
  240. package/src/contracts/path-template.ts +27 -0
  241. package/src/contracts/types.ts +111 -10
  242. package/src/contracts/utils.ts +6 -0
  243. package/src/domain/entity.ts +22 -11
  244. package/src/domain/events.ts +5 -2
  245. package/src/domain/value-object.ts +19 -9
  246. package/src/errors/catalog.ts +40 -16
  247. package/src/errors/response.ts +16 -4
  248. package/src/errors/validation.ts +10 -1
  249. package/src/events/index.ts +134 -0
  250. package/src/idempotency/index.ts +767 -0
  251. package/src/jobs/index.ts +437 -5
  252. package/src/mail/index.ts +149 -0
  253. package/src/notifications/index.ts +771 -0
  254. package/src/openapi/index.ts +133 -16
  255. package/src/outbox/index.ts +1104 -0
  256. package/src/pagination/index.ts +278 -0
  257. package/src/ports/audit.ts +271 -0
  258. package/src/ports/auth.ts +70 -0
  259. package/src/ports/cache.ts +41 -0
  260. package/src/ports/clock.ts +38 -0
  261. package/src/ports/id-generator.ts +37 -0
  262. package/src/ports/index.ts +106 -11
  263. package/src/ports/logger.ts +56 -0
  264. package/src/ports/policy.ts +133 -0
  265. package/src/ports/rate-limit.ts +25 -0
  266. package/src/ports/redaction.ts +101 -0
  267. package/src/ports/storage.ts +100 -0
  268. package/src/ports/testing.ts +47 -0
  269. package/src/ports/unit-of-work.ts +60 -3
  270. package/src/providers/instrumentation.ts +211 -1
  271. package/src/providers/provider.ts +14 -1
  272. package/src/schedules/index.ts +247 -0
  273. package/src/server/health.ts +14 -5
  274. package/src/server/hooks/auth.ts +105 -120
  275. package/src/server/hooks/cors.ts +27 -0
  276. package/src/server/hooks/errors.ts +15 -6
  277. package/src/server/hooks/index.ts +4 -5
  278. package/src/server/hooks/logging.ts +36 -0
  279. package/src/server/hooks/rate-limit.ts +33 -0
  280. package/src/server/http.ts +249 -1
  281. package/src/server/index.ts +19 -1
  282. package/src/server/openapi.ts +5 -3
  283. package/src/server/providers/loadProviderConfig.ts +9 -0
  284. package/src/server/server.ts +296 -30
  285. package/src/testing/index.ts +348 -0
  286. package/src/uploads/client.ts +861 -0
  287. package/src/uploads/index.ts +1067 -0
@@ -55,6 +55,9 @@ export function createRecordingEventBus(): {
55
55
  return { bus, events };
56
56
  }
57
57
 
58
+ /**
59
+ * Expected outcome for one policy matrix case.
60
+ */
58
61
  export type PolicyMatrixExpectation = "allow" | "deny";
59
62
 
60
63
  type PolicyMatrixSubject<TResolver> =
@@ -62,6 +65,9 @@ type PolicyMatrixSubject<TResolver> =
62
65
  ? { subject: Subject }
63
66
  : { subject?: never };
64
67
 
68
+ /**
69
+ * One typed authorization matrix case for a policy ability.
70
+ */
65
71
  export type PolicyMatrixCase<
66
72
  TContext,
67
73
  TPolicies extends readonly PolicyDefinition[] = readonly PolicyDefinition[],
@@ -76,6 +82,9 @@ export type PolicyMatrixCase<
76
82
  } & PolicyMatrixSubject<PolicyMapFromDefinitions<TPolicies>[TAbility]>;
77
83
  }[keyof PolicyMapFromDefinitions<TPolicies> & string];
78
84
 
85
+ /**
86
+ * Untyped policy matrix case used internally for failure reporting.
87
+ */
79
88
  export type UntypedPolicyMatrixCase<TContext> = {
80
89
  name: string;
81
90
  ctx: TContext;
@@ -86,6 +95,9 @@ export type UntypedPolicyMatrixCase<TContext> = {
86
95
  code?: string;
87
96
  };
88
97
 
98
+ /**
99
+ * Result for one evaluated policy matrix case.
100
+ */
89
101
  export type PolicyMatrixResult<
90
102
  TContext,
91
103
  TPolicies extends readonly PolicyDefinition[] = readonly PolicyDefinition[],
@@ -96,19 +108,40 @@ export type PolicyMatrixResult<
96
108
  message?: string;
97
109
  };
98
110
 
111
+ /**
112
+ * Test helper for evaluating authorization policies.
113
+ */
99
114
  export type PolicyTester<
100
115
  TContext,
101
116
  TPolicies extends readonly PolicyDefinition[],
102
117
  > = {
118
+ /**
119
+ * Gate created from the same policies, useful for direct assertions.
120
+ */
103
121
  gate: GatePort<TContext, TPolicies>;
122
+ /**
123
+ * Evaluate cases and return structured pass/fail results.
124
+ */
104
125
  evaluateMatrix(
105
126
  cases: readonly PolicyMatrixCase<TContext, TPolicies>[],
106
127
  ): Promise<PolicyMatrixResult<TContext, TPolicies>[]>;
128
+ /**
129
+ * Evaluate cases and throw a combined assertion error when any fail.
130
+ */
107
131
  assertMatrix(
108
132
  cases: readonly PolicyMatrixCase<TContext, TPolicies>[],
109
133
  ): Promise<void>;
110
134
  };
111
135
 
136
+ /**
137
+ * Create a policy tester from the same options used by `createGate(...)`.
138
+ *
139
+ * Use this for table-driven authorization tests that document who can perform
140
+ * each ability against which subject.
141
+ *
142
+ * @param options - Policy definitions and optional denial mapper.
143
+ * @returns A policy tester with a gate plus matrix helpers.
144
+ */
112
145
  export function createPolicyTester<
113
146
  const TPolicies extends readonly PolicyDefinition[],
114
147
  >(
@@ -126,6 +159,13 @@ export function createPolicyTester<
126
159
  };
127
160
  }
128
161
 
162
+ /**
163
+ * Evaluate a table of policy cases without throwing.
164
+ *
165
+ * @param gate - Gate under test.
166
+ * @param cases - Matrix cases to evaluate.
167
+ * @returns Structured result for each case.
168
+ */
129
169
  export async function evaluatePolicyMatrix<
130
170
  TContext,
131
171
  TPolicies extends readonly PolicyDefinition[],
@@ -152,6 +192,13 @@ export async function evaluatePolicyMatrix<
152
192
  return results;
153
193
  }
154
194
 
195
+ /**
196
+ * Assert that all policy matrix cases pass.
197
+ *
198
+ * @param gate - Gate under test.
199
+ * @param cases - Matrix cases to evaluate.
200
+ * @throws Combined error listing every failed case.
201
+ */
155
202
  export async function assertPolicyMatrix<
156
203
  TContext,
157
204
  TPolicies extends readonly PolicyDefinition[],
@@ -3,6 +3,12 @@ import type { DomainEventDef, EventBusPort, InferEventPayload } from "./events";
3
3
 
4
4
  type MaybePromise<T> = T | Promise<T>;
5
5
 
6
+ /**
7
+ * Work function executed inside a Unit of Work.
8
+ *
9
+ * `tx` contains transaction-scoped app ports, usually repositories plus
10
+ * side-effect recorders such as `events`, `jobs`, or `audit`.
11
+ */
6
12
  export type UnitOfWorkCallback<TxPorts, Result> = (
7
13
  tx: TxPorts,
8
14
  ) => MaybePromise<Result>;
@@ -15,11 +21,19 @@ export type UnitOfWorkCallback<TxPorts, Result> = (
15
21
  * `createNoopUnitOfWork`.
16
22
  */
17
23
  export interface UnitOfWorkPort<TxPorts> {
24
+ /**
25
+ * Run application work with transaction-scoped ports.
26
+ *
27
+ * Durable implementations should commit only if this callback resolves.
28
+ */
18
29
  transaction<Result>(
19
30
  work: UnitOfWorkCallback<TxPorts, Result>,
20
31
  ): Promise<Result>;
21
32
  }
22
33
 
34
+ /**
35
+ * Hooks for `createNoopUnitOfWork(...)`.
36
+ */
23
37
  export interface NoopUnitOfWorkOptions<TxPorts> {
24
38
  /**
25
39
  * Runs after the callback completes successfully.
@@ -37,22 +51,56 @@ export interface NoopUnitOfWorkOptions<TxPorts> {
37
51
  afterRollback?: (error: unknown, tx: TxPorts) => MaybePromise<void>;
38
52
  }
39
53
 
54
+ /**
55
+ * Domain event captured by a buffered event recorder.
56
+ */
40
57
  export interface RecordedDomainEvent {
58
+ /**
59
+ * Event definition used to validate the payload before publishing.
60
+ */
41
61
  event: DomainEventDef;
62
+ /**
63
+ * Stable event name.
64
+ */
42
65
  eventName: string;
66
+ /**
67
+ * Unparsed payload recorded during the transaction.
68
+ */
43
69
  payload: unknown;
44
70
  }
45
71
 
72
+ /**
73
+ * Transaction-scoped port used to record domain events.
74
+ *
75
+ * Use cases record events here during the Unit of Work. The adapter decides
76
+ * whether to publish after commit, enqueue through an outbox, or buffer for a
77
+ * test assertion.
78
+ */
46
79
  export interface DomainEventRecorderPort {
80
+ /**
81
+ * Record a domain event payload.
82
+ */
47
83
  record<E extends DomainEventDef>(
48
84
  event: E,
49
85
  payload: InferEventPayload<E>,
50
- ): void;
86
+ ): Promise<void> | void;
51
87
  }
52
88
 
89
+ /**
90
+ * In-memory event recorder that can be inspected, cleared, or flushed.
91
+ */
53
92
  export interface BufferedDomainEventRecorder extends DomainEventRecorderPort {
93
+ /**
94
+ * Return recorded events without clearing them.
95
+ */
54
96
  entries(): readonly RecordedDomainEvent[];
97
+ /**
98
+ * Remove all recorded events.
99
+ */
55
100
  clear(): void;
101
+ /**
102
+ * Validate and publish all recorded events to an event bus in FIFO order.
103
+ */
56
104
  flush(eventBus: EventBusPort): Promise<void>;
57
105
  }
58
106
 
@@ -62,6 +110,11 @@ export interface BufferedDomainEventRecorder extends DomainEventRecorderPort {
62
110
  *
63
111
  * This helper does not create database transactions. It gives applications the
64
112
  * same UOW shape everywhere and runs commit/rollback hooks around the callback.
113
+ *
114
+ * @param txPortsOrFactory - Transaction-scoped ports or a factory that creates
115
+ * them per transaction call.
116
+ * @param options - Optional commit and rollback hooks.
117
+ * @returns A Unit of Work port with no durable transaction semantics.
65
118
  */
66
119
  export function createNoopUnitOfWork<TxPorts>(
67
120
  txPortsOrFactory: TxPorts | (() => TxPorts),
@@ -99,8 +152,12 @@ export function createNoopUnitOfWork<TxPorts>(
99
152
  }
100
153
 
101
154
  /**
102
- * Buffer domain events during a transaction and publish them only after the
103
- * owning Unit of Work commits.
155
+ * Create a recorder that buffers domain events until the caller flushes them.
156
+ *
157
+ * Unit of Work adapters commonly flush this recorder from an `afterCommit`
158
+ * hook so events are not published when the work rolls back.
159
+ *
160
+ * @returns A buffered domain event recorder for tests or Unit of Work adapters.
104
161
  */
105
162
  export function createDomainEventRecorder(): BufferedDomainEventRecorder {
106
163
  const records: RecordedDomainEvent[] = [];
@@ -1,86 +1,236 @@
1
1
  import { type Redactor, redactValue } from "../ports/redaction";
2
2
 
3
+ /**
4
+ * Logical provider watcher name used to toggle groups of instrumentation
5
+ * events.
6
+ */
3
7
  export type ProviderInstrumentationWatcherName = string;
4
8
 
9
+ /**
10
+ * Common metadata attached to every provider instrumentation event.
11
+ */
5
12
  export interface BaseProviderInstrumentationEvent {
13
+ /**
14
+ * Optional event identifier supplied by the caller.
15
+ */
6
16
  id?: string;
17
+ /**
18
+ * ISO timestamp supplied by the caller. Devtools can also assign a timestamp
19
+ * when one is omitted.
20
+ */
7
21
  timestamp?: string;
22
+ /**
23
+ * Request correlation ID for events emitted during request handling.
24
+ */
8
25
  requestId?: string;
26
+ /**
27
+ * Trace identifier for distributed tracing integrations.
28
+ */
9
29
  traceId?: string;
30
+ /**
31
+ * Span identifier for the current operation.
32
+ */
10
33
  spanId?: string;
34
+ /**
35
+ * Parent span identifier for nested operations.
36
+ */
11
37
  parentSpanId?: string;
38
+ /**
39
+ * W3C traceparent header value, when available.
40
+ */
12
41
  traceparent?: string;
42
+ /**
43
+ * Watcher category used by devtools and instrumentation sinks.
44
+ */
13
45
  watcher?: ProviderInstrumentationWatcherName;
46
+ /**
47
+ * Provider-specific structured details. Values are redacted before being
48
+ * recorded by `createProviderInstrumentation(...)`.
49
+ */
14
50
  details?: unknown;
15
51
  }
16
52
 
53
+ /**
54
+ * HTTP request instrumentation emitted by server adapters.
55
+ */
17
56
  export interface RequestProviderInstrumentationEvent
18
57
  extends BaseProviderInstrumentationEvent {
19
58
  type: "request";
59
+ /**
60
+ * HTTP method.
61
+ */
20
62
  method: string;
63
+ /**
64
+ * Request path.
65
+ */
21
66
  path: string;
67
+ /**
68
+ * Matched contract name, when known.
69
+ */
22
70
  contractName?: string;
71
+ /**
72
+ * Response status code.
73
+ */
23
74
  status?: number;
75
+ /**
76
+ * Request duration in milliseconds.
77
+ */
24
78
  durationMs?: number;
79
+ /**
80
+ * Human-readable summary for devtools lists.
81
+ */
25
82
  summary?: string;
26
83
  }
27
84
 
85
+ /**
86
+ * Error instrumentation emitted by framework hooks, use cases, or providers.
87
+ */
28
88
  export interface ErrorProviderInstrumentationEvent
29
89
  extends BaseProviderInstrumentationEvent {
30
90
  type: "error";
91
+ /**
92
+ * Error message.
93
+ */
31
94
  message: string;
95
+ /**
96
+ * Error stack trace, when available.
97
+ */
32
98
  stack?: string;
99
+ /**
100
+ * Contract associated with the error.
101
+ */
33
102
  contractName?: string;
103
+ /**
104
+ * Use case associated with the error.
105
+ */
34
106
  useCaseName?: string;
35
107
  }
36
108
 
109
+ /**
110
+ * Use-case lifecycle instrumentation.
111
+ */
37
112
  export interface UseCaseProviderInstrumentationEvent
38
113
  extends BaseProviderInstrumentationEvent {
39
114
  type: "usecase";
115
+ /**
116
+ * Use-case name.
117
+ */
40
118
  name: string;
119
+ /**
120
+ * Optional classification used by devtools.
121
+ */
41
122
  kind?: "command" | "query";
123
+ /**
124
+ * Lifecycle phase.
125
+ */
42
126
  phase: "start" | "end" | "error";
127
+ /**
128
+ * Duration in milliseconds for completed phases.
129
+ */
43
130
  durationMs?: number;
131
+ /**
132
+ * Error summary for failed phases.
133
+ */
44
134
  error?: string;
45
135
  }
46
136
 
137
+ /**
138
+ * Event bus instrumentation.
139
+ */
47
140
  export interface EventBusProviderInstrumentationEvent
48
141
  extends BaseProviderInstrumentationEvent {
49
142
  type: "eventBus";
143
+ /**
144
+ * Domain or integration event name.
145
+ */
50
146
  eventName: string;
51
147
  }
52
148
 
149
+ /**
150
+ * Background job instrumentation.
151
+ */
53
152
  export interface JobProviderInstrumentationEvent
54
153
  extends BaseProviderInstrumentationEvent {
55
154
  type: "job";
155
+ /**
156
+ * Job name.
157
+ */
56
158
  jobName: string;
57
- status: "scheduled" | "started" | "completed" | "failed";
159
+ /**
160
+ * Job lifecycle status.
161
+ */
162
+ status:
163
+ | "scheduled"
164
+ | "started"
165
+ | "completed"
166
+ | "failed"
167
+ | "retryScheduled"
168
+ | "deadLettered";
58
169
  }
59
170
 
171
+ /**
172
+ * Scheduled task instrumentation.
173
+ */
60
174
  export interface ScheduleProviderInstrumentationEvent
61
175
  extends BaseProviderInstrumentationEvent {
62
176
  type: "schedule";
177
+ /**
178
+ * Schedule name.
179
+ */
63
180
  scheduleName: string;
181
+ /**
182
+ * Schedule run lifecycle status.
183
+ */
64
184
  status: "started" | "completed" | "failed";
185
+ /**
186
+ * Cron expression for the schedule, when known.
187
+ */
65
188
  cron?: string;
189
+ /**
190
+ * Time zone used by the schedule, when known.
191
+ */
66
192
  timezone?: string;
67
193
  }
68
194
 
195
+ /**
196
+ * Provider lifecycle instrumentation emitted during setup, start, and stop.
197
+ */
69
198
  export interface ProviderLifecycleInstrumentationEvent
70
199
  extends BaseProviderInstrumentationEvent {
71
200
  type: "provider";
201
+ /**
202
+ * Provider name.
203
+ */
72
204
  providerName: string;
205
+ /**
206
+ * Lifecycle action.
207
+ */
73
208
  action: "setup" | "start" | "stop";
74
209
  }
75
210
 
211
+ /**
212
+ * Custom provider instrumentation event for provider-owned operations.
213
+ */
76
214
  export interface CustomProviderInstrumentationEvent
77
215
  extends BaseProviderInstrumentationEvent {
78
216
  type: "custom";
217
+ /**
218
+ * Stable event name.
219
+ */
79
220
  name: string;
221
+ /**
222
+ * Short display label for devtools.
223
+ */
80
224
  label?: string;
225
+ /**
226
+ * Human-readable summary for devtools lists.
227
+ */
81
228
  summary?: string;
82
229
  }
83
230
 
231
+ /**
232
+ * Union of instrumentation events that provider sinks can receive.
233
+ */
84
234
  export type ProviderInstrumentationEvent =
85
235
  | RequestProviderInstrumentationEvent
86
236
  | ErrorProviderInstrumentationEvent
@@ -91,36 +241,82 @@ export type ProviderInstrumentationEvent =
91
241
  | ProviderLifecycleInstrumentationEvent
92
242
  | CustomProviderInstrumentationEvent;
93
243
 
244
+ /**
245
+ * Input accepted by provider instrumentation recorders.
246
+ */
94
247
  export type ProviderInstrumentationEventInput = ProviderInstrumentationEvent;
95
248
 
249
+ /**
250
+ * Custom instrumentation input. `type: "custom"` is added by the helper.
251
+ */
96
252
  export type ProviderCustomInstrumentationEventInput = Omit<
97
253
  CustomProviderInstrumentationEvent,
98
254
  "type"
99
255
  >;
100
256
 
257
+ /**
258
+ * Sink for provider instrumentation events.
259
+ */
101
260
  export interface ProviderInstrumentationPort<
102
261
  EventInput extends
103
262
  ProviderInstrumentationEventInput = ProviderInstrumentationEventInput,
104
263
  WatcherName extends string = ProviderInstrumentationWatcherName,
105
264
  RecordResult = unknown,
106
265
  > {
266
+ /**
267
+ * Record an instrumentation event.
268
+ */
107
269
  record(event: EventInput): RecordResult;
270
+ /**
271
+ * Return whether a watcher is enabled. Missing methods are treated as
272
+ * enabled so simple sinks only need to implement `record`.
273
+ */
108
274
  isWatcherEnabled?(name: WatcherName): boolean;
109
275
  }
110
276
 
277
+ /**
278
+ * Options for creating a provider instrumentation helper.
279
+ */
111
280
  export interface ProviderInstrumentationOptions {
281
+ /**
282
+ * Provider name to attach to custom event details.
283
+ */
112
284
  providerName: string;
285
+ /**
286
+ * Default watcher name for events emitted by the helper.
287
+ */
113
288
  watcher?: ProviderInstrumentationWatcherName;
289
+ /**
290
+ * Optional redactor applied after Beignet's default redaction pass.
291
+ */
114
292
  redact?: Redactor<ProviderInstrumentationEventInput>;
115
293
  }
116
294
 
295
+ /**
296
+ * Convenience wrapper used by providers to emit safe, watcher-aware events.
297
+ */
117
298
  export interface ProviderInstrumentation {
299
+ /**
300
+ * Resolved instrumentation port, when one is available.
301
+ */
118
302
  port: ProviderInstrumentationPort | undefined;
303
+ /**
304
+ * Return whether the given watcher is enabled.
305
+ */
119
306
  isEnabled(watcher?: ProviderInstrumentationWatcherName): boolean;
307
+ /**
308
+ * Record a fully formed provider instrumentation event.
309
+ */
120
310
  record(event: ProviderInstrumentationEventInput): unknown;
311
+ /**
312
+ * Record a provider-owned custom event.
313
+ */
121
314
  custom(event: ProviderCustomInstrumentationEventInput): unknown;
122
315
  }
123
316
 
317
+ /**
318
+ * Values accepted by `createProviderInstrumentation(...)`.
319
+ */
124
320
  export type ProviderInstrumentationTarget =
125
321
  | ProviderInstrumentationPort
126
322
  | ProviderInstrumentation
@@ -134,6 +330,9 @@ function isObject(value: unknown): value is Record<string, unknown> {
134
330
  return typeof value === "object" && value !== null;
135
331
  }
136
332
 
333
+ /**
334
+ * Return whether a value is a provider instrumentation helper.
335
+ */
137
336
  export function isProviderInstrumentation(
138
337
  value: unknown,
139
338
  ): value is ProviderInstrumentation {
@@ -145,6 +344,9 @@ export function isProviderInstrumentation(
145
344
  );
146
345
  }
147
346
 
347
+ /**
348
+ * Return whether a value is a provider instrumentation port.
349
+ */
148
350
  export function isProviderInstrumentationPort(
149
351
  value: unknown,
150
352
  ): value is ProviderInstrumentationPort {
@@ -159,6 +361,10 @@ function resolveProviderInstrumentationValue(
159
361
  return undefined;
160
362
  }
161
363
 
364
+ /**
365
+ * Resolve an instrumentation port from a direct port, helper, or context-like
366
+ * object.
367
+ */
162
368
  export function resolveProviderInstrumentationPort(
163
369
  target: ProviderInstrumentationTarget,
164
370
  ): ProviderInstrumentationPort | undefined {
@@ -187,6 +393,10 @@ function withProviderDetails(providerName: string, details: unknown): unknown {
187
393
  return { providerName, value: details };
188
394
  }
189
395
 
396
+ /**
397
+ * Create a provider instrumentation helper that handles watcher checks,
398
+ * default watcher assignment, redaction, and sink failures.
399
+ */
190
400
  export function createProviderInstrumentation(
191
401
  target: ProviderInstrumentationTarget,
192
402
  options: ProviderInstrumentationOptions,
@@ -38,12 +38,24 @@ export interface ProviderConfigDef<CfgSchema extends StandardSchemaV1> {
38
38
  envPrefix?: string;
39
39
  }
40
40
 
41
+ /**
42
+ * Value or promise of that value.
43
+ */
41
44
  export type MaybePromise<T> = T | Promise<T>;
42
45
 
46
+ /**
47
+ * Context passed to provider lifecycle hooks.
48
+ */
43
49
  export type ProviderLifecycleContext = {
50
+ /**
51
+ * Final app ports after provider setup.
52
+ */
44
53
  ports: Readonly<ProviderPorts>;
45
54
  };
46
55
 
56
+ /**
57
+ * Result returned from provider setup.
58
+ */
47
59
  export type ProviderSetupResult<ProvidedPorts extends ProviderPorts> = {
48
60
  /**
49
61
  * Ports contributed by this provider.
@@ -113,7 +125,8 @@ export interface ServiceProvider<
113
125
 
114
126
  /**
115
127
  * Setup phase: create the ports this provider contributes.
116
- * Called during app initialization, before routes are registered.
128
+ * Called during server initialization before provider `start` hooks and
129
+ * before the server handles requests.
117
130
  *
118
131
  * @param ctx.ports - Ports contributed by previous providers
119
132
  * @param ctx.config - Validated config (if config was defined), or undefined