@beignet/core 0.0.1 → 0.0.2

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 (274) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +149 -4
  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 +110 -0
  84. package/dist/jobs/index.d.ts.map +1 -1
  85. package/dist/jobs/index.js +22 -0
  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 +469 -0
  100. package/dist/outbox/index.d.ts.map +1 -0
  101. package/dist/outbox/index.js +482 -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 +204 -0
  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 +57 -0
  175. package/dist/server/hooks/auth.d.ts.map +1 -1
  176. package/dist/server/hooks/auth.js.map +1 -1
  177. package/dist/server/hooks/cors.d.ts +27 -0
  178. package/dist/server/hooks/cors.d.ts.map +1 -1
  179. package/dist/server/hooks/cors.js +12 -0
  180. package/dist/server/hooks/cors.js.map +1 -1
  181. package/dist/server/hooks/errors.d.ts +15 -6
  182. package/dist/server/hooks/errors.d.ts.map +1 -1
  183. package/dist/server/hooks/errors.js.map +1 -1
  184. package/dist/server/hooks/index.d.ts +3 -0
  185. package/dist/server/hooks/index.d.ts.map +1 -1
  186. package/dist/server/hooks/index.js +3 -0
  187. package/dist/server/hooks/index.js.map +1 -1
  188. package/dist/server/hooks/logging.d.ts +36 -0
  189. package/dist/server/hooks/logging.d.ts.map +1 -1
  190. package/dist/server/hooks/logging.js +6 -0
  191. package/dist/server/hooks/logging.js.map +1 -1
  192. package/dist/server/hooks/rate-limit.d.ts +33 -0
  193. package/dist/server/hooks/rate-limit.d.ts.map +1 -1
  194. package/dist/server/hooks/rate-limit.js +11 -0
  195. package/dist/server/hooks/rate-limit.js.map +1 -1
  196. package/dist/server/http.d.ts +170 -0
  197. package/dist/server/http.d.ts.map +1 -1
  198. package/dist/server/index.d.ts +18 -0
  199. package/dist/server/index.d.ts.map +1 -1
  200. package/dist/server/index.js +6 -0
  201. package/dist/server/index.js.map +1 -1
  202. package/dist/server/openapi.d.ts +5 -3
  203. package/dist/server/openapi.d.ts.map +1 -1
  204. package/dist/server/openapi.js +4 -2
  205. package/dist/server/openapi.js.map +1 -1
  206. package/dist/server/providers/loadProviderConfig.d.ts +9 -0
  207. package/dist/server/providers/loadProviderConfig.d.ts.map +1 -1
  208. package/dist/server/providers/loadProviderConfig.js +9 -0
  209. package/dist/server/providers/loadProviderConfig.js.map +1 -1
  210. package/dist/server/server.d.ts +107 -8
  211. package/dist/server/server.d.ts.map +1 -1
  212. package/dist/server/server.js +27 -7
  213. package/dist/server/server.js.map +1 -1
  214. package/dist/testing/index.d.ts +167 -0
  215. package/dist/testing/index.d.ts.map +1 -0
  216. package/dist/testing/index.js +119 -0
  217. package/dist/testing/index.js.map +1 -0
  218. package/package.json +21 -1
  219. package/src/application/index.ts +85 -22
  220. package/src/client/client.ts +73 -12
  221. package/src/client/index.ts +12 -0
  222. package/src/client/types.ts +70 -9
  223. package/src/config/index.ts +86 -0
  224. package/src/contracts/contract-builder.ts +49 -22
  225. package/src/contracts/contract-group.ts +35 -19
  226. package/src/contracts/contract-like.ts +4 -4
  227. package/src/contracts/index.ts +28 -1
  228. package/src/contracts/openapi-meta.ts +8 -8
  229. package/src/contracts/path-template.ts +27 -0
  230. package/src/contracts/types.ts +111 -10
  231. package/src/contracts/utils.ts +6 -0
  232. package/src/domain/entity.ts +22 -11
  233. package/src/domain/events.ts +5 -2
  234. package/src/domain/value-object.ts +19 -9
  235. package/src/errors/catalog.ts +40 -16
  236. package/src/errors/response.ts +16 -4
  237. package/src/errors/validation.ts +10 -1
  238. package/src/events/index.ts +134 -0
  239. package/src/idempotency/index.ts +767 -0
  240. package/src/jobs/index.ts +111 -0
  241. package/src/mail/index.ts +149 -0
  242. package/src/notifications/index.ts +771 -0
  243. package/src/openapi/index.ts +133 -16
  244. package/src/outbox/index.ts +1024 -0
  245. package/src/pagination/index.ts +278 -0
  246. package/src/ports/audit.ts +271 -0
  247. package/src/ports/auth.ts +70 -0
  248. package/src/ports/cache.ts +41 -0
  249. package/src/ports/clock.ts +38 -0
  250. package/src/ports/id-generator.ts +37 -0
  251. package/src/ports/index.ts +106 -11
  252. package/src/ports/logger.ts +56 -0
  253. package/src/ports/policy.ts +133 -0
  254. package/src/ports/rate-limit.ts +25 -0
  255. package/src/ports/redaction.ts +101 -0
  256. package/src/ports/storage.ts +100 -0
  257. package/src/ports/testing.ts +47 -0
  258. package/src/ports/unit-of-work.ts +60 -3
  259. package/src/providers/instrumentation.ts +204 -0
  260. package/src/providers/provider.ts +14 -1
  261. package/src/schedules/index.ts +247 -0
  262. package/src/server/health.ts +14 -5
  263. package/src/server/hooks/auth.ts +58 -0
  264. package/src/server/hooks/cors.ts +27 -0
  265. package/src/server/hooks/errors.ts +15 -6
  266. package/src/server/hooks/index.ts +3 -0
  267. package/src/server/hooks/logging.ts +36 -0
  268. package/src/server/hooks/rate-limit.ts +33 -0
  269. package/src/server/http.ts +170 -1
  270. package/src/server/index.ts +18 -1
  271. package/src/server/openapi.ts +5 -3
  272. package/src/server/providers/loadProviderConfig.ts +9 -0
  273. package/src/server/server.ts +107 -9
  274. package/src/testing/index.ts +337 -0
