@owlmeans/server-api 0.1.18-rc.8 → 0.1.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/README.md +203 -46
  2. package/agent-meta/manifest.json +3 -3
  3. package/agent-meta/skills/server-api/SKILL.md +142 -22
  4. package/build/consts.local.d.ts +2 -0
  5. package/build/consts.local.d.ts.map +1 -0
  6. package/build/consts.local.js +2 -0
  7. package/build/consts.local.js.map +1 -0
  8. package/build/errors.d.ts +12 -0
  9. package/build/errors.d.ts.map +1 -1
  10. package/build/errors.js +16 -0
  11. package/build/errors.js.map +1 -1
  12. package/build/helper.d.ts +3 -6
  13. package/build/helper.d.ts.map +1 -1
  14. package/build/helper.js +10 -1
  15. package/build/helper.js.map +1 -1
  16. package/build/hold.d.ts +35 -0
  17. package/build/hold.d.ts.map +1 -0
  18. package/build/hold.js +57 -0
  19. package/build/hold.js.map +1 -0
  20. package/build/index.d.ts +2 -0
  21. package/build/index.d.ts.map +1 -1
  22. package/build/index.js +2 -0
  23. package/build/index.js.map +1 -1
  24. package/build/protocol.d.ts +18 -0
  25. package/build/protocol.d.ts.map +1 -0
  26. package/build/protocol.js +87 -0
  27. package/build/protocol.js.map +1 -0
  28. package/build/server.d.ts +2 -3
  29. package/build/server.d.ts.map +1 -1
  30. package/build/server.js +42 -34
  31. package/build/server.js.map +1 -1
  32. package/build/types.d.ts +31 -2
  33. package/build/types.d.ts.map +1 -1
  34. package/build/types.local.d.ts +38 -0
  35. package/build/types.local.d.ts.map +1 -0
  36. package/build/types.local.js +2 -0
  37. package/build/types.local.js.map +1 -0
  38. package/build/utils/consts.d.ts +2 -0
  39. package/build/utils/consts.d.ts.map +1 -0
  40. package/build/utils/consts.js +2 -0
  41. package/build/utils/consts.js.map +1 -0
  42. package/build/utils/consts.local.d.ts +5 -0
  43. package/build/utils/consts.local.d.ts.map +1 -0
  44. package/build/utils/consts.local.js +5 -0
  45. package/build/utils/consts.local.js.map +1 -0
  46. package/build/utils/context/types.d.ts +13 -0
  47. package/build/utils/context/types.d.ts.map +1 -0
  48. package/build/utils/context/types.js +2 -0
  49. package/build/utils/context/types.js.map +1 -0
  50. package/build/utils/context.d.ts +3 -3
  51. package/build/utils/context.d.ts.map +1 -1
  52. package/build/utils/context.js +5 -2
  53. package/build/utils/context.js.map +1 -1
  54. package/build/utils/error/types.d.ts +52 -0
  55. package/build/utils/error/types.d.ts.map +1 -0
  56. package/build/utils/error/types.js +2 -0
  57. package/build/utils/error/types.js.map +1 -0
  58. package/build/utils/error.d.ts +5 -15
  59. package/build/utils/error.d.ts.map +1 -1
  60. package/build/utils/error.js +134 -29
  61. package/build/utils/error.js.map +1 -1
  62. package/build/utils/guards.d.ts +1 -5
  63. package/build/utils/guards.d.ts.map +1 -1
  64. package/build/utils/guards.js +15 -19
  65. package/build/utils/guards.js.map +1 -1
  66. package/build/utils/index.d.ts +6 -1
  67. package/build/utils/index.d.ts.map +1 -1
  68. package/build/utils/index.js +2 -1
  69. package/build/utils/index.js.map +1 -1
  70. package/build/utils/log/consts.d.ts +14 -0
  71. package/build/utils/log/consts.d.ts.map +1 -0
  72. package/build/utils/log/consts.js +17 -0
  73. package/build/utils/log/consts.js.map +1 -0
  74. package/build/utils/log/types.d.ts +42 -0
  75. package/build/utils/log/types.d.ts.map +1 -0
  76. package/build/utils/log/types.js +2 -0
  77. package/build/utils/log/types.js.map +1 -0
  78. package/build/utils/log.d.ts +4 -0
  79. package/build/utils/log.d.ts.map +1 -0
  80. package/build/utils/log.js +170 -0
  81. package/build/utils/log.js.map +1 -0
  82. package/build/utils/payload/types.d.ts +18 -0
  83. package/build/utils/payload/types.d.ts.map +1 -0
  84. package/build/utils/payload/types.js +2 -0
  85. package/build/utils/payload/types.js.map +1 -0
  86. package/build/utils/payload.d.ts +3 -15
  87. package/build/utils/payload.d.ts.map +1 -1
  88. package/build/utils/payload.js +41 -57
  89. package/build/utils/payload.js.map +1 -1
  90. package/build/utils/schema.d.ts +7 -0
  91. package/build/utils/schema.d.ts.map +1 -0
  92. package/build/utils/schema.js +20 -0
  93. package/build/utils/schema.js.map +1 -0
  94. package/build/utils/server.d.ts +3 -6
  95. package/build/utils/server.d.ts.map +1 -1
  96. package/build/utils/server.js +12 -13
  97. package/build/utils/server.js.map +1 -1
  98. package/build/utils/types.d.ts +18 -0
  99. package/build/utils/types.d.ts.map +1 -0
  100. package/build/utils/types.js +2 -0
  101. package/build/utils/types.js.map +1 -0
  102. package/build/utils/types.local.d.ts +9 -0
  103. package/build/utils/types.local.d.ts.map +1 -0
  104. package/build/utils/types.local.js +2 -0
  105. package/build/utils/types.local.js.map +1 -0
  106. package/package.json +23 -19
  107. package/src/consts.local.ts +1 -0
  108. package/src/errors.ts +18 -0
  109. package/src/helper.ts +13 -8
  110. package/src/hold.ts +66 -0
  111. package/src/index.ts +2 -0
  112. package/src/protocol.ts +149 -0
  113. package/src/server.ts +72 -69
  114. package/src/types.local.ts +44 -0
  115. package/src/types.ts +38 -4
  116. package/src/utils/consts.local.ts +7 -0
  117. package/src/utils/consts.ts +1 -0
  118. package/src/utils/context/types.ts +13 -0
  119. package/src/utils/context.ts +9 -4
  120. package/src/utils/error/types.ts +52 -0
  121. package/src/utils/error.ts +156 -29
  122. package/src/utils/guards.ts +16 -26
  123. package/src/utils/index.ts +6 -1
  124. package/src/utils/log/consts.ts +18 -0
  125. package/src/utils/log/types.ts +43 -0
  126. package/src/utils/log.ts +193 -0
  127. package/src/utils/payload/types.ts +18 -0
  128. package/src/utils/payload.ts +42 -60
  129. package/src/utils/schema.ts +23 -0
  130. package/src/utils/server.ts +16 -21
  131. package/src/utils/types.local.ts +12 -0
  132. package/src/utils/types.ts +18 -0
  133. package/tests/error-status.spec.ts +349 -0
  134. package/tests/handlers.spec.ts +83 -0
  135. package/tests/json-body.spec.ts +72 -0
  136. package/tests/log-adapter.spec.ts +62 -0
  137. package/tests/log-redaction.spec.ts +181 -0
  138. package/tests/tsconfig.json +12 -0
  139. package/tsconfig.json +1 -0
  140. package/build/.gitkeep +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/utils/context/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AAErD,sDAAsD;AACtD,MAAM,WAAW,oBAAoB;IACnC,yCAAyC;IACzC,eAAe,EAAE,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,KAAK,IAAI,CAAA;IAC7E;;;;OAIG;IACH,cAAc,EAAE,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,MAAM,KAAK,CAAC,CAAA;CAC1F"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/utils/context/types.ts"],"names":[],"mappings":""}
