@nest-omni/core 3.1.2-7 → 3.1.2-9
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/audit/audit.module.d.ts +10 -0
- package/audit/audit.module.js +254 -0
- package/audit/controllers/audit.controller.d.ts +20 -0
- package/audit/controllers/audit.controller.js +142 -0
- package/audit/controllers/index.d.ts +1 -0
- package/audit/controllers/index.js +17 -0
- package/audit/decorators/audit-controller.decorator.d.ts +5 -0
- package/audit/decorators/audit-controller.decorator.js +17 -0
- package/audit/decorators/audit-operation.decorator.d.ts +7 -0
- package/audit/decorators/audit-operation.decorator.js +25 -0
- package/audit/decorators/entity-audit.decorator.d.ts +10 -0
- package/audit/decorators/entity-audit.decorator.js +70 -0
- package/audit/decorators/index.d.ts +3 -0
- package/audit/decorators/index.js +19 -0
- package/audit/dto/audit-log-query.dto.d.ts +14 -0
- package/audit/dto/audit-log-query.dto.js +95 -0
- package/audit/dto/begin-transaction.dto.d.ts +3 -0
- package/audit/dto/begin-transaction.dto.js +22 -0
- package/audit/dto/compare-entities.dto.d.ts +6 -0
- package/audit/dto/compare-entities.dto.js +44 -0
- package/audit/dto/index.d.ts +5 -0
- package/audit/dto/index.js +21 -0
- package/audit/dto/pre-check-restore.dto.d.ts +5 -0
- package/audit/dto/pre-check-restore.dto.js +32 -0
- package/audit/dto/restore-entity.dto.d.ts +9 -0
- package/audit/dto/restore-entity.dto.js +53 -0
- package/audit/entities/entity-audit-log.entity.d.ts +23 -0
- package/audit/entities/entity-audit-log.entity.js +110 -0
- package/audit/entities/entity-transaction.entity.d.ts +21 -0
- package/audit/entities/entity-transaction.entity.js +80 -0
- package/audit/entities/index.d.ts +4 -0
- package/audit/entities/index.js +20 -0
- package/audit/entities/manual-operation-log.entity.d.ts +13 -0
- package/audit/entities/manual-operation-log.entity.js +65 -0
- package/audit/entities/operation-template.entity.d.ts +11 -0
- package/audit/entities/operation-template.entity.js +65 -0
- package/audit/enums/audit.enums.d.ts +42 -0
- package/audit/enums/audit.enums.js +53 -0
- package/audit/enums/index.d.ts +1 -0
- package/audit/enums/index.js +17 -0
- package/audit/index.d.ts +12 -0
- package/audit/index.js +48 -0
- package/audit/interceptors/audit.interceptor.d.ts +12 -0
- package/audit/interceptors/audit.interceptor.js +95 -0
- package/audit/interceptors/index.d.ts +1 -0
- package/audit/interceptors/index.js +17 -0
- package/audit/interfaces/audit.interfaces.d.ts +180 -0
- package/audit/interfaces/audit.interfaces.js +2 -0
- package/audit/interfaces/index.d.ts +1 -0
- package/audit/interfaces/index.js +17 -0
- package/audit/services/audit-context.service.d.ts +10 -0
- package/audit/services/audit-context.service.js +55 -0
- package/audit/services/audit-strategy.service.d.ts +19 -0
- package/audit/services/audit-strategy.service.js +89 -0
- package/audit/services/entity-audit.service.d.ts +65 -0
- package/audit/services/entity-audit.service.js +626 -0
- package/audit/services/index.d.ts +6 -0
- package/audit/services/index.js +22 -0
- package/audit/services/multi-database.service.d.ts +10 -0
- package/audit/services/multi-database.service.js +59 -0
- package/audit/services/operation-description.service.d.ts +21 -0
- package/audit/services/operation-description.service.js +213 -0
- package/audit/services/transaction-audit.service.d.ts +22 -0
- package/audit/services/transaction-audit.service.js +201 -0
- package/audit/subscribers/entity-audit.subscriber.d.ts +14 -0
- package/audit/subscribers/entity-audit.subscriber.js +136 -0
- package/audit/subscribers/index.d.ts +1 -0
- package/audit/subscribers/index.js +17 -0
- package/common/utils.d.ts +1 -0
- package/common/utils.js +6 -0
- package/http-client/config/http-client.config.d.ts +6 -0
- package/http-client/config/http-client.config.js +87 -0
- package/http-client/config/index.d.ts +1 -0
- package/http-client/config/index.js +17 -0
- package/http-client/decorators/http-client.decorators.d.ts +72 -0
- package/http-client/decorators/http-client.decorators.js +204 -0
- package/http-client/decorators/index.d.ts +1 -0
- package/http-client/decorators/index.js +17 -0
- package/http-client/entities/http-log.entity.d.ts +98 -0
- package/http-client/entities/http-log.entity.js +143 -0
- package/http-client/entities/index.d.ts +1 -0
- package/http-client/entities/index.js +17 -0
- package/http-client/errors/http-client.errors.d.ts +56 -0
- package/http-client/errors/http-client.errors.js +149 -0
- package/http-client/errors/index.d.ts +1 -0
- package/http-client/errors/index.js +17 -0
- package/http-client/examples/advanced-usage.example.d.ts +23 -0
- package/http-client/examples/advanced-usage.example.js +332 -0
- package/http-client/examples/auth-with-waiting-lock.example.d.ts +17 -0
- package/http-client/examples/auth-with-waiting-lock.example.js +336 -0
- package/http-client/examples/basic-usage.example.d.ts +53 -0
- package/http-client/examples/basic-usage.example.js +161 -0
- package/http-client/examples/index.d.ts +3 -0
- package/http-client/examples/index.js +19 -0
- package/http-client/examples/multi-api-configuration.example.d.ts +98 -0
- package/http-client/examples/multi-api-configuration.example.js +353 -0
- package/http-client/http-client.module.d.ts +11 -0
- package/http-client/http-client.module.js +257 -0
- package/http-client/index.d.ts +10 -0
- package/http-client/index.js +27 -0
- package/http-client/interfaces/api-client-config.interface.d.ts +152 -0
- package/http-client/interfaces/api-client-config.interface.js +12 -0
- package/http-client/interfaces/http-client-config.interface.d.ts +123 -0
- package/http-client/interfaces/http-client-config.interface.js +2 -0
- package/http-client/services/api-client-registry.service.d.ts +41 -0
- package/http-client/services/api-client-registry.service.js +412 -0
- package/http-client/services/cache.service.d.ts +24 -0
- package/http-client/services/cache.service.js +264 -0
- package/http-client/services/circuit-breaker.service.d.ts +33 -0
- package/http-client/services/circuit-breaker.service.js +180 -0
- package/http-client/services/http-client.service.d.ts +60 -0
- package/http-client/services/http-client.service.js +504 -0
- package/http-client/services/http-log-query.service.d.ts +76 -0
- package/http-client/services/http-log-query.service.js +590 -0
- package/http-client/services/http-replay.service.d.ts +58 -0
- package/http-client/services/http-replay.service.js +266 -0
- package/http-client/services/index.d.ts +7 -0
- package/http-client/services/index.js +23 -0
- package/http-client/services/log-cleanup.service.d.ts +64 -0
- package/http-client/services/log-cleanup.service.js +268 -0
- package/http-client/services/logging.service.d.ts +36 -0
- package/http-client/services/logging.service.js +445 -0
- package/http-client/utils/call-stack-extractor.util.d.ts +29 -0
- package/http-client/utils/call-stack-extractor.util.js +138 -0
- package/http-client/utils/context-extractor.util.d.ts +44 -0
- package/http-client/utils/context-extractor.util.js +173 -0
- package/http-client/utils/curl-generator.util.d.ts +9 -0
- package/http-client/utils/curl-generator.util.js +169 -0
- package/http-client/utils/index.d.ts +4 -0
- package/http-client/utils/index.js +20 -0
- package/http-client/utils/request-id.util.d.ts +4 -0
- package/http-client/utils/request-id.util.js +34 -0
- package/http-client/utils/retry-recorder.util.d.ts +30 -0
- package/http-client/utils/retry-recorder.util.js +143 -0
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/package.json +1 -1
- package/setup/bootstrap.setup.js +5 -1
|
@@ -0,0 +1,412 @@
|
|
|
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 ApiClientRegistryService_1;
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.ApiClientRegistryService = exports.AuthType = void 0;
|
|
23
|
+
const common_1 = require("@nestjs/common");
|
|
24
|
+
const axios_1 = require("axios");
|
|
25
|
+
const axios_retry_1 = require("axios-retry");
|
|
26
|
+
const api_client_config_interface_1 = require("../interfaces/api-client-config.interface");
|
|
27
|
+
Object.defineProperty(exports, "AuthType", { enumerable: true, get: function () { return api_client_config_interface_1.AuthType; } });
|
|
28
|
+
const http_client_service_1 = require("./http-client.service");
|
|
29
|
+
const circuit_breaker_service_1 = require("./circuit-breaker.service");
|
|
30
|
+
const logging_service_1 = require("./logging.service");
|
|
31
|
+
const cache_service_1 = require("./cache.service");
|
|
32
|
+
const context_extractor_util_1 = require("../utils/context-extractor.util");
|
|
33
|
+
const utils_1 = require("../../common/utils");
|
|
34
|
+
let ApiClientRegistryService = ApiClientRegistryService_1 = class ApiClientRegistryService {
|
|
35
|
+
constructor(httpClientService, circuitBreakerService, loggingService, cacheService, globalConfig = {}) {
|
|
36
|
+
this.httpClientService = httpClientService;
|
|
37
|
+
this.circuitBreakerService = circuitBreakerService;
|
|
38
|
+
this.loggingService = loggingService;
|
|
39
|
+
this.cacheService = cacheService;
|
|
40
|
+
this.logger = new common_1.Logger(ApiClientRegistryService_1.name);
|
|
41
|
+
this.clients = new Map();
|
|
42
|
+
this.clientConfigs = new Map();
|
|
43
|
+
this.globalDefaults = Object.assign({ globalDefaults: {
|
|
44
|
+
retry: { enabled: true, retries: 3 },
|
|
45
|
+
logging: {
|
|
46
|
+
enabled: true,
|
|
47
|
+
logRequests: true,
|
|
48
|
+
logResponses: true,
|
|
49
|
+
logErrors: true,
|
|
50
|
+
logHeaders: true,
|
|
51
|
+
logBody: true,
|
|
52
|
+
maxBodyLength: 10000,
|
|
53
|
+
sanitizeHeaders: ['authorization', 'api-key'],
|
|
54
|
+
logLevel: 'info',
|
|
55
|
+
},
|
|
56
|
+
timeout: 30000,
|
|
57
|
+
}, logCleanup: {
|
|
58
|
+
enabled: true,
|
|
59
|
+
retentionDays: 30,
|
|
60
|
+
maxRecords: 1000000,
|
|
61
|
+
}, enableGlobalRetry: true, enableCircuitBreaker: true }, globalConfig);
|
|
62
|
+
}
|
|
63
|
+
createClient(config) {
|
|
64
|
+
const clientName = config.name;
|
|
65
|
+
const existingClient = this.clients.get(clientName);
|
|
66
|
+
if (existingClient && !config.override) {
|
|
67
|
+
this.logger.warn(`Client '${clientName}' already exists. Use override: true to replace it.`);
|
|
68
|
+
return existingClient;
|
|
69
|
+
}
|
|
70
|
+
const finalConfig = this.mergeEnvironmentConfig(config);
|
|
71
|
+
const axiosInstance = this.createAxiosInstance(finalConfig);
|
|
72
|
+
const client = new ApiClientInstance(clientName, finalConfig, axiosInstance, this.httpClientService, this.loggingService, this.cacheService);
|
|
73
|
+
this.clients.set(clientName, client);
|
|
74
|
+
this.clientConfigs.set(clientName, finalConfig);
|
|
75
|
+
this.logger.log(`Created API client: ${clientName}`);
|
|
76
|
+
return client;
|
|
77
|
+
}
|
|
78
|
+
getClient(name) {
|
|
79
|
+
const client = this.clients.get(name);
|
|
80
|
+
if (!client) {
|
|
81
|
+
throw new Error(`API client '${name}' not found. Available clients: ${Array.from(this.clients.keys()).join(', ')}`);
|
|
82
|
+
}
|
|
83
|
+
return client;
|
|
84
|
+
}
|
|
85
|
+
listClients() {
|
|
86
|
+
return Array.from(this.clients.keys());
|
|
87
|
+
}
|
|
88
|
+
updateClient(name, config) {
|
|
89
|
+
const existingConfig = this.clientConfigs.get(name);
|
|
90
|
+
if (!existingConfig) {
|
|
91
|
+
throw new Error(`API client '${name}' not found`);
|
|
92
|
+
}
|
|
93
|
+
const updatedConfig = Object.assign(Object.assign({}, existingConfig), config);
|
|
94
|
+
this.clientConfigs.set(name, updatedConfig);
|
|
95
|
+
this.createClient(Object.assign(Object.assign({}, updatedConfig), { name, override: true }));
|
|
96
|
+
this.logger.log(`Updated API client: ${name}`);
|
|
97
|
+
}
|
|
98
|
+
removeClient(name) {
|
|
99
|
+
const removed = this.clients.delete(name);
|
|
100
|
+
this.clientConfigs.delete(name);
|
|
101
|
+
if (removed) {
|
|
102
|
+
this.logger.log(`Removed API client: ${name}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
getClientConfig(name) {
|
|
106
|
+
return this.clientConfigs.get(name);
|
|
107
|
+
}
|
|
108
|
+
getAllClientConfigs() {
|
|
109
|
+
const configs = {};
|
|
110
|
+
this.clientConfigs.forEach((config, name) => {
|
|
111
|
+
configs[name] = config;
|
|
112
|
+
});
|
|
113
|
+
return configs;
|
|
114
|
+
}
|
|
115
|
+
createClients(configs) {
|
|
116
|
+
configs.forEach((config) => {
|
|
117
|
+
try {
|
|
118
|
+
this.createClient(config);
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
this.logger.error(`Failed to create client '${config.name}':`, error);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
getGlobalConfig() {
|
|
126
|
+
return this.globalDefaults;
|
|
127
|
+
}
|
|
128
|
+
updateGlobalConfig(config) {
|
|
129
|
+
Object.assign(this.globalDefaults, config);
|
|
130
|
+
this.logger.log('Global configuration updated');
|
|
131
|
+
}
|
|
132
|
+
getAllStats() {
|
|
133
|
+
const stats = {};
|
|
134
|
+
this.clients.forEach((client, name) => {
|
|
135
|
+
stats[name] = client.getStats();
|
|
136
|
+
});
|
|
137
|
+
return stats;
|
|
138
|
+
}
|
|
139
|
+
resetAllStats() {
|
|
140
|
+
this.clients.forEach((client) => {
|
|
141
|
+
client.resetStats();
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
healthCheck() {
|
|
145
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
146
|
+
const results = {};
|
|
147
|
+
let overallStatus = 'healthy';
|
|
148
|
+
for (const [name, client] of this.clients.entries()) {
|
|
149
|
+
try {
|
|
150
|
+
client.getStats();
|
|
151
|
+
results[name] = { status: 'healthy' };
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
results[name] = { status: 'unhealthy', error: error.message };
|
|
155
|
+
overallStatus = 'unhealthy';
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
status: overallStatus,
|
|
160
|
+
clients: results,
|
|
161
|
+
};
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
createAxiosInstance(config) {
|
|
165
|
+
var _a, _b, _c, _d, _e, _f;
|
|
166
|
+
const axiosConfig = {
|
|
167
|
+
baseURL: config.baseURL,
|
|
168
|
+
timeout: ((_a = config.httpConfig) === null || _a === void 0 ? void 0 : _a.timeout) || 30000,
|
|
169
|
+
headers: Object.assign({ 'Content-Type': 'application/json', 'User-Agent': `${config.name}/1.0.0` }, config.defaultHeaders),
|
|
170
|
+
params: config.defaultParams,
|
|
171
|
+
};
|
|
172
|
+
const instance = axios_1.default.create(axiosConfig);
|
|
173
|
+
const clientName = config.name;
|
|
174
|
+
instance.interceptors.request.use((config) => {
|
|
175
|
+
const authenticatedConfig = config;
|
|
176
|
+
if (config.auth) {
|
|
177
|
+
switch (config.auth.type) {
|
|
178
|
+
case api_client_config_interface_1.AuthType.BEARER_TOKEN:
|
|
179
|
+
const bearerConfig = config.auth.config;
|
|
180
|
+
authenticatedConfig.headers = authenticatedConfig.headers || {};
|
|
181
|
+
authenticatedConfig.headers['Authorization'] =
|
|
182
|
+
`Bearer ${bearerConfig.token}`;
|
|
183
|
+
break;
|
|
184
|
+
case api_client_config_interface_1.AuthType.API_KEY:
|
|
185
|
+
const apiKeyConfig = config.auth.config;
|
|
186
|
+
if (apiKeyConfig.location === 'header') {
|
|
187
|
+
const headerName = apiKeyConfig.name || 'X-API-Key';
|
|
188
|
+
const value = apiKeyConfig.prefix
|
|
189
|
+
? `${apiKeyConfig.prefix} ${apiKeyConfig.key}`
|
|
190
|
+
: apiKeyConfig.key;
|
|
191
|
+
authenticatedConfig.headers = authenticatedConfig.headers || {};
|
|
192
|
+
authenticatedConfig.headers[headerName] = value;
|
|
193
|
+
}
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const context = context_extractor_util_1.ContextExtractor.getHttpContext();
|
|
198
|
+
const requestId = context.requestId || (0, utils_1.generateRequestId)();
|
|
199
|
+
authenticatedConfig.headers = authenticatedConfig.headers || {};
|
|
200
|
+
authenticatedConfig.headers['X-Request-ID'] = requestId;
|
|
201
|
+
if (context.userId) {
|
|
202
|
+
authenticatedConfig.headers['X-User-ID'] = context.userId;
|
|
203
|
+
}
|
|
204
|
+
authenticatedConfig.headers['X-API-Client'] = clientName;
|
|
205
|
+
return authenticatedConfig;
|
|
206
|
+
}, (error) => {
|
|
207
|
+
this.logger.error(`[${clientName}] Request interceptor error`, error);
|
|
208
|
+
return Promise.reject(error);
|
|
209
|
+
});
|
|
210
|
+
instance.interceptors.response.use((response) => {
|
|
211
|
+
return this.applyResponseTransformer(response, config.responseTransformer);
|
|
212
|
+
}, (error) => {
|
|
213
|
+
var _a;
|
|
214
|
+
if ((_a = config.responseTransformer) === null || _a === void 0 ? void 0 : _a.error) {
|
|
215
|
+
return config.responseTransformer.error(error);
|
|
216
|
+
}
|
|
217
|
+
return Promise.reject(error);
|
|
218
|
+
});
|
|
219
|
+
if (((_b = config.retry) === null || _b === void 0 ? void 0 : _b.enabled) !== false &&
|
|
220
|
+
this.globalDefaults.enableGlobalRetry) {
|
|
221
|
+
(0, axios_retry_1.default)(instance, {
|
|
222
|
+
retries: ((_c = config.retry) === null || _c === void 0 ? void 0 : _c.retries) ||
|
|
223
|
+
((_e = (_d = this.globalDefaults.globalDefaults) === null || _d === void 0 ? void 0 : _d.retry) === null || _e === void 0 ? void 0 : _e.retries) ||
|
|
224
|
+
3,
|
|
225
|
+
retryDelay: (retryCount) => Math.pow(2, retryCount) * 1000,
|
|
226
|
+
retryCondition: ((_f = config.retry) === null || _f === void 0 ? void 0 : _f.retryCondition) ||
|
|
227
|
+
((error) => {
|
|
228
|
+
if (!error.response)
|
|
229
|
+
return true;
|
|
230
|
+
const status = error.response.status;
|
|
231
|
+
return status >= 500 || status === 429;
|
|
232
|
+
}),
|
|
233
|
+
shouldResetTimeout: true,
|
|
234
|
+
onRetry: (retryCount, error, requestConfig) => {
|
|
235
|
+
var _a;
|
|
236
|
+
this.logger.warn(`[${config.name}] Retrying request (attempt ${retryCount}): ${(_a = requestConfig.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()} ${requestConfig.url}`);
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
return instance;
|
|
241
|
+
}
|
|
242
|
+
applyAuthentication(config, auth) {
|
|
243
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
244
|
+
if (!auth) {
|
|
245
|
+
return config;
|
|
246
|
+
}
|
|
247
|
+
const authenticatedConfig = Object.assign({}, config);
|
|
248
|
+
switch (auth.type) {
|
|
249
|
+
case api_client_config_interface_1.AuthType.NONE:
|
|
250
|
+
break;
|
|
251
|
+
case api_client_config_interface_1.AuthType.API_KEY:
|
|
252
|
+
const apiKeyConfig = auth.config;
|
|
253
|
+
if (apiKeyConfig.location === 'header') {
|
|
254
|
+
const headerName = apiKeyConfig.name || 'X-API-Key';
|
|
255
|
+
const value = apiKeyConfig.prefix
|
|
256
|
+
? `${apiKeyConfig.prefix} ${apiKeyConfig.key}`
|
|
257
|
+
: apiKeyConfig.key;
|
|
258
|
+
authenticatedConfig.headers = Object.assign(Object.assign({}, authenticatedConfig.headers), { [headerName]: value });
|
|
259
|
+
}
|
|
260
|
+
else if (apiKeyConfig.location === 'query') {
|
|
261
|
+
const paramName = apiKeyConfig.name || 'api_key';
|
|
262
|
+
authenticatedConfig.params = Object.assign(Object.assign({}, authenticatedConfig.params), { [paramName]: apiKeyConfig.key });
|
|
263
|
+
}
|
|
264
|
+
break;
|
|
265
|
+
case api_client_config_interface_1.AuthType.BEARER_TOKEN:
|
|
266
|
+
const bearerConfig = auth.config;
|
|
267
|
+
const scheme = bearerConfig.scheme || 'Bearer';
|
|
268
|
+
authenticatedConfig.headers = Object.assign(Object.assign({}, authenticatedConfig.headers), { Authorization: `${scheme} ${bearerConfig.token}` });
|
|
269
|
+
break;
|
|
270
|
+
case api_client_config_interface_1.AuthType.BASIC_AUTH:
|
|
271
|
+
const basicConfig = auth.config;
|
|
272
|
+
const credentials = Buffer.from(`${basicConfig.username}:${basicConfig.password}`).toString('base64');
|
|
273
|
+
authenticatedConfig.headers = Object.assign(Object.assign({}, authenticatedConfig.headers), { Authorization: `Basic ${credentials}` });
|
|
274
|
+
break;
|
|
275
|
+
case api_client_config_interface_1.AuthType.OAUTH2:
|
|
276
|
+
const oauthConfig = auth.config;
|
|
277
|
+
authenticatedConfig.headers = Object.assign(Object.assign({}, authenticatedConfig.headers), { Authorization: `Bearer ${oauthConfig.clientSecret}` });
|
|
278
|
+
break;
|
|
279
|
+
case api_client_config_interface_1.AuthType.CUSTOM:
|
|
280
|
+
const customConfig = auth.config;
|
|
281
|
+
return yield customConfig.authenticator(authenticatedConfig);
|
|
282
|
+
}
|
|
283
|
+
return authenticatedConfig;
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
applyResponseTransformer(response, transformer) {
|
|
287
|
+
if (!transformer) {
|
|
288
|
+
return response;
|
|
289
|
+
}
|
|
290
|
+
if (transformer.success) {
|
|
291
|
+
response.data = transformer.success(response);
|
|
292
|
+
}
|
|
293
|
+
if (transformer.dataPath) {
|
|
294
|
+
response.data = this.extractDataByPath(response.data, transformer.dataPath);
|
|
295
|
+
}
|
|
296
|
+
if (transformer.validateResponse && transformer.validator) {
|
|
297
|
+
if (!transformer.validator(response.data)) {
|
|
298
|
+
throw new Error(`Response validation failed for ${transformer.dataPath}`);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return response;
|
|
302
|
+
}
|
|
303
|
+
extractDataByPath(data, path) {
|
|
304
|
+
const keys = path.split('.');
|
|
305
|
+
let current = data;
|
|
306
|
+
for (const key of keys) {
|
|
307
|
+
if (current === null || current === undefined) {
|
|
308
|
+
return undefined;
|
|
309
|
+
}
|
|
310
|
+
current = current[key];
|
|
311
|
+
}
|
|
312
|
+
return current;
|
|
313
|
+
}
|
|
314
|
+
mergeEnvironmentConfig(config) {
|
|
315
|
+
var _a;
|
|
316
|
+
const env = process.env.NODE_ENV || 'development';
|
|
317
|
+
const environmentConfig = ((_a = config.environments) === null || _a === void 0 ? void 0 : _a[env]) || {};
|
|
318
|
+
return Object.assign(Object.assign({}, config), environmentConfig);
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
exports.ApiClientRegistryService = ApiClientRegistryService;
|
|
322
|
+
exports.ApiClientRegistryService = ApiClientRegistryService = ApiClientRegistryService_1 = __decorate([
|
|
323
|
+
(0, common_1.Injectable)(),
|
|
324
|
+
__metadata("design:paramtypes", [http_client_service_1.HttpClientService,
|
|
325
|
+
circuit_breaker_service_1.HttpCircuitBreakerService,
|
|
326
|
+
logging_service_1.HttpLoggingService,
|
|
327
|
+
cache_service_1.HttpCacheService, Object])
|
|
328
|
+
], ApiClientRegistryService);
|
|
329
|
+
class ApiClientInstance {
|
|
330
|
+
constructor(name, config, axiosInstance, httpClientService, loggingService, cacheService) {
|
|
331
|
+
this.name = name;
|
|
332
|
+
this.config = config;
|
|
333
|
+
this.axiosInstance = axiosInstance;
|
|
334
|
+
this.httpClientService = httpClientService;
|
|
335
|
+
this.loggingService = loggingService;
|
|
336
|
+
this.cacheService = cacheService;
|
|
337
|
+
this.stats = {
|
|
338
|
+
totalRequests: 0,
|
|
339
|
+
successfulRequests: 0,
|
|
340
|
+
failedRequests: 0,
|
|
341
|
+
totalResponseTime: 0,
|
|
342
|
+
averageResponseTime: 0,
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
get(url, config) {
|
|
346
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
347
|
+
return this.request(Object.assign(Object.assign({}, config), { method: 'GET', url }));
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
post(url, data, config) {
|
|
351
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
352
|
+
return this.request(Object.assign(Object.assign({}, config), { method: 'POST', url, data }));
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
put(url, data, config) {
|
|
356
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
357
|
+
return this.request(Object.assign(Object.assign({}, config), { method: 'PUT', url, data }));
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
patch(url, data, config) {
|
|
361
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
362
|
+
return this.request(Object.assign(Object.assign({}, config), { method: 'PATCH', url, data }));
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
delete(url, config) {
|
|
366
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
367
|
+
return this.request(Object.assign(Object.assign({}, config), { method: 'DELETE', url }));
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
request(config) {
|
|
371
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
372
|
+
const startTime = Date.now();
|
|
373
|
+
this.stats.totalRequests++;
|
|
374
|
+
try {
|
|
375
|
+
const response = yield this.axiosInstance.request(config);
|
|
376
|
+
const responseTime = Date.now() - startTime;
|
|
377
|
+
this.updateStats(true, responseTime);
|
|
378
|
+
return response.data;
|
|
379
|
+
}
|
|
380
|
+
catch (error) {
|
|
381
|
+
const responseTime = Date.now() - startTime;
|
|
382
|
+
this.updateStats(false, responseTime);
|
|
383
|
+
throw error;
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
getStats() {
|
|
388
|
+
return Object.assign({}, this.stats);
|
|
389
|
+
}
|
|
390
|
+
resetStats() {
|
|
391
|
+
this.stats.totalRequests = 0;
|
|
392
|
+
this.stats.successfulRequests = 0;
|
|
393
|
+
this.stats.failedRequests = 0;
|
|
394
|
+
this.stats.totalResponseTime = 0;
|
|
395
|
+
this.stats.averageResponseTime = 0;
|
|
396
|
+
}
|
|
397
|
+
updateStats(success, responseTime) {
|
|
398
|
+
if (success) {
|
|
399
|
+
this.stats.successfulRequests++;
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
this.stats.failedRequests++;
|
|
403
|
+
}
|
|
404
|
+
this.stats.totalResponseTime += responseTime;
|
|
405
|
+
this.stats.averageResponseTime =
|
|
406
|
+
this.stats.totalResponseTime / this.stats.totalRequests;
|
|
407
|
+
if (this.config.responseTimeWarningThreshold &&
|
|
408
|
+
responseTime > this.config.responseTimeWarningThreshold) {
|
|
409
|
+
console.warn(`[API Client ${this.name}] Slow response detected: ${responseTime}ms for ${this.axiosInstance.defaults.baseURL}`);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
|
+
import { HttpCacheConfig } from '../interfaces/http-client-config.interface';
|
|
3
|
+
import { CacheService } from '../../cache/cache.service';
|
|
4
|
+
export declare class HttpCacheService {
|
|
5
|
+
private readonly cacheService;
|
|
6
|
+
private readonly logger;
|
|
7
|
+
constructor(cacheService: CacheService);
|
|
8
|
+
get(config: AxiosRequestConfig, cacheOptions: HttpCacheConfig): Promise<AxiosResponse | null>;
|
|
9
|
+
set(config: AxiosRequestConfig, response: AxiosResponse, cacheOptions: HttpCacheConfig): Promise<void>;
|
|
10
|
+
clear(pattern?: string): Promise<void>;
|
|
11
|
+
getStats(): Promise<any>;
|
|
12
|
+
cleanup(): Promise<void>;
|
|
13
|
+
warmup(urls: string[], headers?: Record<string, string>): Promise<void>;
|
|
14
|
+
getDetailedStats(): Promise<any>;
|
|
15
|
+
private generateCacheKey;
|
|
16
|
+
private buildCacheKey;
|
|
17
|
+
private mapLayers;
|
|
18
|
+
private selectHeadersForCacheKey;
|
|
19
|
+
private isResponseCacheable;
|
|
20
|
+
private hashObject;
|
|
21
|
+
private calculateHitRate;
|
|
22
|
+
private getRecentCacheActivity;
|
|
23
|
+
private generateRecommendations;
|
|
24
|
+
}
|