@saihu/common 1.1.79 → 1.1.80
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/dist/decorators/index.d.ts +1 -0
- package/dist/decorators/index.d.ts.map +1 -1
- package/dist/decorators/index.js +1 -0
- package/dist/decorators/operlog.decorator.d.ts +7 -0
- package/dist/decorators/operlog.decorator.d.ts.map +1 -0
- package/dist/decorators/operlog.decorator.js +11 -0
- package/dist/interceptors/operlog.interceptor.d.ts.map +1 -1
- package/dist/interceptors/operlog.interceptor.js +7 -0
- package/dist/interfaces/options.interface.d.ts +9 -3
- package/dist/interfaces/options.interface.d.ts.map +1 -1
- package/dist/saihu-common.module.d.ts.map +1 -1
- package/dist/saihu-common.module.js +16 -9
- package/dist/services/redis.service.d.ts.map +1 -1
- package/dist/services/redis.service.js +13 -10
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/decorators/index.ts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/decorators/index.ts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC;AAC5C,cAAc,qBAAqB,CAAC"}
|
package/dist/decorators/index.js
CHANGED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const OPERLOG_KEY = "operlog";
|
|
2
|
+
/**
|
|
3
|
+
* Decorator to enable operation logging for specific endpoints
|
|
4
|
+
* @param enabled - Whether to enable logging for this endpoint (default: true)
|
|
5
|
+
*/
|
|
6
|
+
export declare const LogOperation: (enabled?: boolean) => import("@nestjs/common").CustomDecorator<string>;
|
|
7
|
+
//# sourceMappingURL=operlog.decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operlog.decorator.d.ts","sourceRoot":"","sources":["../../src/decorators/operlog.decorator.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,WAAW,YAAY,CAAC;AAErC;;;GAGG;AACH,eAAO,MAAM,YAAY,GAAI,UAAS,OAAc,qDACjB,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LogOperation = exports.OPERLOG_KEY = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
exports.OPERLOG_KEY = 'operlog';
|
|
6
|
+
/**
|
|
7
|
+
* Decorator to enable operation logging for specific endpoints
|
|
8
|
+
* @param enabled - Whether to enable logging for this endpoint (default: true)
|
|
9
|
+
*/
|
|
10
|
+
const LogOperation = (enabled = true) => (0, common_1.SetMetadata)(exports.OPERLOG_KEY, enabled);
|
|
11
|
+
exports.LogOperation = LogOperation;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"operlog.interceptor.d.ts","sourceRoot":"","sources":["../../src/interceptors/operlog.interceptor.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,eAAe,EACf,gBAAgB,EAChB,WAAW,EAGZ,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAElC,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AAKtE,OAAO,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;
|
|
1
|
+
{"version":3,"file":"operlog.interceptor.d.ts","sourceRoot":"","sources":["../../src/interceptors/operlog.interceptor.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,eAAe,EACf,gBAAgB,EAChB,WAAW,EAGZ,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAElC,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AAKtE,OAAO,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AAKlE,qBACa,kBAAmB,YAAW,eAAe;IAMtD,OAAO,CAAC,SAAS;IACS,OAAO,CAAC,QAAQ,CAAC,eAAe;IAE1D,OAAO,CAAC,QAAQ,CAAC,OAAO;IAR1B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuC;IAC9D,OAAO,CAAC,SAAS,CAAU;IAC3B,OAAO,CAAC,SAAS,CAAW;gBAGlB,SAAS,EAAE,SAAS,EACe,eAAe,EAAE,eAAe,EAE1D,OAAO,EAAE,mBAAmB;IAa/C,SAAS,CAAC,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,CAAC,GAAG,CAAC;IAgFxE,OAAO,CAAC,WAAW;IAiCnB;;OAEG;IACH,OAAO,CAAC,gBAAgB;CAWzB"}
|
|
@@ -18,6 +18,7 @@ const common_1 = require("@nestjs/common");
|
|
|
18
18
|
const core_1 = require("@nestjs/core");
|
|
19
19
|
const operators_1 = require("rxjs/operators");
|
|
20
20
|
const common_consts_1 = require("../consts/common.consts");
|
|
21
|
+
const operlog_decorator_1 = require("../decorators/operlog.decorator");
|
|
21
22
|
let OperlogInterceptor = OperlogInterceptor_1 = class OperlogInterceptor {
|
|
22
23
|
constructor(reflector, rabbitmqService, options) {
|
|
23
24
|
this.reflector = reflector;
|
|
@@ -35,6 +36,12 @@ let OperlogInterceptor = OperlogInterceptor_1 = class OperlogInterceptor {
|
|
|
35
36
|
}
|
|
36
37
|
intercept(context, next) {
|
|
37
38
|
var _a;
|
|
39
|
+
// Check if the decorator is present and enabled
|
|
40
|
+
const isOperlogEnabled = this.reflector.getAllAndOverride(operlog_decorator_1.OPERLOG_KEY, [context.getHandler(), context.getClass()]);
|
|
41
|
+
// If decorator is not present or is disabled, skip logging
|
|
42
|
+
if (isOperlogEnabled !== true) {
|
|
43
|
+
return next.handle();
|
|
44
|
+
}
|
|
38
45
|
if (!this.isEnabled) {
|
|
39
46
|
return next.handle();
|
|
40
47
|
}
|
|
@@ -1,21 +1,27 @@
|
|
|
1
|
-
import { Type } from
|
|
1
|
+
import { Type } from '@nestjs/common';
|
|
2
2
|
export interface CommonModuleOptions {
|
|
3
3
|
/**
|
|
4
4
|
* 模块名称
|
|
5
5
|
*/
|
|
6
6
|
name: string;
|
|
7
7
|
/**
|
|
8
|
-
* 是否开启权限校验
|
|
8
|
+
* 是否开启权限校验 (需要配置 redis)
|
|
9
9
|
*/
|
|
10
10
|
usePerms?: boolean;
|
|
11
11
|
/**
|
|
12
|
-
* 是否开启操作日志
|
|
12
|
+
* 是否开启操作日志 (需要配置 rabbitmq)
|
|
13
13
|
*/
|
|
14
14
|
useOperlog?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Redis 配置 (仅在 usePerms 为 true 时需要)
|
|
17
|
+
*/
|
|
15
18
|
redis?: {
|
|
16
19
|
url: string;
|
|
17
20
|
password: string;
|
|
18
21
|
};
|
|
22
|
+
/**
|
|
23
|
+
* RabbitMQ 配置 (仅在 useOperlog 为 true 时需要)
|
|
24
|
+
*/
|
|
19
25
|
rabbitmq?: {
|
|
20
26
|
url: string;
|
|
21
27
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"options.interface.d.ts","sourceRoot":"","sources":["../../src/interfaces/options.interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAEtC,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,KAAK,CAAC,EAAE;QACN,GAAG,EAAE,MAAM,CAAC;QACZ,QAAQ,EAAE,MAAM,CAAC;KAClB,CAAC;
|
|
1
|
+
{"version":3,"file":"options.interface.d.ts","sourceRoot":"","sources":["../../src/interfaces/options.interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAEtC,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB;;OAEG;IACH,KAAK,CAAC,EAAE;QACN,GAAG,EAAE,MAAM,CAAC;QACZ,QAAQ,EAAE,MAAM,CAAC;KAClB,CAAC;IACF;;OAEG;IACH,QAAQ,CAAC,EAAE;QACT,GAAG,EAAE,MAAM,CAAC;KACb,CAAC;IACF;;OAEG;IACH,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;CAC7B;AAED,MAAM,WAAW,wBAAwB;IACvC,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC;IAChB,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC;IACf,UAAU,EAAE,CACV,GAAG,IAAI,EAAE,GAAG,EAAE,KACX,OAAO,CAAC,mBAAmB,CAAC,GAAG,mBAAmB,CAAC;IACxD,QAAQ,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;IACrB,WAAW,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;CACzB;AAED,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"saihu-common.module.d.ts","sourceRoot":"","sources":["../src/saihu-common.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,aAAa,EAAY,MAAM,gBAAgB,CAAC;AAEjE,OAAO,EACL,wBAAwB,EACxB,mBAAmB,EACpB,MAAM,gCAAgC,CAAC;AAWxC,qBACa,iBAAiB;IAC5B,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,mBAAmB,GAAG,aAAa;IA6B3D,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,wBAAwB,GAAG,aAAa;
|
|
1
|
+
{"version":3,"file":"saihu-common.module.d.ts","sourceRoot":"","sources":["../src/saihu-common.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,aAAa,EAAY,MAAM,gBAAgB,CAAC;AAEjE,OAAO,EACL,wBAAwB,EACxB,mBAAmB,EACpB,MAAM,gCAAgC,CAAC;AAWxC,qBACa,iBAAiB;IAC5B,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,mBAAmB,GAAG,aAAa;IA6B3D,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,wBAAwB,GAAG,aAAa;IAiCrE,MAAM,CAAC,sBAAsB,IAAI,aAAa;CAa/C"}
|
|
@@ -30,18 +30,18 @@ let SaihuCommonModule = SaihuCommonModule_1 = class SaihuCommonModule {
|
|
|
30
30
|
provide: common_consts_1.RABBITMQ_SERVICE,
|
|
31
31
|
useClass: rabbitmq_service_1.RabbitmqServiceImpl,
|
|
32
32
|
},
|
|
33
|
+
// Add OperlogInterceptor as a regular provider so it can be used manually
|
|
34
|
+
interceptors_1.OperlogInterceptor,
|
|
33
35
|
];
|
|
34
|
-
const useOperlog = options.useOperlog !== false;
|
|
35
|
-
if (useOperlog) {
|
|
36
|
-
providers.push({
|
|
37
|
-
provide: core_1.APP_INTERCEPTOR,
|
|
38
|
-
useClass: interceptors_1.OperlogInterceptor,
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
36
|
return {
|
|
42
37
|
module: SaihuCommonModule_1,
|
|
43
38
|
providers,
|
|
44
|
-
exports: [
|
|
39
|
+
exports: [
|
|
40
|
+
common_consts_1.COMMON_MODULE_OPTIONS,
|
|
41
|
+
common_consts_1.REDIS_SERVICE,
|
|
42
|
+
common_consts_1.RABBITMQ_SERVICE,
|
|
43
|
+
interceptors_1.OperlogInterceptor,
|
|
44
|
+
],
|
|
45
45
|
global: true,
|
|
46
46
|
};
|
|
47
47
|
}
|
|
@@ -60,12 +60,19 @@ let SaihuCommonModule = SaihuCommonModule_1 = class SaihuCommonModule {
|
|
|
60
60
|
provide: common_consts_1.RABBITMQ_SERVICE,
|
|
61
61
|
useClass: rabbitmq_service_1.RabbitmqServiceImpl,
|
|
62
62
|
},
|
|
63
|
+
// Add OperlogInterceptor as a regular provider so it can be used manually
|
|
64
|
+
interceptors_1.OperlogInterceptor,
|
|
63
65
|
];
|
|
64
66
|
return {
|
|
65
67
|
module: SaihuCommonModule_1,
|
|
66
68
|
imports: [config_1.ConfigModule, ...(options.imports || [])],
|
|
67
69
|
providers,
|
|
68
|
-
exports: [
|
|
70
|
+
exports: [
|
|
71
|
+
common_consts_1.COMMON_MODULE_OPTIONS,
|
|
72
|
+
common_consts_1.REDIS_SERVICE,
|
|
73
|
+
common_consts_1.RABBITMQ_SERVICE,
|
|
74
|
+
interceptors_1.OperlogInterceptor,
|
|
75
|
+
],
|
|
69
76
|
global: true, // 设置为全局模块
|
|
70
77
|
};
|
|
71
78
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redis.service.d.ts","sourceRoot":"","sources":["../../src/services/redis.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,YAAY,EACZ,eAAe,EAGhB,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAE/D,qBACa,gBACX,YAAW,YAAY,EAAE,YAAY,EAAE,eAAe;IASpD,OAAO,CAAC,QAAQ,CAAC,aAAa;IAPhC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAqC;IAC5D,OAAO,CAAC,WAAW,CAAoB;IACvC,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,SAAS,CAAkB;gBAIhB,aAAa,EAAE,mBAAmB;
|
|
1
|
+
{"version":3,"file":"redis.service.d.ts","sourceRoot":"","sources":["../../src/services/redis.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,YAAY,EACZ,eAAe,EAGhB,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAE/D,qBACa,gBACX,YAAW,YAAY,EAAE,YAAY,EAAE,eAAe;IASpD,OAAO,CAAC,QAAQ,CAAC,aAAa;IAPhC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAqC;IAC5D,OAAO,CAAC,WAAW,CAAoB;IACvC,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,SAAS,CAAkB;gBAIhB,aAAa,EAAE,mBAAmB;IAM/C,YAAY;IAIZ,UAAU;IA0CV,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAYtC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAW5D,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIrC,eAAe;CAMhB"}
|
|
@@ -26,13 +26,15 @@ let RedisServiceImpl = RedisServiceImpl_1 = class RedisServiceImpl {
|
|
|
26
26
|
this.logger = new common_1.Logger(RedisServiceImpl_1.name);
|
|
27
27
|
this.isInitialized = false;
|
|
28
28
|
this.isEnabled = false;
|
|
29
|
-
|
|
29
|
+
// Only enable Redis if usePerms is explicitly true
|
|
30
|
+
this.isEnabled = commonOptions.usePerms === true;
|
|
30
31
|
}
|
|
31
32
|
async onModuleInit() {
|
|
32
33
|
await this.initialize();
|
|
33
34
|
}
|
|
34
35
|
async initialize() {
|
|
35
36
|
if (!this.isEnabled) {
|
|
37
|
+
this.logger.log('Redis service disabled (usePerms is not enabled)');
|
|
36
38
|
return;
|
|
37
39
|
}
|
|
38
40
|
if (this.isInitialized) {
|
|
@@ -40,16 +42,16 @@ let RedisServiceImpl = RedisServiceImpl_1 = class RedisServiceImpl {
|
|
|
40
42
|
}
|
|
41
43
|
try {
|
|
42
44
|
if (!this.commonOptions.redis) {
|
|
43
|
-
this.logger.error('
|
|
44
|
-
throw new Error('
|
|
45
|
+
this.logger.error('Redis options not found. Redis is required when usePerms is true.');
|
|
46
|
+
throw new Error('Redis configuration is required when usePerms is enabled');
|
|
45
47
|
}
|
|
46
48
|
if (!this.commonOptions.redis.url) {
|
|
47
|
-
this.logger.error('
|
|
48
|
-
throw new Error('
|
|
49
|
+
this.logger.error('Redis url not found');
|
|
50
|
+
throw new Error('Redis url is required');
|
|
49
51
|
}
|
|
50
52
|
if (!this.commonOptions.redis.password) {
|
|
51
|
-
this.logger.error('
|
|
52
|
-
throw new Error('
|
|
53
|
+
this.logger.error('Redis password not found');
|
|
54
|
+
throw new Error('Redis password is required');
|
|
53
55
|
}
|
|
54
56
|
const url = this.commonOptions.redis.url;
|
|
55
57
|
const password = this.commonOptions.redis.password;
|
|
@@ -57,10 +59,11 @@ let RedisServiceImpl = RedisServiceImpl_1 = class RedisServiceImpl {
|
|
|
57
59
|
password,
|
|
58
60
|
db: common_consts_1.REDIS_AUTH_DB,
|
|
59
61
|
});
|
|
60
|
-
|
|
62
|
+
this.logger.log('Redis connected successfully');
|
|
63
|
+
this.isInitialized = true;
|
|
61
64
|
}
|
|
62
65
|
catch (error) {
|
|
63
|
-
this.logger.error('
|
|
66
|
+
this.logger.error('Failed to connect to Redis:', error);
|
|
64
67
|
throw error;
|
|
65
68
|
}
|
|
66
69
|
}
|
|
@@ -89,7 +92,7 @@ let RedisServiceImpl = RedisServiceImpl_1 = class RedisServiceImpl {
|
|
|
89
92
|
await this.redisClient.del(key);
|
|
90
93
|
}
|
|
91
94
|
onModuleDestroy() {
|
|
92
|
-
if (!this.isEnabled) {
|
|
95
|
+
if (!this.isEnabled || !this.redisClient) {
|
|
93
96
|
return;
|
|
94
97
|
}
|
|
95
98
|
this.redisClient.disconnect();
|