@@ -1,4 +1,4 @@
1
- import type { Config, Context, Request } from '../types.js';
2
- export declare const populateContext: <C extends Config, T extends Context<C>>(req: Request, context: T) => void;
3
- export declare const extractContext: <C extends Config, T extends Context<C>>(req: Request, ctx?: T, location?: string) => T;
1
+ import type { Request } from '../types.js';
2
+ import type { RequestContextHelper } from './context/types.js';
3
+ export declare const makeRequestContextHelper: (req: Request) => RequestContextHelper;
4
4
  //# sourceMappingURL=context.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/utils/context.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AAG3D,eAAO,MAAM,eAAe,GAAI,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,OAAO,CAAC,CAAC,CAAC,EAAE,KAAK,OAAO,EAAE,SAAS,CAAC,KAAG,IAC/D,CAAA;AAEpC,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,OAAO,CAAC,CAAC,CAAC,EAAE,KAAK,OAAO,EAAE,MAAM,CAAC,EAAE,WAAW,MAAM,KAAG,CACxC,CAAA"}
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/utils/context.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAmB,OAAO,EAAE,MAAM,aAAa,CAAA;AAE3D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AAE9D,eAAO,MAAM,wBAAwB,QAAS,OAAO,KAAG,oBAQvD,CAAA"}
@@ -1,5 +1,8 @@
1
1
  import { assertContext } from '@owlmeans/context';
2
2
  import { DEFAULT_ALIAS } from '../consts.js';
3
- export const populateContext = (req, context) => void (req._ctx = context);
4
- export const extractContext = (req, ctx, location) => assertContext(req._ctx ?? ctx, location ?? DEFAULT_ALIAS);
3
+ export const makeRequestContextHelper = (req) => {
4
+ const populateContext = (context) => void (req._ctx = context);
5
+ const extractContext = (ctx, location) => assertContext(req._ctx ?? ctx, location ?? DEFAULT_ALIAS);
6
+ return { populateContext, extractContext };
7
+ };
5
8
  //# sourceMappingURL=context.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/utils/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAEjD,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAE5C,MAAM,CAAC,MAAM,eAAe,GAAG,CAAyC,GAAY,EAAE,OAAU,EAAQ,EAAE,CACxG,KAAK,CAAE,GAAW,CAAC,IAAI,GAAG,OAAO,CAAC,CAAA;AAEpC,MAAM,CAAC,MAAM,cAAc,GAAG,CAAyC,GAAY,EAAE,GAAO,EAAE,QAAiB,EAAK,EAAE,CACpH,aAAa,CAAQ,GAAW,CAAC,IAAI,IAAI,GAAG,EAAE,QAAQ,IAAI,aAAa,CAAC,CAAA"}
