@bymax-one/nest-logger 1.4.13 → 1.4.15

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.
@@ -1,8 +1,104 @@
1
1
  import * as _nestjs_common from '@nestjs/common';
2
- import { ModuleMetadata, Type, DynamicModule, INestApplication, LoggerService, OnApplicationShutdown, ExceptionFilter, ArgumentsHost, NestInterceptor, ExecutionContext, CallHandler, MiddlewareConsumer, NestMiddleware, CustomDecorator } from '@nestjs/common';
2
+ import { ModuleMetadata, Type, NestModule, MiddlewareConsumer, DynamicModule, INestApplication, LoggerService, OnApplicationShutdown, ExceptionFilter, ArgumentsHost, NestInterceptor, ExecutionContext, CallHandler, NestMiddleware, CustomDecorator } from '@nestjs/common';
3
+ import { HttpAdapterHost } from '@nestjs/core';
3
4
  import { Logger } from 'pino';
4
5
  import { Observable } from 'rxjs';
5
6
 
7
+ /**
8
+ * Minimal HTTP request/response contracts the logging layer reads.
9
+ *
10
+ * Layer: server/interfaces — exported structural contracts shared by the HTTP
11
+ * logging interceptor, the exception filter and the request-id middleware. They
12
+ * are part of the public API: they appear in those components' published
13
+ * signatures, so a consumer calling `RequestIdMiddleware.use()` directly needs
14
+ * to be able to name them. Declared structurally, listing only the members
15
+ * those three actually touch, so the published declarations depend on no HTTP
16
+ * framework's types. Naming Express's
17
+ * `Request`/`Response` here would make the emitted `.d.ts` import from
18
+ * `express`, forcing every consumer that compiles with `skipLibCheck: false` to
19
+ * install `@types/express` whether or not they use those signatures.
20
+ *
21
+ * The shapes mirror what the Express adapter provides — `status().json()` and
22
+ * `setHeader()` are Express/Node response methods — so this is not an
23
+ * adapter-agnostic abstraction; a Fastify adapter remains unsupported. An
24
+ * Express request and response satisfy these contracts structurally, with no
25
+ * cast at the call site.
26
+ */
27
+ /** Raw inbound headers, as the HTTP layer delivers them. */
28
+ type IncomingHeaders = Record<string, string | string[] | undefined>;
29
+ /**
30
+ * The inbound request members the logging layer reads.
31
+ *
32
+ * `user` is populated by upstream auth in the consumer app (a guard or
33
+ * middleware). The acting-user id is read as `sub` first, then `id`: a JWT principal
34
+ * (every `@bymax-one/nest-auth` token) names it `sub`, while an ORM-style principal
35
+ * names it `id`. The JWT subject wins when both are present, since it is the
36
+ * authenticated identity. Reading only `id` — as this once did — silently dropped
37
+ * the user field for every JWT-authenticated request.
38
+ */
39
+ interface LoggableRequest {
40
+ /** Inbound headers — read for correlation ids and the user agent. */
41
+ readonly headers: IncomingHeaders;
42
+ /** HTTP verb, logged as-is. */
43
+ readonly method: string;
44
+ /** Request target, normalized and query-stripped before it reaches a log. */
45
+ readonly url: string;
46
+ /**
47
+ * The request target as it arrived, before any mount point was stripped.
48
+ *
49
+ * Middleware mounted at a path sees `url` RELATIVE to that mount: measured
50
+ * under `setGlobalPrefix('api')`, a request for `/api/users/7` reaches a
51
+ * root-mounted middleware as `url = '/users/7'` and
52
+ * `originalUrl = '/api/users/7'`, and `/api` itself arrives as `url = '/'`.
53
+ * Logging `url` there would drop the prefix from every entry and make an
54
+ * `excludePaths` pattern written against the real path stop matching. Absent
55
+ * on adapters that do not provide it, in which case `url` is already whole.
56
+ */
57
+ readonly originalUrl?: string | undefined;
58
+ /** Client address when the adapter resolves one. */
59
+ readonly ip?: string | undefined;
60
+ /**
61
+ * Authenticated principal, when upstream auth attached one. `id` is the
62
+ * ORM-style identifier; `sub` is the JWT subject claim. The logging layer reads
63
+ * whichever is present.
64
+ */
65
+ readonly user?: {
66
+ readonly id?: string | undefined;
67
+ readonly sub?: string | undefined;
68
+ } | undefined;
69
+ }
70
+ /**
71
+ * The outbound response members the logging layer writes or reads: the status
72
+ * for the terminal log entry, `setHeader` to echo the correlation id, and the
73
+ * chainable `status().json()` used by the exception filter to emit a body.
74
+ */
75
+ interface LoggableResponse {
76
+ /** Status code, read when logging the completed request. */
77
+ readonly statusCode: number;
78
+ /**
79
+ * `true` once every byte has been flushed to the socket. Read in the `'close'`
80
+ * handler to tell a delivered response from one the client never received:
81
+ * `'close'` fires for both, and `'finish'` never fires for an aborted request
82
+ * at all. It is what keeps an aborted request from being logged as the success
83
+ * the handler intended.
84
+ */
85
+ readonly writableFinished: boolean;
86
+ /** Sets a response header (used for the correlation id). */
87
+ setHeader(name: string, value: string): void;
88
+ /** Selects the status code and returns the chainable body writer. */
89
+ status(code: number): {
90
+ json(body: unknown): unknown;
91
+ };
92
+ /**
93
+ * Registers a lifecycle listener. Only `'close'` is used: it fires exactly
94
+ * once whether the response completed or the connection died early, where
95
+ * `'finish'` is blind to the aborted case.
96
+ */
97
+ on(event: 'close', listener: () => void): unknown;
98
+ }
99
+ /** Continuation passed to a middleware; invoked with no arguments here. */
100
+ type NextHandler = () => void;
101
+
6
102
  /**
7
103
  * Pino log levels (mirrored to NestJS-compatible names).
8
104
  *
@@ -295,9 +391,34 @@ interface HttpOptions {
295
391
  * crafted URL into a ReDoS vector. The defaults are anchored and safe.
296
392
  */