@@ -1,155 +1,381 @@
1
1
  import type { StandardSchemaV1 } from "@standard-schema/spec";
2
2
 
3
+ /**
4
+ * Any Standard Schema compatible validator.
5
+ */
3
6
  export type StandardSchema = StandardSchemaV1<unknown, unknown>;
7
+
8
+ /**
9
+ * Value or promise of that value.
10
+ */
4
11
  export type MaybePromise<T> = T | Promise<T>;
5
12
 
13
+ /**
14
+ * Infer the parsed output type from a Standard Schema.
15
+ */
6
16
  export type InferSchemaOutput<T extends StandardSchemaV1> =
7
17
  StandardSchemaV1.InferOutput<T>;
8
18
 
19
+ /**
20
+ * Date input accepted by schedule runners.
21
+ */
9
22
  export type ScheduleDateInput = Date | string | number;
10
23
 
24
+ /**
25
+ * Metadata for one schedule run.
26
+ */
11
27
  export interface ScheduleRunContext {
28
+ /**
29
+ * Optional provider run ID.
30
+ */
12
31
  readonly id?: string;
32
+ /**
33
+ * Time the provider planned the run.
34
+ */
13
35
  readonly scheduledAt?: Date;
36
+ /**
37
+ * Time the runner triggered this execution.
38
+ */
14
39
  readonly triggeredAt: Date;
40
+ /**
41
+ * Optional provider or app source label.
42
+ */
15
43
  readonly source?: string;
16
44
  }
17
45
 
46
+ /**
47
+ * Minimal schedule definition shape accepted by schedule helpers.
48
+ */
18
49
  export interface SchedulePayloadDef<
19
50
  Name extends string = string,
20
51
  Payload extends StandardSchema = StandardSchema,