1
+ {"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/utils/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAEjD,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAG5C,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,GAAY,EAAwB,EAAE;IAC7E,MAAM,eAAe,GAAG,CAAyC,OAAU,EAAQ,EAAE,CACnF,KAAK,CAAE,GAAW,CAAC,IAAI,GAAG,OAAO,CAAC,CAAA;IAEpC,MAAM,cAAc,GAAG,CAAyC,GAAO,EAAE,QAAiB,EAAK,EAAE,CAC/F,aAAa,CAAQ,GAAW,CAAC,IAAI,IAAI,GAAG,EAAE,QAAQ,IAAI,aAAa,CAAC,CAAA;IAE1E,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,CAAA;AAC5C,CAAC,CAAA"}
@@ -0,0 +1,52 @@
1
+ import type { FastifyReply } from 'fastify';
2
+ import type { Config, HttpErrorExposure } from '../../types.js';
3
+ import type { SerializedHttpError } from '../types.js';
4
+ /** How the HTTP boundary answers a thrown error: its status, its wire body, its headers and its log entry. */
5
+ export interface HttpErrorHelper {
6
+ /**
7
+ * The HTTP status an error's class declares, or `null` when it declares none that may be honoured.
8
+ *
9
+ * Read through the constructor chain — a static property is inherited, so a subclass answers what
10
+ * its nearest declaring ancestor says, and redeclares to change it. An integer 4xx is honoured
11
+ * directly. A 5xx is honoured only with `allowServerErrorStatus = true`, so a domain class must
12
+ * explicitly opt in to exposing a precise availability response. The read is structural, so a
13
+ * class from a duplicate module copy declares exactly what the imported one does.
14
+ */
15
+ declaredErrorStatus: (error: unknown) => number | null;
16
+ /**
17
+ * The HTTP status an error is answered with, read off the error exactly as given.
18
+ *
19
+ * Two families reach here and both must be recognised. `AuthFailedError` / `AccessError` are this
20
+ * package's own, raised by the request pipeline itself. `AuthorizationError` / `AuthForbidden`
21
+ * come from `@owlmeans/auth` and are what the guards and gates actually throw — an unrecognised
22
+ * one answers 500, which reports a refused request as a crashed server: the client cannot tell
23
+ * "sign in again" from "the service is broken", and a consumer watching statuses concludes the
24
+ * application fell over. A family member is recognised by class OR by registered type name, so a
25
+ * class from a duplicate module copy answers the same status.
26
+ *
27
+ * **Order matters.** `AuthForbidden extends AuthorizationError`, so the 403 branch has to be
28
+ * tested first or every refusal of a permission is reported as a failure to authenticate. Both
29
+ * auth branches come before a class's own declaration, so an auth refusal can never be restated
30
+ * as something else. Everything that is neither an auth error nor a class declaring an allowed
31
+ * {@link HttpErrorHelper.declaredErrorStatus} answers 500.
32
+ */
33
+ errorStatus: (error: unknown) => number;
34
+ /** Invalid or absent configuration always resolves to the production-safe policy. */
35
+ errorExposure: (config?: Pick<Config, 'http'>) => HttpErrorExposure;
36
+ /** One central wire shape for every server error. */
37
+ serializeError: (error: Error, exposure?: HttpErrorExposure) => SerializedHttpError;
38
+ /** Apply safe response metadata carried by an error without coupling its package to Fastify. */
39
+ applyErrorHeaders: (error: unknown, reply: FastifyReply) => void;
40
+ /**
41
+ * Answer a thrown error: a status, and the marshalled `ResilientError` as the body.
42
+ *
43
+ * The status is resolved in two steps. The error AS THROWN comes first: it is the one object whose
44
+ * class is certainly the class that was raised, while `ResilientError.ensure` rebuilds through the
45
+ * registry of whichever `@owlmeans/error` copy this module loaded — under duplicate module copies
46
+ * that turns a perfectly recognisable auth refusal into a plain `ResilientError` answering 500.
47
+ * Only when the thrown error answers 500 is the ENSURED error asked, because the rebuild is what
48
+ * gives a status to a marshalled error that crossed a hop as a plain `Error`.
49
+ */
50
+ handleError: (error: Error, reply: FastifyReply, exposure?: HttpErrorExposure) => void;
51
+ }
52
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/utils/error/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAC3C,OAAO,KAAK,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AAC/D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA;AAEtD,8GAA8G;AAC9G,MAAM,WAAW,eAAe;IAC9B;;;;;;;;OAQG;IACH,mBAAmB,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,GAAG,IAAI,CAAA;IACtD;;;;;;;;;;;;;;;;OAgBG;IACH,WAAW,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,CAAA;IACvC,qFAAqF;IACrF,aAAa,EAAE,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,iBAAiB,CAAA;IACnE,qDAAqD;IACrD,cAAc,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,QAAQ,CAAC,EAAE,iBAAiB,KAAK,mBAAmB,CAAA;IACnF,gGAAgG;IAChG,iBAAiB,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,YAAY,KAAK,IAAI,CAAA;IAChE;;;;;;;;;OASG;IACH,WAAW,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,QAAQ,CAAC,EAAE,iBAAiB,KAAK,IAAI,CAAA;CACvF"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/utils/error/types.ts"],"names":[],"mappings":""}
@@ -1,16 +1,6 @@
1
- import type { FastifyReply } from 'fastify';
2
- /**
3
- * Map a thrown error onto an HTTP status.
4
- *
5
- * Two families reach here and both must be recognised. `AuthFailedError` / `AccessError` are this
6
- * package's own, raised by the request pipeline itself. `AuthorizationError` / `AuthForbidden`
7
- * come from `@owlmeans/auth` and are what the guards and gates actually throw — an unrecognised
8
- * one answers 500, which reports a refused request as a crashed server: the client cannot tell
9
- * "sign in again" from "the service is broken", and a consumer watching statuses concludes the
10
- * application fell over.
11
- *
12
- * **Order matters.** `AuthForbidden extends AuthorizationError`, so the 403 branch has to be
13
- * tested first or every refusal of a permission is reported as a failure to authenticate.
14
- */
15
- export declare const handleError: (error: Error, reply: FastifyReply) => void;
1
+ import type { HttpErrorHelper } from './error/types.js';
2
+ export declare const createHttpErrorHelper: () => HttpErrorHelper;
3
+ export declare const httpErrorHelper: HttpErrorHelper;
4
+ /** @deprecated compat:factory-refactor — use `httpErrorHelper.errorStatus(…)` */
5
+ export declare const errorStatus: (error: unknown) => number;
16
6
  //# sourceMappingURL=error.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/utils/error.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAK3C;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,WAAW,GAAI,OAAO,KAAK,EAAE,OAAO,YAAY,SAe5D,CAAA"}