297
393
  excludePaths?: readonly RegExp[];
298
- /** Header name carrying the tenant identifier. Default: `x-tenant-id`. */
299
- tenantIdHeader?: string;
394
+ /**
395
+ * Header name carrying the tenant identifier, or `false` to read no header. Default:
396
+ * `x-tenant-id`.
397
+ *
398
+ * SECURITY: a `tenantId` taken from a header is what the CLIENT asserted. Any client can send any
399
+ * tenant, and every entry of its requests will carry it, so do not read it as who the request
400
+ * belongs to. A host that authenticates the tenant sets `false` or supplies
401
+ * {@link HttpOptions.tenantIdResolver}.
402
+ */
403
+ tenantIdHeader?: string | false;
404
+ /**
405
+ * Resolves the tenant id of a request on the server side, from what the host trusts (a session,
406
+ * a verified token an earlier middleware put on the request). When given it is the only source:
407
+ * the header is not read, whether the resolver answers or not. The middleware runs before guards,
408
+ * so the resolver sees only what an earlier middleware set; a tenant known later is set with
409
+ * `LogContextService.set('tenantId', ...)`. An answer that is not a string of at most 256
410
+ * characters drawn from `A-Z a-z 0-9 . _ : / + = -`, and a resolver that throws, give no tenant id.
411
+ * The resolver is synchronous: a promise it answers is no tenant id, and its rejection is absorbed.
412
+ */
413
+ tenantIdResolver?: (request: LoggableRequest) => string | undefined;
300
414
  }
415
+ /**
416
+ * The `http` options after defaults: every field is filled but the resolver, whose absence means the
417
+ * header is read.
418
+ */
419
+ type ResolvedHttpOptions = Required<Omit<HttpOptions, 'tenantIdResolver'>> & {
420
+ tenantIdResolver: HttpOptions['tenantIdResolver'];
421
+ };
301
422
  /**
302
423
  * OpenTelemetry integration tuning — applied by `TraceContextMixin` when the
303
424
  * `@opentelemetry/api` peer dependency is installed.
@@ -463,7 +584,7 @@ interface BymaxLoggerModuleOptions {
463
584
  * `BymaxLoggerModuleOptions` the consumer supplies.
464
585
  */
465
586
  type ResolvedBymaxLoggerModuleOptions = Readonly<Required<Omit<BymaxLoggerModuleOptions, 'http' | 'otel'>> & {
466
- http: Required<HttpOptions>;
587
+ http: ResolvedHttpOptions;
467
588
  otel: Required<OtelOptions>;
468
589
  }>;
