@beignet/core 0.0.1
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 +5 -0
- package/README.md +288 -0
- package/dist/application/index.d.ts +260 -0
- package/dist/application/index.d.ts.map +1 -0
- package/dist/application/index.js +324 -0
- package/dist/application/index.js.map +1 -0
- package/dist/client/client.d.ts +241 -0
- package/dist/client/client.d.ts.map +1 -0
- package/dist/client/client.js +531 -0
- package/dist/client/client.js.map +1 -0
- package/dist/client/index.d.ts +10 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +8 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +139 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +2 -0
- package/dist/client/types.js.map +1 -0
- package/dist/config/index.d.ts +122 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +216 -0
- package/dist/config/index.js.map +1 -0
- package/dist/contracts/contract-builder.d.ts +121 -0
- package/dist/contracts/contract-builder.d.ts.map +1 -0
- package/dist/contracts/contract-builder.js +346 -0
- package/dist/contracts/contract-builder.js.map +1 -0
- package/dist/contracts/contract-group.d.ts +106 -0
- package/dist/contracts/contract-group.d.ts.map +1 -0
- package/dist/contracts/contract-group.js +240 -0
- package/dist/contracts/contract-group.js.map +1 -0
- package/dist/contracts/contract-like.d.ts +21 -0
- package/dist/contracts/contract-like.d.ts.map +1 -0
- package/dist/contracts/contract-like.js +9 -0
- package/dist/contracts/contract-like.js.map +1 -0
- package/dist/contracts/index.d.ts +15 -0
- package/dist/contracts/index.d.ts.map +1 -0
- package/dist/contracts/index.js +11 -0
- package/dist/contracts/index.js.map +1 -0
- package/dist/contracts/openapi-meta.d.ts +23 -0
- package/dist/contracts/openapi-meta.d.ts.map +1 -0
- package/dist/contracts/openapi-meta.js +2 -0
- package/dist/contracts/openapi-meta.js.map +1 -0
- package/dist/contracts/path-template.d.ts +17 -0
- package/dist/contracts/path-template.d.ts.map +1 -0
- package/dist/contracts/path-template.js +50 -0
- package/dist/contracts/path-template.js.map +1 -0
- package/dist/contracts/rate-limit.d.ts +50 -0
- package/dist/contracts/rate-limit.d.ts.map +1 -0
- package/dist/contracts/rate-limit.js +2 -0
- package/dist/contracts/rate-limit.js.map +1 -0
- package/dist/contracts/types.d.ts +97 -0
- package/dist/contracts/types.d.ts.map +1 -0
- package/dist/contracts/types.js +54 -0
- package/dist/contracts/types.js.map +1 -0
- package/dist/contracts/utils.d.ts +3 -0
- package/dist/contracts/utils.d.ts.map +1 -0
- package/dist/contracts/utils.js +44 -0
- package/dist/contracts/utils.js.map +1 -0
- package/dist/domain/entity.d.ts +87 -0
- package/dist/domain/entity.d.ts.map +1 -0
- package/dist/domain/entity.js +155 -0
- package/dist/domain/entity.js.map +1 -0
- package/dist/domain/events.d.ts +41 -0
- package/dist/domain/events.d.ts.map +1 -0
- package/dist/domain/events.js +21 -0
- package/dist/domain/events.js.map +1 -0
- package/dist/domain/index.d.ts +14 -0
- package/dist/domain/index.d.ts.map +1 -0
- package/dist/domain/index.js +14 -0
- package/dist/domain/index.js.map +1 -0
- package/dist/domain/value-object.d.ts +60 -0
- package/dist/domain/value-object.d.ts.map +1 -0
- package/dist/domain/value-object.js +87 -0
- package/dist/domain/value-object.js.map +1 -0
- package/dist/errors/catalog.d.ts +71 -0
- package/dist/errors/catalog.d.ts.map +1 -0
- package/dist/errors/catalog.js +71 -0
- package/dist/errors/catalog.js.map +1 -0
- package/dist/errors/http.d.ts +77 -0
- package/dist/errors/http.d.ts.map +1 -0
- package/dist/errors/http.js +74 -0
- package/dist/errors/http.js.map +1 -0
- package/dist/errors/index.d.ts +10 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +14 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/errors/response.d.ts +26 -0
- package/dist/errors/response.d.ts.map +1 -0
- package/dist/errors/response.js +34 -0
- package/dist/errors/response.js.map +1 -0
- package/dist/errors/validation.d.ts +18 -0
- package/dist/errors/validation.d.ts.map +1 -0
- package/dist/errors/validation.js +21 -0
- package/dist/errors/validation.js.map +1 -0
- package/dist/events/index.d.ts +58 -0
- package/dist/events/index.d.ts.map +1 -0
- package/dist/events/index.js +102 -0
- package/dist/events/index.js.map +1 -0
- package/dist/jobs/index.d.ts +56 -0
- package/dist/jobs/index.d.ts.map +1 -0
- package/dist/jobs/index.js +89 -0
- package/dist/jobs/index.js.map +1 -0
- package/dist/mail/index.d.ts +75 -0
- package/dist/mail/index.d.ts.map +1 -0
- package/dist/mail/index.js +84 -0
- package/dist/mail/index.js.map +1 -0
- package/dist/openapi/index.d.ts +207 -0
- package/dist/openapi/index.d.ts.map +1 -0
- package/dist/openapi/index.js +449 -0
- package/dist/openapi/index.js.map +1 -0
- package/dist/openapi/schema-introspector.d.ts +38 -0
- package/dist/openapi/schema-introspector.d.ts.map +1 -0
- package/dist/openapi/schema-introspector.js +67 -0
- package/dist/openapi/schema-introspector.js.map +1 -0
- package/dist/ports/audit.d.ts +58 -0
- package/dist/ports/audit.d.ts.map +1 -0
- package/dist/ports/audit.js +74 -0
- package/dist/ports/audit.js.map +1 -0
- package/dist/ports/auth.d.ts +23 -0
- package/dist/ports/auth.d.ts.map +1 -0
- package/dist/ports/auth.js +31 -0
- package/dist/ports/auth.js.map +1 -0
- package/dist/ports/builder.d.ts +61 -0
- package/dist/ports/builder.d.ts.map +1 -0
- package/dist/ports/builder.js +48 -0
- package/dist/ports/builder.js.map +1 -0
- package/dist/ports/cache.d.ts +15 -0
- package/dist/ports/cache.d.ts.map +1 -0
- package/dist/ports/cache.js +57 -0
- package/dist/ports/cache.js.map +1 -0
- package/dist/ports/clock.d.ts +10 -0
- package/dist/ports/clock.d.ts.map +1 -0
- package/dist/ports/clock.js +21 -0
- package/dist/ports/clock.js.map +1 -0
- package/dist/ports/events.d.ts +71 -0
- package/dist/ports/events.d.ts.map +1 -0
- package/dist/ports/events.js +2 -0
- package/dist/ports/events.js.map +1 -0
- package/dist/ports/id-generator.d.ts +12 -0
- package/dist/ports/id-generator.d.ts.map +1 -0
- package/dist/ports/id-generator.js +22 -0
- package/dist/ports/id-generator.js.map +1 -0
- package/dist/ports/index.d.ts +98 -0
- package/dist/ports/index.d.ts.map +1 -0
- package/dist/ports/index.js +67 -0
- package/dist/ports/index.js.map +1 -0
- package/dist/ports/logger.d.ts +22 -0
- package/dist/ports/logger.d.ts.map +1 -0
- package/dist/ports/logger.js +34 -0
- package/dist/ports/logger.js.map +1 -0
- package/dist/ports/mailer.d.ts +6 -0
- package/dist/ports/mailer.d.ts.map +1 -0
- package/dist/ports/mailer.js +2 -0
- package/dist/ports/mailer.js.map +1 -0
- package/dist/ports/policy.d.ts +53 -0
- package/dist/ports/policy.d.ts.map +1 -0
- package/dist/ports/policy.js +81 -0
- package/dist/ports/policy.js.map +1 -0
- package/dist/ports/rate-limit.d.ts +41 -0
- package/dist/ports/rate-limit.d.ts.map +1 -0
- package/dist/ports/rate-limit.js +37 -0
- package/dist/ports/rate-limit.js.map +1 -0
- package/dist/ports/redaction.d.ts +26 -0
- package/dist/ports/redaction.d.ts.map +1 -0
- package/dist/ports/redaction.js +126 -0
- package/dist/ports/redaction.js.map +1 -0
- package/dist/ports/schedules.d.ts +9 -0
- package/dist/ports/schedules.d.ts.map +1 -0
- package/dist/ports/schedules.js +2 -0
- package/dist/ports/schedules.js.map +1 -0
- package/dist/ports/storage.d.ts +47 -0
- package/dist/ports/storage.d.ts.map +1 -0
- package/dist/ports/storage.js +185 -0
- package/dist/ports/storage.js.map +1 -0
- package/dist/ports/testing.d.ts +73 -0
- package/dist/ports/testing.d.ts.map +1 -0
- package/dist/ports/testing.js +105 -0
- package/dist/ports/testing.js.map +1 -0
- package/dist/ports/unit-of-work.d.ts +56 -0
- package/dist/ports/unit-of-work.d.ts.map +1 -0
- package/dist/ports/unit-of-work.js +64 -0
- package/dist/ports/unit-of-work.js.map +1 -0
- package/dist/providers/index.d.ts +8 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +8 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/instrumentation.d.ts +91 -0
- package/dist/providers/instrumentation.d.ts.map +1 -0
- package/dist/providers/instrumentation.js +93 -0
- package/dist/providers/instrumentation.js.map +1 -0
- package/dist/providers/provider.d.ts +146 -0
- package/dist/providers/provider.d.ts.map +1 -0
- package/dist/providers/provider.js +31 -0
- package/dist/providers/provider.js.map +1 -0
- package/dist/schedules/index.d.ts +105 -0
- package/dist/schedules/index.d.ts.map +1 -0
- package/dist/schedules/index.js +178 -0
- package/dist/schedules/index.js.map +1 -0
- package/dist/server/contract-like.d.ts +5 -0
- package/dist/server/contract-like.d.ts.map +1 -0
- package/dist/server/contract-like.js +5 -0
- package/dist/server/contract-like.js.map +1 -0
- package/dist/server/health.d.ts +41 -0
- package/dist/server/health.d.ts.map +1 -0
- package/dist/server/health.js +46 -0
- package/dist/server/health.js.map +1 -0
- package/dist/server/hooks/auth.d.ts +42 -0
- package/dist/server/hooks/auth.d.ts.map +1 -0
- package/dist/server/hooks/auth.js +61 -0
- package/dist/server/hooks/auth.js.map +1 -0
- package/dist/server/hooks/cors.d.ts +13 -0
- package/dist/server/hooks/cors.d.ts.map +1 -0
- package/dist/server/hooks/cors.js +70 -0
- package/dist/server/hooks/cors.js.map +1 -0
- package/dist/server/hooks/errors.d.ts +66 -0
- package/dist/server/hooks/errors.d.ts.map +1 -0
- package/dist/server/hooks/errors.js +83 -0
- package/dist/server/hooks/errors.js.map +1 -0
- package/dist/server/hooks/index.d.ts +12 -0
- package/dist/server/hooks/index.d.ts.map +1 -0
- package/dist/server/hooks/index.js +12 -0
- package/dist/server/hooks/index.js.map +1 -0
- package/dist/server/hooks/logging.d.ts +33 -0
- package/dist/server/hooks/logging.d.ts.map +1 -0
- package/dist/server/hooks/logging.js +90 -0
- package/dist/server/hooks/logging.js.map +1 -0
- package/dist/server/hooks/rate-limit.d.ts +29 -0
- package/dist/server/hooks/rate-limit.d.ts.map +1 -0
- package/dist/server/hooks/rate-limit.js +93 -0
- package/dist/server/hooks/rate-limit.js.map +1 -0
- package/dist/server/hooks/utils.d.ts +9 -0
- package/dist/server/hooks/utils.d.ts.map +1 -0
- package/dist/server/hooks/utils.js +16 -0
- package/dist/server/hooks/utils.js.map +1 -0
- package/dist/server/hooks.d.ts +2 -0
- package/dist/server/hooks.d.ts.map +1 -0
- package/dist/server/hooks.js +2 -0
- package/dist/server/hooks.js.map +1 -0
- package/dist/server/http.d.ts +124 -0
- package/dist/server/http.d.ts.map +1 -0
- package/dist/server/http.js +2 -0
- package/dist/server/http.js.map +1 -0
- package/dist/server/index.d.ts +19 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +15 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/openapi.d.ts +32 -0
- package/dist/server/openapi.d.ts.map +1 -0
- package/dist/server/openapi.js +43 -0
- package/dist/server/openapi.js.map +1 -0
- package/dist/server/providers/index.d.ts +4 -0
- package/dist/server/providers/index.d.ts.map +1 -0
- package/dist/server/providers/index.js +4 -0
- package/dist/server/providers/index.js.map +1 -0
- package/dist/server/providers/loadProviderConfig.d.ts +7 -0
- package/dist/server/providers/loadProviderConfig.d.ts.map +1 -0
- package/dist/server/providers/loadProviderConfig.js +42 -0
- package/dist/server/providers/loadProviderConfig.js.map +1 -0
- package/dist/server/server.d.ts +86 -0
- package/dist/server/server.d.ts.map +1 -0
- package/dist/server/server.js +1031 -0
- package/dist/server/server.js.map +1 -0
- package/dist/server/types.d.ts +3 -0
- package/dist/server/types.d.ts.map +1 -0
- package/dist/server/types.js +3 -0
- package/dist/server/types.js.map +1 -0
- package/package.json +129 -0
- package/src/application/index.ts +747 -0
- package/src/client/client.ts +1105 -0
- package/src/client/index.ts +45 -0
- package/src/client/types.ts +305 -0
- package/src/config/index.ts +497 -0
- package/src/contracts/contract-builder.ts +583 -0
- package/src/contracts/contract-group.ts +502 -0
- package/src/contracts/contract-like.ts +29 -0
- package/src/contracts/index.ts +53 -0
- package/src/contracts/openapi-meta.ts +22 -0
- package/src/contracts/path-template.ts +91 -0
- package/src/contracts/rate-limit.ts +50 -0
- package/src/contracts/types.ts +207 -0
- package/src/contracts/utils.ts +56 -0
- package/src/domain/entity.ts +256 -0
- package/src/domain/events.ts +52 -0
- package/src/domain/index.ts +18 -0
- package/src/domain/value-object.ts +135 -0
- package/src/errors/catalog.ts +149 -0
- package/src/errors/http.ts +80 -0
- package/src/errors/index.ts +28 -0
- package/src/errors/response.ts +54 -0
- package/src/errors/validation.ts +35 -0
- package/src/events/index.ts +246 -0
- package/src/jobs/index.ts +211 -0
- package/src/mail/index.ts +177 -0
- package/src/openapi/index.ts +865 -0
- package/src/openapi/schema-introspector.ts +107 -0
- package/src/ports/audit.ts +176 -0
- package/src/ports/auth.ts +76 -0
- package/src/ports/builder.ts +97 -0
- package/src/ports/cache.ts +94 -0
- package/src/ports/clock.ts +34 -0
- package/src/ports/events.ts +100 -0
- package/src/ports/id-generator.ts +36 -0
- package/src/ports/index.ts +221 -0
- package/src/ports/logger.ts +67 -0
- package/src/ports/policy.ts +242 -0
- package/src/ports/rate-limit.ts +91 -0
- package/src/ports/redaction.ts +199 -0
- package/src/ports/storage.ts +282 -0
- package/src/ports/testing.ts +234 -0
- package/src/ports/unit-of-work.ts +134 -0
- package/src/providers/index.ts +40 -0
- package/src/providers/instrumentation.ts +248 -0
- package/src/providers/provider.ts +191 -0
- package/src/schedules/index.ts +442 -0
- package/src/server/contract-like.ts +8 -0
- package/src/server/health.ts +82 -0
- package/src/server/hooks/auth.ts +147 -0
- package/src/server/hooks/cors.ts +87 -0
- package/src/server/hooks/errors.ts +126 -0
- package/src/server/hooks/index.ts +43 -0
- package/src/server/hooks/logging.ts +121 -0
- package/src/server/hooks/rate-limit.ts +171 -0
- package/src/server/hooks/utils.ts +16 -0
- package/src/server/hooks.ts +1 -0
- package/src/server/http.ts +189 -0
- package/src/server/index.ts +35 -0
- package/src/server/openapi.ts +72 -0
- package/src/server/providers/index.ts +3 -0
- package/src/server/providers/loadProviderConfig.ts +72 -0
- package/src/server/server.ts +1521 -0
- package/src/server/types.ts +2 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Health check handler
|
|
3
|
+
* Health check handler for Beignet server adapters.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Create a health check handler
|
|
7
|
+
* Returns a function that can be called with an HttpRequestLike to get an HttpResponseLike
|
|
8
|
+
*/
|
|
9
|
+
export function createHealthHandler(ports, healthConfig, env) {
|
|
10
|
+
return async (_req) => {
|
|
11
|
+
let result;
|
|
12
|
+
if (healthConfig?.check) {
|
|
13
|
+
try {
|
|
14
|
+
result = await healthConfig.check(ports);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
// Health check function threw - treat as unhealthy
|
|
18
|
+
// Only include error details in development/test to avoid leaking sensitive info
|
|
19
|
+
const includeErrorDetails = env === "development" || env === "test";
|
|
20
|
+
result = {
|
|
21
|
+
ok: false,
|
|
22
|
+
details: {
|
|
23
|
+
error: {
|
|
24
|
+
ok: false,
|
|
25
|
+
message: includeErrorDetails
|
|
26
|
+
? error instanceof Error
|
|
27
|
+
? error.message
|
|
28
|
+
: String(error)
|
|
29
|
+
: "Health check failed",
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
result = { ok: true };
|
|
37
|
+
}
|
|
38
|
+
const status = result.ok ? 200 : 503;
|
|
39
|
+
return {
|
|
40
|
+
status,
|
|
41
|
+
body: result,
|
|
42
|
+
headers: { "Content-Type": "application/json" },
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=health.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"health.js","sourceRoot":"","sources":["../../src/server/health.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAkCH;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CACjC,KAAY,EACZ,YAA6C,EAC7C,GAAmB;IAEnB,OAAO,KAAK,EAAE,IAAqB,EAA6B,EAAE;QAChE,IAAI,MAAyB,CAAC;QAC9B,IAAI,YAAY,EAAE,KAAK,EAAE,CAAC;YACxB,IAAI,CAAC;gBACH,MAAM,GAAG,MAAM,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YAC3C,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,mDAAmD;gBACnD,iFAAiF;gBACjF,MAAM,mBAAmB,GAAG,GAAG,KAAK,aAAa,IAAI,GAAG,KAAK,MAAM,CAAC;gBACpE,MAAM,GAAG;oBACP,EAAE,EAAE,KAAK;oBACT,OAAO,EAAE;wBACP,KAAK,EAAE;4BACL,EAAE,EAAE,KAAK;4BACT,OAAO,EAAE,mBAAmB;gCAC1B,CAAC,CAAC,KAAK,YAAY,KAAK;oCACtB,CAAC,CAAC,KAAK,CAAC,OAAO;oCACf,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;gCACjB,CAAC,CAAC,qBAAqB;yBAC1B;qBACF;iBACF,CAAC;YACJ,CAAC;QACH,CAAC;aAAM,CAAC;YACN,MAAM,GAAG,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC;QACxB,CAAC;QAED,MAAM,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;QAErC,OAAO;YACL,MAAM;YACN,IAAI,EAAE,MAAM;YACZ,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;SAChD,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type AuthPort, type AuthSession } from "../../ports";
|
|
2
|
+
import type { HttpRequestLike, HttpResponse, ServerHook } from "../types";
|
|
3
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
4
|
+
export type AuthHookMode = "public" | "optional" | "required";
|
|
5
|
+
export type AuthHookModeInput = AuthHookMode | boolean | null | undefined;
|
|
6
|
+
export type CtxWithAuthPort = {
|
|
7
|
+
ports: {
|
|
8
|
+
auth: AuthPort;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export type AuthHookArgs<Ctx> = {
|
|
12
|
+
req: HttpRequestLike;
|
|
13
|
+
ctx: Ctx;
|
|
14
|
+
contract: {
|
|
15
|
+
metadata?: Record<string, unknown>;
|
|
16
|
+
};
|
|
17
|
+
path: unknown;
|
|
18
|
+
query: unknown;
|
|
19
|
+
headers: unknown;
|
|
20
|
+
body: unknown;
|
|
21
|
+
};
|
|
22
|
+
export type AuthHookAssignArgs<Ctx, Session> = AuthHookArgs<Ctx> & {
|
|
23
|
+
session: Session | null;
|
|
24
|
+
};
|
|
25
|
+
export type AuthHookUnauthorizedArgs<Ctx, Session> = AuthHookArgs<Ctx> & {
|
|
26
|
+
session: Session | null;
|
|
27
|
+
};
|
|
28
|
+
export type AuthHooksOptions<Ctx, Session> = {
|
|
29
|
+
name?: string;
|
|
30
|
+
mode?: (args: AuthHookArgs<Ctx>) => MaybePromise<AuthHookModeInput>;
|
|
31
|
+
getSession?: (args: AuthHookArgs<Ctx>) => MaybePromise<Session | null>;
|
|
32
|
+
isAuthenticated?: (session: Session | null) => boolean;
|
|
33
|
+
assign?: (args: AuthHookAssignArgs<Ctx, Session>) => MaybePromise<Ctx>;
|
|
34
|
+
unauthorized?: (args: AuthHookUnauthorizedArgs<Ctx, Session>) => MaybePromise<HttpResponse>;
|
|
35
|
+
};
|
|
36
|
+
type InferAuthSession<TAuth> = TAuth extends AuthPort<infer User, infer SessionMetadata> ? AuthSession<User, SessionMetadata> : unknown;
|
|
37
|
+
export declare function createAuthHooks<Ctx extends CtxWithAuthPort>(options?: AuthHooksOptions<Ctx, InferAuthSession<Ctx["ports"]["auth"]>>): ServerHook<Ctx, Ctx["ports"]>;
|
|
38
|
+
export declare function createAuthHooks<Ctx, Session>(options: AuthHooksOptions<Ctx, Session> & {
|
|
39
|
+
getSession: (args: AuthHookArgs<Ctx>) => MaybePromise<Session | null>;
|
|
40
|
+
}): ServerHook<Ctx>;
|
|
41
|
+
export {};
|
|
42
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../../src/server/hooks/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,WAAW,EAEjB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAE1E,KAAK,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAEtC,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,UAAU,GAAG,UAAU,CAAC;AAC9D,MAAM,MAAM,iBAAiB,GAAG,YAAY,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC;AAE1E,MAAM,MAAM,eAAe,GAAG;IAC5B,KAAK,EAAE;QACL,IAAI,EAAE,QAAQ,CAAC;KAChB,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,YAAY,CAAC,GAAG,IAAI;IAC9B,GAAG,EAAE,eAAe,CAAC;IACrB,GAAG,EAAE,GAAG,CAAC;IACT,QAAQ,EAAE;QACR,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KACpC,CAAC;IACF,IAAI,EAAE,OAAO,CAAC;IACd,KAAK,EAAE,OAAO,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,OAAO,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,kBAAkB,CAAC,GAAG,EAAE,OAAO,IAAI,YAAY,CAAC,GAAG,CAAC,GAAG;IACjE,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;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AAO,EAAE,gBAAgB,CAAC,GAAG,EAAE,OAAO,CAAC,GAAG;IACxC,UAAU,EAAE,CAAC,IAAI,EAAE,YAAY,CAAC,GAAG,CAAC,KAAK,YAAY,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC;CACvE,GACA,UAAU,CAAC,GAAG,CAAC,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { AuthUnauthorizedError, } from "../../ports";
|
|
2
|
+
function modeFromInput(input) {
|
|
3
|
+
if (input === true || input === "required")
|
|
4
|
+
return "required";
|
|
5
|
+
if (input === "optional")
|
|
6
|
+
return "optional";
|
|
7
|
+
return "public";
|
|
8
|
+
}
|
|
9
|
+
function defaultMode(args) {
|
|
10
|
+
return modeFromInput(args.contract.metadata?.auth);
|
|
11
|
+
}
|
|
12
|
+
async function defaultGetSession(args) {
|
|
13
|
+
const auth = args.ctx.ports?.auth;
|
|
14
|
+
if (!auth) {
|
|
15
|
+
throw new Error("createAuthHooks requires ctx.ports.auth or an explicit getSession option.");
|
|
16
|
+
}
|
|
17
|
+
return auth.getSession(args.req);
|
|
18
|
+
}
|
|
19
|
+
function defaultIsAuthenticated(session) {
|
|
20
|
+
return session !== null;
|
|
21
|
+
}
|
|
22
|
+
export function createAuthHooks(options = {}) {
|
|
23
|
+
return {
|
|
24
|
+
name: options.name ?? "auth",
|
|
25
|
+
beforeHandle: async ({ req, ctx, contract, path, query, headers, body, }) => {
|
|
26
|
+
const args = {
|
|
27
|
+
req,
|
|
28
|
+
ctx,
|
|
29
|
+
contract,
|
|
30
|
+
path,
|
|
31
|
+
query,
|
|
32
|
+
headers,
|
|
33
|
+
body,
|
|
34
|
+
};
|
|
35
|
+
const mode = modeFromInput(options.mode ? await options.mode(args) : defaultMode(args));
|
|
36
|
+
if (mode === "public") {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
const session = options.getSession
|
|
40
|
+
? await options.getSession(args)
|
|
41
|
+
: (await defaultGetSession(args));
|
|
42
|
+
const isAuthenticated = options.isAuthenticated?.(session) ?? defaultIsAuthenticated(session);
|
|
43
|
+
if (mode === "required" && !isAuthenticated) {
|
|
44
|
+
if (options.unauthorized) {
|
|
45
|
+
return {
|
|
46
|
+
ctx,
|
|
47
|
+
response: await options.unauthorized({ ...args, session }),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
throw new AuthUnauthorizedError();
|
|
51
|
+
}
|
|
52
|
+
if (!options.assign) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
ctx: await options.assign({ ...args, session }),
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../../src/server/hooks/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,qBAAqB,GACtB,MAAM,aAAa,CAAC;AAkDrB,SAAS,aAAa,CAAC,KAAwB;IAC7C,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,UAAU;QAAE,OAAO,UAAU,CAAC;IAC9D,IAAI,KAAK,KAAK,UAAU;QAAE,OAAO,UAAU,CAAC;IAC5C,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED,SAAS,WAAW,CAAM,IAAuB;IAC/C,OAAO,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,IAAyB,CAAC,CAAC;AAC1E,CAAC;AAED,KAAK,UAAU,iBAAiB,CAC9B,IAAuB;IAEvB,MAAM,IAAI,GAAI,IAAI,CAAC,GAAuC,CAAC,KAAK,EAAE,IAAI,CAAC;IACvE,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,MAAM,IAAI,KAAK,CACb,2EAA2E,CAC5E,CAAC;IACJ,CAAC;IAED,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACnC,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,IAAI,qBAAqB,EAAE,CAAC;YACpC,CAAC;YAED,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;gBACpB,OAAO,SAAS,CAAC;YACnB,CAAC;YAED,OAAO;gBACL,GAAG,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,CAAC;aAChD,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CORS hook utilities for @beignet/core/server
|
|
3
|
+
*/
|
|
4
|
+
import type { HttpRequestLike, ServerHook } from "../types";
|
|
5
|
+
export interface CorsConfig {
|
|
6
|
+
origins?: string[] | "*";
|
|
7
|
+
methods?: string[];
|
|
8
|
+
headers?: string[];
|
|
9
|
+
credentials?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare function applyCorsHeaders(headers: Record<string, string>, req: HttpRequestLike, corsConfig: CorsConfig): void;
|
|
12
|
+
export declare function createCorsHooks<Ctx>(config: CorsConfig): ServerHook<Ctx>;
|
|
13
|
+
//# sourceMappingURL=cors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cors.d.ts","sourceRoot":"","sources":["../../../src/server/hooks/cors.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAE5D,MAAM,WAAW,UAAU;IACzB,OAAO,CAAC,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IACzB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAmBD,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC/B,GAAG,EAAE,eAAe,EACpB,UAAU,EAAE,UAAU,GACrB,IAAI,CA4BN;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,GAAG,UAAU,CAAC,GAAG,CAAC,CAsBxE"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CORS hook utilities for @beignet/core/server
|
|
3
|
+
*/
|
|
4
|
+
const DEFAULT_CORS = {
|
|
5
|
+
origins: "*",
|
|
6
|
+
methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
|
|
7
|
+
headers: ["Content-Type", "Authorization"],
|
|
8
|
+
credentials: false,
|
|
9
|
+
};
|
|
10
|
+
function appendVaryOrigin(headers) {
|
|
11
|
+
const varyKey = Object.keys(headers).find((key) => key.toLowerCase() === "vary") ?? "Vary";
|
|
12
|
+
const current = headers[varyKey];
|
|
13
|
+
const values = current?.split(",").map((value) => value.trim().toLowerCase());
|
|
14
|
+
if (values?.includes("origin"))
|
|
15
|
+
return;
|
|
16
|
+
headers[varyKey] = current ? `${current}, Origin` : "Origin";
|
|
17
|
+
}
|
|
18
|
+
export function applyCorsHeaders(headers, req, corsConfig) {
|
|
19
|
+
const origins = corsConfig.origins ?? DEFAULT_CORS.origins;
|
|
20
|
+
const methods = corsConfig.methods ?? DEFAULT_CORS.methods;
|
|
21
|
+
const allowedHeaders = corsConfig.headers ?? DEFAULT_CORS.headers;
|
|
22
|
+
const credentials = corsConfig.credentials ?? DEFAULT_CORS.credentials;
|
|
23
|
+
if (credentials && origins === "*") {
|
|
24
|
+
const requestOrigin = req.headers.get("Origin");
|
|
25
|
+
if (requestOrigin) {
|
|
26
|
+
headers["Access-Control-Allow-Origin"] = requestOrigin;
|
|
27
|
+
appendVaryOrigin(headers);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
else if (origins === "*") {
|
|
31
|
+
headers["Access-Control-Allow-Origin"] = "*";
|
|
32
|
+
}
|
|
33
|
+
else if (Array.isArray(origins)) {
|
|
34
|
+
const requestOrigin = req.headers.get("Origin");
|
|
35
|
+
if (requestOrigin && origins.includes(requestOrigin)) {
|
|
36
|
+
headers["Access-Control-Allow-Origin"] = requestOrigin;
|
|
37
|
+
appendVaryOrigin(headers);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
headers["Access-Control-Allow-Methods"] = methods.join(", ");
|
|
41
|
+
headers["Access-Control-Allow-Headers"] = allowedHeaders.join(", ");
|
|
42
|
+
if (credentials) {
|
|
43
|
+
headers["Access-Control-Allow-Credentials"] = "true";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export function createCorsHooks(config) {
|
|
47
|
+
return {
|
|
48
|
+
name: "cors",
|
|
49
|
+
onRequest: ({ req }) => {
|
|
50
|
+
if (req.method !== "OPTIONS")
|
|
51
|
+
return undefined;
|
|
52
|
+
const headers = {};
|
|
53
|
+
applyCorsHeaders(headers, req, config);
|
|
54
|
+
return {
|
|
55
|
+
status: 204,
|
|
56
|
+
headers,
|
|
57
|
+
body: null,
|
|
58
|
+
};
|
|
59
|
+
},
|
|
60
|
+
beforeSend: ({ req, response }) => {
|
|
61
|
+
const headers = { ...(response.headers ?? {}) };
|
|
62
|
+
applyCorsHeaders(headers, req, config);
|
|
63
|
+
return {
|
|
64
|
+
...response,
|
|
65
|
+
headers,
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=cors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cors.js","sourceRoot":"","sources":["../../../src/server/hooks/cors.ts"],"names":[],"mappings":"AAAA;;GAEG;AAWH,MAAM,YAAY,GAAyB;IACzC,OAAO,EAAE,GAAG;IACZ,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,CAAC;IAC7D,OAAO,EAAE,CAAC,cAAc,EAAE,eAAe,CAAC;IAC1C,WAAW,EAAE,KAAK;CACnB,C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,IAAI,EAAE,CAAC,EAAE,CAAC;YAChD,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC;YACvC,OAAO;gBACL,GAAG,QAAQ;gBACX,OAAO;aACR,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-agnostic error mapping utilities for @beignet/core/server
|
|
3
|
+
*/
|
|
4
|
+
import type { AppEnvironment } from "../health";
|
|
5
|
+
/**
|
|
6
|
+
* Re-export AppEnvironment for convenience
|
|
7
|
+
*/
|
|
8
|
+
export type { AppEnvironment } from "../health";
|
|
9
|
+
/**
|
|
10
|
+
* Error mapping result
|
|
11
|
+
*/
|
|
12
|
+
export interface ErrorMappingResult {
|
|
13
|
+
status: number;
|
|
14
|
+
body: unknown;
|
|
15
|
+
headers?: Record<string, string>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Error mapping configuration
|
|
19
|
+
*/
|
|
20
|
+
export interface ErrorMappingConfig<Ctx> {
|
|
21
|
+
/** Custom error mapper function */
|
|
22
|
+
mapErrorToResponse?: (err: unknown, ctx: Ctx) => ErrorMappingResult;
|
|
23
|
+
/** Include stack traces in error responses (default: true in dev/test, false in production) */
|
|
24
|
+
includeStackInResponse?: boolean;
|
|
25
|
+
/** Application environment */
|
|
26
|
+
env?: AppEnvironment;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Default error mapping function that handles unknown errors and converts them
|
|
30
|
+
* to a standard error response format.
|
|
31
|
+
*
|
|
32
|
+
* **Important:** This function does NOT handle AppError instances from @beignet/core/errors.
|
|
33
|
+
* AppError is handled separately in the router's error handling flow before reaching
|
|
34
|
+
* this function. This function is only called for truly unexpected errors that bypass normal
|
|
35
|
+
* error handling (e.g., unhandled exceptions, infrastructure errors).
|
|
36
|
+
*
|
|
37
|
+
* This function:
|
|
38
|
+
* 1. First tries the custom mapErrorToResponse if provided
|
|
39
|
+
* 2. Falls back to a default 500 error response
|
|
40
|
+
* 3. Optionally includes stack traces in development/test environments
|
|
41
|
+
*
|
|
42
|
+
* @param err - The error that was thrown (excluding AppError instances)
|
|
43
|
+
* @param ctx - The request context
|
|
44
|
+
* @param config - Error mapping configuration
|
|
45
|
+
* @returns An error mapping result with status, body, and optional headers
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* const errorConfig = {
|
|
50
|
+
* mapErrorToResponse: (err, ctx) => ({
|
|
51
|
+
* status: 500,
|
|
52
|
+
* body: {
|
|
53
|
+
* code: "INTERNAL_SERVER_ERROR",
|
|
54
|
+
* message: "Custom error",
|
|
55
|
+
* requestId: ctx.requestId,
|
|
56
|
+
* },
|
|
57
|
+
* }),
|
|
58
|
+
* includeStackInResponse: true,
|
|
59
|
+
* env: "development",
|
|
60
|
+
* };
|
|
61
|
+
*
|
|
62
|
+
* const result = defaultMapErrorToResponse(error, ctx, errorConfig);
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export declare function defaultMapErrorToResponse<Ctx>(err: unknown, ctx: Ctx, config: ErrorMappingConfig<Ctx>): ErrorMappingResult;
|
|
66
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../../src/server/hooks/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAGhD;;GAEG;AACH,YAAY,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEhD;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB,CAAC,GAAG;IACrC,mCAAmC;IACnC,kBAAkB,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,KAAK,kBAAkB,CAAC;IAEpE,+FAA+F;IAC/F,sBAAsB,CAAC,EAAE,OAAO,CAAC;IAEjC,8BAA8B;IAC9B,GAAG,CAAC,EAAE,cAAc,CAAC;CACtB;AA0BD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,yBAAyB,CAAC,GAAG,EAC3C,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,GAAG,EACR,MAAM,EAAE,kBAAkB,CAAC,GAAG,CAAC,GAC9B,kBAAkB,CAwBpB"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-agnostic error mapping utilities for @beignet/core/server
|
|
3
|
+
*/
|
|
4
|
+
import { createErrorResponseBody } from "../../errors";
|
|
5
|
+
import { getRequestIdFromContext } from "./utils";
|
|
6
|
+
/**
|
|
7
|
+
* Create default error response body
|
|
8
|
+
*/
|
|
9
|
+
function createDefaultErrorBody(err, includeStack, requestId) {
|
|
10
|
+
return createErrorResponseBody({
|
|
11
|
+
code: "INTERNAL_SERVER_ERROR",
|
|
12
|
+
message: "Internal server error",
|
|
13
|
+
requestId,
|
|
14
|
+
details: includeStack && err instanceof Error
|
|
15
|
+
? {
|
|
16
|
+
error: {
|
|
17
|
+
message: err.message,
|
|
18
|
+
stack: err.stack,
|
|
19
|
+
},
|
|
20
|
+
}
|
|
21
|
+
: undefined,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Default error mapping function that handles unknown errors and converts them
|
|
26
|
+
* to a standard error response format.
|
|
27
|
+
*
|
|
28
|
+
* **Important:** This function does NOT handle AppError instances from @beignet/core/errors.
|
|
29
|
+
* AppError is handled separately in the router's error handling flow before reaching
|
|
30
|
+
* this function. This function is only called for truly unexpected errors that bypass normal
|
|
31
|
+
* error handling (e.g., unhandled exceptions, infrastructure errors).
|
|
32
|
+
*
|
|
33
|
+
* This function:
|
|
34
|
+
* 1. First tries the custom mapErrorToResponse if provided
|
|
35
|
+
* 2. Falls back to a default 500 error response
|
|
36
|
+
* 3. Optionally includes stack traces in development/test environments
|
|
37
|
+
*
|
|
38
|
+
* @param err - The error that was thrown (excluding AppError instances)
|
|
39
|
+
* @param ctx - The request context
|
|
40
|
+
* @param config - Error mapping configuration
|
|
41
|
+
* @returns An error mapping result with status, body, and optional headers
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* const errorConfig = {
|
|
46
|
+
* mapErrorToResponse: (err, ctx) => ({
|
|
47
|
+
* status: 500,
|
|
48
|
+
* body: {
|
|
49
|
+
* code: "INTERNAL_SERVER_ERROR",
|
|
50
|
+
* message: "Custom error",
|
|
51
|
+
* requestId: ctx.requestId,
|
|
52
|
+
* },
|
|
53
|
+
* }),
|
|
54
|
+
* includeStackInResponse: true,
|
|
55
|
+
* env: "development",
|
|
56
|
+
* };
|
|
57
|
+
*
|
|
58
|
+
* const result = defaultMapErrorToResponse(error, ctx, errorConfig);
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export function defaultMapErrorToResponse(err, ctx, config) {
|
|
62
|
+
// First, try the user's custom error handler
|
|
63
|
+
if (config.mapErrorToResponse) {
|
|
64
|
+
try {
|
|
65
|
+
return config.mapErrorToResponse(err, ctx);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
// Fall through to default error response below
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// Determine if stack traces should be included
|
|
72
|
+
const includeStack = config.includeStackInResponse ??
|
|
73
|
+
(config.env === "development" || config.env === "test");
|
|
74
|
+
// Default error response
|
|
75
|
+
const requestId = getRequestIdFromContext(ctx);
|
|
76
|
+
const body = createDefaultErrorBody(err, includeStack, requestId);
|
|
77
|
+
return {
|
|
78
|
+
status: 500,
|
|
79
|
+
body,
|
|
80
|
+
headers: { "Content-Type": "application/json" },
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/server/hooks/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,uBAAuB,EAA0B,MAAM,cAAc,CAAC;AAE/E,OAAO,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAC;AA8BlD;;GAEG;AACH,SAAS,sBAAsB,CAC7B,GAAY,EACZ,YAAqB,EACrB,SAAkB;IAElB,OAAO,uBAAuB,CAAC;QAC7B,IAAI,EAAE,uBAAuB;QAC7B,OAAO,EAAE,uBAAuB;QAChC,SAAS;QACT,OAAO,EACL,YAAY,IAAI,GAAG,YAAY,KAAK;YAClC,CAAC,CAAC;gBACE,KAAK,EAAE;oBACL,OAAO,EAAE,GAAG,CAAC,OAAO;oBACpB,KAAK,EAAE,GAAG,CAAC,KAAK;iBACjB;aACF;YACH,CAAC,CAAC,SAAS;KAChB,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,MAAM,UAAU,yBAAyB,CACvC,GAAY,EACZ,GAAQ,EACR,MAA+B;IAE/B,6CAA6C;IAC7C,IAAI,MAAM,CAAC,kBAAkB,EAAE,CAAC;QAC9B,IAAI,CAAC;YACH,OAAO,MAAM,CAAC,kBAAkB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QAC7C,CAAC;QAAC,MAAM,CAAC;YACP,+CAA+C;QACjD,CAAC;IACH,CAAC;IAED,+CAA+C;IAC/C,MAAM,YAAY,GAChB,MAAM,CAAC,sBAAsB;QAC7B,CAAC,MAAM,CAAC,GAAG,KAAK,aAAa,IAAI,MAAM,CAAC,GAAG,KAAK,MAAM,CAAC,CAAC;IAE1D,yBAAyB;IACzB,MAAM,SAAS,GAAG,uBAAuB,CAAC,GAAG,CAAC,CAAC;IAC/C,MAAM,IAAI,GAAG,sBAAsB,CAAC,GAAG,EAAE,YAAY,EAAE,SAAS,CAAC,CAAC;IAElE,OAAO;QACL,MAAM,EAAE,GAAG;QACX,IAAI;QACJ,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;KAChD,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook utilities for @beignet/core/server
|
|
3
|
+
*/
|
|
4
|
+
import type { AnyPorts } from "../../ports";
|
|
5
|
+
import type { ServerHook } from "../http";
|
|
6
|
+
export { type AuthHookArgs, type AuthHookAssignArgs, type AuthHookMode, type AuthHookModeInput, type AuthHooksOptions, type AuthHookUnauthorizedArgs, type CtxWithAuthPort, createAuthHooks, } from "./auth";
|
|
7
|
+
export { applyCorsHeaders, type CorsConfig, createCorsHooks, } from "./cors";
|
|
8
|
+
export { defaultMapErrorToResponse, type ErrorMappingConfig, type ErrorMappingResult, } from "./errors";
|
|
9
|
+
export { createLoggingHooks, type Logger, type LoggingConfig, } from "./logging";
|
|
10
|
+
export { type CtxWithRateLimit, createRateLimitHooks, type RateLimitOptions, } from "./rate-limit";
|
|
11
|
+
export declare function composeHooks<Ctx, Ports extends AnyPorts = AnyPorts>(...hooks: (ServerHook<Ctx, Ports> | readonly ServerHook<Ctx, Ports>[])[]): ServerHook<Ctx, Ports>[];
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/server/hooks/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1C,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,kBAAkB,EACvB,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,KAAK,wBAAwB,EAC7B,KAAK,eAAe,EACpB,eAAe,GAChB,MAAM,QAAQ,CAAC;AAChB,OAAO,EACL,gBAAgB,EAChB,KAAK,UAAU,EACf,eAAe,GAChB,MAAM,QAAQ,CAAC;AAChB,OAAO,EACL,yBAAyB,EACzB,KAAK,kBAAkB,EACvB,KAAK,kBAAkB,GACxB,MAAM,UAAU,CAAC;AAClB,OAAO,EACL,kBAAkB,EAClB,KAAK,MAAM,EACX,KAAK,aAAa,GACnB,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,KAAK,gBAAgB,EACrB,oBAAoB,EACpB,KAAK,gBAAgB,GACtB,MAAM,cAAc,CAAC;AAEtB,wBAAgB,YAAY,CAAC,GAAG,EAAE,KAAK,SAAS,QAAQ,GAAG,QAAQ,EACjE,GAAG,KAAK,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG,SAAS,UAAU,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,CAAC,EAAE,GACvE,UAAU,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,CAE1B"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook utilities for @beignet/core/server
|
|
3
|
+
*/
|
|
4
|
+
export { createAuthHooks, } from "./auth";
|
|
5
|
+
export { applyCorsHeaders, createCorsHooks, } from "./cors";
|
|
6
|
+
export { defaultMapErrorToResponse, } from "./errors";
|
|
7
|
+
export { createLoggingHooks, } from "./logging";
|
|
8
|
+
export { createRateLimitHooks, } from "./rate-limit";
|
|
9
|
+
export function composeHooks(...hooks) {
|
|
10
|
+
return hooks.flatMap((hook) => (Array.isArray(hook) ? hook : [hook]));
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/server/hooks/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH,OAAO,EAQL,eAAe,GAChB,MAAM,QAAQ,CAAC;AAChB,OAAO,EACL,gBAAgB,EAEhB,eAAe,GAChB,MAAM,QAAQ,CAAC;AAChB,OAAO,EACL,yBAAyB,GAG1B,MAAM,UAAU,CAAC;AAClB,OAAO,EACL,kBAAkB,GAGnB,MAAM,WAAW,CAAC;AACnB,OAAO,EAEL,oBAAoB,GAErB,MAAM,cAAc,CAAC;AAEtB,MAAM,UAAU,YAAY,CAC1B,GAAG,KAAqE;IAExE,OAAO,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACxE,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logging hook utilities for @beignet/core/server
|
|
3
|
+
*/
|
|
4
|
+
import type { HttpContractConfig } from "../../contracts";
|
|
5
|
+
import type { HttpRequestLike, ServerHook } from "../types";
|
|
6
|
+
export interface Logger {
|
|
7
|
+
info: (...args: unknown[]) => void;
|
|
8
|
+
error: (...args: unknown[]) => void;
|
|
9
|
+
warn?: (...args: unknown[]) => void;
|
|
10
|
+
debug?: (...args: unknown[]) => void;
|
|
11
|
+
}
|
|
12
|
+
export interface LoggingConfig<Ctx> {
|
|
13
|
+
logger?: Logger;
|
|
14
|
+
requestIdHeader?: string;
|
|
15
|
+
onRequestStart?: (args: {
|
|
16
|
+
ctx?: Ctx;
|
|
17
|
+
req: HttpRequestLike;
|
|
18
|
+
contract?: HttpContractConfig;
|
|
19
|
+
}) => void;
|
|
20
|
+
onRequestEnd?: (args: {
|
|
21
|
+
ctx?: Ctx;
|
|
22
|
+
req: HttpRequestLike;
|
|
23
|
+
res: {
|
|
24
|
+
status: number;
|
|
25
|
+
headers: Record<string, string>;
|
|
26
|
+
};
|
|
27
|
+
durationMs: number;
|
|
28
|
+
contract?: HttpContractConfig;
|
|
29
|
+
error?: unknown;
|
|
30
|
+
}) => void;
|
|
31
|
+
}
|
|
32
|
+
export declare function createLoggingHooks<Ctx>(config: LoggingConfig<Ctx>): ServerHook<Ctx>;
|
|
33
|
+
//# sourceMappingURL=logging.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logging.d.ts","sourceRoot":"","sources":["../../../src/server/hooks/logging.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAC1D,OAAO,KAAK,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAG5D,MAAM,WAAW,MAAM;IACrB,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC;IACnC,KAAK,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC;IACpC,IAAI,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC;IACpC,KAAK,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC;CACtC;AAED,MAAM,WAAW,aAAa,CAAC,GAAG;IAChC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,cAAc,CAAC,EAAE,CAAC,IAAI,EAAE;QACtB,GAAG,CAAC,EAAE,GAAG,CAAC;QACV,GAAG,EAAE,eAAe,CAAC;QACrB,QAAQ,CAAC,EAAE,kBAAkB,CAAC;KAC/B,KAAK,IAAI,CAAC;IACX,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE;QACpB,GAAG,CAAC,EAAE,GAAG,CAAC;QACV,GAAG,EAAE,eAAe,CAAC;QACrB,GAAG,EAAE;YAAE,MAAM,EAAE,MAAM,CAAC;YAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;SAAE,CAAC;QACzD,UAAU,EAAE,MAAM,CAAC;QACnB,QAAQ,CAAC,EAAE,kBAAkB,CAAC;QAC9B,KAAK,CAAC,EAAE,OAAO,CAAC;KACjB,KAAK,IAAI,CAAC;CACZ;AAED,wBAAgB,kBAAkB,CAAC,GAAG,EACpC,MAAM,EAAE,aAAa,CAAC,GAAG,CAAC,GACzB,UAAU,CAAC,GAAG,CAAC,CAqFjB"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logging hook utilities for @beignet/core/server
|
|
3
|
+
*/
|
|
4
|
+
import { getRequestIdFromContext } from "./utils";
|
|
5
|
+
export function createLoggingHooks(config) {
|
|
6
|
+
const requestIdHeader = config.requestIdHeader;
|
|
7
|
+
return {
|
|
8
|
+
name: "logging",
|
|
9
|
+
onRequest: ({ req, contract }) => {
|
|
10
|
+
if (config.onRequestStart) {
|
|
11
|
+
try {
|
|
12
|
+
config.onRequestStart({ req, contract, ctx: undefined });
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
// Ignore logging errors
|
|
16
|
+
}
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
if (!config.logger)
|
|
20
|
+
return undefined;
|
|
21
|
+
try {
|
|
22
|
+
const url = new URL(req.url);
|
|
23
|
+
config.logger.info({ method: req.method, url: url.pathname }, "Request start");
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
// Ignore logging errors
|
|
27
|
+
}
|
|
28
|
+
return undefined;
|
|
29
|
+
},
|
|
30
|
+
...(requestIdHeader
|
|
31
|
+
? {
|
|
32
|
+
beforeSend: ({ ctx, response }) => {
|
|
33
|
+
const requestId = getRequestIdFromContext(ctx);
|
|
34
|
+
if (!requestId) {
|
|
35
|
+
return response;
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
...response,
|
|
39
|
+
headers: {
|
|
40
|
+
...(response.headers ?? {}),
|
|
41
|
+
[requestIdHeader]: String(requestId),
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
}
|
|
46
|
+
: {}),
|
|
47
|
+
afterSend: ({ ctx, req, response, durationMs, contract, error }) => {
|
|
48
|
+
const headers = response.headers ?? {};
|
|
49
|
+
if (config.onRequestEnd) {
|
|
50
|
+
try {
|
|
51
|
+
config.onRequestEnd({
|
|
52
|
+
ctx,
|
|
53
|
+
req,
|
|
54
|
+
res: { status: response.status, headers },
|
|
55
|
+
durationMs,
|
|
56
|
+
contract,
|
|
57
|
+
error,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// Ignore logging errors
|
|
62
|
+
}
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (!config.logger)
|
|
66
|
+
return;
|
|
67
|
+
try {
|
|
68
|
+
const url = new URL(req.url);
|
|
69
|
+
const requestId = getRequestIdFromContext(ctx);
|
|
70
|
+
const payload = {
|
|
71
|
+
method: req.method,
|
|
72
|
+
url: url.pathname,
|
|
73
|
+
status: response.status,
|
|
74
|
+
durationMs: Math.round(durationMs),
|
|
75
|
+
...(requestId !== undefined ? { requestId } : {}),
|
|
76
|
+
...(error !== undefined ? { error } : {}),
|
|
77
|
+
};
|
|
78
|
+
if (error !== undefined) {
|
|
79
|
+
config.logger.error(payload, "Request complete with error");
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
config.logger.info(payload, "Request complete");
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
// Ignore logging errors
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=logging.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logging.js","sourceRoot":"","sources":["../../../src/server/hooks/logging.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,OAAO,EAAE,uB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C;gBACF,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;oBACxB,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,6BAA6B,CAAC,CAAC;oBAC5D,OAAO;gBACT,CAAC;gBACD,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,kBAAkB,CAAC,CAAC;YAClD,CAAC;YAAC,MAAM,CAAC;gBACP,wBAAwB;YAC1B,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate limit hooks for @beignet/core/server
|
|
3
|
+
*/
|
|
4
|
+
import type { RateLimitScope } from "../../contracts";
|
|
5
|
+
import type { ActivityActor, RateLimitPort } from "../../ports";
|
|
6
|
+
import type { HttpRequestLike, ServerHook } from "../types";
|
|
7
|
+
export type RateLimitPorts = {
|
|
8
|
+
rateLimit: RateLimitPort;
|
|
9
|
+
};
|
|
10
|
+
export type CtxWithRateLimit = {
|
|
11
|
+
ports: RateLimitPorts;
|
|
12
|
+
actor?: ActivityActor;
|
|
13
|
+
};
|
|
14
|
+
type EarlyRateLimitScope = Exclude<RateLimitScope, "user">;
|
|
15
|
+
export interface RateLimitOptions<Ctx> {
|
|
16
|
+
key?: (args: {
|
|
17
|
+
ctx: Ctx;
|
|
18
|
+
req: HttpRequestLike;
|
|
19
|
+
scope: RateLimitScope;
|
|
20
|
+
}) => string;
|
|
21
|
+
earlyKey?: (args: {
|
|
22
|
+
req: HttpRequestLike;
|
|
23
|
+
scope: EarlyRateLimitScope;
|
|
24
|
+
}) => string;
|
|
25
|
+
getClientIp?: (req: HttpRequestLike) => string | undefined;
|
|
26
|
+
}
|
|
27
|
+
export declare function createRateLimitHooks<Ctx extends CtxWithRateLimit>(options?: RateLimitOptions<Ctx>): ServerHook<Ctx, RateLimitPorts>;
|
|
28
|
+
export {};
|
|
29
|
+
//# sourceMappingURL=rate-limit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limit.d.ts","sourceRoot":"","sources":["../../../src/server/hooks/rate-limit.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEtD,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAE5D,MAAM,MAAM,cAAc,GAAG;IAC3B,SAAS,EAAE,aAAa,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,KAAK,EAAE,cAAc,CAAC;IACtB,KAAK,CAAC,EAAE,aAAa,CAAC;CACvB,CAAC;AAEF,KAAK,mBAAmB,GAAG,OAAO,CAAC,cAAc,EAAE,MAAM,CAAC,CAAC;AAE3D,MAAM,WAAW,gBAAgB,CAAC,GAAG;IACnC,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE;QACX,GAAG,EAAE,GAAG,CAAC;QACT,GAAG,EAAE,eAAe,CAAC;QACrB,KAAK,EAAE,cAAc,CAAC;KACvB,KAAK,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE;QAChB,GAAG,EAAE,eAAe,CAAC;QACrB,KAAK,EAAE,mBAAmB,CAAC;KAC5B,KAAK,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,CAAC,GAAG,EAAE,eAAe,KAAK,MAAM,GAAG,SAAS,CAAC;CAC5D;AAmFD,wBAAgB,oBAAoB,CAAC,GAAG,SAAS,gBAAgB,EAC/D,OAAO,GAAE,gBAAgB,CAAC,GAAG,CAAM,GAClC,UAAU,CAAC,GAAG,EAAE,cAAc,CAAC,CAsDjC"}
|