21
52
  > {
53
+ /**
54
+ * Stable schedule name.
55
+ */
22
56
  readonly name: Name;
57
+ /**
58
+ * Standard Schema payload validator.
59
+ */
23
60
  readonly payload: Payload;
24
61
  }
25
62
 
63
+ /**
64
+ * Schedule definition created by `defineSchedule(...)`.
65
+ */
26
66
  export interface ScheduleDef<
27
67
  Name extends string = string,
28
68
  Payload extends StandardSchema = StandardSchema,
29
69
  Ctx = unknown,
30
70
  > extends SchedulePayloadDef<Name, Payload> {
71
+ /**
72
+ * Discriminator for schedule definitions.
73
+ */
31
74
  readonly kind: "schedule";
75
+ /**
76
+ * Cron expression consumed by schedule providers.
77
+ */
32
78
  readonly cron: string;
79
+ /**
80
+ * Optional IANA timezone consumed by schedule providers.
81
+ */
33
82
  readonly timezone?: string;
83
+ /**
84
+ * Optional human-readable description for docs and tooling.
85
+ */
34
86
  readonly description?: string;
87
+ /**
88
+ * Build a payload when the provider does not supply one.
89
+ */
35
90
  createPayload?(
36
91
  args: ScheduleCreatePayloadArgs<ScheduleDef<Name, Payload, Ctx>>,
37
92
  ): MaybePromise<InferSchemaOutput<Payload>>;
93
+ /**
94
+ * Handle a parsed schedule payload.
95
+ */
38
96
  handle(
39
97
  args: ScheduleHandleArgs<ScheduleDef<Name, Payload, Ctx>, Ctx>,
40
98
  ): MaybePromise<void>;
41
99
  }
42
100
 
101
+ /**
102
+ * Infer the parsed payload type for a schedule definition.
103
+ */
43
104
  export type InferSchedulePayload<S extends SchedulePayloadDef> =
44
105
  S["payload"] extends StandardSchemaV1<unknown, infer Output> ? Output : never;
45
106
 
107
+ /**
108
+ * Arguments passed to a schedule `createPayload` callback.
109
+ */
46
110
  export interface ScheduleCreatePayloadArgs<S extends SchedulePayloadDef> {
111
+ /**
112
+ * Schedule definition being run.
113
+ */
47
114
  schedule: S;
115
+ /**
116
+ * Run metadata.
117
+ */
48
118
  run: ScheduleRunContext;
49
119
  }
50
120
 
121
+ /**
122
+ * Arguments passed to a schedule handler.
123
+ */
51
124
  export interface ScheduleHandleArgs<S extends ScheduleDef, Ctx> {
125
+ /**
126
+ * Schedule definition being handled.
127
+ */
52
128
  schedule: S;
129
+ /**
130
+ * Parsed schedule payload.
131
+ */
53
132
  payload: InferSchedulePayload<S>;
133
+ /**
134
+ * Handler context.
135
+ */
54
136
  ctx: Ctx;
137
+ /**
138
+ * Run metadata.
139
+ */
55
140
  run: ScheduleRunContext;
56
141
  }
57
142
 
143
+ /**
144
+ * Options for `defineSchedule(...)`.
145
+ */
58
146
  export interface DefineScheduleOptions<
59
147
  Name extends string,
60
148
  Payload extends StandardSchema,
61
149
  Ctx,
62
150
  > {
151
+ /**
152
+ * Cron expression consumed by schedule providers.
153
+ */
63
154
  cron: string;
155
+ /**
156
+ * Optional IANA timezone consumed by schedule providers.
157
+ */
64
158
  timezone?: string;
159
+ /**
160
+ * Standard Schema payload validator.
161
+ */
65
162
  payload: Payload;
163
+ /**
164
+ * Optional human-readable description for docs and tooling.
165
+ */
66
166
  description?: string;
167
+ /**
168
+ * Build a payload when the provider does not supply one.
169
+ */
67
170
  createPayload?(
68
171
  args: ScheduleCreatePayloadArgs<ScheduleDef<Name, Payload, Ctx>>,
69
172
  ): MaybePromise<InferSchemaOutput<Payload>>;
173
+ /**
174
+ * Handle a parsed schedule payload.
175
+ */
70
176
  handle(
71
177
  args: ScheduleHandleArgs<ScheduleDef<Name, Payload, Ctx>, Ctx>,
72
178
  ): MaybePromise<void>;
73
179
  }