469
590
  /**
@@ -525,7 +646,20 @@ declare const BymaxLoggerModuleBase: _nestjs_common.ConfigurableModuleCls<BymaxL
525
646
  declare const OPTIONS_TYPE: BymaxLoggerModuleOptions & Partial<BymaxLoggerModuleExtras>;
526
647
  declare const ASYNC_OPTIONS_TYPE: _nestjs_common.ConfigurableModuleAsyncOptions<BymaxLoggerModuleOptions, "createLoggerOptions"> & Partial<BymaxLoggerModuleExtras>;
527
648
 
528
- declare class BymaxLoggerModule extends BymaxLoggerModuleBase {
649
+ declare class BymaxLoggerModule extends BymaxLoggerModuleBase implements NestModule {
650
+ private readonly httpAdapterHost;
651
+ /**
652
+ * @param httpAdapterHost - The HTTP adapter, absent in an application with no HTTP server.
653
+ */
654
+ constructor(httpAdapterHost: HttpAdapterHost);
655
+ /**
656
+ * Mount the error layer that keeps the type of a body-parser error for the exception filter.
657
+ * It runs after the body parser, which Nest registers first, and before Nest's exception
658
+ * handler, and it passes every error on. Only the Express adapter has such a layer.
659
+ *
660
+ * @param _consumer - The middleware consumer, unused: an error layer is not consumer middleware.
661
+ */
662
+ configure(_consumer: MiddlewareConsumer): void;
529
663
  /**
530
664
  * Synchronous registration.
531
665
  *
@@ -1263,6 +1397,7 @@ declare class HttpExceptionFilter implements ExceptionFilter {
1263
1397
  * @param clientError - The status and message the error exposes.
1264
1398
  * @param res - The response to write.
1265
1399
  * @param request - The request facts every entry carries.
1400
+ * @param rawUrls - The request's own URLs, whose query string a logged message must not carry.
1266
1401
  */
1267
1402
  private answerClientError;
1268
1403
  /**
@@ -1342,6 +1477,7 @@ declare class HttpLoggingInterceptor implements NestInterceptor {
1342
1477
  * @param url - The normalized request URL.
1343
1478
  * @param userId - The acting user id, when known.
1344
1479
  * @param duration - The elapsed time in milliseconds.
1480
+ * @param rawUrls - The request's own URLs, whose query string a logged message must not carry.
1345
1481
  */
1346
1482
  private logError;
1347
1483
  /**
@@ -1352,6 +1488,7 @@ declare class HttpLoggingInterceptor implements NestInterceptor {
1352
1488
  * @param url - The normalized request URL.
1353
1489
  * @param userId - The acting user id, when known.
1354
1490
  * @param duration - The elapsed time in milliseconds.
1491
+ * @param rawUrls - The request's own URLs, whose query string a logged message must not carry.
1355
1492
  */
1356
1493
  private logClientError;
1357
1494
  }
@@ -1456,101 +1593,6 @@ declare function applyAccessLog(app: INestApplication): void;
1456
1593
  */
1457
1594
  declare function applyRequestIdMiddleware(consumer: MiddlewareConsumer, routes?: string): void;
1458
1595
 
