@nest-omni/core 4.1.3-2 → 4.1.3-3

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 (235) hide show
  1. package/audit/audit.module.d.ts +10 -0
  2. package/audit/audit.module.js +15 -0
  3. package/audit/controllers/audit.controller.d.ts +24 -0
  4. package/audit/controllers/audit.controller.js +24 -0
  5. package/audit/decorators/audit-controller.decorator.d.ts +8 -0
  6. package/audit/decorators/audit-controller.decorator.js +9 -0
  7. package/audit/decorators/audit-operation.decorator.d.ts +45 -0
  8. package/audit/decorators/audit-operation.decorator.js +49 -0
  9. package/audit/decorators/entity-audit.decorator.d.ts +8 -0
  10. package/audit/decorators/entity-audit.decorator.js +9 -0
  11. package/audit/dto/audit-log-query.dto.d.ts +3 -0
  12. package/audit/dto/audit-log-query.dto.js +3 -0
  13. package/audit/dto/begin-transaction.dto.d.ts +3 -0
  14. package/audit/dto/begin-transaction.dto.js +3 -0
  15. package/audit/dto/compare-entities.dto.d.ts +3 -0
  16. package/audit/dto/compare-entities.dto.js +3 -0
  17. package/audit/dto/pre-check-restore.dto.d.ts +3 -0
  18. package/audit/dto/pre-check-restore.dto.js +3 -0
  19. package/audit/dto/restore-entity.dto.d.ts +3 -0
  20. package/audit/dto/restore-entity.dto.js +3 -0
  21. package/audit/entities/entity-audit-log.entity.d.ts +3 -0
  22. package/audit/entities/entity-audit-log.entity.js +3 -0
  23. package/audit/entities/entity-transaction.entity.d.ts +3 -0
  24. package/audit/entities/entity-transaction.entity.js +3 -0
  25. package/audit/entities/manual-operation-log.entity.d.ts +4 -0
  26. package/audit/entities/manual-operation-log.entity.js +4 -0
  27. package/audit/entities/operation-template.entity.d.ts +4 -0
  28. package/audit/entities/operation-template.entity.js +4 -0
  29. package/audit/enums/audit.enums.d.ts +17 -2
  30. package/audit/enums/audit.enums.js +15 -0
  31. package/audit/index.js +10 -0
  32. package/audit/interceptors/audit.interceptor.d.ts +15 -0
  33. package/audit/interceptors/audit.interceptor.js +23 -1
  34. package/audit/interfaces/audit.interfaces.d.ts +42 -0
  35. package/audit/services/audit-context.service.d.ts +15 -0
  36. package/audit/services/audit-context.service.js +15 -0
  37. package/audit/services/audit-strategy.service.d.ts +6 -0
  38. package/audit/services/audit-strategy.service.js +13 -0
  39. package/audit/services/entity-audit.service.d.ts +57 -0
  40. package/audit/services/entity-audit.service.js +91 -0
  41. package/audit/services/manual-audit-log.service.d.ts +124 -0
  42. package/audit/services/manual-audit-log.service.js +138 -0
  43. package/audit/services/multi-database.service.d.ts +12 -0
  44. package/audit/services/multi-database.service.js +12 -0
  45. package/audit/services/operation-description.service.d.ts +59 -0
  46. package/audit/services/operation-description.service.js +76 -2
  47. package/audit/services/transaction-audit.service.d.ts +30 -0
  48. package/audit/services/transaction-audit.service.js +47 -0
  49. package/audit/subscribers/entity-audit.subscriber.d.ts +15 -0
  50. package/audit/subscribers/entity-audit.subscriber.js +29 -1
  51. package/cache/cache-metrics.service.d.ts +67 -0
  52. package/cache/cache-metrics.service.js +68 -4
  53. package/cache/cache-serialization.service.d.ts +31 -0
  54. package/cache/cache-serialization.service.js +25 -0
  55. package/cache/cache.constants.d.ts +9 -0
  56. package/cache/cache.constants.js +9 -0
  57. package/cache/cache.health.d.ts +26 -0
  58. package/cache/cache.health.js +30 -0
  59. package/cache/cache.module.d.ts +86 -0
  60. package/cache/cache.module.js +71 -0
  61. package/cache/cache.service.d.ts +140 -0
  62. package/cache/cache.service.js +157 -0
  63. package/cache/cache.warmup.service.d.ts +39 -0
  64. package/cache/cache.warmup.service.js +32 -0
  65. package/cache/decorators/cache-evict.decorator.d.ts +47 -0
  66. package/cache/decorators/cache-evict.decorator.js +56 -0
  67. package/cache/decorators/cache-put.decorator.d.ts +34 -0
  68. package/cache/decorators/cache-put.decorator.js +39 -0
  69. package/cache/decorators/cacheable.decorator.d.ts +40 -0
  70. package/cache/decorators/cacheable.decorator.js +55 -0
  71. package/cache/dependencies/callback.dependency.d.ts +33 -0
  72. package/cache/dependencies/callback.dependency.js +39 -1
  73. package/cache/dependencies/chain.dependency.d.ts +28 -0
  74. package/cache/dependencies/chain.dependency.js +34 -0
  75. package/cache/dependencies/db.dependency.d.ts +45 -0
  76. package/cache/dependencies/db.dependency.js +48 -1
  77. package/cache/dependencies/file.dependency.d.ts +32 -0
  78. package/cache/dependencies/file.dependency.js +34 -0
  79. package/cache/dependencies/tag.dependency.d.ts +36 -0
  80. package/cache/dependencies/tag.dependency.js +36 -0
  81. package/cache/dependencies/time.dependency.d.ts +43 -0
  82. package/cache/dependencies/time.dependency.js +43 -0
  83. package/cache/examples/basic-usage.d.ts +15 -0
  84. package/cache/examples/basic-usage.js +62 -8
  85. package/cache/index.js +9 -0
  86. package/cache/interfaces/cache-dependency.interface.d.ts +53 -0
  87. package/cache/interfaces/cache-options.interface.d.ts +81 -0
  88. package/cache/interfaces/cache-options.interface.js +6 -0
  89. package/cache/interfaces/cache-provider.interface.d.ts +78 -0
  90. package/cache/providers/base-cache.provider.d.ts +14 -0
  91. package/cache/providers/base-cache.provider.js +16 -0
  92. package/cache/providers/cls-cache.provider.d.ts +20 -0
  93. package/cache/providers/cls-cache.provider.js +28 -0
  94. package/cache/providers/memory-cache.provider.d.ts +23 -0
  95. package/cache/providers/memory-cache.provider.js +26 -0
  96. package/cache/providers/redis-cache.provider.d.ts +26 -0
  97. package/cache/providers/redis-cache.provider.js +29 -0
  98. package/cache/utils/dependency-manager.util.d.ts +52 -0
  99. package/cache/utils/dependency-manager.util.js +59 -0
  100. package/cache/utils/key-generator.util.d.ts +42 -0
  101. package/cache/utils/key-generator.util.js +53 -1
  102. package/common/abstract.entity.d.ts +14 -0
  103. package/common/abstract.entity.js +14 -0
  104. package/common/boilerplate.polyfill.d.ts +142 -0
  105. package/common/boilerplate.polyfill.js +17 -0
  106. package/common/dto/dto-container.d.ts +16 -0
  107. package/common/dto/dto-container.js +20 -0
  108. package/common/dto/dto-decorators.d.ts +18 -0
  109. package/common/dto/dto-decorators.js +14 -0
  110. package/common/dto/dto-extensions.d.ts +11 -0
  111. package/common/dto/dto-extensions.js +9 -0
  112. package/common/dto/dto-service-accessor.d.ts +17 -0
  113. package/common/dto/dto-service-accessor.js +18 -0
  114. package/common/dto/dto-transformer.d.ts +12 -0
  115. package/common/dto/dto-transformer.js +9 -0
  116. package/common/dto/index.js +2 -0
  117. package/common/examples/paginate-and-map.example.d.ts +6 -0
  118. package/common/examples/paginate-and-map.example.js +26 -0
  119. package/common/utils.d.ts +15 -0
  120. package/common/utils.js +15 -0
  121. package/constants/language-code.js +1 -0
  122. package/decorators/field.decorators.js +8 -1
  123. package/decorators/property.decorators.js +1 -0
  124. package/decorators/public-route.decorator.js +1 -0
  125. package/decorators/transform.decorators.d.ts +27 -0
  126. package/decorators/transform.decorators.js +29 -0
  127. package/decorators/translate.decorator.js +1 -0
  128. package/decorators/user.decorator.js +1 -0
  129. package/decorators/validator.decorators.d.ts +8 -18
  130. package/decorators/validator.decorators.js +22 -190
  131. package/filters/constraint-errors.js +1 -0
  132. package/helpers/common.helper.d.ts +13 -0
  133. package/helpers/common.helper.js +13 -0
  134. package/http-client/config/http-client.config.d.ts +15 -0
  135. package/http-client/config/http-client.config.js +25 -9
  136. package/http-client/decorators/http-client.decorators.d.ts +63 -0
  137. package/http-client/decorators/http-client.decorators.js +71 -3
  138. package/http-client/entities/http-log.entity.d.ts +229 -0
  139. package/http-client/entities/http-log.entity.js +6 -1
  140. package/http-client/errors/http-client.errors.d.ts +57 -0
  141. package/http-client/errors/http-client.errors.js +58 -0
  142. package/http-client/examples/advanced-usage.example.d.ts +41 -0
  143. package/http-client/examples/advanced-usage.example.js +68 -24
  144. package/http-client/examples/auth-with-waiting-lock.example.d.ts +31 -0
  145. package/http-client/examples/auth-with-waiting-lock.example.js +52 -5
  146. package/http-client/examples/basic-usage.example.d.ts +60 -0
  147. package/http-client/examples/basic-usage.example.js +60 -0
  148. package/http-client/examples/multi-api-configuration.example.d.ts +60 -0
  149. package/http-client/examples/multi-api-configuration.example.js +76 -5
  150. package/http-client/http-client.module.d.ts +13 -0
  151. package/http-client/http-client.module.js +19 -0
  152. package/http-client/index.js +8 -0
  153. package/http-client/interfaces/api-client-config.interface.d.ts +125 -0
  154. package/http-client/interfaces/api-client-config.interface.js +3 -0
  155. package/http-client/interfaces/http-client-config.interface.d.ts +60 -0
  156. package/http-client/services/api-client-registry.service.d.ts +57 -0
  157. package/http-client/services/api-client-registry.service.js +84 -1
  158. package/http-client/services/cache.service.d.ts +52 -0
  159. package/http-client/services/cache.service.js +72 -3
  160. package/http-client/services/circuit-breaker.service.d.ts +46 -0
  161. package/http-client/services/circuit-breaker.service.js +52 -0
  162. package/http-client/services/http-client.service.d.ts +67 -0
  163. package/http-client/services/http-client.service.js +105 -4
  164. package/http-client/services/http-log-query.service.d.ts +83 -0
  165. package/http-client/services/http-log-query.service.js +122 -1
  166. package/http-client/services/http-replay.service.d.ts +101 -0
  167. package/http-client/services/http-replay.service.js +86 -0
  168. package/http-client/services/log-cleanup.service.d.ts +63 -0
  169. package/http-client/services/log-cleanup.service.js +54 -2
  170. package/http-client/services/logging.service.d.ts +40 -0
  171. package/http-client/services/logging.service.js +53 -0
  172. package/http-client/utils/call-stack-extractor.util.d.ts +37 -0
  173. package/http-client/utils/call-stack-extractor.util.js +48 -0
  174. package/http-client/utils/context-extractor.util.d.ts +49 -0
  175. package/http-client/utils/context-extractor.util.js +52 -0
  176. package/http-client/utils/curl-generator.util.d.ts +21 -0
  177. package/http-client/utils/curl-generator.util.js +44 -3
  178. package/http-client/utils/request-id.util.d.ts +18 -0
  179. package/http-client/utils/request-id.util.js +20 -0
  180. package/http-client/utils/retry-recorder.util.d.ts +42 -0
  181. package/http-client/utils/retry-recorder.util.js +44 -0
  182. package/index.js +8 -0
  183. package/interceptors/translation-interceptor.service.js +5 -0
  184. package/package.json +1 -1
  185. package/providers/context.provider.js +2 -0
  186. package/providers/generator.provider.d.ts +4 -0
  187. package/providers/generator.provider.js +4 -0
  188. package/redis-lock/examples/lock-strategy.examples.d.ts +89 -0
  189. package/redis-lock/examples/lock-strategy.examples.js +130 -15
  190. package/redis-lock/index.js +3 -0
  191. package/redis-lock/redis-lock.decorator.d.ts +101 -0
  192. package/redis-lock/redis-lock.decorator.js +120 -0
  193. package/redis-lock/redis-lock.module.d.ts +60 -0
  194. package/redis-lock/redis-lock.module.js +46 -0
  195. package/redis-lock/redis-lock.service.d.ts +251 -0
  196. package/redis-lock/redis-lock.service.js +219 -3
  197. package/setup/bootstrap.setup.js +20 -0
  198. package/setup/mode.setup.d.ts +44 -0
  199. package/setup/mode.setup.js +44 -0
  200. package/setup/schedule.decorator.d.ts +226 -0
  201. package/setup/schedule.decorator.js +214 -1
  202. package/setup/worker.decorator.d.ts +86 -0
  203. package/setup/worker.decorator.js +88 -0
  204. package/shared/serviceRegistryModule.js +5 -1
  205. package/shared/services/api-config.service.d.ts +3 -0
  206. package/shared/services/api-config.service.js +20 -9
  207. package/validator-json/decorators.d.ts +17 -0
  208. package/validator-json/decorators.js +17 -2
  209. package/validator-json/default.d.ts +6 -0
  210. package/validator-json/default.js +30 -2
  211. package/validator-json/defaultConverters.js +1 -0
  212. package/validator-json/options.d.ts +23 -0
  213. package/validators/common-validators.d.ts +143 -0
  214. package/validators/common-validators.js +249 -0
  215. package/validators/custom-validate.examples.d.ts +23 -0
  216. package/validators/custom-validate.examples.js +78 -6
  217. package/validators/custom-validate.validator.d.ts +107 -0
  218. package/validators/custom-validate.validator.js +84 -0
  219. package/validators/index.d.ts +1 -0
  220. package/validators/index.js +1 -0
  221. package/validators/is-exists.validator.d.ts +11 -0
  222. package/validators/is-exists.validator.js +22 -0
  223. package/validators/is-unique.validator.d.ts +11 -0
  224. package/validators/is-unique.validator.js +18 -0
  225. package/validators/skip-empty.validator.d.ts +5 -0
  226. package/validators/skip-empty.validator.js +5 -0
  227. package/vault/interfaces/vault-options.interface.d.ts +9 -0
  228. package/vault/vault-config.loader.d.ts +30 -0
  229. package/vault/vault-config.loader.js +48 -1
  230. package/vault/vault-config.service.d.ts +53 -0
  231. package/vault/vault-config.service.js +57 -0
  232. package/vault/vault.module.d.ts +4 -0
  233. package/vault/vault.module.js +4 -0
  234. package/decorators/examples/validation-decorators.example.d.ts +0 -69
  235. package/decorators/examples/validation-decorators.example.js +0 -331