74
180
 
181
+ /**
182
+ * Options for one manual schedule run.
183
+ */
75
184
  export interface ScheduleRunOptions<Payload = unknown> {
185
+ /**
186
+ * Payload supplied by the provider or manual runner.
187
+ */
76
188
  payload?: Payload;
189
+ /**
190
+ * Optional provider run ID.
191
+ */
77
192
  id?: string;
193
+ /**
194
+ * Time the provider planned the run.
195
+ */
78
196
  scheduledAt?: ScheduleDateInput;
197
+ /**
198
+ * Time the runner triggered the execution.
199
+ */
79
200
  triggeredAt?: ScheduleDateInput;
201
+ /**
202
+ * Optional provider or app source label.
203
+ */
80
204
  source?: string;
81
205
  }
82
206
 
207
+ /**
208
+ * Arguments for `runSchedule(...)`.
209
+ */
83
210
  export type ScheduleRunArgs<
84
211
  Ctx,
85
212
  Payload = unknown,
86
213
  > = ScheduleRunOptions<Payload> & {
214
+ /**
215
+ * Handler context.
216
+ */
87
217
  ctx: Ctx;
88
218
  };
89
219
 
220
+ /**
221
+ * Arguments passed to schedule lifecycle hooks.
222
+ */
90
223
  export interface ScheduleLifecycleArgs<S extends ScheduleDef = ScheduleDef> {
224
+ /**
225
+ * Schedule definition being run.
226
+ */
91
227
  schedule: S;
228
+ /**
229
+ * Parsed schedule payload.
230
+ */
92
231
  payload: InferSchedulePayload<S>;
232
+ /**
233
+ * Run metadata.
234
+ */
93
235
  run: ScheduleRunContext;
94
236
  }
95
237
 
238
+ /**
239
+ * Arguments passed to a schedule error hook.
240
+ */
96
241
  export interface ScheduleErrorArgs<S extends ScheduleDef = ScheduleDef> {
242
+ /**
243
+ * Schedule definition being run.
244
+ */
97
245
  schedule: S;
246
+ /**
247
+ * Parsed payload when validation or payload creation completed.
248
+ */
98
249
  payload?: InferSchedulePayload<S>;
250
+ /**
251
+ * Run metadata.
252
+ */
99
253
  run: ScheduleRunContext;
254
+ /**
255
+ * Error thrown by payload creation, validation, or the handler.
256
+ */
100
257
  error: unknown;
101
258
  }
102
259
 
260
+ /**
261
+ * Schedule lifecycle hook names.
262
+ */
103
263
  export type ScheduleHookName = "start" | "success" | "error";
104
264
 
265
+ /**
266
+ * Arguments passed when a schedule lifecycle hook itself fails.
267
+ */
105
268
  export interface ScheduleHookErrorArgs<S extends ScheduleDef = ScheduleDef> {
269
+ /**
270
+ * Schedule definition being run.
271
+ */
106
272
  schedule: S;
273
+ /**
274
+ * Parsed payload when available.
275
+ */
107
276
  payload?: InferSchedulePayload<S>;
277
+ /**
278
+ * Run metadata.
279
+ */
108
280
  run: ScheduleRunContext;
281
+ /**
282
+ * Lifecycle hook that failed.
283
+ */
109
284
  hook: ScheduleHookName;
285
+ /**
286
+ * Hook error.
287
+ */
110
288
  error: unknown;
289
+ /**
290
+ * Original schedule error when the failing hook is `onError`.
291
+ */
111
292
  scheduleError?: unknown;
112
293
  }
113
294
 