1459
- /**
1460
- * Minimal HTTP request/response contracts the logging layer reads.
1461
- *
1462
- * Layer: server/interfaces — exported structural contracts shared by the HTTP
1463
- * logging interceptor, the exception filter and the request-id middleware. They
1464
- * are part of the public API: they appear in those components' published
1465
- * signatures, so a consumer calling `RequestIdMiddleware.use()` directly needs
1466
- * to be able to name them. Declared structurally, listing only the members
1467
- * those three actually touch, so the published declarations depend on no HTTP
1468
- * framework's types. Naming Express's
1469
- * `Request`/`Response` here would make the emitted `.d.ts` import from
1470
- * `express`, forcing every consumer that compiles with `skipLibCheck: false` to
1471
- * install `@types/express` whether or not they use those signatures.
1472
- *
1473
- * The shapes mirror what the Express adapter provides — `status().json()` and
1474
- * `setHeader()` are Express/Node response methods — so this is not an
1475
- * adapter-agnostic abstraction; a Fastify adapter remains unsupported. An
1476
- * Express request and response satisfy these contracts structurally, with no
1477
- * cast at the call site.
1478
- */
1479
- /** Raw inbound headers, as the HTTP layer delivers them. */
1480
- type IncomingHeaders = Record<string, string | string[] | undefined>;
1481
- /**
1482
- * The inbound request members the logging layer reads.
1483
- *
1484
- * `user` is populated by upstream auth in the consumer app (a guard or
1485
- * middleware). The acting-user id is read as `sub` first, then `id`: a JWT principal
1486
- * (every `@bymax-one/nest-auth` token) names it `sub`, while an ORM-style principal
1487
- * names it `id`. The JWT subject wins when both are present, since it is the
1488
- * authenticated identity. Reading only `id` — as this once did — silently dropped
1489
- * the user field for every JWT-authenticated request.
1490
- */
1491
- interface LoggableRequest {
1492
- /** Inbound headers — read for correlation ids and the user agent. */
1493
- readonly headers: IncomingHeaders;
1494
- /** HTTP verb, logged as-is. */
1495
- readonly method: string;
1496
- /** Request target, normalized and query-stripped before it reaches a log. */
1497
- readonly url: string;
1498
- /**
1499
- * The request target as it arrived, before any mount point was stripped.
1500
- *
1501
- * Middleware mounted at a path sees `url` RELATIVE to that mount: measured
1502
- * under `setGlobalPrefix('api')`, a request for `/api/users/7` reaches a
1503
- * root-mounted middleware as `url = '/users/7'` and
1504
- * `originalUrl = '/api/users/7'`, and `/api` itself arrives as `url = '/'`.
1505
- * Logging `url` there would drop the prefix from every entry and make an
1506
- * `excludePaths` pattern written against the real path stop matching. Absent
1507
- * on adapters that do not provide it, in which case `url` is already whole.
1508
- */
1509
- readonly originalUrl?: string | undefined;
1510
- /** Client address when the adapter resolves one. */
1511
- readonly ip?: string | undefined;
1512
- /**
1513
- * Authenticated principal, when upstream auth attached one. `id` is the
1514
- * ORM-style identifier; `sub` is the JWT subject claim. The logging layer reads
1515
- * whichever is present.
1516
- */
1517
- readonly user?: {
1518
- readonly id?: string | undefined;
1519
- readonly sub?: string | undefined;
1520
- } | undefined;
1521
- }
1522
- /**
1523
- * The outbound response members the logging layer writes or reads: the status
1524
- * for the terminal log entry, `setHeader` to echo the correlation id, and the
1525
- * chainable `status().json()` used by the exception filter to emit a body.
1526
- */
1527
- interface LoggableResponse {
1528
- /** Status code, read when logging the completed request. */
1529
- readonly statusCode: number;
1530
- /**
1531
- * `true` once every byte has been flushed to the socket. Read in the `'close'`
1532
- * handler to tell a delivered response from one the client never received:
1533
- * `'close'` fires for both, and `'finish'` never fires for an aborted request
1534
- * at all. It is what keeps an aborted request from being logged as the success
1535
- * the handler intended.
1536
- */
1537
- readonly writableFinished: boolean;
1538
- /** Sets a response header (used for the correlation id). */
1539
- setHeader(name: string, value: string): void;
1540
- /** Selects the status code and returns the chainable body writer. */
1541
- status(code: number): {
1542
- json(body: unknown): unknown;
1543
- };
1544
- /**
1545
- * Registers a lifecycle listener. Only `'close'` is used: it fires exactly
1546
- * once whether the response completed or the connection died early, where
1547
- * `'finish'` is blind to the aborted case.
1548
- */
1549
- on(event: 'close', listener: () => void): unknown;
1550
- }
1551
- /** Continuation passed to a middleware; invoked with no arguments here. */
1552
- type NextHandler = () => void;
1553
-
1554
1596
  /**
1555
1597
  * Records the HTTP access log for every request, including the ones no
1556
1598
  * interceptor can observe.
@@ -1606,8 +1648,10 @@ declare class HttpAccessLogMiddleware implements NestMiddleware {
1606
1648
  */
1607
1649
  declare class RequestIdMiddleware implements NestMiddleware {
1608
1650
  private readonly logContext;
1609
- /** Header name carrying the tenant id, resolved from module options. */
1651
+ /** Header name carrying the tenant id, or `false` when no header is read. */
1610
1652
  private readonly tenantIdHeader;
1653
+ /** Server-side resolver of the tenant id; when set, the header is not read. */
1654
+ private readonly tenantIdResolver;
1611
1655
  /** Whether to mint a `requestId` when the inbound header is absent. */
1612
1656
  private readonly shouldGenerateRequestId;
1613
1657
  /**
@@ -1683,15 +1727,21 @@ declare class RequestIdMiddleware implements NestMiddleware {
1683
1727
  */
1684
1728
  private resolveRequestId;
1685
1729
  /**
1686
- * The tenant id carried by the configured header, when it is acceptable.
1730
+ * The tenant id of the request, when it is acceptable.
1687
1731
  *
1688
- * Never minted: a tenant is an assertion about who the request belongs to, and
1689
- * inventing one would be a fabricated claim rather than a correlation aid.
1732
+ * From the host's resolver when it gave one, else from the configured header, else none. Never
1733
+ * minted: a tenant is an assertion about who the request belongs to, and inventing one would be a
1734
+ * fabricated claim rather than a correlation aid.
1690
1735
  *
1691
1736
  * @param req - The incoming request.
1692
1737
  * @returns The tenant id, or `undefined`.
1693
1738
  */
1694
1739
  private resolveTenantId;
1740
+ /**
1741
+ * The answer of the host's resolver, when it is acceptable. A resolver that throws gives none: a
1742
+ * log context never costs the request.
1743
+ */
1744
+ private resolvedTenantId;
1695
1745
  }