@@ -23,25 +23,39 @@ exports.HttpReplayService = void 0;
23
23
  const common_1 = require("@nestjs/common");
24
24
  const http_client_service_1 = require("./http-client.service");
25
25
  const http_log_query_service_1 = require("./http-log-query.service");
26
+ /**
27
+ * HTTP请求重放服务
28
+ * 提供基于HTTP日志的请求重放功能,支持单个请求和批量请求重放
29
+ */
26
30
  let HttpReplayService = HttpReplayService_1 = class HttpReplayService {
27
31
  constructor(httpClient, logQueryService) {
28
32
  this.httpClient = httpClient;
29
33
  this.logQueryService = logQueryService;
30
34
  this.logger = new common_1.Logger(HttpReplayService_1.name);
31
35
  }
36
+ /**
37
+ * 根据日志ID重放请求
38
+ * @param logId 日志ID
39
+ * @param options 重放选项
40
+ * @returns 重放结果
41
+ */
32
42
  replayByLogId(logId_1) {
33
43
  return __awaiter(this, arguments, void 0, function* (logId, options = {}) {
34
44
  const startTime = Date.now();
35
45
  const replayId = this.generateReplayId();
36
46
  try {
47
+ // 获取原始日志
37
48
  const originalLog = yield this.logQueryService.findLogById(logId, true);
38
49
  if (!originalLog) {
39
50
  throw new Error(`Log with ID ${logId} not found`);
40
51
  }
52
+ // 构建重放配置
41
53
  const requestConfig = this.buildReplayConfig(originalLog, options);
54
+ // 记录重放开始
42
55
  if (options.logReplay !== false) {
43
56
  this.logger.log(`Starting replay for log ${logId} (replay ID: ${replayId})`);
44
57
  }
58
+ // 执行重放请求
45
59
  const response = yield this.executeReplayRequest(requestConfig, options);
46
60
  const endTime = Date.now();
47
61
  const responseTime = endTime - startTime;
@@ -77,6 +91,12 @@ let HttpReplayService = HttpReplayService_1 = class HttpReplayService {
77
91
  }
78
92
  });
79
93
  }
94
+ /**
95
+ * 根据请求ID重放请求
96
+ * @param requestId 请求ID
97
+ * @param options 重放选项
98
+ * @returns 重放结果
99
+ */
80
100
  replayByRequestId(requestId_1) {
81
101
  return __awaiter(this, arguments, void 0, function* (requestId, options = {}) {
82
102
  const originalLog = yield this.logQueryService.findLogByRequestId(requestId);
@@ -86,10 +106,18 @@ let HttpReplayService = HttpReplayService_1 = class HttpReplayService {
86
106
  return this.replayByLogId(originalLog.id, options);
87
107
  });
88
108
  }
109
+ /**
110
+ * 批量重放请求
111
+ * @param logIds 日志ID数组
112
+ * @param options 重放选项
113
+ * @param concurrency 并发数
114
+ * @returns 批量重放结果
115
+ */
89
116
  batchReplay(logIds_1) {
90
117
  return __awaiter(this, arguments, void 0, function* (logIds, options = {}, concurrency = 5) {
91
118
  const startTime = Date.now();
92
119
  this.logger.log(`Starting batch replay for ${logIds.length} logs`);
120
+ // 分批处理
93
121
  const results = [];
94
122
  const chunks = this.chunkArray(logIds, concurrency);
95
123
  for (const chunk of chunks) {
@@ -114,6 +142,7 @@ let HttpReplayService = HttpReplayService_1 = class HttpReplayService {
114
142
  const chunkResults = yield Promise.all(chunkPromises);
115
143
  results.push(...chunkResults);
116
144
  }
145
+ // 统计结果
117
146
  const successfulRequests = results.filter((r) => r.success).length;
118
147
  const failedRequests = results.length - successfulRequests;
119
148
  const totalResponseTime = results.reduce((sum, r) => sum + r.responseTime, 0);
@@ -131,9 +160,18 @@ let HttpReplayService = HttpReplayService_1 = class HttpReplayService {
131
160
  return batchResult;
132
161
  });
133
162
  }
163
+ /**
164
+ * 根据查询条件批量重放请求
165
+ * @param queryOptions 查询选项
166
+ * @param replayOptions 重放选项
167
+ * @param concurrency 并发数
168
+ * @returns 批量重放结果
169
+ */
134
170
  batchReplayByQuery() {
135
171
  return __awaiter(this, arguments, void 0, function* (queryOptions = {}, replayOptions = {}, concurrency = 5) {
172
+ // 限制查询数量以避免过大的批量操作
136
173
  const limitedQueryOptions = Object.assign(Object.assign({}, queryOptions), { limit: Math.min(queryOptions.limit || 50, 100) });
174
+ // 查询日志
137
175
  const queryResult = yield this.logQueryService.findLogs(limitedQueryOptions);
138
176
  const logIds = queryResult.logs.map((log) => log.id);
139
177
  if (logIds.length === 0) {
@@ -152,6 +190,12 @@ let HttpReplayService = HttpReplayService_1 = class HttpReplayService {
152
190
  return this.batchReplay(logIds, replayOptions, concurrency);
153
191
  });
154
192
  }
193
+ /**
194
+ * 重放请求并比较结果
195
+ * @param logId 日志ID
196
+ * @param options 重放选项
197
+ * @returns 包含原始和重放结果的比较
198
+ */
155
199
  replayAndCompare(logId_1) {
156
200
  return __awaiter(this, arguments, void 0, function* (logId, options = {}) {
157
201
  var _a, _b, _c;
@@ -160,6 +204,7 @@ let HttpReplayService = HttpReplayService_1 = class HttpReplayService {
160
204
  throw new Error(`Log with ID ${logId} not found`);
161
205
  }
162
206
  const replayResult = yield this.replayByLogId(logId, options);
207
+ // 比较结果
163
208
  const comparison = {
164
209
  statusMatch: originalLog.statusCode === ((_a = replayResult.response) === null || _a === void 0 ? void 0 : _a.status),
165
210
  responseTimeDifference: replayResult.responseTime - originalLog.responseTime,
@@ -173,6 +218,12 @@ let HttpReplayService = HttpReplayService_1 = class HttpReplayService {
173
218
  };
174
219
  });
175
220
  }
221
+ /**
222
+ * 构建重放配置
223
+ * @param log 原始日志
224
+ * @param options 重放选项
225
+ * @returns 请求配置
226
+ */
176
227
  buildReplayConfig(log, options) {
177
228
  const config = {
178
229
  method: log.method,
@@ -180,6 +231,7 @@ let HttpReplayService = HttpReplayService_1 = class HttpReplayService {
180
231
  headers: Object.assign({}, log.headers),
181
232
  params: Object.assign({}, log.params),
182
233
  };
234
+ // 处理请求体
183
235
  if (log.body) {
184
236
  try {
185
237
  config.data =
@@ -189,6 +241,7 @@ let HttpReplayService = HttpReplayService_1 = class HttpReplayService {
189
241
  config.data = log.body;
190
242
  }
191
243
  }
244
+ // 应用覆盖选项
192
245
  if (options.overrideHeaders) {
193
246
  config.headers = Object.assign(Object.assign({}, config.headers), options.additionalHeaders);
194
247
  }
@@ -196,29 +249,49 @@ let HttpReplayService = HttpReplayService_1 = class HttpReplayService {
196
249
  config.headers = Object.assign(Object.assign({}, config.headers), options.additionalHeaders);
197
250
  }
198
251
  if (options.overrideBody && options.additionalHeaders) {
252
+ // 如果有额外的body数据,在这里处理
199
253
  }
200
254
  if (options.overrideParams && options.metadata) {
201
255
  config.params = Object.assign(Object.assign({}, config.params), options.metadata);
202
256
  }
257
+ // 添加超时
203
258
  if (options.timeout) {
204
259
  config.timeout = options.timeout;
205
260
  }
261
+ // 添加重放元数据(作为自定义属性)
206
262
  config.metadata = Object.assign({ originalLogId: log.id, originalRequestId: log.requestId, replayTimestamp: new Date().toISOString(), replayedBy: 'http-replay-service' }, options.metadata);
207
263
  return config;
208
264
  }
265
+ /**
266
+ * 执行重放请求
267
+ * @param config 请求配置
268
+ * @param options 重放选项
269
+ * @returns 响应结果
270
+ */
209
271
  executeReplayRequest(config, options) {
210
272
  return __awaiter(this, void 0, void 0, function* () {
211
273
  try {
212
274
  return yield this.httpClient.request(config);
213
275
  }
214
276
  catch (error) {
277
+ // 如果请求失败,重新抛出错误
215
278
  throw error;
216
279
  }
217
280
  });
218
281
  }
282
+ /**
283
+ * 生成重放ID
284
+ * @returns 重放ID
285
+ */
219
286
  generateReplayId() {
220
287
  return `replay-${Date.now()}-${Math.random().toString(36).substring(2, 15)}`;
221
288
  }
289
+ /**
290
+ * 数组分块
291
+ * @param array 原始数组
292
+ * @param size 块大小
293
+ * @returns 分块后的数组
294
+ */
222
295
  chunkArray(array, size) {
223
296
  const chunks = [];
224
297
  for (let i = 0; i < array.length; i += size) {
@@ -226,7 +299,14 @@ let HttpReplayService = HttpReplayService_1 = class HttpReplayService {
226
299
  }
227
300
  return chunks;
228
301
  }
302
+ /**
303
+ * 比较响应头
304
+ * @param original 原始响应头
305
+ * @param replay 重放响应头
306
+ * @returns 是否匹配
307
+ */
229
308
  compareHeaders(original, replay) {
309
+ // 简化比较:忽略时间相关的头部
230
310
  const ignoreHeaders = ['date', 'expires', 'last-modified', 'etag'];
231
311
  const originalFiltered = Object.keys(original)
232
312
  .filter((key) => !ignoreHeaders.includes(key.toLowerCase()))
@@ -242,6 +322,12 @@ let HttpReplayService = HttpReplayService_1 = class HttpReplayService {
242
322
  }, {});
243
323
  return JSON.stringify(originalFiltered) === JSON.stringify(replayFiltered);
244
324
  }
325
+ /**
326
+ * 比较响应体
327
+ * @param original 原始响应体
328
+ * @param replay 重放响应体
329
+ * @returns 是否匹配
330
+ */
245
331
  compareBodies(original, replay) {
246
332
  if (!original && !replay)
247
333
  return true;
@@ -1,31 +1,61 @@
1
1
  import { OnModuleInit } from '@nestjs/common';
2
2
  import { CronExpression } from '@nestjs/schedule';
3
3
  import { HttpLogQueryService } from './http-log-query.service';
4
+ /**
5
+ * 日志清理配置接口
6
+ */
4
7
  export interface LogCleanupConfig {
8
+ /** 是否启用自动清理 */
5
9
  enabled: boolean;
10
+ /** 清理策略 */
6
11
  strategy: 'retention' | 'size' | 'combination';
12
+ /** 保留天数 */
7
13
  retentionDays: number;
14
+ /** 最大记录数 */
8
15
  maxRecords?: number;
16
+ /** 定时清理表达式 */
9
17
  schedule?: CronExpression;
18
+ /** 批处理大小 */
10
19
  batchSize?: number;
20
+ /** 是否保留错误日志 */
11
21
  preserveErrorLogs?: boolean;
22
+ /** 错误日志保留天数 */
12
23
  errorLogRetentionDays?: number;
24
+ /** 是否保留重要日志 */
13
25
  preserveImportantLogs?: boolean;
26
+ /** 重要日志保留天数 */
14
27
  importantLogRetentionDays?: number;
28
+ /** 是否保留慢请求日志 */
15
29
  preserveSlowRequests?: boolean;
30
+ /** 慢请求日志保留天数 */
16
31
  slowRequestRetentionDays?: number;
32
+ /** 慢请求阈值(毫秒) */
17
33
  slowRequestThreshold?: number;
18
34
  }
35
+ /**
36
+ * 清理统计信息
37
+ */
19
38
  export interface CleanupStats {
39
+ /** 清理时间 */
20
40
  cleanupTime: Date;
41
+ /** 清理策略 */
21
42
  strategy: string;
43
+ /** 清理前记录数 */
22
44
  beforeCount: number;
45
+ /** 清理后记录数 */
23
46
  afterCount: number;
47
+ /** 删除的记录数 */
24
48
  deletedCount: number;
49
+ /** 处理的批次数量 */
25
50
  batchCount: number;
51
+ /** 耗时(毫秒) */
26
52
  duration: number;
53
+ /** 错误信息 */
27
54
  errors: string[];
28
55
  }
56
+ /**
57
+ * HTTP日志清理服务
58
+ */
29
59
  export declare class HttpLogCleanupService implements OnModuleInit {
30
60
  private readonly logQueryService;
31
61
  private readonly logger;
@@ -33,16 +63,34 @@ export declare class HttpLogCleanupService implements OnModuleInit {
33
63
  constructor(logQueryService: HttpLogQueryService, config?: Partial<LogCleanupConfig>);
34
64
  private get cleanupConfig();
35
65
  onModuleInit(): void;
66
+ /**
67
+ * 定时清理任务
68
+ */
36
69
  scheduledCleanup(): Promise<CleanupStats>;
70
+ /**
71
+ * 执行手动清理
72
+ */
37
73
  manualCleanup(options?: Partial<LogCleanupConfig>): Promise<CleanupStats>;
74
+ /**
75
+ * 获取清理配置
76
+ */
38
77
  getConfig(): LogCleanupConfig;
78
+ /**
79
+ * 更新清理配置
80
+ */
39
81
  updateConfig(config: Partial<LogCleanupConfig>): void;
82
+ /**
83
+ * 检查清理状态
84
+ */
40
85
  getCleanupStatus(): Promise<{
41
86
  enabled: boolean;
42
87
  config: LogCleanupConfig;
43
88
  currentStats: any;
44
89
  recommendations: string[];
45
90
  }>;
91
+ /**
92
+ * 预览清理操作(不实际执行)
93
+ */
46
94
  previewCleanup(config?: Partial<LogCleanupConfig>): Promise<{
47
95
  strategy: string;
48
96
  currentRecords: number;
@@ -56,9 +104,24 @@ export declare class HttpLogCleanupService implements OnModuleInit {
56
104
  importantLogs: number;
57
105
  };
58
106
  }>;
107
+ /**
108
+ * 执行清理操作
109
+ */
59
110
  private performCleanup;
111
+ /**
112
+ * 基于时间的清理策略
113
+ */
60
114
  private cleanupByRetention;
115
+ /**
116
+ * 基于数量的清理策略
117
+ */
61
118
  private cleanupBySize;
119
+ /**
120
+ * 组合清理策略
121
+ */
62
122
  private cleanupByCombination;
123
+ /**
124
+ * 创建清理统计
125
+ */
63
126
  private createCleanupStats;
64
127
  }
@@ -24,6 +24,9 @@ const common_1 = require("@nestjs/common");
24
24
  const schedule_1 = require("@nestjs/schedule");
25
25
  const http_log_query_service_1 = require("./http-log-query.service");
26
26
  const schedule_decorator_1 = require("../../setup/schedule.decorator");
27
+ /**
28
+ * HTTP日志清理服务
29
+ */
27
30
  let HttpLogCleanupService = HttpLogCleanupService_1 = class HttpLogCleanupService {
28
31
  constructor(logQueryService, config = {}) {
29
32
  this.logQueryService = logQueryService;
@@ -33,7 +36,7 @@ let HttpLogCleanupService = HttpLogCleanupService_1 = class HttpLogCleanupServic
33
36
  strategy: 'retention',
34
37
  retentionDays: 30,
35
38
  maxRecords: 1000000,
36
- schedule: schedule_1.CronExpression.EVERY_DAY_AT_2AM,
39
+ schedule: schedule_1.CronExpression.EVERY_DAY_AT_2AM, // 每天凌晨2点执行
37
40
  batchSize: 10000,
38
41
  preserveErrorLogs: true,
39
42
  errorLogRetentionDays: 90,
@@ -43,6 +46,7 @@ let HttpLogCleanupService = HttpLogCleanupService_1 = class HttpLogCleanupServic
43
46
  slowRequestRetentionDays: 60,
44
47
  slowRequestThreshold: 5000,
45
48
  };
49
+ // Override defaults with provided config
46
50
  if (Object.keys(config).length > 0) {
47
51
  Object.assign(this.defaultConfig, config);
48
52
  }
@@ -59,6 +63,9 @@ let HttpLogCleanupService = HttpLogCleanupService_1 = class HttpLogCleanupServic
59
63
  this.logger.warn('HTTP log cleanup is disabled');
60
64
  }
61
65
  }
66
+ /**
67
+ * 定时清理任务
68
+ */
62
69
  scheduledCleanup() {
63
70
  return __awaiter(this, void 0, void 0, function* () {
64
71
  if (!this.cleanupConfig.enabled) {
@@ -68,6 +75,9 @@ let HttpLogCleanupService = HttpLogCleanupService_1 = class HttpLogCleanupServic
68
75
  return this.performCleanup();
69
76
  });
70
77
  }
78
+ /**
79
+ * 执行手动清理
80
+ */
71
81
  manualCleanup(options) {
72
82
  return __awaiter(this, void 0, void 0, function* () {
73
83
  const config = Object.assign(Object.assign({}, this.cleanupConfig), options);
@@ -78,18 +88,28 @@ let HttpLogCleanupService = HttpLogCleanupService_1 = class HttpLogCleanupServic
78
88
  return this.performCleanup(config);
79
89
  });
80
90
  }
91
+ /**
92
+ * 获取清理配置
93
+ */
81
94
  getConfig() {
82
95
  return this.cleanupConfig;
83
96
  }
97
+ /**
98
+ * 更新清理配置
99
+ */
84
100
  updateConfig(config) {
85
101
  Object.assign(this.defaultConfig, config);
86
102
  this.logger.log('Cleanup configuration updated');
87
103
  }
104
+ /**
105
+ * 检查清理状态
106
+ */
88
107
  getCleanupStatus() {
89
108
  return __awaiter(this, void 0, void 0, function* () {
90
109
  const config = this.cleanupConfig;
91
110
  const stats = yield this.logQueryService.getStats();
92
111
  const recommendations = [];
112
+ // 生成建议
93
113
  if (stats.totalRequests > (config.maxRecords || Infinity) * 0.9) {
94
114
  recommendations.push('Current records approaching limit, consider adjusting retention policy');
95
115
  }
@@ -107,12 +127,17 @@ let HttpLogCleanupService = HttpLogCleanupService_1 = class HttpLogCleanupServic
107
127
  };
108
128
  });
109
129
  }
130
+ /**
131
+ * 预览清理操作(不实际执行)
132
+ */
110
133
  previewCleanup(config) {
111
134
  return __awaiter(this, void 0, void 0, function* () {
112
135
  const finalConfig = Object.assign(Object.assign({}, this.cleanupConfig), config);
113
136
  const stats = yield this.logQueryService.getStats();
114
137
  const cutoffDate = new Date();
115
138
  cutoffDate.setDate(cutoffDate.getDate() - finalConfig.retentionDays);
139
+ // 这里应该根据实际的数据库查询来计算分解数据
140
+ // 简化实现:
116
141
  return {
117
142
  strategy: finalConfig.strategy,
118
143
  currentRecords: stats.totalRequests,
@@ -128,6 +153,9 @@ let HttpLogCleanupService = HttpLogCleanupService_1 = class HttpLogCleanupServic
128
153
  };
129
154
  });
130
155
  }
156
+ /**
157
+ * 执行清理操作
158
+ */
131
159
  performCleanup() {
132
160
  return __awaiter(this, arguments, void 0, function* (config = this.cleanupConfig) {
133
161
  const startTime = Date.now();
@@ -135,6 +163,7 @@ let HttpLogCleanupService = HttpLogCleanupService_1 = class HttpLogCleanupServic
135
163
  let deletedCount = 0;
136
164
  const batchCount = 0;
137
165
  try {
166
+ // 获取清理前的总记录数
138
167
  const beforeStats = yield this.logQueryService.getStats();
139
168
  const beforeCount = beforeStats.totalRequests;
140
169
  switch (config.strategy) {
@@ -150,6 +179,7 @@ let HttpLogCleanupService = HttpLogCleanupService_1 = class HttpLogCleanupServic
150
179
  default:
151
180
  throw new Error(`Unknown cleanup strategy: ${config.strategy}`);
152
181
  }
182
+ // 获取清理后的总记录数
153
183
  const afterStats = yield this.logQueryService.getStats();
154
184
  const afterCount = afterStats.totalRequests;
155
185
  const duration = Date.now() - startTime;
@@ -176,12 +206,16 @@ let HttpLogCleanupService = HttpLogCleanupService_1 = class HttpLogCleanupServic
176
206
  }
177
207
  });
178
208
  }
209
+ /**
210
+ * 基于时间的清理策略
211
+ */
179
212
  cleanupByRetention(config, errors) {
180
213
  return __awaiter(this, void 0, void 0, function* () {
181
214
  let totalDeleted = 0;
182
215
  const cutoffDate = new Date();
183
216
  cutoffDate.setDate(cutoffDate.getDate() - config.retentionDays);
184
217
  try {
218
+ // 清理普通日志
185
219
  const normalDeleted = yield this.logQueryService.deleteOldLogs(config.retentionDays);
186
220
  totalDeleted += normalDeleted;
187
221
  }
@@ -189,17 +223,25 @@ let HttpLogCleanupService = HttpLogCleanupService_1 = class HttpLogCleanupServic
189
223
  errors.push(`Retention cleanup failed: ${error.message}`);
190
224
  return 0;
191
225
  }
226
+ // 如果需要保留错误日志
192
227
  if (config.preserveErrorLogs &&
193
228
  config.errorLogRetentionDays > config.retentionDays) {
229
+ // 保留错误日志的逻辑已经在deleteOldLogs中处理
194
230
  this.logger.log(`Preserving error logs for ${config.errorLogRetentionDays} days`);
195
231
  }
232
+ // 清理慢请求日志(如果启用了较长的保留期)
196
233
  if (config.preserveSlowRequests &&
197
234
  config.slowRequestRetentionDays > config.retentionDays) {
235
+ // 这里需要特殊的逻辑来保留慢请求
236
+ // 由于当前的deleteOldLogs不区分日志类型,我们需要实现特殊逻辑
198
237
  this.logger.log(`Preserving slow requests for ${config.slowRequestRetentionDays} days`);
199
238
  }
200
239
  return totalDeleted;
201
240
  });
202
241
  }
242
+ /**
243
+ * 基于数量的清理策略
244
+ */
203
245
  cleanupBySize(config, errors) {
204
246
  return __awaiter(this, void 0, void 0, function* () {
205
247
  if (!config.maxRecords) {
@@ -213,10 +255,12 @@ let HttpLogCleanupService = HttpLogCleanupService_1 = class HttpLogCleanupServic
213
255
  }
214
256
  const excessCount = stats.totalRequests - config.maxRecords;
215
257
  const targetCutoffDate = new Date();
216
- const averageDailyRecords = stats.totalRequests / 30;
258
+ // 计算需要删除的记录对应的时间点
259
+ const averageDailyRecords = stats.totalRequests / 30; // 假设30天的数据
217
260
  const daysToDelete = Math.ceil(excessCount / averageDailyRecords);
218
261
  targetCutoffDate.setDate(targetCutoffDate.getDate() - daysToDelete);
219
262
  try {
263
+ // 使用更精确的日期进行清理
220
264
  const deleted = yield this.logQueryService.deleteOldLogs(Math.ceil((Date.now() - targetCutoffDate.getTime()) / (24 * 60 * 60 * 1000)));
221
265
  this.logger.log(`Size-based cleanup: deleted ${deleted} records, cutoff date: ${targetCutoffDate.toISOString()}`);
222
266
  return deleted;
@@ -227,11 +271,16 @@ let HttpLogCleanupService = HttpLogCleanupService_1 = class HttpLogCleanupServic
227
271
  }
228
272
  });
229
273
  }
274
+ /**
275
+ * 组合清理策略
276
+ */
230
277
  cleanupByCombination(config, errors) {
231
278
  return __awaiter(this, void 0, void 0, function* () {
232
279
  let totalDeleted = 0;
280
+ // 首先基于时间清理
233
281
  const retentionDeleted = yield this.cleanupByRetention(config, errors);
234
282
  totalDeleted += retentionDeleted;
283
+ // 如果仍然超过最大记录数,进行基于数量的清理
235
284
  if (config.maxRecords) {
236
285
  const stats = yield this.logQueryService.getStats();
237
286
  if (stats.totalRequests > config.maxRecords) {
@@ -242,6 +291,9 @@ let HttpLogCleanupService = HttpLogCleanupService_1 = class HttpLogCleanupServic
242
291
  return totalDeleted;
243
292
  });
244
293
  }
294
+ /**
295
+ * 创建清理统计
296
+ */
245
297
  createCleanupStats(strategy, beforeCount, afterCount, deletedCount, batchCount, errors) {
246
298
  return {
247
299
  cleanupTime: new Date(),
@@ -1,14 +1,30 @@
1
1
  import { DataSource } from 'typeorm';
2
2
  import { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';
3
3
  import { HttpLogEntity } from '../entities/http-log.entity';
4
+ /**
5
+ * HTTP日志服务
6
+ * 基于Spring Boot的请求日志记录机制,集成现有的ContextProvider
7
+ */
4
8
  export declare class HttpLoggingService {
5
9
  private readonly dataSource?;
6
10
  private readonly logger;
7
11
  private logRepository;
8
12
  constructor(dataSource?: DataSource);
13
+ /**
14
+ * 记录请求开始
15
+ */
9
16
  logRequestStart(config: AxiosRequestConfig, loggingOptions: any, requestId?: string): string;
17
+ /**
18
+ * 记录请求成功响应
19
+ */
10
20
  logRequestSuccess(response: AxiosResponse, startTime: number, requestId: string, loggingOptions: any, databaseLogging?: boolean, retryRecords?: any[], cacheHit?: boolean, circuitBreakerState?: string, decoratorContext?: any): void;
21
+ /**
22
+ * 记录请求错误
23
+ */
11
24
  logRequestError(error: AxiosError, startTime: number, requestId: string, attemptCount: number, loggingOptions: any, databaseLogging?: boolean, retryRecords?: any[], cacheHit?: boolean, circuitBreakerState?: string, decoratorContext?: any): void;
25
+ /**
26
+ * 查询请求日志
27
+ */
12
28
  findLogs(options: {
13
29
  requestId?: string;
14
30
  userId?: string;
@@ -21,16 +37,40 @@ export declare class HttpLoggingService {
21
37
  limit?: number;
22
38
  offset?: number;
23
39
  }): Promise<HttpLogEntity[]>;
40
+ /**
41
+ * 获取请求统计信息
42
+ */
24
43
  getStatistics(options: {
25
44
  startDate?: Date;
26
45
  endDate?: Date;
27
46
  userId?: string;
28
47
  }): Promise<any>;
48
+ /**
49
+ * 初始化数据库日志记录
50
+ */
29
51
  private initializeDatabaseLogging;
52
+ /**
53
+ * 提取调用信息
54
+ */
30
55
  private extractCallInfo;
56
+ /**
57
+ * 保存到数据库
58
+ */
31
59
  private saveToDatabase;
60
+ /**
61
+ * 转换请求体为字符串
62
+ */
32
63
  private sanitizeBodyAsString;
64
+ /**
65
+ * 清理敏感头信息
66
+ */
33
67
  private sanitizeHeaders;
68
+ /**
69
+ * 清理敏感请求体信息
70
+ */
34
71
  private sanitizeBody;
72
+ /**
73
+ * 递归清理对象中的敏感字段
74
+ */
35
75
  private sanitizeObject;
36
76
  }