@nest-omni/core 3.1.2-6 → 3.1.2-8

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 (64) hide show
  1. package/common/dto/dto-extensions.js +6 -5
  2. package/common/utils.d.ts +1 -0
  3. package/common/utils.js +6 -0
  4. package/http-client/config/http-client.config.d.ts +6 -0
  5. package/http-client/config/http-client.config.js +87 -0
  6. package/http-client/config/index.d.ts +1 -0
  7. package/http-client/config/index.js +17 -0
  8. package/http-client/decorators/http-client.decorators.d.ts +72 -0
  9. package/http-client/decorators/http-client.decorators.js +204 -0
  10. package/http-client/decorators/index.d.ts +1 -0
  11. package/http-client/decorators/index.js +17 -0
  12. package/http-client/entities/http-log.entity.d.ts +98 -0
  13. package/http-client/entities/http-log.entity.js +143 -0
  14. package/http-client/entities/index.d.ts +1 -0
  15. package/http-client/entities/index.js +17 -0
  16. package/http-client/errors/http-client.errors.d.ts +56 -0
  17. package/http-client/errors/http-client.errors.js +149 -0
  18. package/http-client/errors/index.d.ts +1 -0
  19. package/http-client/errors/index.js +17 -0
  20. package/http-client/examples/advanced-usage.example.d.ts +23 -0
  21. package/http-client/examples/advanced-usage.example.js +319 -0
  22. package/http-client/examples/basic-usage.example.d.ts +61 -0
  23. package/http-client/examples/basic-usage.example.js +171 -0
  24. package/http-client/examples/index.d.ts +3 -0
  25. package/http-client/examples/index.js +19 -0
  26. package/http-client/examples/multi-api-configuration.example.d.ts +98 -0
  27. package/http-client/examples/multi-api-configuration.example.js +353 -0
  28. package/http-client/http-client.module.d.ts +11 -0
  29. package/http-client/http-client.module.js +254 -0
  30. package/http-client/index.d.ts +10 -0
  31. package/http-client/index.js +27 -0
  32. package/http-client/interfaces/api-client-config.interface.d.ts +152 -0
  33. package/http-client/interfaces/api-client-config.interface.js +12 -0
  34. package/http-client/interfaces/http-client-config.interface.d.ts +123 -0
  35. package/http-client/interfaces/http-client-config.interface.js +2 -0
  36. package/http-client/services/api-client-registry.service.d.ts +40 -0
  37. package/http-client/services/api-client-registry.service.js +411 -0
  38. package/http-client/services/cache.service.d.ts +24 -0
  39. package/http-client/services/cache.service.js +264 -0
  40. package/http-client/services/circuit-breaker.service.d.ts +33 -0
  41. package/http-client/services/circuit-breaker.service.js +180 -0
  42. package/http-client/services/http-client.service.d.ts +43 -0
  43. package/http-client/services/http-client.service.js +384 -0
  44. package/http-client/services/http-log-query.service.d.ts +56 -0
  45. package/http-client/services/http-log-query.service.js +414 -0
  46. package/http-client/services/index.d.ts +7 -0
  47. package/http-client/services/index.js +23 -0
  48. package/http-client/services/log-cleanup.service.d.ts +64 -0
  49. package/http-client/services/log-cleanup.service.js +268 -0
  50. package/http-client/services/logging.service.d.ts +36 -0
  51. package/http-client/services/logging.service.js +445 -0
  52. package/http-client/utils/call-stack-extractor.util.d.ts +29 -0
  53. package/http-client/utils/call-stack-extractor.util.js +138 -0
  54. package/http-client/utils/context-extractor.util.d.ts +44 -0
  55. package/http-client/utils/context-extractor.util.js +173 -0
  56. package/http-client/utils/curl-generator.util.d.ts +9 -0
  57. package/http-client/utils/curl-generator.util.js +169 -0
  58. package/http-client/utils/index.d.ts +4 -0
  59. package/http-client/utils/index.js +20 -0
  60. package/http-client/utils/retry-recorder.util.d.ts +30 -0
  61. package/http-client/utils/retry-recorder.util.js +143 -0
  62. package/index.d.ts +1 -0
  63. package/index.js +1 -0
  64. package/package.json +1 -1