1696
1746
 
1697
1747
  /**
@@ -1,8 +1,104 @@
1
1
  import * as _nestjs_common from '@nestjs/common';
2
- import { ModuleMetadata, Type, DynamicModule, INestApplication, LoggerService, OnApplicationShutdown, ExceptionFilter, ArgumentsHost, NestInterceptor, ExecutionContext, CallHandler, MiddlewareConsumer, NestMiddleware, CustomDecorator } from '@nestjs/common';
2
+ import { ModuleMetadata, Type, NestModule, MiddlewareConsumer, DynamicModule, INestApplication, LoggerService, OnApplicationShutdown, ExceptionFilter, ArgumentsHost, NestInterceptor, ExecutionContext, CallHandler, NestMiddleware, CustomDecorator } from '@nestjs/common';
3
+ import { HttpAdapterHost } from '@nestjs/core';
3
4
  import { Logger } from 'pino';
4
5
  import { Observable } from 'rxjs';
5
6
 
7
+ /**
8
+ * Minimal HTTP request/response contracts the logging layer reads.
9
+ *
10
+ * Layer: server/interfaces — exported structural contracts shared by the HTTP
11
+ * logging interceptor, the exception filter and the request-id middleware. They
12
+ * are part of the public API: they appear in those components' published
13
+ * signatures, so a consumer calling `RequestIdMiddleware.use()` directly needs
14
+ * to be able to name them. Declared structurally, listing only the members
15
+ * those three actually touch, so the published declarations depend on no HTTP
16
+ * framework's types. Naming Express's
17
+ * `Request`/`Response` here would make the emitted `.d.ts` import from
18
+ * `express`, forcing every consumer that compiles with `skipLibCheck: false` to
19
+ * install `@types/express` whether or not they use those signatures.
20
+ *
21
+ * The shapes mirror what the Express adapter provides — `status().json()` and
22
+ * `setHeader()` are Express/Node response methods — so this is not an
23
+ * adapter-agnostic abstraction; a Fastify adapter remains unsupported. An
24
+ * Express request and response satisfy these contracts structurally, with no
25
+ * cast at the call site.
26
+ */
27
+ /** Raw inbound headers, as the HTTP layer delivers them. */
28
+ type IncomingHeaders = Record<string, string | string[] | undefined>;
29
+ /**
30
+ * The inbound request members the logging layer reads.
31
+ *
32
+ * `user` is populated by upstream auth in the consumer app (a guard or
33
+ * middleware). The acting-user id is read as `sub` first, then `id`: a JWT principal
34
+ * (every `@bymax-one/nest-auth` token) names it `sub`, while an ORM-style principal
35
+ * names it `id`. The JWT subject wins when both are present, since it is the
36
+ * authenticated identity. Reading only `id` — as this once did — silently dropped
37
+ * the user field for every JWT-authenticated request.
38
+ */
39
+ interface LoggableRequest {
40
+ /** Inbound headers — read for correlation ids and the user agent. */
41
+ readonly headers: IncomingHeaders;
42
+ /** HTTP verb, logged as-is. */
43
+ readonly method: string;
44
+ /** Request target, normalized and query-stripped before it reaches a log. */
45
+ readonly url: string;
46
+ /**
47
+ * The request target as it arrived, before any mount point was stripped.
48
+ *
49
+ * Middleware mounted at a path sees `url` RELATIVE to that mount: measured
50
+ * under `setGlobalPrefix('api')`, a request for `/api/users/7` reaches a
51
+ * root-mounted middleware as `url = '/users/7'` and
52
+ * `originalUrl = '/api/users/7'`, and `/api` itself arrives as `url = '/'`.
53
+ * Logging `url` there would drop the prefix from every entry and make an
54
+ * `excludePaths` pattern written against the real path stop matching. Absent
55
+ * on adapters that do not provide it, in which case `url` is already whole.
56
+ */
57
+ readonly originalUrl?: string | undefined;
58
+ /** Client address when the adapter resolves one. */
59
+ readonly ip?: string | undefined;
60
+ /**
61
+ * Authenticated principal, when upstream auth attached one. `id` is the
62
+ * ORM-style identifier; `sub` is the JWT subject claim. The logging layer reads
63
+ * whichever is present.
64
+ */
65
+ readonly user?: {
66
+ readonly id?: string | undefined;
67
+ readonly sub?: string | undefined;
68
+ } | undefined;
69
+ }
70
+ /**
71
+ * The outbound response members the logging layer writes or reads: the status
72
+ * for the terminal log entry, `setHeader` to echo the correlation id, and the
73
+ * chainable `status().json()` used by the exception filter to emit a body.
74
+ */
75
+ interface LoggableResponse {
76
+ /** Status code, read when logging the completed request. */
77
+ readonly statusCode: number;
78
+ /**
79
+ * `true` once every byte has been flushed to the socket. Read in the `'close'`
80
+ * handler to tell a delivered response from one the client never received:
81
+ * `'close'` fires for both, and `'finish'` never fires for an aborted request
82
+ * at all. It is what keeps an aborted request from being logged as the success
83
+ * the handler intended.
84
+ */
85
+ readonly writableFinished: boolean;
86
+ /** Sets a response header (used for the correlation id). */
87
+ setHeader(name: string, value: string): void;
88
+ /** Selects the status code and returns the chainable body writer. */
89
+ status(code: number): {
90
+ json(body: unknown): unknown;
91
+ };
92
+ /**
93
+ * Registers a lifecycle listener. Only `'close'` is used: it fires exactly
94
+ * once whether the response completed or the connection died early, where
95
+ * `'finish'` is blind to the aborted case.
96
+ */
97
+ on(event: 'close', listener: () => void): unknown;
98
+ }
99
+ /** Continuation passed to a middleware; invoked with no arguments here. */
100
+ type NextHandler = () => void;
101
+
6
102
  /**
7
103
  * Pino log levels (mirrored to NestJS-compatible names).
8
104
  *
@@ -295,9 +391,34 @@ interface HttpOptions {
295
391
  * crafted URL into a ReDoS vector. The defaults are anchored and safe.
296
392
  */
