@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.
- package/CHANGELOG.md +27 -0
- package/README.md +202 -8
- 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 +248 -4
- package/dist/jobs/index.d.ts.map +1 -1
- package/dist/jobs/index.js +183 -1
- 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 +474 -0
- package/dist/outbox/index.d.ts.map +1 -0
- package/dist/outbox/index.js +538 -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 +205 -1
- 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 +68 -26
- package/dist/server/hooks/auth.d.ts.map +1 -1
- package/dist/server/hooks/auth.js +44 -55
- 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 +4 -1
- 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 +222 -0
- package/dist/server/http.d.ts.map +1 -1
- package/dist/server/http.js +20 -1
- package/dist/server/http.js.map +1 -1
- package/dist/server/index.d.ts +19 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +7 -1
- 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 +159 -19
- package/dist/server/server.d.ts.map +1 -1
- package/dist/server/server.js +72 -31
- package/dist/server/server.js.map +1 -1
- package/dist/testing/index.d.ts +171 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +127 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/uploads/client.d.ts +278 -0
- package/dist/uploads/client.d.ts.map +1 -0
- package/dist/uploads/client.js +428 -0
- package/dist/uploads/client.js.map +1 -0
- package/dist/uploads/index.d.ts +361 -0
- package/dist/uploads/index.d.ts.map +1 -0
- package/dist/uploads/index.js +543 -0
- package/dist/uploads/index.js.map +1 -0
- package/package.json +31 -2
- 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 +437 -5
- 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 +1104 -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 +211 -1
- 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 +105 -120
- package/src/server/hooks/cors.ts +27 -0
- package/src/server/hooks/errors.ts +15 -6
- package/src/server/hooks/index.ts +4 -5
- package/src/server/hooks/logging.ts +36 -0
- package/src/server/hooks/rate-limit.ts +33 -0
- package/src/server/http.ts +249 -1
- package/src/server/index.ts +19 -1
- package/src/server/openapi.ts +5 -3
- package/src/server/providers/loadProviderConfig.ts +9 -0
- package/src/server/server.ts +296 -30
- package/src/testing/index.ts +348 -0
- package/src/uploads/client.ts +861 -0
- package/src/uploads/index.ts +1067 -0
package/src/server/http.ts
CHANGED
|
@@ -6,9 +6,24 @@ import type {
|
|
|
6
6
|
} from "../contracts";
|
|
7
7
|
import type { AnyPorts } from "../ports";
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Framework-neutral request shape consumed by Beignet server adapters.
|
|
11
|
+
*
|
|
12
|
+
* Platform adapters should convert their native request into this shape before
|
|
13
|
+
* passing it to `server.api(...)` or a single route handler.
|
|
14
|
+
*/
|
|
9
15
|
export interface HttpRequestLike {
|
|
16
|
+
/**
|
|
17
|
+
* HTTP method as received from the platform.
|
|
18
|
+
*/
|
|
10
19
|
method: string;
|
|
20
|
+
/**
|
|
21
|
+
* Absolute request URL.
|
|
22
|
+
*/
|
|
11
23
|
url: string;
|
|
24
|
+
/**
|
|
25
|
+
* Request headers.
|
|
26
|
+
*/
|
|
12
27
|
headers: Headers;
|
|
13
28
|
/**
|
|
14
29
|
* The platform request when an adapter has one available.
|
|
@@ -17,20 +32,56 @@ export interface HttpRequestLike {
|
|
|
17
32
|
* framework-agnostic methods below when possible.
|
|
18
33
|
*/
|
|
19
34
|
raw?: Request;
|
|
35
|
+
/**
|
|
36
|
+
* Parse the request body as JSON.
|
|
37
|
+
*/
|
|
20
38
|
json(): Promise<unknown>;
|
|
39
|
+
/**
|
|
40
|
+
* Parse the request body as text.
|
|
41
|
+
*/
|
|
21
42
|
text(): Promise<string>;
|
|
43
|
+
/**
|
|
44
|
+
* Parse the request body as an array buffer when the platform supports it.
|
|
45
|
+
*/
|
|
22
46
|
arrayBuffer?(): Promise<ArrayBuffer>;
|
|
47
|
+
/**
|
|
48
|
+
* Parse the request body as a Blob when the platform supports it.
|
|
49
|
+
*/
|
|
23
50
|
blob?(): Promise<Blob>;
|
|
51
|
+
/**
|
|
52
|
+
* Parse the request body as form data when the platform supports it.
|
|
53
|
+
*/
|
|
24
54
|
formData?(): Promise<FormData>;
|
|
55
|
+
/**
|
|
56
|
+
* Clone the request when the platform supports replaying the body.
|
|
57
|
+
*/
|
|
25
58
|
clone?(): HttpRequestLike;
|
|
26
59
|
}
|
|
27
60
|
|
|
61
|
+
/**
|
|
62
|
+
* Framework-neutral response object returned by route handlers and hooks.
|
|
63
|
+
*/
|
|
28
64
|
export interface HttpResponseLike {
|
|
65
|
+
/**
|
|
66
|
+
* HTTP status code.
|
|
67
|
+
*/
|
|
29
68
|
status: number;
|
|
69
|
+
/**
|
|
70
|
+
* Response headers.
|
|
71
|
+
*/
|
|
30
72
|
headers?: Record<string, string>;
|
|
73
|
+
/**
|
|
74
|
+
* JSON-serializable body or an adapter-specific body value.
|
|
75
|
+
*/
|
|
31
76
|
body?: unknown;
|
|
32
77
|
}
|
|
33
78
|
|
|
79
|
+
/**
|
|
80
|
+
* Response accepted by Beignet handlers.
|
|
81
|
+
*
|
|
82
|
+
* Use `HttpResponseLike` for framework-neutral responses. Return a native
|
|
83
|
+
* `Response` only when the current adapter can pass it through unchanged.
|
|
84
|
+
*/
|
|
34
85
|
export type HttpResponse = HttpResponseLike | Response;
|
|
35
86
|
|
|
36
87
|
type InferSchemaOrFallback<
|
|
@@ -38,44 +89,170 @@ type InferSchemaOrFallback<
|
|
|
38
89
|
Fallback,
|
|
39
90
|
> = T extends StandardSchema ? InferOutput<T> : Fallback;
|
|
40
91
|
|
|
92
|
+
/**
|
|
93
|
+
* Infer the handler path parameter type for a contract.
|
|
94
|
+
*/
|
|
41
95
|
export type InferPath<C extends HttpContractConfig> = InferSchemaOrFallback<
|
|
42
96
|
C["pathParams"],
|
|
43
97
|
Record<string, string>
|
|
44
98
|
>;
|
|
45
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Infer the handler query parameter type for a contract.
|
|
102
|
+
*/
|
|
46
103
|
export type InferQuery<C extends HttpContractConfig> = InferSchemaOrFallback<
|
|
47
104
|
C["query"],
|
|
48
105
|
Record<string, string | string[]>
|
|
49
106
|
>;
|
|
50
107
|
|
|
108
|
+
/**
|
|
109
|
+
* Infer the handler request body type for a contract.
|
|
110
|
+
*/
|
|
51
111
|
export type InferBody<C extends HttpContractConfig> = InferSchemaOrFallback<
|
|
52
112
|
C["body"],
|
|
53
113
|
unknown
|
|
54
114
|
>;
|
|
55
115
|
|
|
116
|
+
/**
|
|
117
|
+
* Infer the merged request header type for a contract.
|
|
118
|
+
*/
|
|
56
119
|
export type InferHeaders<C extends HttpContractConfig> =
|
|
57
120
|
InferHeaderSchemaOutput<Exclude<C["headers"], undefined>> extends undefined
|
|
58
121
|
? Record<string, string>
|
|
59
122
|
: InferHeaderSchemaOutput<Exclude<C["headers"], undefined>>;
|
|
60
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Arguments passed to a route handler after request parsing and validation.
|
|
126
|
+
*/
|
|
61
127
|
export interface HandlerArgs<Ctx, C extends HttpContractConfig> {
|
|
128
|
+
/**
|
|
129
|
+
* Framework-neutral request.
|
|
130
|
+
*/
|
|
62
131
|
req: HttpRequestLike;
|
|
132
|
+
/**
|
|
133
|
+
* Application context returned by `createContext`.
|
|
134
|
+
*/
|
|
63
135
|
ctx: Ctx;
|
|
136
|
+
/**
|
|
137
|
+
* Matched contract config.
|
|
138
|
+
*/
|
|
64
139
|
contract: C;
|
|
65
140
|
|
|
66
|
-
|
|
141
|
+
/**
|
|
142
|
+
* Parsed path parameters.
|
|
143
|
+
*/
|
|
67
144
|
path: InferPath<C>;
|
|
145
|
+
/**
|
|
146
|
+
* Parsed query parameters.
|
|
147
|
+
*/
|
|
68
148
|
query: InferQuery<C>;
|
|
149
|
+
/**
|
|
150
|
+
* Parsed request headers.
|
|
151
|
+
*/
|
|
69
152
|
headers: InferHeaders<C>;
|
|
153
|
+
/**
|
|
154
|
+
* Parsed request body.
|
|
155
|
+
*/
|
|
70
156
|
body: InferBody<C>;
|
|
71
157
|
}
|
|
72
158
|
|
|
159
|
+
/**
|
|
160
|
+
* Route handler function for a contract.
|
|
161
|
+
*/
|
|
73
162
|
export type Handler<Ctx, C extends HttpContractConfig> = (
|
|
74
163
|
args: HandlerArgs<Ctx, C>,
|
|
75
164
|
) => Promise<HttpResponse> | HttpResponse;
|
|
76
165
|
|
|
166
|
+
/**
|
|
167
|
+
* Value or promise of that value.
|
|
168
|
+
*/
|
|
77
169
|
export type MaybePromise<T> = T | Promise<T>;
|
|
78
170
|
|
|
171
|
+
/**
|
|
172
|
+
* Arguments passed to a route-scoped hook after request parsing and context
|
|
173
|
+
* creation.
|
|
174
|
+
*/
|
|
175
|
+
export type RouteHookArgs<
|
|
176
|
+
Ctx,
|
|
177
|
+
C extends HttpContractConfig = HttpContractConfig,
|
|
178
|
+
> = HandlerArgs<Ctx, C>;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Hook that runs only for the route or route group where it is attached.
|
|
182
|
+
*
|
|
183
|
+
* Route hooks are for scoped policy and context enrichment such as
|
|
184
|
+
* authentication, tenant resolution, feature gates, and idempotency. They add
|
|
185
|
+
* fields to the handler context instead of replacing the app context.
|
|
186
|
+
*/
|
|
187
|
+
export interface RouteHook<
|
|
188
|
+
Ctx,
|
|
189
|
+
AddedCtx extends object = Record<string, never>,
|
|
190
|
+
> {
|
|
191
|
+
/**
|
|
192
|
+
* Optional name used in diagnostics and devtools.
|
|
193
|
+
*/
|
|
194
|
+
name?: string;
|
|
195
|
+
/**
|
|
196
|
+
* Resolve additional context for this route or throw to stop handling.
|
|
197
|
+
*/
|
|
198
|
+
resolve: (args: RouteHookArgs<Ctx>) => MaybePromise<AddedCtx | undefined>;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Builder for route-scoped hooks.
|
|
203
|
+
*/
|
|
204
|
+
export type RouteHookBuilder<Ctx> = {
|
|
205
|
+
/**
|
|
206
|
+
* Assign a diagnostic name to the hook.
|
|
207
|
+
*/
|
|
208
|
+
name: (name: string) => RouteHookNamedBuilder<Ctx>;
|
|
209
|
+
/**
|
|
210
|
+
* Define the hook resolver.
|
|
211
|
+
*/
|
|
212
|
+
resolve: <AddedCtx extends object>(
|
|
213
|
+
resolve: RouteHook<Ctx, AddedCtx>["resolve"],
|
|
214
|
+
) => RouteHook<Ctx, AddedCtx>;
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Named builder for route-scoped hooks.
|
|
219
|
+
*/
|
|
220
|
+
export type RouteHookNamedBuilder<Ctx> = {
|
|
221
|
+
/**
|
|
222
|
+
* Define the hook resolver.
|
|
223
|
+
*/
|
|
224
|
+
resolve: <AddedCtx extends object>(
|
|
225
|
+
resolve: RouteHook<Ctx, AddedCtx>["resolve"],
|
|
226
|
+
) => RouteHook<Ctx, AddedCtx>;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Define a route-scoped hook.
|
|
231
|
+
*
|
|
232
|
+
* Route hooks enrich handler context for one route or route group. They should
|
|
233
|
+
* throw application/framework errors for denials instead of returning HTTP
|
|
234
|
+
* responses directly.
|
|
235
|
+
*/
|
|
236
|
+
export function defineRouteHook<Ctx>(): RouteHookBuilder<Ctx> {
|
|
237
|
+
return {
|
|
238
|
+
name: (name) => ({
|
|
239
|
+
resolve: (resolve) => ({
|
|
240
|
+
name,
|
|
241
|
+
resolve,
|
|
242
|
+
}),
|
|
243
|
+
}),
|
|
244
|
+
resolve: (resolve) => ({
|
|
245
|
+
resolve,
|
|
246
|
+
}),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Hook that runs after a route is matched but before request parsing and
|
|
252
|
+
* context creation.
|
|
253
|
+
*
|
|
254
|
+
* Returning a response short-circuits the rest of the request pipeline.
|
|
255
|
+
*/
|
|
79
256
|
export type OnRequestHook<
|
|
80
257
|
Ports extends AnyPorts = AnyPorts,
|
|
81
258
|
C extends HttpContractConfig = HttpContractConfig,
|
|
@@ -86,6 +263,12 @@ export type OnRequestHook<
|
|
|
86
263
|
params: Record<string, string>;
|
|
87
264
|
}) => MaybePromise<HttpResponse | undefined>;
|
|
88
265
|
|
|
266
|
+
/**
|
|
267
|
+
* Result from a `beforeHandle` hook.
|
|
268
|
+
*
|
|
269
|
+
* Returning a plain response short-circuits the handler. Returning an object can
|
|
270
|
+
* replace the context, short-circuit with a response, or do both.
|
|
271
|
+
*/
|
|
89
272
|
export type BeforeHandleResult<Ctx> =
|
|
90
273
|
| undefined
|
|
91
274
|
| HttpResponse
|
|
@@ -94,6 +277,9 @@ export type BeforeHandleResult<Ctx> =
|
|
|
94
277
|
response?: HttpResponse;
|
|
95
278
|
};
|
|
96
279
|
|
|
280
|
+
/**
|
|
281
|
+
* Hook that runs after request parsing/context creation and before the handler.
|
|
282
|
+
*/
|
|
97
283
|
export type BeforeHandleHook<
|
|
98
284
|
Ctx,
|
|
99
285
|
C extends HttpContractConfig = HttpContractConfig,
|
|
@@ -107,6 +293,12 @@ export type BeforeHandleHook<
|
|
|
107
293
|
body: InferBody<C>;
|
|
108
294
|
}) => MaybePromise<BeforeHandleResult<Ctx>>;
|
|
109
295
|
|
|
296
|
+
/**
|
|
297
|
+
* Hook that runs before a framework-neutral response is returned.
|
|
298
|
+
*
|
|
299
|
+
* Return a response to replace or decorate the outgoing response. Hooks run in
|
|
300
|
+
* declaration order.
|
|
301
|
+
*/
|
|
110
302
|
export type BeforeSendHook<
|
|
111
303
|
Ctx,
|
|
112
304
|
C extends HttpContractConfig = HttpContractConfig,
|
|
@@ -122,6 +314,12 @@ export type BeforeSendHook<
|
|
|
122
314
|
error?: unknown;
|
|
123
315
|
}) => MaybePromise<HttpResponseLike | undefined>;
|
|
124
316
|
|
|
317
|
+
/**
|
|
318
|
+
* Hook that runs after the response has been prepared.
|
|
319
|
+
*
|
|
320
|
+
* This is for logging and observability. Errors thrown by `afterSend` hooks are
|
|
321
|
+
* ignored by the server pipeline.
|
|
322
|
+
*/
|
|
125
323
|
export type AfterSendHook<
|
|
126
324
|
Ctx,
|
|
127
325
|
C extends HttpContractConfig = HttpContractConfig,
|
|
@@ -138,6 +336,9 @@ export type AfterSendHook<
|
|
|
138
336
|
durationMs: number;
|
|
139
337
|
}) => MaybePromise<void>;
|
|
140
338
|
|
|
339
|
+
/**
|
|
340
|
+
* Hook notified when the framework catches an error while handling a request.
|
|
341
|
+
*/
|
|
141
342
|
export type ServerCaughtErrorHook<
|
|
142
343
|
Ctx,
|
|
143
344
|
C extends HttpContractConfig = HttpContractConfig,
|
|
@@ -152,6 +353,12 @@ export type ServerCaughtErrorHook<
|
|
|
152
353
|
body?: InferBody<C>;
|
|
153
354
|
}) => MaybePromise<void>;
|
|
154
355
|
|
|
356
|
+
/**
|
|
357
|
+
* Hook that may map an unexpected error to a custom response.
|
|
358
|
+
*
|
|
359
|
+
* Return `undefined` to let the server's default unhandled-error mapper create
|
|
360
|
+
* the response.
|
|
361
|
+
*/
|
|
155
362
|
export type ServerUnhandledErrorMapper<
|
|
156
363
|
Ctx,
|
|
157
364
|
C extends HttpContractConfig = HttpContractConfig,
|
|
@@ -166,22 +373,63 @@ export type ServerUnhandledErrorMapper<
|
|
|
166
373
|
body?: InferBody<C>;
|
|
167
374
|
}) => MaybePromise<HttpResponse | undefined>;
|
|
168
375
|
|
|
376
|
+
/**
|
|
377
|
+
* Server lifecycle hook collection.
|
|
378
|
+
*
|
|
379
|
+
* Hooks run in the order they are registered. `onRequest` can short-circuit
|
|
380
|
+
* before context creation; `beforeHandle` can replace context or short-circuit;
|
|
381
|
+
* `beforeSend` can replace the outgoing response; `afterSend` observes the
|
|
382
|
+
* final response.
|
|
383
|
+
*/
|
|
169
384
|
export interface ServerHook<Ctx, Ports extends AnyPorts = AnyPorts> {
|
|
385
|
+
/**
|
|
386
|
+
* Optional name used in diagnostics and devtools.
|
|
387
|
+
*/
|
|
170
388
|
name?: string;
|
|
389
|
+
/**
|
|
390
|
+
* Runs after route matching and before body/query/header parsing.
|
|
391
|
+
*/
|
|
171
392
|
onRequest?: OnRequestHook<Ports>;
|
|
393
|
+
/**
|
|
394
|
+
* Runs after request parsing and context creation.
|
|
395
|
+
*/
|
|
172
396
|
beforeHandle?: BeforeHandleHook<Ctx>;
|
|
397
|
+
/**
|
|
398
|
+
* Runs before a framework-neutral response is returned.
|
|
399
|
+
*/
|
|
173
400
|
beforeSend?: BeforeSendHook<Ctx>;
|
|
401
|
+
/**
|
|
402
|
+
* Observes the final response after send preparation.
|
|
403
|
+
*/
|
|
174
404
|
afterSend?: AfterSendHook<Ctx>;
|
|
405
|
+
/**
|
|
406
|
+
* Observes framework-caught errors.
|
|
407
|
+
*/
|
|
175
408
|
onCaughtError?: ServerCaughtErrorHook<Ctx>;
|
|
409
|
+
/**
|
|
410
|
+
* Maps unexpected errors to responses.
|
|
411
|
+
*/
|
|
176
412
|
mapUnhandledError?: ServerUnhandledErrorMapper<Ctx>;
|
|
177
413
|
}
|
|
178
414
|
|
|
415
|
+
/**
|
|
416
|
+
* Compiled route entry used by server internals and adapter helpers.
|
|
417
|
+
*/
|
|
179
418
|
export type ResolvedRoute<_Ctx, C extends HttpContractConfig> = {
|
|
419
|
+
/**
|
|
420
|
+
* Contract config for the route.
|
|
421
|
+
*/
|
|
180
422
|
contract: C;
|
|
423
|
+
/**
|
|
424
|
+
* Handler that receives the raw request and optional path params.
|
|
425
|
+
*/
|
|
181
426
|
handler: (
|
|
182
427
|
req: HttpRequestLike,
|
|
183
428
|
params?: Record<string, string>,
|
|
184
429
|
) => Promise<HttpResponse>;
|
|
430
|
+
/**
|
|
431
|
+
* Test whether the route matches an incoming method and pathname.
|
|
432
|
+
*/
|
|
185
433
|
match: (
|
|
186
434
|
method: string,
|
|
187
435
|
pathname: string,
|
package/src/server/index.ts
CHANGED
|
@@ -5,30 +5,48 @@
|
|
|
5
5
|
* Shared by Beignet server adapters.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Contract and schema types re-exported for server users.
|
|
10
|
+
*/
|
|
9
11
|
export type {
|
|
10
12
|
HttpContractConfig,
|
|
11
13
|
InferOutput,
|
|
12
14
|
StandardSchema,
|
|
13
15
|
StandardSchemaV1,
|
|
14
16
|
} from "../contracts";
|
|
17
|
+
/**
|
|
18
|
+
* Application error base class re-exported for server users.
|
|
19
|
+
*/
|
|
15
20
|
export { AppError } from "../errors";
|
|
21
|
+
/**
|
|
22
|
+
* Any-ports helper type re-exported for server users.
|
|
23
|
+
*/
|
|
16
24
|
export type { AnyPorts } from "../ports";
|
|
25
|
+
/**
|
|
26
|
+
* Provider type re-exported for server users.
|
|
27
|
+
*/
|
|
17
28
|
export type { ServiceProvider } from "../providers";
|
|
18
29
|
export * from "./health";
|
|
19
30
|
export * from "./hooks";
|
|
20
31
|
export * from "./http";
|
|
21
32
|
export * from "./openapi";
|
|
22
33
|
export * from "./providers";
|
|
34
|
+
/**
|
|
35
|
+
* Server configuration and route types.
|
|
36
|
+
*/
|
|
23
37
|
export type {
|
|
24
38
|
CreateServerOptions,
|
|
25
39
|
RouteDef,
|
|
26
40
|
RouteGroup,
|
|
27
41
|
ServerInstance,
|
|
28
42
|
} from "./server";
|
|
43
|
+
/**
|
|
44
|
+
* Server runtime helpers.
|
|
45
|
+
*/
|
|
29
46
|
export {
|
|
30
47
|
contractsFromRoutes,
|
|
31
48
|
createServer,
|
|
49
|
+
defineRoute,
|
|
32
50
|
defineRouteGroup,
|
|
33
51
|
defineRoutes,
|
|
34
52
|
} from "./server";
|
package/src/server/openapi.ts
CHANGED
|
@@ -8,7 +8,7 @@ import type { AppEnvironment } from "./health";
|
|
|
8
8
|
import type { HttpRequestLike, HttpResponseLike } from "./types";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* OpenAPI configuration
|
|
11
|
+
* OpenAPI route handler configuration.
|
|
12
12
|
*/
|
|
13
13
|
export interface OpenAPIConfig {
|
|
14
14
|
/** Enable OpenAPI endpoint (default: false) */
|
|
@@ -28,8 +28,10 @@ export interface OpenAPIConfig {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
|
-
* Create
|
|
32
|
-
*
|
|
31
|
+
* Create a framework-neutral OpenAPI JSON handler.
|
|
32
|
+
*
|
|
33
|
+
* The OpenAPI document is generated lazily on the first request and then cached
|
|
34
|
+
* for the lifetime of the handler.
|
|
33
35
|
*/
|
|
34
36
|
export function createOpenAPIHandler(
|
|
35
37
|
contracts: readonly HttpContractConfig[],
|
|
@@ -30,6 +30,12 @@ async function parseStandardSchema<
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Load and validate config for a provider.
|
|
35
|
+
*
|
|
36
|
+
* Explicit overrides take precedence over env-derived config. When a provider
|
|
37
|
+
* declares `envPrefix`, matching env keys are stripped before validation.
|
|
38
|
+
*/
|
|
33
39
|
export async function loadProviderConfig<
|
|
34
40
|
Ports extends AnyPorts,
|
|
35
41
|
Schema extends StandardSchemaV1<unknown, unknown>,
|
|
@@ -69,4 +75,7 @@ export async function loadProviderConfig<
|
|
|
69
75
|
}
|
|
70
76
|
}
|
|
71
77
|
|
|
78
|
+
/**
|
|
79
|
+
* Parse provider config with Standard Schema and normalize validation errors.
|
|
80
|
+
*/
|
|
72
81
|
export { parseStandardSchema };
|