@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
@@ -0,0 +1,369 @@
1
+ import type { StandardSchemaV1 } from "@standard-schema/spec";
2
+ import type { SendMailOptions } from "../mail";
3
+ import type { ProviderInstrumentationTarget } from "../providers";
4
+ /**
5
+ * Any Standard Schema compatible validator.
6
+ */
7
+ export type StandardSchema = StandardSchemaV1<unknown, unknown>;
8
+ /**
9
+ * Value or promise of that value.
10
+ */
11
+ export type MaybePromise<T> = T | Promise<T>;
12
+ /**
13
+ * Infer the parsed output type from a Standard Schema.
14
+ */
15
+ export type InferSchemaOutput<T extends StandardSchemaV1> = StandardSchemaV1.InferOutput<T>;
16
+ /**
17
+ * Minimal notification definition shape accepted by notification ports.
18
+ */
19
+ export interface NotificationPayloadDef<Name extends string = string, Payload extends StandardSchema = StandardSchema> {
20
+ /**
21
+ * Stable notification name used by dispatchers, tests, and tooling.
22
+ */
23
+ readonly name: Name;
24
+ /**
25
+ * Standard Schema payload validator.
26
+ */
27
+ readonly payload: Payload;
28
+ /**
29
+ * Optional human-readable description for docs and tooling.
30
+ */
31
+ readonly description?: string;
32
+ }
33
+ /**
34
+ * Infer the parsed payload type for a notification definition.
35
+ */
36
+ export type InferNotificationPayload<N extends NotificationPayloadDef> = N["payload"] extends StandardSchemaV1<unknown, infer Output> ? Output : never;
37
+ /**
38
+ * Result for one notification channel.
39
+ */
40
+ export interface NotificationChannelResult {
41
+ /**
42
+ * Channel name, such as `email`, `sms`, `push`, or `inApp`.
43
+ */
44
+ channel: string;
45
+ /**
46
+ * Delivery outcome for this channel.
47
+ */
48
+ status: "sent" | "skipped" | "failed";
49
+ /**
50
+ * Provider delivery ID when available.
51
+ */
52
+ id?: string;
53
+ /**
54
+ * Provider name when available.
55
+ */
56
+ provider?: string;
57
+ /**
58
+ * Human-readable skip or failure reason.
59
+ */
60
+ reason?: string;
61
+ /**
62
+ * Channel-specific metadata. Dispatchers should keep this safe to log.
63
+ */
64
+ details?: Record<string, unknown>;
65
+ }
66
+ /**
67
+ * Arguments passed to a notification channel handler.
68
+ */
69
+ export interface NotificationChannelHandleArgs<Payload extends StandardSchema, Ctx> {
70
+ /**
71
+ * Notification definition being delivered.
72
+ */
73
+ notification: NotificationDef<string, Payload, Ctx>;
74
+ /**
75
+ * Parsed notification payload.
76
+ */
77
+ payload: InferSchemaOutput<Payload>;
78
+ /**
79
+ * Handler context.
80
+ */
81
+ ctx: Ctx;
82
+ /**
83
+ * Channel name being delivered.
84
+ */
85
+ channel: string;
86
+ }
87
+ /**
88
+ * Handler for one notification channel.
89
+ */
90
+ export type NotificationChannelHandler<Payload extends StandardSchema, Ctx> = (args: NotificationChannelHandleArgs<Payload, Ctx>) => MaybePromise<NotificationChannelResult | undefined>;
91
+ /**
92
+ * Notification channel handlers keyed by channel name.
93
+ */
94
+ export type NotificationChannels<Payload extends StandardSchema, Ctx> = Record<string, NotificationChannelHandler<Payload, Ctx>>;
95
+ /**
96
+ * Notification definition created by `defineNotification(...)`.
97
+ */
98
+ export interface NotificationDef<Name extends string = string, Payload extends StandardSchema = StandardSchema, Ctx = unknown> extends NotificationPayloadDef<Name, Payload> {
99
+ /**
100
+ * Discriminator for notification definitions.
101
+ */
102
+ readonly kind: "notification";
103
+ /**
104
+ * Channel handlers that deliver the notification.
105
+ */
106
+ readonly channels: NotificationChannels<Payload, Ctx>;
107
+ }
108
+ /**
109
+ * Options for declaring a typed notification.
110
+ */
111
+ export interface DefineNotificationOptions<Payload extends StandardSchema, Ctx> {
112
+ /**
113
+ * Standard Schema payload validator.
114
+ */
115
+ payload: Payload;
116
+ /**
117
+ * Optional human-readable description for docs and tooling.
118
+ */
119
+ description?: string;
120
+ /**
121
+ * Channel handlers that deliver the notification.
122
+ */
123
+ channels: NotificationChannels<Payload, Ctx>;
124
+ }
125
+ /**
126
+ * Options passed when sending a notification.
127
+ */
128
+ export interface SendNotificationOptions {
129
+ /**
130
+ * Subset of channels to deliver. Defaults to all channels on the definition.
131
+ */
132
+ channels?: readonly string[];
133
+ /**
134
+ * Optional app metadata attached to memory deliveries and instrumentation.
135
+ */
136
+ metadata?: Record<string, unknown>;
137
+ /**
138
+ * Request correlation ID for instrumentation.
139
+ */
140
+ requestId?: string;
141
+ /**
142
+ * Trace identifier for instrumentation.
143
+ */
144
+ traceId?: string;
145
+ /**
146
+ * Span identifier for instrumentation.
147
+ */
148
+ spanId?: string;
149
+ /**
150
+ * Parent span identifier for instrumentation.
151
+ */
152
+ parentSpanId?: string;
153
+ /**
154
+ * W3C traceparent header value for instrumentation.
155
+ */
156
+ traceparent?: string;
157
+ }
158
+ /**
159
+ * Result returned after a notification send attempt.
160
+ */
161
+ export interface SendNotificationResult {
162
+ /**
163
+ * Notification name.
164
+ */
165
+ notificationName: string;
166
+ /**
167
+ * Parsed notification payload.
168
+ */
169
+ payload: unknown;
170
+ /**
171
+ * Channels selected for delivery.
172
+ */
173
+ channels: readonly string[];
174
+ /**
175
+ * Per-channel delivery results.
176
+ */
177
+ results: readonly NotificationChannelResult[];
178
+ }
179
+ /**
180
+ * App-facing notification port.
181
+ */
182
+ export interface NotificationPort {
183
+ /**
184
+ * Send a typed notification.
185
+ */
186
+ send<N extends NotificationDef>(notification: N, payload: InferNotificationPayload<N>, options?: SendNotificationOptions): Promise<SendNotificationResult>;
187
+ }
188
+ /**
189
+ * Options for the inline notification dispatcher.
190
+ */
191
+ export interface InlineNotificationDispatcherOptions<Ctx> {
192
+ /**
193
+ * Static notification context or factory evaluated for each send.
194
+ */
195
+ ctx?: Ctx | (() => MaybePromise<Ctx>);
196
+ /**
197
+ * Called when a channel handler fails. When omitted, errors are rethrown to
198
+ * the caller as `NotificationDeliveryError`.
199
+ */
200
+ onError?: (error: unknown, args: NotificationChannelHandleArgs<StandardSchema, Ctx>) => MaybePromise<NotificationChannelResult | undefined>;
201
+ /**
202
+ * Optional devtools/provider instrumentation target.
203
+ */
204
+ instrumentation?: ProviderInstrumentationTarget;
205
+ }
206
+ /**
207
+ * Delivery captured by the memory notification port.
208
+ */
209
+ export interface MemoryNotificationDelivery {
210
+ /**
211
+ * Generated delivery ID.
212
+ */
213
+ id: string;
214
+ /**
215
+ * Notification name.
216
+ */
217
+ notificationName: string;
218
+ /**
219
+ * Parsed payload that would have been sent.
220
+ */
221
+ payload: unknown;
222
+ /**
223
+ * Selected channels.
224
+ */
225
+ channels: readonly string[];
226
+ /**
227
+ * Optional app metadata supplied by the caller.
228
+ */
229
+ metadata?: Record<string, unknown>;
230
+ /**
231
+ * Timestamp assigned by the memory port.
232
+ */
233
+ sentAt: Date;
234
+ }
235
+ /**
236
+ * In-memory notification port for tests and local examples.
237
+ */
238
+ export interface MemoryNotificationPort extends NotificationPort {
239
+ /**
240
+ * Captured notification sends.
241
+ */
242
+ readonly deliveries: readonly MemoryNotificationDelivery[];
243
+ /**
244
+ * Clear captured notification sends.
245
+ */
246
+ clear(): void;
247
+ }
248
+ /**
249
+ * Options for `createMemoryNotificationPort(...)`.
250
+ */
251
+ export interface CreateMemoryNotificationPortOptions {
252
+ /**
253
+ * Clock used for captured deliveries.
254
+ */
255
+ now?: () => Date;
256
+ /**
257
+ * ID factory used for captured deliveries.
258
+ */
259
+ id?: () => string;
260
+ /**
261
+ * Observer called after a delivery is captured.
262
+ */
263
+ onSend?: (delivery: MemoryNotificationDelivery) => MaybePromise<void>;
264
+ }
265
+ /**
266
+ * Context shape required by `defineMailNotificationChannel(...)`.
267
+ */
268
+ export interface MailNotificationContext {
269
+ ports: {
270
+ mailer: {
271
+ send(message: SendMailOptions): MaybePromise<{
272
+ id?: string;
273
+ provider?: string;
274
+ }>;
275
+ };
276
+ };
277
+ }
278
+ /**
279
+ * Render a mail message for one notification payload.
280
+ */
281
+ export type MailNotificationRenderer<Payload extends StandardSchema, Ctx extends MailNotificationContext> = (args: NotificationChannelHandleArgs<Payload, Ctx>) => MaybePromise<SendMailOptions | undefined>;
282
+ /**
283
+ * Context-bound notification helper factory.
284
+ */
285
+ export interface NotificationHandlers<Ctx> {
286
+ /**
287
+ * Define a notification with the bound context type.
288
+ */
289
+ defineNotification<Name extends string, Payload extends StandardSchema>(name: Name, options: DefineNotificationOptions<Payload, Ctx>): NotificationDef<Name, Payload, Ctx>;
290
+ /**
291
+ * Create an inline dispatcher with the bound context type.
292
+ */
293
+ createInlineNotificationDispatcher(options?: InlineNotificationDispatcherOptions<Ctx>): NotificationPort;
294
+ }
295
+ /**
296
+ * Error thrown when notification payload validation fails.
297
+ */
298
+ export declare class NotificationValidationError extends Error {
299
+ /**
300
+ * Raw Standard Schema validation issues.
301
+ */
302
+ readonly issues: readonly StandardSchemaV1.Issue[];
303
+ constructor(args: {
304
+ name: string;
305
+ issues: readonly StandardSchemaV1.Issue[];
306
+ });
307
+ }
308
+ /**
309
+ * Error thrown when notification delivery fails.
310
+ */
311
+ export declare class NotificationDeliveryError extends Error {
312
+ /**
313
+ * Notification name.
314
+ */
315
+ readonly notificationName: string;
316
+ /**
317
+ * Channel that failed.
318
+ */
319
+ readonly channel: string;
320
+ /**
321
+ * Original channel error.
322
+ */
323
+ readonly cause: unknown;
324
+ constructor(args: {
325
+ notificationName: string;
326
+ channel: string;
327
+ cause: unknown;
328
+ });
329
+ }
330
+ /**
331
+ * Define a typed notification.
332
+ *
333
+ * Notifications represent user-facing communication intent. Channel handlers
334
+ * decide how that intent becomes mail, SMS, push, in-app delivery, or another
335
+ * app-owned channel.
336
+ */
337
+ export declare function defineNotification<Name extends string, Payload extends StandardSchema, Ctx = unknown>(name: Name, options: DefineNotificationOptions<Payload, Ctx>): NotificationDef<Name, Payload, Ctx>;
338
+ /**
339
+ * Validate and parse a notification payload with the notification's Standard
340
+ * Schema.
341
+ */
342
+ export declare function parseNotificationPayload<N extends NotificationPayloadDef>(notification: N, payload: unknown): Promise<InferNotificationPayload<N>>;
343
+ /**
344
+ * Create an inline notification dispatcher.
345
+ *
346
+ * The dispatcher validates payloads and runs selected channel handlers
347
+ * immediately. Use this directly in tests and local apps, or wrap it in jobs
348
+ * and outbox delivery for production background execution.
349
+ */
350
+ export declare function createInlineNotificationDispatcher<Ctx>(options?: InlineNotificationDispatcherOptions<Ctx>): NotificationPort;
351
+ /**
352
+ * Define a mail-backed notification channel.
353
+ *
354
+ * Return `undefined` from the renderer when the channel should be skipped, for
355
+ * example when a recipient does not have an email address.
356
+ */
357
+ export declare function defineMailNotificationChannel<Payload extends StandardSchema, Ctx extends MailNotificationContext>(render: MailNotificationRenderer<Payload, Ctx>): NotificationChannelHandler<Payload, Ctx>;
358
+ /**
359
+ * Create an in-memory notification port for tests and examples.
360
+ *
361
+ * The memory port validates payloads and records notification intent without
362
+ * running channel handlers.
363
+ */
364
+ export declare function createMemoryNotificationPort(options?: CreateMemoryNotificationPortOptions): MemoryNotificationPort;
365
+ /**
366
+ * Create notification helper methods bound to an application context type.
367
+ */
368
+ export declare function createNotificationHandlers<Ctx>(): NotificationHandlers<Ctx>;
369
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/notifications/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,cAAc,CAAC;AAGlE;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAEhE;;GAEG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAE7C;;GAEG;AACH,MAAM,MAAM,iBAAiB,CAAC,CAAC,SAAS,gBAAgB,IACtD,gBAAgB,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;AAElC;;GAEG;AACH,MAAM,WAAW,sBAAsB,CACrC,IAAI,SAAS,MAAM,GAAG,MAAM,EAC5B,OAAO,SAAS,cAAc,GAAG,cAAc;IAE/C;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IACpB;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B;;OAEG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED;;GAEG;AACH,MAAM,MAAM,wBAAwB,CAAC,CAAC,SAAS,sBAAsB,IACnE,CAAC,CAAC,SAAS,CAAC,SAAS,gBAAgB,CAAC,OAAO,EAAE,MAAM,MAAM,CAAC,GAAG,MAAM,GAAG,KAAK,CAAC;AAEhF;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACxC;;OAEG;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AAE,MAAM,CAAC;QACzB,OAAO,EAAE,MAAM,CAAC;QAChB,KAAK,EAAE,OAAO,CAAC;KAChB;CAWF;AAyFD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,SAAS,MAAM,EACnB,OAAO,SAAS,cAAc,EAC9B,GAAG,GAAG,OAAO,EAEb,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,yBAAyB,CAAC,OAAO,EAAE,GAAG,CAAC,GAC/C,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,CAAC,CAQrC;AAED;;;GAGG;AACH,wBAAsB,wBAAwB,CAC5C,CAAC,SAAS,sBAAsB,EAChC,YAAY,EAAE,CAAC,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,wBAAwB,CAAC,CAAC,CAAC,CAAC,CAIzE;AAED;;;;;;GAMG;AACH,wBAAgB,kCAAkC,CAAC,GAAG,EACpD,OAAO,GAAE,mCAAmC,CAAC,GAAG,CAAM,GACrD,gBAAgB,CAqHlB;AAED;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAC3C,OAAO,SAAS,cAAc,EAC9B,GAAG,SAAS,uBAAuB,EAEnC,MAAM,EAAE,wBAAwB,CAAC,OAAO,EAAE,GAAG,CAAC,GAC7C,0BAA0B,CAAC,OAAO,EAAE,GAAG,CAAC,CAmB1C;AAED;;;;;GAKG;AACH,wBAAgB,4BAA4B,CAC1C,OAAO,GAAE,mCAAwC,GAChD,sBAAsB,CAiDxB;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,GAAG,KAAK,oBAAoB,CAAC,GAAG,CAAC,CAe3E"}
@@ -0,0 +1,310 @@
1
+ import { createProviderInstrumentation } from "../providers";
2
+ /**
3
+ * Error thrown when notification payload validation fails.
4
+ */
5
+ export class NotificationValidationError extends Error {
6
+ /**
7
+ * Raw Standard Schema validation issues.
8
+ */
9
+ issues;
10
+ constructor(args) {
11
+ super(`Notification "${args.name}" payload validation failed: ${formatIssues(args.issues)}`);
12
+ this.name = "NotificationValidationError";
13
+ this.issues = args.issues;
14
+ }
15
+ }
16
+ /**
17
+ * Error thrown when notification delivery fails.
18
+ */
19
+ export class NotificationDeliveryError extends Error {
20
+ /**
21
+ * Notification name.
22
+ */
23
+ notificationName;
24
+ /**
25
+ * Channel that failed.
26
+ */
27
+ channel;
28
+ /**
29
+ * Original channel error.
30
+ */
31
+ cause;
32
+ constructor(args) {
33
+ super(`Notification "${args.notificationName}" failed on channel "${args.channel}": ${args.cause instanceof Error ? args.cause.message : String(args.cause)}`);
34
+ this.name = "NotificationDeliveryError";
35
+ this.notificationName = args.notificationName;
36
+ this.channel = args.channel;
37
+ this.cause = args.cause;
38
+ }
39
+ }
40
+ function formatPath(path) {
41
+ if (!path?.length)
42
+ return "";
43
+ return path
44
+ .map((segment) => typeof segment === "object" && segment !== null && "key" in segment
45
+ ? String(segment.key)
46
+ : String(segment))
47
+ .join(".");
48
+ }
49
+ function formatIssues(issues) {
50
+ return issues
51
+ .map((issue) => {
52
+ const path = formatPath(issue.path);
53
+ return path ? `${path}: ${issue.message}` : issue.message;
54
+ })
55
+ .join("; ");
56
+ }
57
+ async function parsePayload(schema, input, args) {
58
+ const result = await schema["~standard"].validate(input);
59
+ if (result.issues?.length) {
60
+ throw new NotificationValidationError({
61
+ name: args.name,
62
+ issues: result.issues,
63
+ });
64
+ }
65
+ if ("value" in result) {
66
+ return result.value;
67
+ }
68
+ throw new Error("Invalid Standard Schema result: missing value");
69
+ }
70
+ async function resolveCtx(ctx) {
71
+ if (typeof ctx === "function") {
72
+ return ctx();
73
+ }
74
+ return ctx;
75
+ }
76
+ function resolveChannelNames(notification, options) {
77
+ const available = Object.keys(notification.channels);
78
+ const selected = options?.channels ?? available;
79
+ for (const channel of selected) {
80
+ if (!notification.channels[channel]) {
81
+ throw new Error(`Notification "${notification.name}" does not define channel "${channel}".`);
82
+ }
83
+ }
84
+ return selected;
85
+ }
86
+ function summarizeResults(results) {
87
+ const sent = results.filter((result) => result.status === "sent").length;
88
+ const skipped = results.filter((result) => result.status === "skipped").length;
89
+ const failed = results.filter((result) => result.status === "failed").length;
90
+ return { sent, skipped, failed };
91
+ }
92
+ /**
93
+ * Define a typed notification.
94
+ *
95
+ * Notifications represent user-facing communication intent. Channel handlers
96
+ * decide how that intent becomes mail, SMS, push, in-app delivery, or another
97
+ * app-owned channel.
98
+ */
99
+ export function defineNotification(name, options) {
100
+ return {
101
+ kind: "notification",
102
+ name,
103
+ payload: options.payload,
104
+ description: options.description,
105
+ channels: options.channels,
106
+ };
107
+ }
108
+ /**
109
+ * Validate and parse a notification payload with the notification's Standard
110
+ * Schema.
111
+ */
112
+ export async function parseNotificationPayload(notification, payload) {
113
+ return (await parsePayload(notification.payload, payload, {
114
+ name: notification.name,
115
+ }));
116
+ }
117
+ /**
118
+ * Create an inline notification dispatcher.
119
+ *
120
+ * The dispatcher validates payloads and runs selected channel handlers
121
+ * immediately. Use this directly in tests and local apps, or wrap it in jobs
122
+ * and outbox delivery for production background execution.
123
+ */
124
+ export function createInlineNotificationDispatcher(options = {}) {
125
+ const instrumentation = createProviderInstrumentation(options.instrumentation, {
126
+ providerName: "notifications",
127
+ watcher: "notifications",
128
+ });
129
+ return {
130
+ async send(notification, payload, sendOptions = {}) {
131
+ const parsed = await parseNotificationPayload(notification, payload);
132
+ const channels = resolveChannelNames(notification, sendOptions);
133
+ const ctx = await resolveCtx(options.ctx);
134
+ const results = [];
135
+ instrumentation.custom({
136
+ name: "notification.send.started",
137
+ label: "Notification started",
138
+ summary: notification.name,
139
+ requestId: sendOptions.requestId,
140
+ traceId: sendOptions.traceId,
141
+ spanId: sendOptions.spanId,
142
+ parentSpanId: sendOptions.parentSpanId,
143
+ traceparent: sendOptions.traceparent,
144
+ details: {
145
+ notificationName: notification.name,
146
+ channels,
147
+ metadata: sendOptions.metadata,
148
+ },
149
+ });
150
+ for (const channel of channels) {
151
+ const handler = notification.channels[channel];
152
+ try {
153
+ const result = await handler({
154
+ notification,
155
+ payload: parsed,
156
+ ctx,
157
+ channel,
158
+ });
159
+ results.push(result ?? {
160
+ channel,
161
+ status: "sent",
162
+ });
163
+ }
164
+ catch (error) {
165
+ const args = {
166
+ notification,
167
+ payload: parsed,
168
+ ctx,
169
+ channel,
170
+ };
171
+ const handled = await options.onError?.(error, args);
172
+ if (handled) {
173
+ results.push(handled);
174
+ continue;
175
+ }
176
+ instrumentation.custom({
177
+ name: "notification.send.failed",
178
+ label: "Notification failed",
179
+ summary: `${notification.name} (${channel})`,
180
+ requestId: sendOptions.requestId,
181
+ traceId: sendOptions.traceId,
182
+ spanId: sendOptions.spanId,
183
+ parentSpanId: sendOptions.parentSpanId,
184
+ traceparent: sendOptions.traceparent,
185
+ details: {
186
+ notificationName: notification.name,
187
+ channel,
188
+ error: error instanceof Error ? error.message : String(error),
189
+ metadata: sendOptions.metadata,
190
+ },
191
+ });
192
+ throw new NotificationDeliveryError({
193
+ notificationName: notification.name,
194
+ channel,
195
+ cause: error,
196
+ });
197
+ }
198
+ }
199
+ const summary = summarizeResults(results);
200
+ instrumentation.custom({
201
+ name: "notification.send.completed",
202
+ label: "Notification sent",
203
+ summary: `${notification.name} (${summary.sent} sent, ${summary.skipped} skipped, ${summary.failed} failed)`,
204
+ requestId: sendOptions.requestId,
205
+ traceId: sendOptions.traceId,
206
+ spanId: sendOptions.spanId,
207
+ parentSpanId: sendOptions.parentSpanId,
208
+ traceparent: sendOptions.traceparent,
209
+ details: {
210
+ notificationName: notification.name,
211
+ channels,
212
+ results,
213
+ metadata: sendOptions.metadata,
214
+ },
215
+ });
216
+ return {
217
+ notificationName: notification.name,
218
+ payload: parsed,
219
+ channels,
220
+ results,
221
+ };
222
+ },
223
+ };
224
+ }
225
+ /**
226
+ * Define a mail-backed notification channel.
227
+ *
228
+ * Return `undefined` from the renderer when the channel should be skipped, for
229
+ * example when a recipient does not have an email address.
230
+ */
231
+ export function defineMailNotificationChannel(render) {
232
+ return async (args) => {
233
+ const message = await render(args);
234
+ if (!message) {
235
+ return {
236
+ channel: args.channel,
237
+ status: "skipped",
238
+ reason: "No mail message was returned.",
239
+ };
240
+ }
241
+ const result = await args.ctx.ports.mailer.send(message);
242
+ return {
243
+ channel: args.channel,
244
+ status: "sent",
245
+ id: result.id,
246
+ provider: result.provider,
247
+ };
248
+ };
249
+ }
250
+ /**
251
+ * Create an in-memory notification port for tests and examples.
252
+ *
253
+ * The memory port validates payloads and records notification intent without
254
+ * running channel handlers.
255
+ */
256
+ export function createMemoryNotificationPort(options = {}) {
257
+ const deliveries = [];
258
+ const now = options.now ?? (() => new Date());
259
+ const id = options.id ?? (() => crypto.randomUUID());
260
+ return {
261
+ get deliveries() {
262
+ return deliveries;
263
+ },
264
+ async send(notification, payload, sendOptions = {}) {
265
+ const parsed = await parseNotificationPayload(notification, payload);
266
+ const channels = resolveChannelNames(notification, sendOptions);
267
+ const delivery = {
268
+ id: id(),
269
+ notificationName: notification.name,
270
+ payload: parsed,
271
+ channels,
272
+ metadata: sendOptions.metadata,
273
+ sentAt: now(),
274
+ };
275
+ deliveries.push(delivery);
276
+ await options.onSend?.(delivery);
277
+ return {
278
+ notificationName: notification.name,
279
+ payload: parsed,
280
+ channels,
281
+ results: channels.map((channel) => ({
282
+ channel,
283
+ status: "sent",
284
+ id: delivery.id,
285
+ provider: "memory",
286
+ details: {
287
+ mode: "intent",
288
+ },
289
+ })),
290
+ };
291
+ },
292
+ clear() {
293
+ deliveries.length = 0;
294
+ },
295
+ };
296
+ }
297
+ /**
298
+ * Create notification helper methods bound to an application context type.
299
+ */
300
+ export function createNotificationHandlers() {
301
+ return {
302
+ defineNotification(name, options) {
303
+ return defineNotification(name, options);
304
+ },
305
+ createInlineNotificationDispatcher(options = {}) {
306
+ return createInlineNotificationDispatcher(options);
307
+ },
308
+ };
309
+ }
310
+ //# sourceMappingURL=index.js.map