297
393
  excludePaths?: readonly RegExp[];
298
- /** Header name carrying the tenant identifier. Default: `x-tenant-id`. */
299
- tenantIdHeader?: string;
394
+ /**
395
+ * Header name carrying the tenant identifier, or `false` to read no header. Default:
396
+ * `x-tenant-id`.
397
+ *
398
+ * SECURITY: a `tenantId` taken from a header is what the CLIENT asserted. Any client can send any
399
+ * tenant, and every entry of its requests will carry it, so do not read it as who the request
400
+ * belongs to. A host that authenticates the tenant sets `false` or supplies
401
+ * {@link HttpOptions.tenantIdResolver}.
402
+ */
403
+ tenantIdHeader?: string | false;
404
+ /**
405
+ * Resolves the tenant id of a request on the server side, from what the host trusts (a session,
406
+ * a verified token an earlier middleware put on the request). When given it is the only source:
407
+ * the header is not read, whether the resolver answers or not. The middleware runs before guards,
408
+ * so the resolver sees only what an earlier middleware set; a tenant known later is set with
409
+ * `LogContextService.set('tenantId', ...)`. An answer that is not a string of at most 256
410
+ * characters drawn from `A-Z a-z 0-9 . _ : / + = -`, and a resolver that throws, give no tenant id.
411
+ * The resolver is synchronous: a promise it answers is no tenant id, and its rejection is absorbed.
412
+ */
413
+ tenantIdResolver?: (request: LoggableRequest) => string | undefined;
300
414
  }
415
+ /**
416
+ * The `http` options after defaults: every field is filled but the resolver, whose absence means the
417
+ * header is read.
418
+ */
419
+ type ResolvedHttpOptions = Required<Omit<HttpOptions, 'tenantIdResolver'>> & {
420
+ tenantIdResolver: HttpOptions['tenantIdResolver'];
421
+ };
301
422
  /**
302
423
  * OpenTelemetry integration tuning — applied by `TraceContextMixin` when the
303
424
  * `@opentelemetry/api` peer dependency is installed.
@@ -463,7 +584,7 @@ interface BymaxLoggerModuleOptions {
463
584
  * `BymaxLoggerModuleOptions` the consumer supplies.
464
585
  */
465
586
  type ResolvedBymaxLoggerModuleOptions = Readonly<Required<Omit<BymaxLoggerModuleOptions, 'http' | 'otel'>> & {
466
- http: Required<HttpOptions>;
587
+ http: ResolvedHttpOptions;
467
588
  otel: Required<OtelOptions>;
468
589
  }>;
