@elchinabilov/nestjs-audit-logs 1.0.0 → 1.0.2

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 (2) hide show
  1. package/README.md +43 -15
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -3,18 +3,22 @@
3
3
  NestJS audit log module for TypeORM with CLS context support.
4
4
 
5
5
  ## Features
6
+
6
7
  - TypeORM entity and service for storing audit logs
7
8
  - Interceptor that captures request metadata
8
9
  - Decorator for per-route metadata
9
10
  - CLS integration via `nestjs-cls` (installed automatically)
10
11
 
11
12
  ## Installation
13
+
12
14
  ```bash
13
15
  npm install @elchinabilov/nestjs-audit-logs
14
16
  ```
15
17
 
16
18
  ## Peer dependencies
19
+
17
20
  Make sure your app has these installed:
21
+
18
22
  - `@nestjs/common`
19
23
  - `@nestjs/core`
20
24
  - `@nestjs/typeorm`
@@ -27,9 +31,10 @@ Make sure your app has these installed:
27
31
  ## Usage
28
32
 
29
33
  ### 1) Import the module
34
+
30
35
  ```ts
31
- import { Module } from '@nestjs/common';
32
- import { AuditLogModule } from '@elchinabilov/nestjs-audit-logs';
36
+ import { Module } from "@nestjs/common";
37
+ import { AuditLogModule } from "@elchinabilov/nestjs-audit-logs";
33
38
 
34
39
  @Module({
35
40
  imports: [AuditLogModule],
@@ -38,11 +43,12 @@ export class AppModule {}
38
43
  ```
39
44
 
40
45
  ### 2) Ensure the entity is registered
46
+
41
47
  If you use `autoLoadEntities: true`, it will be picked up automatically. Otherwise, add `AuditLogEntity` to your TypeORM entities.
42
48
 
43
49
  ```ts
44
- import { TypeOrmModule } from '@nestjs/typeorm';
45
- import { AuditLogEntity } from '@elchinabilov/nestjs-audit-logs';
50
+ import { TypeOrmModule } from "@nestjs/typeorm";
51
+ import { AuditLogEntity } from "@elchinabilov/nestjs-audit-logs";
46
52
 
47
53
  TypeOrmModule.forRoot({
48
54
  // ...
@@ -51,6 +57,7 @@ TypeOrmModule.forRoot({
51
57
  ```
52
58
 
53
59
  ### 3) Register the interceptor
60
+
54
61
  You can apply it globally or per-controller.
55
62
 
56
63
  ```ts
@@ -66,6 +73,7 @@ providers: [
66
73
  ```
67
74
 
68
75
  ### 4) Add metadata (optional)
76
+
69
77
  ```ts
70
78
  import { AuditLog } from '@elchinabilov/nestjs-audit-logs';
71
79
 
@@ -75,26 +83,46 @@ findAll() {}
75
83
  ```
76
84
 
77
85
  ### 5) Set CLS values
86
+
78
87
  The interceptor reads `userId`, `role`, `ip`, and `requestId` from the CLS context. Populate these values in a guard, middleware, or interceptor before the audit interceptor runs.
79
88
 
80
89
  ```ts
81
- import { Injectable, NestMiddleware } from '@nestjs/common';
82
- import { ClsService } from 'nestjs-cls';
90
+ import { ExecutionContext, Injectable } from "@nestjs/common";
91
+ import { AuthGuard } from "@nestjs/passport";
92
+ import { ClsService } from "nestjs-cls";
93
+ import { v4 as uuid } from "uuid";
83
94
 
84
95
  @Injectable()
85
- export class AuditContextMiddleware implements NestMiddleware {
86
- constructor(private readonly cls: ClsService) {}
87
-
88
- use(req: any, res: any, next: () => void) {
89
- this.cls.set('userId', req.user?.id);
90
- this.cls.set('role', req.user?.role);
91
- this.cls.set('ip', req.ip);
92
- this.cls.set('requestId', req.headers['x-request-id']);
93
- next();
96
+ export class JwtAuthGuard extends AuthGuard("jwt") {
97
+ constructor(
98
+ // Add this line
99
+ private readonly cls: ClsService
100
+ ) {
101
+ super();
102
+ }
103
+
104
+ canActivate(context: ExecutionContext) {
105
+ // ...
106
+ }
107
+
108
+ // Add this code block
109
+ handleRequest(err, user, info, context: ExecutionContext) {
110
+ const req = context.switchToHttp().getRequest();
111
+
112
+ if (user) {
113
+ this.cls.set("userId", user.id);
114
+ this.cls.set("role", user.role?.name);
115
+ }
116
+
117
+ this.cls.set("ip", req.ip);
118
+ this.cls.set("requestId", uuid());
119
+
120
+ return user;
94
121
  }
95
122
  }
96
123
  ```
97
124
 
98
125
  ## Notes
126
+
99
127
  - The module calls `ClsModule.forRoot({ global: true, middleware: { mount: true } })` internally.
100
128
  - Logs are stored in the `audit_logs` table.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elchinabilov/nestjs-audit-logs",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "NestJS audit log module with TypeORM and CLS context",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",