@jsfsi-core/ts-nestjs 1.1.41 → 1.1.42

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/README.md CHANGED
@@ -39,7 +39,8 @@ src/
39
39
  ├── guards/
40
40
  │ └── InMemoryRateLimitGuard.ts # In-memory rate limit guard
41
41
  ├── middlewares/
42
- └── RequestMiddleware.ts # Request logging
42
+ ├── RequestMiddleware.ts # Request logging
43
+ │ └── RequestMiddlewareLogCustomizer.ts # Optional custom log payload
43
44
  └── validators/
44
45
  └── ZodValidator.ts # Request validators
45
46
  ```
@@ -226,7 +227,9 @@ The `CustomLogger` extends NestJS's `Logger` and provides a consistent interface
226
227
 
227
228
  ### Request Middleware
228
229
 
229
- Automatic request logging:
230
+ Automatic request logging with an optional custom payload.
231
+
232
+ **Basic usage:**
230
233
 
231
234
  ```typescript
232
235
  import { RequestMiddleware } from '@jsfsi-core/ts-nestjs';
@@ -242,13 +245,63 @@ export class AppModule implements NestModule {
242
245
  }
243
246
  ```
244
247
 
245
- Logs include:
248
+ **Base log payload** (always included):
249
+
250
+ - `method` – HTTP method
251
+ - `url` – Request URL
252
+ - `statusCode` – Response status code
253
+ - `timeSpentMs` – Response time in milliseconds
254
+ - `domain` – Client IP (from request)
255
+ - `requestHeaders` – Request headers
256
+ - `responseHeaders` – Response headers
257
+
258
+ Log severity is chosen from the status code (e.g. 2XX → verbose, 4XX → warn, 5XX → error).
259
+
260
+ **Custom payload** – To add extra fields to each request log (e.g. `userId`, `tenantId`), provide a customizer:
261
+
262
+ 1. Implement `RequestMiddlewareLogCustomizer` with `buildLogPayload(req, res): Record<string, unknown>`.
263
+ 2. Register it under the `REQUEST_MIDDLEWARE_LOG_CUSTOMIZER` token in your module `providers`.
264
+
265
+ The middleware merges the custom payload with the base payload; customizer keys override base keys if names collide.
266
+
267
+ ```typescript
268
+ import {
269
+ REQUEST_MIDDLEWARE_LOG_CUSTOMIZER,
270
+ RequestMiddleware,
271
+ RequestMiddlewareLogCustomizer,
272
+ } from '@jsfsi-core/ts-nestjs';
273
+ import { Request, Response } from 'express';
274
+ import { Module, NestModule, MiddlewareConsumer, Provider } from '@nestjs/common';
275
+
276
+ // 1. Implement the customizer
277
+ export class RequestUserLogCustomizer implements RequestMiddlewareLogCustomizer {
278
+ buildLogPayload(req: Request & { user?: { id: string } }, _res: Response): Record<string, unknown> {
279
+ return {
280
+ userId: req.user?.id,
281
+ };
282
+ }
283
+ }
284
+
285
+ const middlewares: Provider[] = [
286
+ {
287
+ provide: REQUEST_MIDDLEWARE_LOG_CUSTOMIZER,
288
+ useClass: RequestUserLogCustomizer,
289
+ },
290
+ ];
291
+
292
+ @Module({
293
+ imports: [appConfigModuleSetup()],
294
+ controllers: [...controllers],
295
+ providers: [...services, ...adapters, ...middlewares],
296
+ })
297
+ export class AppModule implements NestModule {
298
+ configure(consumer: MiddlewareConsumer): void {
299
+ consumer.apply(RequestMiddleware).forRoutes('*');
300
+ }
301
+ }
302
+ ```
246
303
 
247
- - HTTP method and URL
248
- - Status code
249
- - Response time
250
- - Request/response headers
251
- - Severity level based on status code
304
+ With the customizer above, each request log will include the base fields plus `userId` when the request has an authenticated user.
252
305
 
253
306
  ### In-Memory Rate Limit Guard
254
307