469
590
  /**
@@ -525,7 +646,20 @@ declare const BymaxLoggerModuleBase: _nestjs_common.ConfigurableModuleCls<BymaxL
525
646
  declare const OPTIONS_TYPE: BymaxLoggerModuleOptions & Partial<BymaxLoggerModuleExtras>;
526
647
  declare const ASYNC_OPTIONS_TYPE: _nestjs_common.ConfigurableModuleAsyncOptions<BymaxLoggerModuleOptions, "createLoggerOptions"> & Partial<BymaxLoggerModuleExtras>;
527
648
 
528
- declare class BymaxLoggerModule extends BymaxLoggerModuleBase {
649
+ declare class BymaxLoggerModule extends BymaxLoggerModuleBase implements NestModule {
650
+ private readonly httpAdapterHost;
651
+ /**
652
+ * @param httpAdapterHost - The HTTP adapter, absent in an application with no HTTP server.
653
+ */
654
+ constructor(httpAdapterHost: HttpAdapterHost);
655
+ /**
656
+ * Mount the error layer that keeps the type of a body-parser error for the exception filter.
657
+ * It runs after the body parser, which Nest registers first, and before Nest's exception
658
+ * handler, and it passes every error on. Only the Express adapter has such a layer.
659
+ *
660
+ * @param _consumer - The middleware consumer, unused: an error layer is not consumer middleware.
661
+ */
662
+ configure(_consumer: MiddlewareConsumer): void;
529
663
  /**
530
664
  * Synchronous registration.
531
665
  *
@@ -1263,6 +1397,7 @@ declare class HttpExceptionFilter implements ExceptionFilter {
1263
1397
  * @param clientError - The status and message the error exposes.
1264
1398
  * @param res - The response to write.
1265
1399
  * @param request - The request facts every entry carries.
1400
+ * @param rawUrls - The request's own URLs, whose query string a logged message must not carry.
1266
1401
  */
1267
1402
  private answerClientError;
1268
1403
  /**
@@ -1342,6 +1477,7 @@ declare class HttpLoggingInterceptor implements NestInterceptor {
1342
1477
  * @param url - The normalized request URL.
1343
1478
  * @param userId - The acting user id, when known.
1344
1479
  * @param duration - The elapsed time in milliseconds.
1480
+ * @param rawUrls - The request's own URLs, whose query string a logged message must not carry.
1345
1481
  */
1346
1482
  private logError;
1347
1483
  /**
@@ -1352,6 +1488,7 @@ declare class HttpLoggingInterceptor implements NestInterceptor {
1352
1488
  * @param url - The normalized request URL.
1353
1489
  * @param userId - The acting user id, when known.
1354
1490
  * @param duration - The elapsed time in milliseconds.
1491
+ * @param rawUrls - The request's own URLs, whose query string a logged message must not carry.
1355
1492
  */
1356
1493
  private logClientError;
1357
1494
  }
@@ -1456,101 +1593,6 @@ declare function applyAccessLog(app: INestApplication): void;
1456
1593
  */
1457
1594
  declare function applyRequestIdMiddleware(consumer: MiddlewareConsumer, routes?: string): void;
1458
1595
 