@@ -0,0 +1,268 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
12
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
13
+ return new (P || (P = Promise))(function (resolve, reject) {
14
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
15
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
16
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
17
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
18
+ });
19
+ };
20
+ var HttpLogCleanupService_1;
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.HttpLogCleanupService = void 0;
23
+ const common_1 = require("@nestjs/common");
24
+ const schedule_1 = require("@nestjs/schedule");
25
+ const http_log_query_service_1 = require("./http-log-query.service");
26
+ const setup_1 = require("../../setup");
27
+ let HttpLogCleanupService = HttpLogCleanupService_1 = class HttpLogCleanupService {
28
+ constructor(logQueryService, config = {}) {
29
+ this.logQueryService = logQueryService;
30
+ this.logger = new common_1.Logger(HttpLogCleanupService_1.name);
31
+ this.defaultConfig = {
32
+ enabled: true,
33
+ strategy: 'retention',
34
+ retentionDays: 30,
35
+ maxRecords: 1000000,
36
+ schedule: schedule_1.CronExpression.EVERY_DAY_AT_2AM,
37
+ batchSize: 10000,
38
+ preserveErrorLogs: true,
39
+ errorLogRetentionDays: 90,
40
+ preserveImportantLogs: true,
41
+ importantLogRetentionDays: 60,
42
+ preserveSlowRequests: true,
43
+ slowRequestRetentionDays: 60,
44
+ slowRequestThreshold: 5000,
45
+ };
46
+ if (Object.keys(config).length > 0) {
47
+ Object.assign(this.defaultConfig, config);
48
+ }
49
+ }
50
+ get cleanupConfig() {
51
+ return this.defaultConfig;
52
+ }
53
+ onModuleInit() {
54
+ if (this.cleanupConfig.enabled) {
55
+ this.logger.log(`HTTP log cleanup enabled with strategy: ${this.cleanupConfig.strategy}`);
56
+ this.logger.log(`Schedule: ${this.cleanupConfig.schedule}`);
57
+ }
58
+ else {
59
+ this.logger.warn('HTTP log cleanup is disabled');
60
+ }
61
+ }
62
+ scheduledCleanup() {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ if (!this.cleanupConfig.enabled) {
65
+ return this.createCleanupStats('scheduled', 0, 0, 0, 0, []);
66
+ }
67
+ this.logger.log('Starting scheduled HTTP log cleanup...');
68
+ return this.performCleanup();
69
+ });
70
+ }
71
+ manualCleanup(options) {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ const config = Object.assign(Object.assign({}, this.cleanupConfig), options);
74
+ if (!config.enabled) {
75
+ throw new Error('Log cleanup is disabled');
76
+ }
77
+ this.logger.log(`Starting manual HTTP log cleanup with strategy: ${config.strategy}`);
78
+ return this.performCleanup(config);
79
+ });
80
+ }
81
+ getConfig() {
82
+ return this.cleanupConfig;
83
+ }
84
+ updateConfig(config) {
85
+ Object.assign(this.defaultConfig, config);
86
+ this.logger.log('Cleanup configuration updated');
87
+ }
88
+ getCleanupStatus() {
89
+ return __awaiter(this, void 0, void 0, function* () {
90
+ const config = this.cleanupConfig;
91
+ const stats = yield this.logQueryService.getStats();
92
+ const recommendations = [];
93
+ if (stats.totalRequests > (config.maxRecords || Infinity) * 0.9) {
94
+ recommendations.push('Current records approaching limit, consider adjusting retention policy');
95
+ }
96
+ if (stats.averageResponseTime > 1000) {
97
+ recommendations.push('High average response time detected, consider performance optimization');
98
+ }
99
+ if (stats.failedRequests / stats.totalRequests > 0.1) {
100
+ recommendations.push('High error rate detected, consider error handling improvements');
101
+ }
102
+ return {
103
+ enabled: config.enabled,
104
+ config,
105
+ currentStats: stats,
106
+ recommendations,
107
+ };
108
+ });
109
+ }
110
+ previewCleanup(config) {
111
+ return __awaiter(this, void 0, void 0, function* () {
112
+ const finalConfig = Object.assign(Object.assign({}, this.cleanupConfig), config);
113
+ const stats = yield this.logQueryService.getStats();
114
+ const cutoffDate = new Date();
115
+ cutoffDate.setDate(cutoffDate.getDate() - finalConfig.retentionDays);
116
+ return {
117
+ strategy: finalConfig.strategy,
118
+ currentRecords: stats.totalRequests,
119
+ estimatedDeletion: Math.max(0, stats.totalRequests - finalConfig.maxRecords || Infinity),
120
+ estimatedAfterCount: Math.min(stats.totalRequests, finalConfig.maxRecords || Infinity),
121
+ cutoffDate,
122
+ breakdown: {
123
+ normalLogs: Math.floor(stats.totalRequests * 0.7),
124
+ errorLogs: Math.floor(stats.totalRequests * 0.2),
125
+ slowRequests: Math.floor(stats.totalRequests * 0.05),
126
+ importantLogs: Math.floor(stats.totalRequests * 0.05),
127
+ },
128
+ };
129
+ });
130
+ }
131
+ performCleanup() {
132
+ return __awaiter(this, arguments, void 0, function* (config = this.cleanupConfig) {
133
+ const startTime = Date.now();
134
+ const errors = [];
135
+ let deletedCount = 0;
136
+ const batchCount = 0;
137
+ try {
138
+ const beforeStats = yield this.logQueryService.getStats();
139
+ const beforeCount = beforeStats.totalRequests;
140
+ switch (config.strategy) {
141
+ case 'retention':
142
+ deletedCount = yield this.cleanupByRetention(config, errors);
143
+ break;
144
+ case 'size':
145
+ deletedCount = yield this.cleanupBySize(config, errors);
146
+ break;
147
+ case 'combination':
148
+ deletedCount = yield this.cleanupByCombination(config, errors);
149
+ break;
150
+ default:
151
+ throw new Error(`Unknown cleanup strategy: ${config.strategy}`);
152
+ }
153
+ const afterStats = yield this.logQueryService.getStats();
154
+ const afterCount = afterStats.totalRequests;
155
+ const duration = Date.now() - startTime;
156
+ const stats = {
157
+ cleanupTime: new Date(),
158
+ strategy: config.strategy,
159
+ beforeCount,
160
+ afterCount,
161
+ deletedCount,
162
+ batchCount,
163
+ duration,
164
+ errors,
165
+ };
166
+ this.logger.log(`Cleanup completed: deleted ${deletedCount} records in ${duration}ms`);
167
+ if (errors.length > 0) {
168
+ this.logger.warn(`Cleanup completed with ${errors.length} errors`, errors);
169
+ }
170
+ return stats;
171
+ }
172
+ catch (error) {
173
+ const duration = Date.now() - startTime;
174
+ this.logger.error(`Cleanup failed after ${duration}ms`, error);
175
+ throw error;
176
+ }
177
+ });
178
+ }
179
+ cleanupByRetention(config, errors) {
180
+ return __awaiter(this, void 0, void 0, function* () {
181
+ let totalDeleted = 0;
182
+ const cutoffDate = new Date();
183
+ cutoffDate.setDate(cutoffDate.getDate() - config.retentionDays);
184
+ try {
185
+ const normalDeleted = yield this.logQueryService.deleteOldLogs(config.retentionDays);
186
+ totalDeleted += normalDeleted;
187
+ }
188
+ catch (error) {
189
+ errors.push(`Retention cleanup failed: ${error.message}`);
190
+ return 0;
191
+ }
192
+ if (config.preserveErrorLogs &&
193
+ config.errorLogRetentionDays > config.retentionDays) {
194
+ this.logger.log(`Preserving error logs for ${config.errorLogRetentionDays} days`);
195
+ }
196
+ if (config.preserveSlowRequests &&
197
+ config.slowRequestRetentionDays > config.retentionDays) {
198
+ this.logger.log(`Preserving slow requests for ${config.slowRequestRetentionDays} days`);
199
+ }
200
+ return totalDeleted;
201
+ });
202
+ }
203
+ cleanupBySize(config, errors) {
204
+ return __awaiter(this, void 0, void 0, function* () {
205
+ if (!config.maxRecords) {
206
+ errors.push('Max records not configured for size-based cleanup');
207
+ return 0;
208
+ }
209
+ const stats = yield this.logQueryService.getStats();
210
+ if (stats.totalRequests <= config.maxRecords) {
211
+ this.logger.log(`Current records (${stats.totalRequests}) within limit (${config.maxRecords})`);
212
+ return 0;
213
+ }
214
+ const excessCount = stats.totalRequests - config.maxRecords;
215
+ const targetCutoffDate = new Date();
216
+ const averageDailyRecords = stats.totalRequests / 30;
217
+ const daysToDelete = Math.ceil(excessCount / averageDailyRecords);
218
+ targetCutoffDate.setDate(targetCutoffDate.getDate() - daysToDelete);
219
+ try {
220
+ const deleted = yield this.logQueryService.deleteOldLogs(Math.ceil((Date.now() - targetCutoffDate.getTime()) / (24 * 60 * 60 * 1000)));
221
+ this.logger.log(`Size-based cleanup: deleted ${deleted} records, cutoff date: ${targetCutoffDate.toISOString()}`);
222
+ return deleted;
223
+ }
224
+ catch (error) {
225
+ errors.push(`Size-based cleanup failed: ${error.message}`);
226
+ return 0;
227
+ }
228
+ });
229
+ }
230
+ cleanupByCombination(config, errors) {
231
+ return __awaiter(this, void 0, void 0, function* () {
232
+ let totalDeleted = 0;
233
+ const retentionDeleted = yield this.cleanupByRetention(config, errors);
234
+ totalDeleted += retentionDeleted;
235
+ if (config.maxRecords) {
236
+ const stats = yield this.logQueryService.getStats();
237
+ if (stats.totalRequests > config.maxRecords) {
238
+ const sizeDeleted = yield this.cleanupBySize(config, errors);
239
+ totalDeleted += sizeDeleted;
240
+ }
241
+ }
242
+ return totalDeleted;
243
+ });
244
+ }
245
+ createCleanupStats(strategy, beforeCount, afterCount, deletedCount, batchCount, errors) {
246
+ return {
247
+ cleanupTime: new Date(),
248
+ strategy,
249
+ beforeCount,
250
+ afterCount,
251
+ deletedCount,
252
+ batchCount,
253
+ duration: 0,
254
+ errors,
255
+ };
256
+ }
257
+ };
258
+ exports.HttpLogCleanupService = HttpLogCleanupService;
259
+ __decorate([
260
+ (0, setup_1.WorkerCronSmart)(schedule_1.CronExpression.EVERY_DAY_AT_2AM),
261
+ __metadata("design:type", Function),
262
+ __metadata("design:paramtypes", []),
263
+ __metadata("design:returntype", Promise)
264
+ ], HttpLogCleanupService.prototype, "scheduledCleanup", null);
265
+ exports.HttpLogCleanupService = HttpLogCleanupService = HttpLogCleanupService_1 = __decorate([
266
+ (0, common_1.Injectable)(),
267
+ __metadata("design:paramtypes", [http_log_query_service_1.HttpLogQueryService, Object])
268
+ ], HttpLogCleanupService);
@@ -0,0 +1,36 @@
1
+ import { DataSource } from 'typeorm';
2
+ import { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';
3
+ import { HttpLogEntity } from '../entities/http-log.entity';
4
+ export declare class HttpLoggingService {
5
+ private readonly dataSource?;
6
+ private readonly logger;
7
+ private logRepository;
8
+ constructor(dataSource?: DataSource);
9
+ logRequestStart(config: AxiosRequestConfig, loggingOptions: any, requestId?: string): string;
10
+ logRequestSuccess(response: AxiosResponse, startTime: number, requestId: string, loggingOptions: any, databaseLogging?: boolean, retryRecords?: any[], cacheHit?: boolean, circuitBreakerState?: string, decoratorContext?: any): void;
11
+ logRequestError(error: AxiosError, startTime: number, requestId: string, attemptCount: number, loggingOptions: any, databaseLogging?: boolean, retryRecords?: any[], cacheHit?: boolean, circuitBreakerState?: string, decoratorContext?: any): void;
12
+ findLogs(options: {
13
+ requestId?: string;
14
+ userId?: string;
15
+ method?: string;
16
+ url?: string;
17
+ statusCode?: number;
18
+ success?: boolean;
19
+ startDate?: Date;
20
+ endDate?: Date;
21
+ limit?: number;
22
+ offset?: number;
23
+ }): Promise<HttpLogEntity[]>;
24
+ getStatistics(options: {
25
+ startDate?: Date;
26
+ endDate?: Date;
27
+ userId?: string;
28
+ }): Promise<any>;
29
+ private initializeDatabaseLogging;
30
+ private extractCallInfo;
31
+ private saveToDatabase;
32
+ private sanitizeBodyAsString;
33
+ private sanitizeHeaders;
34
+ private sanitizeBody;
35
+ private sanitizeObject;
36
+ }