295
+ /**
296
+ * Options for the inline schedule runner.
297
+ */
114
298
  export interface InlineScheduleRunnerOptions<Ctx> {
299
+ /**
300
+ * Static schedule context or factory evaluated for each run.
301
+ */
115
302
  ctx?: Ctx | (() => MaybePromise<Ctx>);
303
+ /**
304
+ * Clock used when run timestamps are not provided.
305
+ */
116
306
  now?: () => Date;
307
+ /**
308
+ * Called after payload validation and before the schedule handler.
309
+ */
117
310
  onStart?<S extends ScheduleDef<string, StandardSchema, Ctx>>(
118
311
  args: ScheduleLifecycleArgs<S>,
119
312
  ): MaybePromise<void>;
313
+ /**
314
+ * Called after the schedule handler completes.
315
+ */
120
316
  onSuccess?<S extends ScheduleDef<string, StandardSchema, Ctx>>(
121
317
  args: ScheduleLifecycleArgs<S>,
122
318
  ): MaybePromise<void>;
319
+ /**
320
+ * Called when payload creation, validation, or the handler fails.
321
+ */
123
322
  onError?<S extends ScheduleDef<string, StandardSchema, Ctx>>(
124
323
  args: ScheduleErrorArgs<S>,
125
324
  ): MaybePromise<void>;
325
+ /**
326
+ * Called when a lifecycle hook throws.
327
+ */
126
328
  onHookError?<S extends ScheduleDef<string, StandardSchema, Ctx>>(
127
329
  args: ScheduleHookErrorArgs<S>,
128
330
  ): MaybePromise<void>;
129
331
  }
130
332
 
333
+ /**
334
+ * Port shape for running schedules.
335
+ */
131
336
  export interface ScheduleRunnerPort<Ctx = unknown> {
337
+ /**
338
+ * Run a schedule with optional provider metadata and payload.
339
+ */
132
340
  run<S extends ScheduleDef<string, StandardSchema, Ctx>>(
133
341
  schedule: S,
134
342
  options?: ScheduleRunOptions<InferSchedulePayload<S>>,
135
343
  ): Promise<void>;
136
344
  }
137
345
 
346
+ /**
347
+ * Local/test schedule runner that executes handlers inline.
348
+ */
138
349
  export interface InlineScheduleRunner<Ctx = unknown>
139
350
  extends ScheduleRunnerPort<Ctx> {}
140
351
 
352
+ /**
353
+ * Context-bound schedule helper factory.
354
+ */
141
355
  export interface ScheduleHandlers<Ctx> {
356
+ /**
357
+ * Define a schedule with the bound context type.
358
+ */
142
359
  defineSchedule<Name extends string, Payload extends StandardSchema>(
143
360
  name: Name,
144
361
  options: DefineScheduleOptions<Name, Payload, Ctx>,
145
362
  ): ScheduleDef<Name, Payload, Ctx>;
146
363
 
364
+ /**
365
+ * Create an inline schedule runner with the bound context type.
366
+ */
147
367
  createInlineScheduleRunner(
148
368
  options?: InlineScheduleRunnerOptions<Ctx>,
149
369
  ): InlineScheduleRunner<Ctx>;
150
370
  }
151
371
 
372
+ /**
373
+ * Error thrown when schedule payload validation fails.
374
+ */
152
375
  export class ScheduleValidationError extends Error {
376
+ /**
377
+ * Raw Standard Schema validation issues.
378
+ */
153
379
  readonly issues: readonly StandardSchemaV1.Issue[];
154
380
 
155
381
  constructor(args: {
@@ -164,6 +390,9 @@ export class ScheduleValidationError extends Error {
164
390
  }
165
391
  }
166
392
 
393
+ /**
394
+ * Error thrown when schedule run metadata cannot be normalized.
395
+ */
167
396
  export class ScheduleRunContextError extends Error {
168
397
  constructor(message: string) {
169
398
  super(message);
@@ -327,6 +556,12 @@ async function runErrorHook<
327
556
  }
328
557
  }
329
558
 
559
+ /**
560
+ * Define a typed schedule.
561
+ *
562
+ * Cron and timezone are metadata for schedule providers. The inline runner only
563
+ * runs schedules when its `run(...)` method is called.
564
+ */
330
565
  export function defineSchedule<
331
566
  Name extends string,
332
567
  Payload extends StandardSchema,
@@ -349,6 +584,9 @@ export function defineSchedule<
349
584
  };
350
585
  }