1459
- /**
1460
- * Minimal HTTP request/response contracts the logging layer reads.
1461
- *
1462
- * Layer: server/interfaces — exported structural contracts shared by the HTTP
1463
- * logging interceptor, the exception filter and the request-id middleware. They
1464
- * are part of the public API: they appear in those components' published
1465
- * signatures, so a consumer calling `RequestIdMiddleware.use()` directly needs
1466
- * to be able to name them. Declared structurally, listing only the members
1467
- * those three actually touch, so the published declarations depend on no HTTP
1468
- * framework's types. Naming Express's
1469
- * `Request`/`Response` here would make the emitted `.d.ts` import from
1470
- * `express`, forcing every consumer that compiles with `skipLibCheck: false` to
1471
- * install `@types/express` whether or not they use those signatures.
1472
- *
1473
- * The shapes mirror what the Express adapter provides — `status().json()` and
1474
- * `setHeader()` are Express/Node response methods — so this is not an
1475
- * adapter-agnostic abstraction; a Fastify adapter remains unsupported. An
1476
- * Express request and response satisfy these contracts structurally, with no
1477
- * cast at the call site.
1478
- */
1479
- /** Raw inbound headers, as the HTTP layer delivers them. */
1480
- type IncomingHeaders = Record<string, string | string[] | undefined>;
1481
- /**
1482
- * The inbound request members the logging layer reads.
1483
- *
1484
- * `user` is populated by upstream auth in the consumer app (a guard or
1485
- * middleware). The acting-user id is read as `sub` first, then `id`: a JWT principal
1486
- * (every `@bymax-one/nest-auth` token) names it `sub`, while an ORM-style principal
1487
- * names it `id`. The JWT subject wins when both are present, since it is the
1488
- * authenticated identity. Reading only `id` — as this once did — silently dropped
1489
- * the user field for every JWT-authenticated request.
1490
- */
1491
- interface LoggableRequest {
1492
- /** Inbound headers — read for correlation ids and the user agent. */
1493
- readonly headers: IncomingHeaders;
1494
- /** HTTP verb, logged as-is. */
1495
- readonly method: string;
1496
- /** Request target, normalized and query-stripped before it reaches a log. */
1497
- readonly url: string;
1498
- /**
1499
- * The request target as it arrived, before any mount point was stripped.
1500
- *
1501
- * Middleware mounted at a path sees `url` RELATIVE to that mount: measured
1502
- * under `setGlobalPrefix('api')`, a request for `/api/users/7` reaches a
1503
- * root-mounted middleware as `url = '/users/7'` and
1504
- * `originalUrl = '/api/users/7'`, and `/api` itself arrives as `url = '/'`.
1505
- * Logging `url` there would drop the prefix from every entry and make an
1506
- * `excludePaths` pattern written against the real path stop matching. Absent
1507
- * on adapters that do not provide it, in which case `url` is already whole.
1508
- */
1509
- readonly originalUrl?: string | undefined;
1510
- /** Client address when the adapter resolves one. */
1511
- readonly ip?: string | undefined;
1512
- /**
1513
- * Authenticated principal, when upstream auth attached one. `id` is the
1514
- * ORM-style identifier; `sub` is the JWT subject claim. The logging layer reads
1515
- * whichever is present.
1516
- */
1517
- readonly user?: {
1518
- readonly id?: string | undefined;
1519
- readonly sub?: string | undefined;
1520
- } | undefined;
1521
- }
1522
- /**
1523
- * The outbound response members the logging layer writes or reads: the status
1524
- * for the terminal log entry, `setHeader` to echo the correlation id, and the
1525
- * chainable `status().json()` used by the exception filter to emit a body.
1526
- */
1527
- interface LoggableResponse {
1528
- /** Status code, read when logging the completed request. */
1529
- readonly statusCode: number;
1530
- /**
1531
- * `true` once every byte has been flushed to the socket. Read in the `'close'`
1532
- * handler to tell a delivered response from one the client never received:
1533
- * `'close'` fires for both, and `'finish'` never fires for an aborted request
1534
- * at all. It is what keeps an aborted request from being logged as the success
1535
- * the handler intended.
1536
- */
1537
- readonly writableFinished: boolean;
1538
- /** Sets a response header (used for the correlation id). */
1539
- setHeader(name: string, value: string): void;
1540
- /** Selects the status code and returns the chainable body writer. */
1541
- status(code: number): {
1542
- json(body: unknown): unknown;
1543
- };
1544
- /**
1545
- * Registers a lifecycle listener. Only `'close'` is used: it fires exactly
1546
- * once whether the response completed or the connection died early, where
1547
- * `'finish'` is blind to the aborted case.
1548
- */
1549
- on(event: 'close', listener: () => void): unknown;
1550
- }
1551
- /** Continuation passed to a middleware; invoked with no arguments here. */
1552
- type NextHandler = () => void;
1553
-
1554
1596
  /**
1555
1597
  * Records the HTTP access log for every request, including the ones no
1556
1598
  * interceptor can observe.
@@ -1606,8 +1648,10 @@ declare class HttpAccessLogMiddleware implements NestMiddleware {
1606
1648
  */
1607
1649
  declare class RequestIdMiddleware implements NestMiddleware {
1608
1650
  private readonly logContext;
1609
- /** Header name carrying the tenant id, resolved from module options. */
1651
+ /** Header name carrying the tenant id, or `false` when no header is read. */
1610
1652
  private readonly tenantIdHeader;
1653
+ /** Server-side resolver of the tenant id; when set, the header is not read. */
1654
+ private readonly tenantIdResolver;
1611
1655
  /** Whether to mint a `requestId` when the inbound header is absent. */
1612
1656
  private readonly shouldGenerateRequestId;
1613
1657
  /**
@@ -1683,15 +1727,21 @@ declare class RequestIdMiddleware implements NestMiddleware {
1683
1727
  */
1684
1728
  private resolveRequestId;
1685
1729
  /**
1686
- * The tenant id carried by the configured header, when it is acceptable.
1730
+ * The tenant id of the request, when it is acceptable.
1687
1731
  *
1688
- * Never minted: a tenant is an assertion about who the request belongs to, and
1689
- * inventing one would be a fabricated claim rather than a correlation aid.
1732
+ * From the host's resolver when it gave one, else from the configured header, else none. Never
1733
+ * minted: a tenant is an assertion about who the request belongs to, and inventing one would be a
1734
+ * fabricated claim rather than a correlation aid.
1690
1735
  *
1691
1736
  * @param req - The incoming request.
1692
1737
  * @returns The tenant id, or `undefined`.
1693
1738
  */
1694
1739
  private resolveTenantId;
1740
+ /**
1741
+ * The answer of the host's resolver, when it is acceptable. A resolver that throws gives none: a
1742
+ * log context never costs the request.
1743
+ */
1744
+ private resolvedTenantId;
1695
1745
  }
1696
1746
 
1697
1747
  /**