1
+ {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/utils/error.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAEvD,eAAO,MAAM,qBAAqB,QAAO,eA6IxC,CAAA;AAED,eAAO,MAAM,eAAe,iBAA0B,CAAA;AAEtD,iFAAiF;AACjF,eAAO,MAAM,WAAW,UAAW,OAAO,KAAG,MAA4C,CAAA"}
@@ -1,33 +1,138 @@
1
- import { FORBIDDEN_ERROR, SERVER_ERROR, UNAUTHORIZED_ERROR } from '@owlmeans/api';
1
+ import { FORBIDDEN_ERROR, SERVER_ERROR, UNAUTHORIZED_ERROR, DENIAL_KIND_HEADER, ACCESS_DENIED_KIND, } from '@owlmeans/api';
2
2
  import { AccessError, AuthFailedError } from '../errors.js';
3
3
  import { AuthForbidden, AuthorizationError } from '@owlmeans/auth';
4
- import { ResilientError } from '@owlmeans/error';
5
- /**
6
- * Map a thrown error onto an HTTP status.
7
- *
8
- * Two families reach here and both must be recognised. `AuthFailedError` / `AccessError` are this
9
- * package's own, raised by the request pipeline itself. `AuthorizationError` / `AuthForbidden`
10
- * come from `@owlmeans/auth` and are what the guards and gates actually throw — an unrecognised
11
- * one answers 500, which reports a refused request as a crashed server: the client cannot tell
12
- * "sign in again" from "the service is broken", and a consumer watching statuses concludes the
13
- * application fell over.
14
- *
15
- * **Order matters.** `AuthForbidden extends AuthorizationError`, so the 403 branch has to be
16
- * tested first or every refusal of a permission is reported as a failure to authenticate.
17
- */
18
- export const handleError = (error, reply) => {
19
- let code = SERVER_ERROR;
20
- if (error instanceof AuthForbidden || error instanceof AccessError) {
21
- // An established identity that lacks the permission. Re-authenticating changes nothing.
22
- code = FORBIDDEN_ERROR;
23
- }
24
- else if (error instanceof AuthorizationError || error instanceof AuthFailedError) {
25
- // No usable credential — absent, expired, revoked, or naming a session this server no longer
26
- // holds. The caller is told to sign in, not that signing in would not help.
27
- code = UNAUTHORIZED_ERROR;
28
- }
29
- if (!reply.sent) {
30
- reply.code(code).send(ResilientError.marshal(ResilientError.ensure(error)).message);
31
- }
4
+ import { isResilientError, ResilientError, SEPARATOR } from '@owlmeans/error';
5
+ import { randomUUID } from 'node:crypto';
6
+ import { fastifyLogUtils } from './log.js';
7
+ import { CLIENT_ERROR_FIRST, CLIENT_ERROR_LAST, SERVER_ERROR_FIRST, SERVER_ERROR_LAST } from './consts.local.js';
8
+ import { INCIDENT_ID_HEADER } from './consts.js';
9
+ export const createHttpErrorHelper = () => {
10
+ const declaredErrorStatus = (error) => {
11
+ if (error == null || typeof error !== 'object') {
12
+ return null;
13
+ }
14
+ const declaration = error.constructor;
15
+ const status = declaration?.httpStatus;
16
+ if (typeof status !== 'number' || !Number.isInteger(status)) {
17
+ return null;
18
+ }
19
+ if (status >= CLIENT_ERROR_FIRST && status <= CLIENT_ERROR_LAST)
20
+ return status;
21
+ if (status >= SERVER_ERROR_FIRST && status <= SERVER_ERROR_LAST
22
+ && declaration?.allowServerErrorStatus === true)
23
+ return status;
24
+ return null;
25
+ };
26
+ /**
27
+ * The registered type names an error carries: its own `type`, and the static `typeName` of every
28
+ * class on its constructor chain.
29
+ *
30
+ * This is how a class is recognised when `instanceof` cannot see it. A process that loads a package
31
+ * twice — `bun --preserve-symlinks` over a linked workspace resolves one package through several
32
+ * `node_modules` paths — holds several copies of every error class, and an error built by one copy
33
+ * is not an instance of another. The registered names are the same in every copy. A subclass's
34
+ * `typeName` does not reliably embed its parent's (`EntitlementRefusal` extends `AuthForbidden`), so
35
+ * a family is recognised by walking the chain and matching names exactly, never by a substring.
36
+ */
37
+ const typeNamesOf = (error) => {
38
+ const names = new Set();
39
+ const type = error.type;
40
+ if (typeof type === 'string') {
41
+ names.add(type);
42
+ }
43
+ let ctor = error.constructor;
44
+ while (typeof ctor === 'function') {
45
+ const name = ctor.typeName;
46
+ if (typeof name === 'string') {
47
+ names.add(name);
48
+ }
49
+ ctor = Object.getPrototypeOf(ctor);
50
+ }
51
+ return names;
52
+ };
53
+ const isOfFamily = (names, family) => family.some(typeName => names.has(typeName));
54
+ const errorStatus = (error) => {
55
+ if (error == null || typeof error !== 'object') {
56
+ return SERVER_ERROR;
57
+ }
58
+ const names = typeNamesOf(error);
59
+ if (error instanceof AuthForbidden || error instanceof AccessError
60
+ || isOfFamily(names, [AuthForbidden.typeName, AccessError.typeName])) {
61
+ // An established identity that lacks the permission. Re-authenticating changes nothing.
62
+ return FORBIDDEN_ERROR;
63
+ }
64
+ if (error instanceof AuthorizationError || error instanceof AuthFailedError
65
+ || isOfFamily(names, [AuthorizationError.typeName, AuthFailedError.typeName])) {
66
+ // No usable credential — absent, expired, revoked, or naming a session this server no longer
67
+ // holds. The caller is told to sign in, not that signing in would not help.
68
+ return UNAUTHORIZED_ERROR;
69
+ }
70
+ return declaredErrorStatus(error) ?? SERVER_ERROR;
71
+ };
72
+ const errorExposure = (config) => config?.http?.errors?.exposure === 'development' ? 'development' : 'production';
73
+ /**
74
+ * Preserve a typed/marshalled error when possible without asking `ensure` to convert a plain
75
+ * error. In particular, `ensure` deliberately rethrows SyntaxError; an HTTP boundary still has
76
+ * to serialize it safely and let the incident log retain the original object.
77
+ */
78
+ const normalizeForWire = (error) => {
79
+ if (error instanceof SyntaxError || isResilientError(error)) {
80
+ return error;
81
+ }
82
+ if (error.message.includes(SEPARATOR)) {
83
+ const ensured = ResilientError.ensure(error);
84
+ // The catch-all converter's legacy constructor shape shifts an ordinary error's fields.
85
+ // Only keep `ensure`'s result when a registered wire prefix actually rebuilt something.
86
+ if (ensured.type !== error.message || ensured.message !== error.stack) {
87
+ return ensured;
88
+ }
89
+ }
90
+ return error;
91
+ };
92
+ const serializeError = (error, exposure = 'production') => {
93
+ const normalized = normalizeForWire(error);
94
+ const thrown = errorStatus(error);
95
+ const status = thrown !== SERVER_ERROR ? thrown : errorStatus(normalized);
96
+ const incidentId = randomUUID();
97
+ // The server log owns every diagnostic. Production callers receive only its correlation key;
98
+ // development keeps the typed wire form so local clients can retain their usual error details.
99
+ Object.assign(error, { incidentId });
100
+ if (isResilientError(normalized))
101
+ normalized.incidentId = incidentId;
102
+ const body = exposure === 'production'
103
+ ? incidentId
104
+ : ResilientError.marshal(normalized, { includeStack: true, incidentId }).message;
105
+ return { status, body, incidentId };
106
+ };
107
+ const applyErrorHeaders = (error, reply) => {
108
+ if (error == null || typeof error !== 'object') {
109
+ return;
110
+ }
111
+ const retryAfter = error.retryAfter;
112
+ if (typeof retryAfter === 'number' && Number.isFinite(retryAfter) && retryAfter > 0) {
113
+ reply.header('Retry-After', String(Math.ceil(retryAfter)));
114
+ }
115
+ // Match exact registered types. EntitlementRefusal also extends AuthForbidden but is a plan
116
+ // decision, not a person's IAM permission denial.
117
+ const type = error.type;
118
+ if (type === AuthForbidden.typeName || type === AccessError.typeName) {
119
+ reply.header(DENIAL_KIND_HEADER, ACCESS_DENIED_KIND);
120
+ }
121
+ };
122
+ const handleError = (error, reply, exposure = 'production') => {
123
+ if (!reply.sent) {
124
+ const serialized = serializeError(error, exposure);
125
+ applyErrorHeaders(error, reply);
126
+ // The one record of a failed request: method, path and status — never what the caller sent.
127
+ fastifyLogUtils.failure(error, reply.request, serialized.status, serialized.incidentId);
128
+ reply.header(INCIDENT_ID_HEADER, serialized.incidentId)
129
+ .code(serialized.status)
130
+ .send(serialized.body);
131
+ }
132
+ };
133
+ return { declaredErrorStatus, errorStatus, errorExposure, serializeError, applyErrorHeaders, handleError };
32
134
  };
135
+ export const httpErrorHelper = createHttpErrorHelper();
136
+ /** @deprecated compat:factory-refactor — use `httpErrorHelper.errorStatus(…)` */
137
+ export const errorStatus = (error) => httpErrorHelper.errorStatus(error);
33
138
  //# sourceMappingURL=error.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"error.js","sourceRoot":"","sources":["../../src/utils/error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AAEjF,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAC3D,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AAClE,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAEhD;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAY,EAAE,KAAmB,EAAE,EAAE;IAC/D,IAAI,IAAI,GAAW,YAAY,CAAA;IAC/B,IAAI,KAAK,YAAY,aAAa,IAAI,KAAK,YAAY,WAAW,EAAE,CAAC;QACnE,wFAAwF;QACxF,IAAI,GAAG,eAAe,CAAA;IACxB,CAAC;SAAM,IAAI,KAAK,YAAY,kBAAkB,IAAI,KAAK,YAAY,eAAe,EAAE,CAAC;QACnF,6FAA6F;QAC7F,4EAA4E;QAC5E,I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c,EAAU,EAAE,CAAC,eAAe,CAAC,WAAW,CAAC,KAAK,CAAC,CAAA"}
@@ -1,9 +1,5 @@
1
1
  import type { FastifyReply, FastifyRequest } from 'fastify';
2
2
  import type { ServerEntrypoint } from '@owlmeans/server-entrypoint';
3
- import type { ServerConfig, ServerContext } from '@owlmeans/server-context';
4
- type Config = ServerConfig;
5
- interface Context<C extends Config = Config> extends ServerContext<C> {
6
- }
3
+ import type { Config, Context } from './types.local.js';
7
4
  export declare const authorize: <C extends Config, T extends Context<C>>(context: T, module: ServerEntrypoint<FastifyRequest>, req: FastifyRequest, reply: FastifyReply) => Promise<[T, ServerEntrypoint<FastifyRequest>]>;
8
- export {};
9
5
  //# sourceMappingURL=guards.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"guards.d.ts","sourceRoot":"","sources":["../../src/utils/guards.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAC3D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAA;AAGnE,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AAK3E,KAAK,MAAM,GAAG,YAAY,CAAA;AAC1B,UAAU,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,aAAa,CAAC,CAAC,CAAC;CAAI;AAEzE,eAAO,MAAM,SAAS,GAAU,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,OAAO,CAAC,CAAC,CAAC,EACpE,SAAS,CAAC,EAAE,QAAQ,gBAAgB,CAAC,cAAc,CAAC,EACpD,KAAK,cAAc,EAAE,OAAO,YAAY,KACvC,OAAO,CAAC,CAAC,CAAC,EAAE,gBAAgB,CAAC,cAAc,CAAC,CAAC,CA0D/C,CAAA"}
1
+ {"version":3,"file":"guards.d.ts","sourceRoot":"","sources":["../../src/utils/guards.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAC3D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAA;AAKnE,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAIvD,eAAO,MAAM,SAAS,GAAU,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,OAAO,CAAC,CAAC,CAAC,WAC3D,CAAC,UAAU,gBAAgB,CAAC,cAAc,CAAC,OAC/C,cAAc,SAAS,YAAY,KACvC,OAAO,CAAC,CAAC,CAAC,EAAE,gBAAgB,CAAC,cAAc,CAAC,CAAC,CAmD/C,CAAA"}
@@ -1,19 +1,19 @@
1
- import { isContextWithoutIds, Layer } from '@owlmeans/context';
2
1
  import { provideResponse } from '@owlmeans/entrypoint';
3
- import { executeResponse, provideRequest } from './payload.js';
2
+ import { payloadHelper } from './payload.js';
4
3
  import { AuthFailedError } from '../errors.js';
4
+ import { makeEntityScope } from '@owlmeans/auth-common';
5
5
  export const authorize = async (context, module, req, reply) => {
6
6
  const guards = module.getGuards();
7
7
  if (guards.length > 0) {
8
8
  const response = provideResponse(reply);
9
- const request = provideRequest(module.alias, req);
9
+ const request = payloadHelper.provideRequest(module.alias, req);
10
10
  let guard = undefined;
11
11
  for (const alias of guards) {
12
12
  const _guard = context.service(alias);
13
13
  if (await _guard.match(request, response)) {
14
14
  guard = _guard;
15
15
  }
16
- executeResponse(response, reply, true);
16
+ payloadHelper.executeResponse(response, reply, true);
17
17
  if (guard != null) {
18
18
  break;
19
19
  }
@@ -25,7 +25,7 @@ export const authorize = async (context, module, req, reply) => {
25
25
  if (!await guard.handle(request, authResponse)) {
26
26
  throw new AuthFailedError(guard.alias);
27
27
  }
28
- executeResponse(authResponse, reply, true);
28
+ payloadHelper.executeResponse(authResponse, reply, true);
29
29
  // Guard that returns true and does not provide an error is an optional guard
30
30
  // if (authResponse.value == null) {
31
31
  // throw SyntaxError(`Guard that returns true and does not provide an error, should provide authorization`)
@@ -34,20 +34,16 @@ export const authorize = async (context, module, req, reply) => {
34
34
  if (request.auth != null) {
35
35
  req._auth = request.auth;
36
36
  }
37
- if (request.auth?.entityId != null) {
38
- // @TODO Probably we need to downgrade context in this case
39
- if (!isContextWithoutIds(context)) {
40
- throw SyntaxError(`Context should be without ids during authorization ${context.cfg.layer}:${context.cfg.layerId}`);
41
- }
42
- if (isContextWithoutIds(context) && context.cfg.layer !== Layer.Service) {
43
- context = await context.updateContext(undefined, Layer.Service);
44
- await context.waitForInitialized();
45
- }
46
- context = await context.updateContext(request.auth.entityId, Layer.Entity);
47
- await context.waitForInitialized();
48
- // We elevate module to the context level if it was changed
49
- module = context.entrypoint(module.alias);
50
- await module.resolve();
37
+ // The token names the organization by slug; everything downstream — grants, records, minted
38
+ // infrastructure names — keys on the entity's stable id. Resolving here, once, is what keeps a
39
+ // rename from being a sweep of every handler: the id arrives on the request, and a slug that
40
+ // has since been retired still resolves to the entity that retired it.
41
+ const entity = await makeEntityScope(request).attachEntity(context);
42
+ if (entity != null) {
43
+ // Carried on the raw request the same way `_auth` is, so the request the handler is given
44
+ // later — a different object, built after this runs — sees the same resolution.
45
+ ;
46
+ req._entity = entity;
51
47
  }
52
48
  }
53
49
  // Update context in request object
@@ -1 +1 @@
1
- {"version":3,"file":"guards.js","sourceRoot":"","sources":["../../src/utils/guards.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAA;AAI9D,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAEtD,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC9D,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAM9C,MAAM,CAAC,MAAM,SAAS,GAAG,KAAK,EAC5B,OAAU,EAAE,MAAwC,EACpD,GAAmB,EAAE,KAAmB,EACQ,EAAE;IAClD,MAAM,MAAM,GAAG,M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CAAC,IAAI,GAAG,YAAY,CAAC,KAAK,CAAC;QAClC,IAAI,OAAO,CAAC,IAAI,IAAI,IAAI,EAAE,CAAC;YACxB,GAAW,CAAC,KAAK,GAAG,OAAO,CAAC,IAAI,CAAA;QACnC,CAAC;QAED,4FAA4F;QAC5F,+FAA+F;QAC/F,6FAA6F;QAC7F,uEAAuE;QACvE,MAAM,MAAM,GAAG,MAAM,eAAe,CAAC,OAAO,CAAC,CAAC,YAAY,CAAC,OAAO,CAAC,CAAA;QACnE,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;YACnB,0FAA0F;YAC1F,gFAAgF;YAChF,CAAC;YAAC,GAAW,CAAC,OAAO,GAAG,MAAM,CAAA;QAChC,CAAC;IACH,CAAC;IACD,mCAAmC;IAClC,GAAW,CAAC,IAAI,GAAG,OAAO,CAAA;IAE3B,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;AAC1B,CAAC,CAAA"}
@@ -1,7 +1,12 @@
1
1
  export * from './error.js';
2
+ export * from './consts.js';
3
+ export type * from './types.js';
4
+ export type * from './error/types.js';
5
+ export type * from './payload/types.js';
6
+ export type * from './context/types.js';
2
7
  export * from './server.js';
3
8
  export * from './payload.js';
4
- export * from './server.js';
9
+ export * from './schema.js';
5
10
  export * from './guards.js';
6
11
  export * from './context.js';
7
12
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AACA,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AACA,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,mBAAmB,YAAY,CAAA;AAC/B,mBAAmB,kBAAkB,CAAA;AACrC,mBAAmB,oBAAoB,CAAA;AACvC,mBAAmB,oBAAoB,CAAA;AACvC,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA"}
@@ -1,7 +1,8 @@
1
1
  export * from './error.js';
2
+ export * from './consts.js';
2
3
  export * from './server.js';
3
4
  export * from './payload.js';
4
- export * from './server.js';
5
+ export * from './schema.js';
5
6
  export * from './guards.js';
6
7
  export * from './context.js';
7
8
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AACA,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AACA,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAK3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Keys of a Fastify log record that carry what a caller sent. They are dropped at every depth: a
3
+ * request body and its raw form (`rawBody`), multipart fields and file buffers, headers (with the
4
+ * authorization and cookie values), cookies and the parsed query string.
5
+ */
6
+ export declare const UNSAFE_LOG_KEYS: ReadonlySet<string>;
7
+ /** Deepest nesting `safeData` walks; anything below is one marker. */
8
+ export declare const SAFE_DATA_DEPTH = 4;
9
+ /**
10
+ * The query string of a path or URL inside free text (a Fastify warning names `"/x?token=…"`): the
11
+ * path is kept (`$1`), the query is cut.
12
+ */
13
+ export declare const QUERY_IN_TEXT: RegExp;
14
+ //# sourceMappingURL=consts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../../../src/utils/log/consts.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,eAAO,MAAM,eAAe,EAAE,WAAW,CAAC,MAAM,CAG9C,CAAA;AAEF,sEAAsE;AACtE,eAAO,MAAM,eAAe,IAAI,CAAA;AAEhC;;;GAGG;AACH,eAAO,MAAM,aAAa,QAAgC,CAAA"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Keys of a Fastify log record that carry what a caller sent. They are dropped at every depth: a
3
+ * request body and its raw form (`rawBody`), multipart fields and file buffers, headers (with the
4
+ * authorization and cookie values), cookies and the parsed query string.
5
+ */
6
+ export const UNSAFE_LOG_KEYS = new Set([
7
+ 'body', 'rawBody', 'payload', 'files', 'file', 'headers', 'rawHeaders', 'trailers', 'rawTrailers',
8
+ 'query', 'querystring', 'cookies', 'cookie', 'authorization',
9
+ ]);
10
+ /** Deepest nesting `safeData` walks; anything below is one marker. */
11
+ export const SAFE_DATA_DEPTH = 4;
12
+ /**
13
+ * The query string of a path or URL inside free text (a Fastify warning names `"/x?token=…"`): the
14
+ * path is kept (`$1`), the query is cut.
15
+ */
16
+ export const QUERY_IN_TEXT = /(\/[^\s?"'`]*)\?[^\s"'`)]+/g;
17
+ //# sourceMappingURL=consts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consts.js","sourceRoot":"","sources":["../../../src/utils/log/consts.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAwB,IAAI,GAAG,CAAC;IAC1D,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa;IACjG,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,QAAQ,EAAE,eAAe;CAC7D,CAAC,CAAA;AAEF,sEAAsE;AACtE,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,CAAA;AAEhC;;;GAGG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,6BAA6B,CAAA"}
@@ -0,0 +1,42 @@
1
+ import type { FastifyBaseLogger, LogController } from 'fastify';
2
+ import type { Logger } from '@owlmeans/log';
3
+ /** All a log record may say about a request: its method and its path — never the query string. */
4
+ export interface RequestSummary {
5
+ method?: string;
6
+ path?: string;
7
+ }
8
+ /**
9
+ * How the API server's Fastify instance logs: through `@owlmeans/log`, and never with what a caller
10
+ * sent — a body, a raw body, multipart bytes, headers, cookies or a query string.
11
+ */
12
+ export interface FastifyLogUtils {
13
+ /**
14
+ * A pino-shaped logger over `log` (scope `http` by default), handed to Fastify as its
15
+ * `loggerInstance`. Every record's data — Fastify's own and any plugin's — passes `safeData`.
16
+ */
17
+ logger: (log?: Logger, bindings?: Record<string, unknown>) => FastifyBaseLogger;
18
+ /**
19
+ * The `logController` Fastify writes its own request lines through: no per-request pair (the
20
+ * server writes one debug record from `onResponse`), a request the default error handler answers
21
+ * is `failure`, a missing route is one debug line without its query.
22
+ */
23
+ controller: () => LogController;
24
+ /**
25
+ * A copy of a record's data that is safe to write: a request becomes its `RequestSummary`, a reply
26
+ * its status, bytes their size, any other class instance its name; the keys that carry what a
27
+ * caller sent (`body`, `rawBody`, `headers`, `query`, `cookies`, files…) are dropped at every depth
28
+ * (a number or boolean under them stays: a count, a flag), and any string loses the query of a URL
29
+ * it names. An `Error` passes as it is (a copy, when its message names a URL with a query) — the
30
+ * log writes only its name, message, code and stack.
31
+ */
32
+ safeData: (data: unknown) => unknown;
33
+ /** The method and the path, without the query string, of a Fastify or Node request. */
34
+ requestOf: (request: unknown) => RequestSummary;
35
+ /**
36
+ * The one record of a failed request, by what its status means: 5xx `error` with the error,
37
+ * 403 `warn` `access.forbidden`, 401 `debug` `auth.refused`, any other 4xx `debug` with the error's
38
+ * code and message. Method, path and status only — never the request's content.
39
+ */
40
+ failure: (error: Error, request: unknown, status: number, incidentId?: string) => void;
41
+ }
42
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/utils/log/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAC/D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAA;AAE3C,kGAAkG;AAClG,MAAM,WAAW,cAAc;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,MAAM,EAAE,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,iBAAiB,CAAA;IAC/E;;;;OAIG;IACH,UAAU,EAAE,MAAM,aAAa,CAAA;IAC/B;;;;;;;OAOG;IACH,QAAQ,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,OAAO,CAAA;IACpC,uFAAuF;IACvF,SAAS,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,cAAc,CAAA;IAC/C;;;;OAIG;IACH,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,KAAK,IAAI,CAAA;CACvF"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/utils/log/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ import type { FastifyLogUtils } from './log/types.js';
2
+ export declare const createFastifyLogUtils: () => FastifyLogUtils;
3
+ export declare const fastifyLogUtils: FastifyLogUtils;
4
+ //# sourceMappingURL=log.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"log.d.ts","sourceRoot":"","sources":["../../src/utils/log.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,eAAe,EAAkB,MAAM,gBAAgB,CAAA;AA4BrE,eAAO,MAAM,qBAAqB,QAAO,eA4JxC,CAAA;AAED,eAAO,MAAM,eAAe,iBAA0B,CAAA"}