351
586
 
587
+ /**
588
+ * Validate and parse a schedule payload with the schedule's Standard Schema.
589
+ */
352
590
  export async function parseSchedulePayload<S extends SchedulePayloadDef>(
353
591
  schedule: S,
354
592
  payload: unknown,
@@ -358,6 +596,9 @@ export async function parseSchedulePayload<S extends SchedulePayloadDef>(
358
596
  })) as InferSchedulePayload<S>;
359
597
  }
360
598
 
599
+ /**
600
+ * Run one schedule directly with an explicit context.
601
+ */
361
602
  export async function runSchedule<
362
603
  Ctx,
363
604
  S extends ScheduleDef<string, StandardSchema, Ctx>,
@@ -376,6 +617,9 @@ export async function runSchedule<
376
617
  });
377
618
  }
378
619
 
620
+ /**
621
+ * Create a local/test schedule runner that executes handlers inline.
622
+ */
379
623
  export function createInlineScheduleRunner<Ctx>(
380
624
  options: InlineScheduleRunnerOptions<Ctx> = {},
381
625
  ): InlineScheduleRunner<Ctx> {
@@ -424,6 +668,9 @@ export function createInlineScheduleRunner<Ctx>(
424
668
  };
425
669
  }
426
670
 
671
+ /**
672
+ * Create schedule helper methods bound to an application context type.
673
+ */
427
674
  export function createScheduleHandlers<Ctx>(): ScheduleHandlers<Ctx> {
428
675
  return {
429
676
  defineSchedule<Name extends string, Payload extends StandardSchema>(
@@ -7,15 +7,21 @@ import type { AnyPorts } from "../ports";
7
7
  import type { HttpRequestLike, HttpResponseLike } from "./types";
8
8
 
9
9
  /**
10
- * Health check result
10
+ * Health check result returned by health handlers.
11
11
  */
12
12
  export interface HealthCheckResult {
13
+ /**
14
+ * Whether the app is healthy.
15
+ */
13
16
  ok: boolean;
17
+ /**
18
+ * Optional per-dependency health details.
19
+ */
14
20
  details?: Record<string, { ok: boolean; message?: string }>;
15
21
  }
16
22
 
17
23
  /**
18
- * Health check configuration
24
+ * Health check configuration.
19
25
  */
20
26
  export interface HealthConfig<Ports> {
21
27
  /** Enable health endpoint (default: false) */
@@ -31,13 +37,16 @@ export interface HealthConfig<Ports> {
31
37
  }
32
38
 
33
39
  /**
34
- * Application environment type
40
+ * Application environment.
35
41
  */
36
42
  export type AppEnvironment = "development" | "production" | "test";
37
43
 
38
44
  /**
39
- * Create a health check handler
40
- * Returns a function that can be called with an HttpRequestLike to get an HttpResponseLike
45
+ * Create a framework-neutral health check handler.
46
+ *
47
+ * The returned handler reports 200 when healthy and 503 when unhealthy. Thrown
48
+ * health check errors include details in development/test and use a generic
49
+ * message in production.
41
50
  */
42
51
  export function createHealthHandler<Ports extends AnyPorts>(
43
52
  ports: Ports,
@@ -7,15 +7,32 @@ import type { HttpRequestLike, HttpResponse, ServerHook } from "../types";
7
7
 
8
8
  type MaybePromise<T> = T | Promise<T>;
9
9
 
10
+ /**
11
+ * Authentication mode for one matched contract.
12
+ */
10
13
  export type AuthHookMode = "public" | "optional" | "required";
14
+
15
+ /**
16
+ * Input accepted when resolving auth mode from contract metadata or options.
17
+ *
18
+ * `true` maps to `"required"`; `false`, `null`, and `undefined` map to
19
+ * `"public"`.
20
+ */
11
21
  export type AuthHookModeInput = AuthHookMode | boolean | null | undefined;
12
22
 
23
+ /**
24
+ * Minimal context shape required when `createAuthHooks(...)` reads
25
+ * `ctx.ports.auth`.
26
+ */
13
27
  export type CtxWithAuthPort = {
14
28
  ports: {
15
29
  auth: AuthPort;
16
30
  };
17
31
  };
18
32
 
33
+ /**
34
+ * Arguments passed to auth hook callbacks.
35
+ */
19
36
  export type AuthHookArgs<Ctx> = {
20
37
  req: HttpRequestLike;
21
38
  ctx: Ctx;
@@ -28,20 +45,48 @@ export type AuthHookArgs<Ctx> = {
28
45
  body: unknown;
29
46
  };
30
47
 
48
+ /**
49
+ * Arguments passed to the auth `assign` callback.
50
+ */
31
51
  export type AuthHookAssignArgs<Ctx, Session> = AuthHookArgs<Ctx> & {
32
52
  session: Session | null;
33
53
  };
34
54
 
55
+ /**
56
+ * Arguments passed to the auth `unauthorized` callback.
57
+ */
35
58
  export type AuthHookUnauthorizedArgs<Ctx, Session> = AuthHookArgs<Ctx> & {
36
59
  session: Session | null;
37
60
  };
38
61
 
62
+ /**
63
+ * Options for `createAuthHooks(...)`.
64
+ */
39
65
  export type AuthHooksOptions<Ctx, Session> = {
66
+ /**
67
+ * Hook name used in diagnostics.
68
+ */
40
69
  name?: string;
70
+ /**
71
+ * Resolve whether the current contract is public, optional-auth, or required-auth.
72
+ */
41
73
  mode?: (args: AuthHookArgs<Ctx>) => MaybePromise<AuthHookModeInput>;
74
+ /**
75
+ * Custom session lookup. Required when context does not expose
76
+ * `ctx.ports.auth`.
77
+ */
42
78
  getSession?: (args: AuthHookArgs<Ctx>) => MaybePromise<Session | null>;
79
+ /**
80
+ * Decide whether a resolved session counts as authenticated.
81
+ */
43
82
  isAuthenticated?: (session: Session | null) => boolean;
83
+ /**
84
+ * Return an updated context with auth/session fields attached.
85
+ */
44
86
  assign?: (args: AuthHookAssignArgs<Ctx, Session>) => MaybePromise<Ctx>;
87
+ /**
88
+ * Return a custom unauthorized response for required-auth routes.
89
+ */
45
90
  unauthorized?: (
46
91
  args: AuthHookUnauthorizedArgs<Ctx, Session>,
47
92
  ) => MaybePromise<HttpResponse>;
@@ -79,6 +124,19 @@ function defaultIsAuthenticated<Session>(session: Session | null): boolean {
79
124
  return session !== null;
80
125
  }
81
126
 
127
+ /**
128
+ * Create metadata-driven authentication hooks.
129
+ *
130
+ * By default the hook reads `contract.metadata.auth`: `"required"` or `true`
131
+ * requires an authenticated session, `"optional"` attaches a session when one
132
+ * exists, and missing/false metadata treats the route as public. The hook
133
+ * identifies a user; business authorization still belongs in policies or use
134
+ * cases.
135
+ *
136
+ * @param options - Optional auth mode, session lookup, context assignment, and
137
+ * unauthorized response customization.
138
+ * @returns A server hook that runs before route handlers.
139
+ */
82
140
  export function createAuthHooks<Ctx extends CtxWithAuthPort>(
83
141
  options?: AuthHooksOptions<Ctx, InferAuthSession<Ctx["ports"]["auth"]>>,
84
142
  ): ServerHook<Ctx, Ctx["ports"]>;
@@ -4,10 +4,25 @@
4
4
 
5
5
  import type { HttpRequestLike, ServerHook } from "../types";
6
6
 
7
+ /**
8
+ * CORS configuration for `createCorsHooks(...)`.
9
+ */
7
10
  export interface CorsConfig {
11
+ /**
12
+ * Allowed origins. Use `"*"` to allow any origin.
13
+ */
8
14
  origins?: string[] | "*";
15
+ /**
16
+ * Allowed HTTP methods.
17
+ */
9
18
  methods?: string[];
19
+ /**
20
+ * Allowed request headers.
21
+ */
10
22
  headers?: string[];
23
+ /**
24
+ * Whether credentialed requests are allowed.
25
+ */
11
26
  credentials?: boolean;
12
27
  }
13
28
 
@@ -28,6 +43,12 @@ function appendVaryOrigin(headers: Record<string, string>): void {
28
43
  headers[varyKey] = current ? `${current}, Origin` : "Origin";
29
44
  }
30
45
 
46
+ /**
47
+ * Apply CORS response headers to a mutable header record.
48
+ *
49
+ * When credentials are enabled with wildcard origins, the request origin is
50
+ * reflected and `Vary: Origin` is appended.
51
+ */
31
52
  export function applyCorsHeaders(
32
53
  headers: Record<string, string>,
33
54
  req: HttpRequestLike,
@@ -62,6 +83,12 @@ export function applyCorsHeaders(
62
83
  }
63
84
  }
64
85
 
86
+ /**
87
+ * Create CORS hooks for preflight and regular responses.
88
+ *
89
+ * `OPTIONS` requests short-circuit with a 204 response. All other responses are
90
+ * decorated in `beforeSend`.
91
+ */
65
92
  export function createCorsHooks<Ctx>(config: CorsConfig): ServerHook<Ctx> {
66
93
  return {
67
94
  name: "cors",
@@ -7,30 +7,39 @@ import type { AppEnvironment } from "../health";
7
7
  import { getRequestIdFromContext } from "./utils";
8
8
 
9
9
  /**
10
- * Re-export AppEnvironment for convenience
10
+ * Re-export `AppEnvironment` for convenience.
11
11
  */
12
12
  export type { AppEnvironment } from "../health";
13
13
 
14
14
  /**
15
- * Error mapping result
15
+ * Framework-neutral response produced by an error mapper.
16
16
  */
17
17
  export interface ErrorMappingResult {
18
+ /**
19
+ * HTTP status code.
20
+ */
18
21
  status: number;
22
+ /**
23
+ * Response body.
24
+ */
19
25
  body: unknown;
26
+ /**
27
+ * Response headers.
28
+ */
20
29
  headers?: Record<string, string>;
21
30
  }
22
31
 
23
32
  /**
24
- * Error mapping configuration
33
+ * Error mapping configuration.
25
34
  */
26
35
  export interface ErrorMappingConfig<Ctx> {
27
- /** Custom error mapper function */
36
+ /** Custom error mapper function. */
28
37
  mapErrorToResponse?: (err: unknown, ctx: Ctx) => ErrorMappingResult;
29
38
 
30
- /** Include stack traces in error responses (default: true in dev/test, false in production) */
39
+ /** Include stack traces in error responses. Defaults to true in dev/test. */
31
40
  includeStackInResponse?: boolean;
32
41
 
33
- /** Application environment */
42
+ /** Application environment. */
34
43
  env?: AppEnvironment;
35
44
  }
36
45
 
@@ -36,6 +36,9 @@ export {
36
36
  type RateLimitOptions,
37
37
  } from "./rate-limit";
38
38
 
39
+ /**
40
+ * Flatten hook arrays into a single hook list.
41
+ */
39
42
  export function composeHooks<Ctx, Ports extends AnyPorts = AnyPorts>(
40
43
  ...hooks: (ServerHook<Ctx, Ports> | readonly ServerHook<Ctx, Ports>[])[]
41
44
  ): ServerHook<Ctx, Ports>[] {