@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.
- package/CHANGELOG.md +11 -0
- package/README.md +149 -4
- package/dist/application/index.d.ts +93 -9
- package/dist/application/index.d.ts.map +1 -1
- package/dist/application/index.js +11 -11
- package/dist/application/index.js.map +1 -1
- package/dist/client/client.d.ts +73 -12
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.js +37 -12
- package/dist/client/client.js.map +1 -1
- package/dist/client/index.d.ts +12 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +6 -0
- package/dist/client/index.js.map +1 -1
- package/dist/client/types.d.ts +69 -8
- package/dist/client/types.d.ts.map +1 -1
- package/dist/config/index.d.ts +84 -0
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +36 -0
- package/dist/config/index.js.map +1 -1
- package/dist/contracts/contract-builder.d.ts +49 -22
- package/dist/contracts/contract-builder.d.ts.map +1 -1
- package/dist/contracts/contract-builder.js +48 -21
- package/dist/contracts/contract-builder.js.map +1 -1
- package/dist/contracts/contract-group.d.ts +35 -19
- package/dist/contracts/contract-group.d.ts.map +1 -1
- package/dist/contracts/contract-group.js +35 -19
- package/dist/contracts/contract-group.js.map +1 -1
- package/dist/contracts/contract-like.d.ts +4 -4
- package/dist/contracts/contract-like.d.ts.map +1 -1
- package/dist/contracts/contract-like.js +2 -1
- package/dist/contracts/contract-like.js.map +1 -1
- package/dist/contracts/index.d.ts +28 -0
- package/dist/contracts/index.d.ts.map +1 -1
- package/dist/contracts/index.js +12 -0
- package/dist/contracts/index.js.map +1 -1
- package/dist/contracts/openapi-meta.d.ts +8 -8
- package/dist/contracts/openapi-meta.d.ts.map +1 -1
- package/dist/contracts/path-template.d.ts +27 -0
- package/dist/contracts/path-template.d.ts.map +1 -1
- package/dist/contracts/path-template.js +6 -0
- package/dist/contracts/path-template.js.map +1 -1
- package/dist/contracts/types.d.ts +104 -10
- package/dist/contracts/types.d.ts.map +1 -1
- package/dist/contracts/types.js +15 -0
- package/dist/contracts/types.js.map +1 -1
- package/dist/contracts/utils.d.ts +6 -0
- package/dist/contracts/utils.d.ts.map +1 -1
- package/dist/contracts/utils.js +6 -0
- package/dist/contracts/utils.js.map +1 -1
- package/dist/domain/entity.d.ts +22 -11
- package/dist/domain/entity.d.ts.map +1 -1
- package/dist/domain/entity.js +5 -1
- package/dist/domain/entity.js.map +1 -1
- package/dist/domain/events.d.ts +5 -2
- package/dist/domain/events.d.ts.map +1 -1
- package/dist/domain/events.js +4 -1
- package/dist/domain/events.js.map +1 -1
- package/dist/domain/value-object.d.ts +19 -9
- package/dist/domain/value-object.d.ts.map +1 -1
- package/dist/domain/value-object.js +5 -1
- package/dist/domain/value-object.js.map +1 -1
- package/dist/errors/catalog.d.ts +40 -16
- package/dist/errors/catalog.d.ts.map +1 -1
- package/dist/errors/catalog.js +18 -7
- package/dist/errors/catalog.js.map +1 -1
- package/dist/errors/response.d.ts +16 -4
- package/dist/errors/response.d.ts.map +1 -1
- package/dist/errors/response.js +3 -3
- package/dist/errors/response.js.map +1 -1
- package/dist/errors/validation.d.ts +10 -1
- package/dist/errors/validation.d.ts.map +1 -1
- package/dist/errors/validation.js +3 -0
- package/dist/errors/validation.js.map +1 -1
- package/dist/events/index.d.ts +133 -0
- package/dist/events/index.d.ts.map +1 -1
- package/dist/events/index.js +30 -0
- package/dist/events/index.js.map +1 -1
- package/dist/idempotency/index.d.ts +355 -0
- package/dist/idempotency/index.d.ts.map +1 -0
- package/dist/idempotency/index.js +360 -0
- package/dist/idempotency/index.js.map +1 -0
- package/dist/jobs/index.d.ts +110 -0
- package/dist/jobs/index.d.ts.map +1 -1
- package/dist/jobs/index.js +22 -0
- package/dist/jobs/index.js.map +1 -1
- package/dist/mail/index.d.ts +149 -0
- package/dist/mail/index.d.ts.map +1 -1
- package/dist/mail/index.js +30 -0
- package/dist/mail/index.js.map +1 -1
- package/dist/notifications/index.d.ts +369 -0
- package/dist/notifications/index.d.ts.map +1 -0
- package/dist/notifications/index.js +310 -0
- package/dist/notifications/index.js.map +1 -0
- package/dist/openapi/index.d.ts +132 -16
- package/dist/openapi/index.d.ts.map +1 -1
- package/dist/openapi/index.js +1 -1
- package/dist/openapi/index.js.map +1 -1
- package/dist/outbox/index.d.ts +469 -0
- package/dist/outbox/index.d.ts.map +1 -0
- package/dist/outbox/index.js +482 -0
- package/dist/outbox/index.js.map +1 -0
- package/dist/pagination/index.d.ts +166 -0
- package/dist/pagination/index.d.ts.map +1 -0
- package/dist/pagination/index.js +96 -0
- package/dist/pagination/index.js.map +1 -0
- package/dist/ports/audit.d.ts +271 -0
- package/dist/ports/audit.d.ts.map +1 -1
- package/dist/ports/audit.js +128 -0
- package/dist/ports/audit.js.map +1 -1
- package/dist/ports/auth.d.ts +70 -0
- package/dist/ports/auth.d.ts.map +1 -1
- package/dist/ports/auth.js +30 -0
- package/dist/ports/auth.js.map +1 -1
- package/dist/ports/cache.d.ts +41 -0
- package/dist/ports/cache.d.ts.map +1 -1
- package/dist/ports/cache.js +10 -0
- package/dist/ports/cache.js.map +1 -1
- package/dist/ports/clock.d.ts +38 -0
- package/dist/ports/clock.d.ts.map +1 -1
- package/dist/ports/clock.js +20 -0
- package/dist/ports/clock.js.map +1 -1
- package/dist/ports/id-generator.d.ts +37 -0
- package/dist/ports/id-generator.d.ts.map +1 -1
- package/dist/ports/id-generator.js +22 -0
- package/dist/ports/id-generator.js.map +1 -1
- package/dist/ports/index.d.ts +83 -0
- package/dist/ports/index.d.ts.map +1 -1
- package/dist/ports/index.js +41 -5
- package/dist/ports/index.js.map +1 -1
- package/dist/ports/logger.d.ts +56 -0
- package/dist/ports/logger.d.ts.map +1 -1
- package/dist/ports/logger.js +17 -0
- package/dist/ports/logger.js.map +1 -1
- package/dist/ports/policy.d.ts +132 -0
- package/dist/ports/policy.d.ts.map +1 -1
- package/dist/ports/policy.js +45 -0
- package/dist/ports/policy.js.map +1 -1
- package/dist/ports/rate-limit.d.ts +25 -0
- package/dist/ports/rate-limit.d.ts.map +1 -1
- package/dist/ports/rate-limit.js +10 -0
- package/dist/ports/rate-limit.js.map +1 -1
- package/dist/ports/redaction.d.ts +101 -0
- package/dist/ports/redaction.d.ts.map +1 -1
- package/dist/ports/redaction.js +59 -0
- package/dist/ports/redaction.js.map +1 -1
- package/dist/ports/storage.d.ts +100 -0
- package/dist/ports/storage.d.ts.map +1 -1
- package/dist/ports/storage.js +10 -0
- package/dist/ports/storage.js.map +1 -1
- package/dist/ports/testing.d.ts +47 -0
- package/dist/ports/testing.d.ts.map +1 -1
- package/dist/ports/testing.js +23 -0
- package/dist/ports/testing.js.map +1 -1
- package/dist/ports/unit-of-work.d.ts +60 -3
- package/dist/ports/unit-of-work.d.ts.map +1 -1
- package/dist/ports/unit-of-work.js +11 -2
- package/dist/ports/unit-of-work.js.map +1 -1
- package/dist/providers/instrumentation.d.ts +204 -0
- package/dist/providers/instrumentation.d.ts.map +1 -1
- package/dist/providers/instrumentation.js +14 -0
- package/dist/providers/instrumentation.js.map +1 -1
- package/dist/providers/provider.d.ts +14 -1
- package/dist/providers/provider.d.ts.map +1 -1
- package/dist/providers/provider.js.map +1 -1
- package/dist/schedules/index.d.ts +246 -0
- package/dist/schedules/index.d.ts.map +1 -1
- package/dist/schedules/index.js +27 -0
- package/dist/schedules/index.js.map +1 -1
- package/dist/server/health.d.ts +14 -5
- package/dist/server/health.d.ts.map +1 -1
- package/dist/server/health.js +5 -2
- package/dist/server/health.js.map +1 -1
- package/dist/server/hooks/auth.d.ts +57 -0
- package/dist/server/hooks/auth.d.ts.map +1 -1
- package/dist/server/hooks/auth.js.map +1 -1
- package/dist/server/hooks/cors.d.ts +27 -0
- package/dist/server/hooks/cors.d.ts.map +1 -1
- package/dist/server/hooks/cors.js +12 -0
- package/dist/server/hooks/cors.js.map +1 -1
- package/dist/server/hooks/errors.d.ts +15 -6
- package/dist/server/hooks/errors.d.ts.map +1 -1
- package/dist/server/hooks/errors.js.map +1 -1
- package/dist/server/hooks/index.d.ts +3 -0
- package/dist/server/hooks/index.d.ts.map +1 -1
- package/dist/server/hooks/index.js +3 -0
- package/dist/server/hooks/index.js.map +1 -1
- package/dist/server/hooks/logging.d.ts +36 -0
- package/dist/server/hooks/logging.d.ts.map +1 -1
- package/dist/server/hooks/logging.js +6 -0
- package/dist/server/hooks/logging.js.map +1 -1
- package/dist/server/hooks/rate-limit.d.ts +33 -0
- package/dist/server/hooks/rate-limit.d.ts.map +1 -1
- package/dist/server/hooks/rate-limit.js +11 -0
- package/dist/server/hooks/rate-limit.js.map +1 -1
- package/dist/server/http.d.ts +170 -0
- package/dist/server/http.d.ts.map +1 -1
- package/dist/server/index.d.ts +18 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +6 -0
- package/dist/server/index.js.map +1 -1
- package/dist/server/openapi.d.ts +5 -3
- package/dist/server/openapi.d.ts.map +1 -1
- package/dist/server/openapi.js +4 -2
- package/dist/server/openapi.js.map +1 -1
- package/dist/server/providers/loadProviderConfig.d.ts +9 -0
- package/dist/server/providers/loadProviderConfig.d.ts.map +1 -1
- package/dist/server/providers/loadProviderConfig.js +9 -0
- package/dist/server/providers/loadProviderConfig.js.map +1 -1
- package/dist/server/server.d.ts +107 -8
- package/dist/server/server.d.ts.map +1 -1
- package/dist/server/server.js +27 -7
- package/dist/server/server.js.map +1 -1
- package/dist/testing/index.d.ts +167 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +119 -0
- package/dist/testing/index.js.map +1 -0
- package/package.json +21 -1
- package/src/application/index.ts +85 -22
- package/src/client/client.ts +73 -12
- package/src/client/index.ts +12 -0
- package/src/client/types.ts +70 -9
- package/src/config/index.ts +86 -0
- package/src/contracts/contract-builder.ts +49 -22
- package/src/contracts/contract-group.ts +35 -19
- package/src/contracts/contract-like.ts +4 -4
- package/src/contracts/index.ts +28 -1
- package/src/contracts/openapi-meta.ts +8 -8
- package/src/contracts/path-template.ts +27 -0
- package/src/contracts/types.ts +111 -10
- package/src/contracts/utils.ts +6 -0
- package/src/domain/entity.ts +22 -11
- package/src/domain/events.ts +5 -2
- package/src/domain/value-object.ts +19 -9
- package/src/errors/catalog.ts +40 -16
- package/src/errors/response.ts +16 -4
- package/src/errors/validation.ts +10 -1
- package/src/events/index.ts +134 -0
- package/src/idempotency/index.ts +767 -0
- package/src/jobs/index.ts +111 -0
- package/src/mail/index.ts +149 -0
- package/src/notifications/index.ts +771 -0
- package/src/openapi/index.ts +133 -16
- package/src/outbox/index.ts +1024 -0
- package/src/pagination/index.ts +278 -0
- package/src/ports/audit.ts +271 -0
- package/src/ports/auth.ts +70 -0
- package/src/ports/cache.ts +41 -0
- package/src/ports/clock.ts +38 -0
- package/src/ports/id-generator.ts +37 -0
- package/src/ports/index.ts +106 -11
- package/src/ports/logger.ts +56 -0
- package/src/ports/policy.ts +133 -0
- package/src/ports/rate-limit.ts +25 -0
- package/src/ports/redaction.ts +101 -0
- package/src/ports/storage.ts +100 -0
- package/src/ports/testing.ts +47 -0
- package/src/ports/unit-of-work.ts +60 -3
- package/src/providers/instrumentation.ts +204 -0
- package/src/providers/provider.ts +14 -1
- package/src/schedules/index.ts +247 -0
- package/src/server/health.ts +14 -5
- package/src/server/hooks/auth.ts +58 -0
- package/src/server/hooks/cors.ts +27 -0
- package/src/server/hooks/errors.ts +15 -6
- package/src/server/hooks/index.ts +3 -0
- package/src/server/hooks/logging.ts +36 -0
- package/src/server/hooks/rate-limit.ts +33 -0
- package/src/server/http.ts +170 -1
- package/src/server/index.ts +18 -1
- package/src/server/openapi.ts +5 -3
- package/src/server/providers/loadProviderConfig.ts +9 -0
- package/src/server/server.ts +107 -9
- package/src/testing/index.ts +337 -0
package/src/schedules/index.ts
CHANGED
|
@@ -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>(
|
package/src/server/health.ts
CHANGED
|
@@ -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
|
|
40
|
+
* Application environment.
|
|
35
41
|
*/
|
|
36
42
|
export type AppEnvironment = "development" | "production" | "test";
|
|
37
43
|
|
|
38
44
|
/**
|
|
39
|
-
* Create a health check handler
|
|
40
|
-
*
|
|
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,
|
package/src/server/hooks/auth.ts
CHANGED
|
@@ -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"]>;
|
package/src/server/hooks/cors.ts
CHANGED
|
@@ -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
|
-
*
|
|
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
|
|
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>[] {
|