@nest-omni/core 3.1.2-7 → 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.
- 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 +319 -0
- package/http-client/examples/basic-usage.example.d.ts +61 -0
- package/http-client/examples/basic-usage.example.js +171 -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 +254 -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 +40 -0
- package/http-client/services/api-client-registry.service.js +411 -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 +43 -0
- package/http-client/services/http-client.service.js +384 -0
- package/http-client/services/http-log-query.service.d.ts +56 -0
- package/http-client/services/http-log-query.service.js +414 -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/retry-recorder.util.d.ts +30 -0
- package/http-client/utils/retry-recorder.util.js +143 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,353 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
exports.ExampleController = exports.ApiManagementService = exports.CustomApiService = exports.WeChatApiService = exports.StripeApiService = exports.GitHubApiService = void 0;
|
|
22
|
+
const common_1 = require("@nestjs/common");
|
|
23
|
+
const api_client_registry_service_1 = require("../services/api-client-registry.service");
|
|
24
|
+
let GitHubApiService = class GitHubApiService {
|
|
25
|
+
constructor(apiRegistry) {
|
|
26
|
+
this.apiRegistry = apiRegistry;
|
|
27
|
+
this.initializeClients();
|
|
28
|
+
}
|
|
29
|
+
get github() {
|
|
30
|
+
return this.apiRegistry.getClient('github');
|
|
31
|
+
}
|
|
32
|
+
initializeClients() {
|
|
33
|
+
this.apiRegistry.createClient({
|
|
34
|
+
name: 'github',
|
|
35
|
+
baseURL: 'https://api.github.com',
|
|
36
|
+
auth: {
|
|
37
|
+
type: api_client_registry_service_1.AuthType.BEARER_TOKEN,
|
|
38
|
+
config: {
|
|
39
|
+
token: process.env.GITHUB_TOKEN,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
responseTransformer: {
|
|
43
|
+
dataPath: 'data',
|
|
44
|
+
success: (response) => response.data,
|
|
45
|
+
error: (error) => {
|
|
46
|
+
var _a, _b;
|
|
47
|
+
if ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) {
|
|
48
|
+
throw new Error(`GitHub API Error: ${error.response.data.message}`);
|
|
49
|
+
}
|
|
50
|
+
throw error;
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
retry: {
|
|
54
|
+
enabled: true,
|
|
55
|
+
retries: 3,
|
|
56
|
+
retryCondition: (error) => {
|
|
57
|
+
var _a, _b;
|
|
58
|
+
return (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 403 || ((_b = error.response) === null || _b === void 0 ? void 0 : _b.status) === 429);
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
responseTimeWarningThreshold: 2000,
|
|
62
|
+
tags: ['github', 'vcs'],
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
exports.GitHubApiService = GitHubApiService;
|
|
67
|
+
exports.GitHubApiService = GitHubApiService = __decorate([
|
|
68
|
+
(0, common_1.Injectable)(),
|
|
69
|
+
__metadata("design:paramtypes", [api_client_registry_service_1.ApiClientRegistryService])
|
|
70
|
+
], GitHubApiService);
|
|
71
|
+
let StripeApiService = class StripeApiService {
|
|
72
|
+
constructor(apiRegistry) {
|
|
73
|
+
this.apiRegistry = apiRegistry;
|
|
74
|
+
this.initializeClients();
|
|
75
|
+
}
|
|
76
|
+
get stripe() {
|
|
77
|
+
return this.apiRegistry.getClient('stripe');
|
|
78
|
+
}
|
|
79
|
+
initializeClients() {
|
|
80
|
+
this.apiRegistry.createClient({
|
|
81
|
+
name: 'stripe',
|
|
82
|
+
baseURL: 'https://api.stripe.com/v1',
|
|
83
|
+
auth: {
|
|
84
|
+
type: api_client_registry_service_1.AuthType.BASIC_AUTH,
|
|
85
|
+
config: {
|
|
86
|
+
username: process.env.STRIPE_SECRET_KEY || '',
|
|
87
|
+
password: '',
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
defaultHeaders: {
|
|
91
|
+
'Stripe-Version': '2023-10-16',
|
|
92
|
+
},
|
|
93
|
+
responseTransformer: {
|
|
94
|
+
dataPath: 'data',
|
|
95
|
+
error: (error) => {
|
|
96
|
+
var _a, _b, _c;
|
|
97
|
+
if ((_c = (_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.error) === null || _c === void 0 ? void 0 : _c.message) {
|
|
98
|
+
throw new Error(`Stripe API Error: ${error.response.data.error.message}`);
|
|
99
|
+
}
|
|
100
|
+
throw error;
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
responseTimeWarningThreshold: 3000,
|
|
104
|
+
tags: ['stripe', 'payment'],
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
exports.StripeApiService = StripeApiService;
|
|
109
|
+
exports.StripeApiService = StripeApiService = __decorate([
|
|
110
|
+
(0, common_1.Injectable)(),
|
|
111
|
+
__metadata("design:paramtypes", [api_client_registry_service_1.ApiClientRegistryService])
|
|
112
|
+
], StripeApiService);
|
|
113
|
+
let WeChatApiService = class WeChatApiService {
|
|
114
|
+
constructor(apiRegistry) {
|
|
115
|
+
this.apiRegistry = apiRegistry;
|
|
116
|
+
this.initializeClients();
|
|
117
|
+
}
|
|
118
|
+
get wechat() {
|
|
119
|
+
return this.apiRegistry.getClient('wechat');
|
|
120
|
+
}
|
|
121
|
+
initializeClients() {
|
|
122
|
+
this.apiRegistry.createClient({
|
|
123
|
+
name: 'wechat',
|
|
124
|
+
baseURL: 'https://api.weixin.qq.com',
|
|
125
|
+
auth: {
|
|
126
|
+
type: api_client_registry_service_1.AuthType.NONE,
|
|
127
|
+
},
|
|
128
|
+
responseTransformer: {
|
|
129
|
+
success: (response) => {
|
|
130
|
+
if (response.data.errcode !== 0) {
|
|
131
|
+
throw new Error(`WeChat API Error (${response.data.errcode}): ${response.data.errmsg}`);
|
|
132
|
+
}
|
|
133
|
+
return response.data.data;
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
tags: ['wechat', 'social'],
|
|
137
|
+
environments: {
|
|
138
|
+
development: {
|
|
139
|
+
baseURL: 'https://api.tunnel.com/wechat',
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
exports.WeChatApiService = WeChatApiService;
|
|
146
|
+
exports.WeChatApiService = WeChatApiService = __decorate([
|
|
147
|
+
(0, common_1.Injectable)(),
|
|
148
|
+
__metadata("design:paramtypes", [api_client_registry_service_1.ApiClientRegistryService])
|
|
149
|
+
], WeChatApiService);
|
|
150
|
+
let CustomApiService = class CustomApiService {
|
|
151
|
+
constructor(apiRegistry) {
|
|
152
|
+
this.apiRegistry = apiRegistry;
|
|
153
|
+
this.initializeClients();
|
|
154
|
+
}
|
|
155
|
+
get customApi() {
|
|
156
|
+
return this.apiRegistry.getClient('custom-api');
|
|
157
|
+
}
|
|
158
|
+
initializeClients() {
|
|
159
|
+
this.apiRegistry.createClient({
|
|
160
|
+
name: 'custom-api',
|
|
161
|
+
baseURL: process.env.CUSTOM_API_URL,
|
|
162
|
+
auth: {
|
|
163
|
+
type: api_client_registry_service_1.AuthType.CUSTOM,
|
|
164
|
+
config: {
|
|
165
|
+
authenticator(config) {
|
|
166
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
167
|
+
const token = yield this.getValidToken();
|
|
168
|
+
config.headers = Object.assign(Object.assign({}, config.headers), { Authorization: `Bearer ${token}`, 'X-Timestamp': Date.now().toString(), 'X-Signature': this.generateSignature(config) });
|
|
169
|
+
return config;
|
|
170
|
+
});
|
|
171
|
+
},
|
|
172
|
+
refresher(config, error) {
|
|
173
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
174
|
+
var _a;
|
|
175
|
+
if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401) {
|
|
176
|
+
const newToken = yield this.refreshToken();
|
|
177
|
+
config.headers['Authorization'] = `Bearer ${newToken}`;
|
|
178
|
+
return config;
|
|
179
|
+
}
|
|
180
|
+
throw error;
|
|
181
|
+
});
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
responseTransformer: {
|
|
186
|
+
success: (response) => {
|
|
187
|
+
const result = response.data;
|
|
188
|
+
if (result.success === false) {
|
|
189
|
+
throw new Error(result.message || 'API request failed');
|
|
190
|
+
}
|
|
191
|
+
return {
|
|
192
|
+
data: result.data || result,
|
|
193
|
+
code: result.code || 0,
|
|
194
|
+
message: result.message || 'Success',
|
|
195
|
+
pagination: result.pagination,
|
|
196
|
+
requestId: result.requestId,
|
|
197
|
+
};
|
|
198
|
+
},
|
|
199
|
+
error: (error) => {
|
|
200
|
+
var _a;
|
|
201
|
+
if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.data) {
|
|
202
|
+
const errorData = error.response.data;
|
|
203
|
+
const customError = new Error(errorData.message || 'API request failed');
|
|
204
|
+
customError.code = errorData.code;
|
|
205
|
+
customError.details = errorData.details;
|
|
206
|
+
throw customError;
|
|
207
|
+
}
|
|
208
|
+
throw error;
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
defaultHeaders: {
|
|
212
|
+
'Content-Type': 'application/json',
|
|
213
|
+
Accept: 'application/json',
|
|
214
|
+
},
|
|
215
|
+
retry: {
|
|
216
|
+
enabled: true,
|
|
217
|
+
retries: 2,
|
|
218
|
+
retryCondition: (error) => {
|
|
219
|
+
if (!error.response)
|
|
220
|
+
return true;
|
|
221
|
+
return error.response.status >= 500;
|
|
222
|
+
},
|
|
223
|
+
},
|
|
224
|
+
tags: ['custom', 'business'],
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
getValidToken() {
|
|
228
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
229
|
+
return 'mock-jwt-token';
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
refreshToken() {
|
|
233
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
234
|
+
return 'refreshed-jwt-token';
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
generateSignature(config) {
|
|
238
|
+
return 'mock-signature';
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
exports.CustomApiService = CustomApiService;
|
|
242
|
+
exports.CustomApiService = CustomApiService = __decorate([
|
|
243
|
+
(0, common_1.Injectable)(),
|
|
244
|
+
__metadata("design:paramtypes", [api_client_registry_service_1.ApiClientRegistryService])
|
|
245
|
+
], CustomApiService);
|
|
246
|
+
let ApiManagementService = class ApiManagementService {
|
|
247
|
+
constructor(apiRegistry) {
|
|
248
|
+
this.apiRegistry = apiRegistry;
|
|
249
|
+
}
|
|
250
|
+
createDynamicClient(name, config) {
|
|
251
|
+
return this.apiRegistry.createClient(Object.assign({ name, baseURL: config.baseURL || '' }, config));
|
|
252
|
+
}
|
|
253
|
+
getAllStats() {
|
|
254
|
+
return this.apiRegistry.getAllStats();
|
|
255
|
+
}
|
|
256
|
+
resetAllStats() {
|
|
257
|
+
this.apiRegistry.resetAllStats();
|
|
258
|
+
}
|
|
259
|
+
healthCheck() {
|
|
260
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
261
|
+
return this.apiRegistry.healthCheck();
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
createMultipleClients(configs) {
|
|
265
|
+
this.apiRegistry.createClients(configs);
|
|
266
|
+
}
|
|
267
|
+
listClients() {
|
|
268
|
+
return this.apiRegistry.listClients();
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
exports.ApiManagementService = ApiManagementService;
|
|
272
|
+
exports.ApiManagementService = ApiManagementService = __decorate([
|
|
273
|
+
(0, common_1.Injectable)(),
|
|
274
|
+
__metadata("design:paramtypes", [api_client_registry_service_1.ApiClientRegistryService])
|
|
275
|
+
], ApiManagementService);
|
|
276
|
+
let ExampleController = class ExampleController {
|
|
277
|
+
constructor(githubApi, stripeApi, wechatApi, customApi, apiManagement) {
|
|
278
|
+
this.githubApi = githubApi;
|
|
279
|
+
this.stripeApi = stripeApi;
|
|
280
|
+
this.wechatApi = wechatApi;
|
|
281
|
+
this.customApi = customApi;
|
|
282
|
+
this.apiManagement = apiManagement;
|
|
283
|
+
}
|
|
284
|
+
getGitHubUser(username) {
|
|
285
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
286
|
+
const github = this.githubApi.github();
|
|
287
|
+
return github.get(`/users/${username}`);
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
createStripePayment(amount, currency) {
|
|
291
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
292
|
+
const stripe = this.stripeApi.stripe();
|
|
293
|
+
return stripe.post('/charges', {
|
|
294
|
+
amount,
|
|
295
|
+
currency,
|
|
296
|
+
source: 'tok_visa',
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
sendWeChatMessage(openid, content) {
|
|
301
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
302
|
+
const wechat = this.wechatApi.wechat();
|
|
303
|
+
return wechat.post('/cgi-bin/message/custom/send', {
|
|
304
|
+
touser: openid,
|
|
305
|
+
msgtype: 'text',
|
|
306
|
+
text: {
|
|
307
|
+
content,
|
|
308
|
+
},
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
getCustomData(id) {
|
|
313
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
314
|
+
const custom = this.customApi.customApi();
|
|
315
|
+
return custom.get(`/data/${id}`);
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
callDynamicApi(url) {
|
|
319
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
320
|
+
const client = this.apiManagement.createDynamicClient('dynamic', {
|
|
321
|
+
baseURL: url,
|
|
322
|
+
auth: {
|
|
323
|
+
type: api_client_registry_service_1.AuthType.API_KEY,
|
|
324
|
+
config: {
|
|
325
|
+
key: process.env.DYNAMIC_API_KEY,
|
|
326
|
+
location: 'header',
|
|
327
|
+
name: 'X-API-Key',
|
|
328
|
+
},
|
|
329
|
+
},
|
|
330
|
+
});
|
|
331
|
+
return client.get('/');
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
getApiHealth() {
|
|
335
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
336
|
+
return this.apiManagement.healthCheck();
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
getApiStats() {
|
|
340
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
341
|
+
return this.apiManagement.getAllStats();
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
exports.ExampleController = ExampleController;
|
|
346
|
+
exports.ExampleController = ExampleController = __decorate([
|
|
347
|
+
(0, common_1.Injectable)(),
|
|
348
|
+
__metadata("design:paramtypes", [GitHubApiService,
|
|
349
|
+
StripeApiService,
|
|
350
|
+
WeChatApiService,
|
|
351
|
+
CustomApiService,
|
|
352
|
+
ApiManagementService])
|
|
353
|
+
], ExampleController);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { HttpClientConfig } from './interfaces/http-client-config.interface';
|
|
3
|
+
export declare class HttpClientModule {
|
|
4
|
+
static forRoot(config?: HttpClientConfig): DynamicModule;
|
|
5
|
+
static forRootAsync(config: {
|
|
6
|
+
useFactory: (...args: any[]) => Promise<HttpClientConfig> | HttpClientConfig;
|
|
7
|
+
inject?: any[];
|
|
8
|
+
imports?: any[];
|
|
9
|
+
}): DynamicModule;
|
|
10
|
+
private static loadConfigFromEnvironment;
|
|
11
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
9
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
10
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
11
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
12
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
13
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
14
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
var HttpClientModule_1;
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.HttpClientModule = void 0;
|
|
20
|
+
const common_1 = require("@nestjs/common");
|
|
21
|
+
const schedule_1 = require("@nestjs/schedule");
|
|
22
|
+
const config_1 = require("@nestjs/config");
|
|
23
|
+
const typeorm_1 = require("@nestjs/typeorm");
|
|
24
|
+
const cache_service_1 = require("../cache/cache.service");
|
|
25
|
+
const http_client_service_1 = require("./services/http-client.service");
|
|
26
|
+
const circuit_breaker_service_1 = require("./services/circuit-breaker.service");
|
|
27
|
+
const logging_service_1 = require("./services/logging.service");
|
|
28
|
+
const cache_service_2 = require("./services/cache.service");
|
|
29
|
+
const http_log_query_service_1 = require("./services/http-log-query.service");
|
|
30
|
+
const log_cleanup_service_1 = require("./services/log-cleanup.service");
|
|
31
|
+
const api_client_registry_service_1 = require("./services/api-client-registry.service");
|
|
32
|
+
const entities_1 = require("./entities");
|
|
33
|
+
const cache_options_interface_1 = require("../cache/interfaces/cache-options.interface");
|
|
34
|
+
let HttpClientModule = HttpClientModule_1 = class HttpClientModule {
|
|
35
|
+
static forRoot(config = {}) {
|
|
36
|
+
var _a, _b, _c;
|
|
37
|
+
const baseProviders = [
|
|
38
|
+
{
|
|
39
|
+
provide: 'HTTP_CLIENT_CONFIG',
|
|
40
|
+
useFactory: (configService, cacheService) => {
|
|
41
|
+
const envConfig = this.loadConfigFromEnvironment(configService);
|
|
42
|
+
return Object.assign(Object.assign({}, config), envConfig);
|
|
43
|
+
},
|
|
44
|
+
inject: [config_1.ConfigService, cache_service_1.CacheService],
|
|
45
|
+
},
|
|
46
|
+
circuit_breaker_service_1.HttpCircuitBreakerService,
|
|
47
|
+
logging_service_1.HttpLoggingService,
|
|
48
|
+
cache_service_2.HttpCacheService,
|
|
49
|
+
http_log_query_service_1.HttpLogQueryService,
|
|
50
|
+
log_cleanup_service_1.HttpLogCleanupService,
|
|
51
|
+
api_client_registry_service_1.ApiClientRegistryService,
|
|
52
|
+
{
|
|
53
|
+
provide: http_client_service_1.HttpClientService,
|
|
54
|
+
useFactory: (circuitBreakerService, loggingService, cacheService, httpConfig) => {
|
|
55
|
+
return new http_client_service_1.HttpClientService(circuitBreakerService, loggingService, cacheService, httpConfig);
|
|
56
|
+
},
|
|
57
|
+
inject: [
|
|
58
|
+
circuit_breaker_service_1.HttpCircuitBreakerService,
|
|
59
|
+
logging_service_1.HttpLoggingService,
|
|
60
|
+
cache_service_2.HttpCacheService,
|
|
61
|
+
'HTTP_CLIENT_CONFIG',
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
];
|
|
65
|
+
const baseExports = [
|
|
66
|
+
http_client_service_1.HttpClientService,
|
|
67
|
+
circuit_breaker_service_1.HttpCircuitBreakerService,
|
|
68
|
+
logging_service_1.HttpLoggingService,
|
|
69
|
+
cache_service_2.HttpCacheService,
|
|
70
|
+
http_log_query_service_1.HttpLogQueryService,
|
|
71
|
+
log_cleanup_service_1.HttpLogCleanupService,
|
|
72
|
+
api_client_registry_service_1.ApiClientRegistryService,
|
|
73
|
+
];
|
|
74
|
+
const dynamicImports = [config_1.ConfigModule];
|
|
75
|
+
if ((_b = (_a = config.logging) === null || _a === void 0 ? void 0 : _a.databaseLogging) === null || _b === void 0 ? void 0 : _b.enabled) {
|
|
76
|
+
dynamicImports.push(typeorm_1.TypeOrmModule.forFeature([entities_1.HttpLogEntity]));
|
|
77
|
+
}
|
|
78
|
+
if ((_c = config.logCleanup) === null || _c === void 0 ? void 0 : _c.enabled) {
|
|
79
|
+
dynamicImports.push(schedule_1.ScheduleModule.forRoot());
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
module: HttpClientModule_1,
|
|
83
|
+
imports: dynamicImports,
|
|
84
|
+
providers: baseProviders,
|
|
85
|
+
exports: baseExports,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
static forRootAsync(config) {
|
|
89
|
+
const baseProviders = [
|
|
90
|
+
{
|
|
91
|
+
provide: 'HTTP_CLIENT_CONFIG',
|
|
92
|
+
useFactory: (...args) => __awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
const userConfig = yield config.useFactory(...args);
|
|
94
|
+
const envConfig = this.loadConfigFromEnvironment(args[0]);
|
|
95
|
+
return Object.assign(Object.assign({}, userConfig), envConfig);
|
|
96
|
+
}),
|
|
97
|
+
inject: config.inject || [config_1.ConfigService],
|
|
98
|
+
},
|
|
99
|
+
circuit_breaker_service_1.HttpCircuitBreakerService,
|
|
100
|
+
logging_service_1.HttpLoggingService,
|
|
101
|
+
cache_service_2.HttpCacheService,
|
|
102
|
+
http_log_query_service_1.HttpLogQueryService,
|
|
103
|
+
log_cleanup_service_1.HttpLogCleanupService,
|
|
104
|
+
api_client_registry_service_1.ApiClientRegistryService,
|
|
105
|
+
{
|
|
106
|
+
provide: http_client_service_1.HttpClientService,
|
|
107
|
+
useFactory: (circuitBreakerService, loggingService, cacheService, httpConfig) => {
|
|
108
|
+
return new http_client_service_1.HttpClientService(circuitBreakerService, loggingService, cacheService, httpConfig);
|
|
109
|
+
},
|
|
110
|
+
inject: [
|
|
111
|
+
circuit_breaker_service_1.HttpCircuitBreakerService,
|
|
112
|
+
logging_service_1.HttpLoggingService,
|
|
113
|
+
cache_service_2.HttpCacheService,
|
|
114
|
+
'HTTP_CLIENT_CONFIG',
|
|
115
|
+
],
|
|
116
|
+
},
|
|
117
|
+
];
|
|
118
|
+
const baseExports = [
|
|
119
|
+
http_client_service_1.HttpClientService,
|
|
120
|
+
circuit_breaker_service_1.HttpCircuitBreakerService,
|
|
121
|
+
logging_service_1.HttpLoggingService,
|
|
122
|
+
cache_service_2.HttpCacheService,
|
|
123
|
+
http_log_query_service_1.HttpLogQueryService,
|
|
124
|
+
log_cleanup_service_1.HttpLogCleanupService,
|
|
125
|
+
api_client_registry_service_1.ApiClientRegistryService,
|
|
126
|
+
];
|
|
127
|
+
const dynamicImports = [config_1.ConfigModule, ...(config.imports || [])];
|
|
128
|
+
return {
|
|
129
|
+
module: HttpClientModule_1,
|
|
130
|
+
imports: dynamicImports,
|
|
131
|
+
providers: baseProviders,
|
|
132
|
+
exports: baseExports,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
static loadConfigFromEnvironment(configService) {
|
|
136
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
137
|
+
return {
|
|
138
|
+
baseURL: configService.get('HTTP_CLIENT_BASE_URL'),
|
|
139
|
+
timeout: configService.get('HTTP_CLIENT_TIMEOUT')
|
|
140
|
+
? parseInt(configService.get('HTTP_CLIENT_TIMEOUT'))
|
|
141
|
+
: undefined,
|
|
142
|
+
retry: {
|
|
143
|
+
enabled: configService.get('HTTP_CLIENT_RETRY_ENABLED') === 'true',
|
|
144
|
+
retries: configService.get('HTTP_CLIENT_RETRY_RETRIES')
|
|
145
|
+
? parseInt(configService.get('HTTP_CLIENT_RETRY_RETRIES'))
|
|
146
|
+
: undefined,
|
|
147
|
+
retryDelay: configService.get('HTTP_CLIENT_RETRY_DELAY_STRATEGY') ===
|
|
148
|
+
'exponential'
|
|
149
|
+
? (retryCount) => Math.pow(2, retryCount) * 1000
|
|
150
|
+
: undefined,
|
|
151
|
+
retryCondition: configService.get('HTTP_CLIENT_RETRY_CONDITION') ===
|
|
152
|
+
'network_only'
|
|
153
|
+
? (error) => !error.response
|
|
154
|
+
: undefined,
|
|
155
|
+
shouldResetTimeout: configService.get('HTTP_CLIENT_RETRY_RESET_TIMEOUT') !==
|
|
156
|
+
'false',
|
|
157
|
+
},
|
|
158
|
+
circuitBreaker: {
|
|
159
|
+
enabled: configService.get('HTTP_CLIENT_CIRCUIT_BREAKER_ENABLED') ===
|
|
160
|
+
'true',
|
|
161
|
+
failureThreshold: configService.get('HTTP_CLIENT_CIRCUIT_BREAKER_FAILURE_THRESHOLD')
|
|
162
|
+
? parseInt(configService.get('HTTP_CLIENT_CIRCUIT_BREAKER_FAILURE_THRESHOLD'))
|
|
163
|
+
: 5,
|
|
164
|
+
recoveryTimeoutMs: configService.get('HTTP_CLIENT_CIRCUIT_BREAKER_RECOVERY_TIMEOUT')
|
|
165
|
+
? parseInt(configService.get('HTTP_CLIENT_CIRCUIT_BREAKER_RECOVERY_TIMEOUT'))
|
|
166
|
+
: 60000,
|
|
167
|
+
monitoringPeriodMs: configService.get('HTTP_CLIENT_CIRCUIT_BREAKER_MONITORING_PERIOD')
|
|
168
|
+
? parseInt(configService.get('HTTP_CLIENT_CIRCUIT_BREAKER_MONITORING_PERIOD'))
|
|
169
|
+
: 10000,
|
|
170
|
+
minimumThroughputThreshold: configService.get('HTTP_CLIENT_CIRCUIT_BREAKER_MINIMUM_THROUGHPUT')
|
|
171
|
+
? parseInt(configService.get('HTTP_CLIENT_CIRCUIT_BREAKER_MINIMUM_THROUGHPUT'))
|
|
172
|
+
: 10,
|
|
173
|
+
countHalfOpenCalls: configService.get('HTTP_CLIENT_CIRCUIT_BREAKER_COUNT_HALF_OPEN') === 'true',
|
|
174
|
+
},
|
|
175
|
+
cache: {
|
|
176
|
+
enabled: configService.get('HTTP_CLIENT_CACHE_ENABLED') !== 'false',
|
|
177
|
+
defaultTtl: configService.get('HTTP_CLIENT_CACHE_DEFAULT_TTL')
|
|
178
|
+
? parseInt(configService.get('HTTP_CLIENT_CACHE_DEFAULT_TTL'))
|
|
179
|
+
: 300000,
|
|
180
|
+
cacheableMethods: ((_b = (_a = configService
|
|
181
|
+
.get('HTTP_CLIENT_CACHEABLE_METHODS')) === null || _a === void 0 ? void 0 : _a.split(',')) === null || _b === void 0 ? void 0 : _b.map((method) => method.trim().toUpperCase())) || [
|
|
182
|
+
'GET',
|
|
183
|
+
'HEAD',
|
|
184
|
+
],
|
|
185
|
+
cacheableStatusCodes: ((_d = (_c = configService
|
|
186
|
+
.get('HTTP_CLIENT_CACHEABLE_STATUS_CODES')) === null || _c === void 0 ? void 0 : _c.split(',')) === null || _d === void 0 ? void 0 : _d.map((code) => parseInt(code.trim()))) || [200, 201, 304],
|
|
187
|
+
options: {
|
|
188
|
+
layers: ((_f = (_e = configService
|
|
189
|
+
.get('HTTP_CLIENT_CACHE_LAYERS')) === null || _e === void 0 ? void 0 : _e.split(',')) === null || _f === void 0 ? void 0 : _f.map((layer) => {
|
|
190
|
+
const trimmed = layer.trim();
|
|
191
|
+
switch (trimmed) {
|
|
192
|
+
case 'cls':
|
|
193
|
+
return cache_options_interface_1.CacheLayer.CLS;
|
|
194
|
+
case 'memory':
|
|
195
|
+
return cache_options_interface_1.CacheLayer.MEMORY;
|
|
196
|
+
case 'redis':
|
|
197
|
+
return cache_options_interface_1.CacheLayer.REDIS;
|
|
198
|
+
default:
|
|
199
|
+
return cache_options_interface_1.CacheLayer.MEMORY;
|
|
200
|
+
}
|
|
201
|
+
})) || [cache_options_interface_1.CacheLayer.MEMORY],
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
logging: {
|
|
205
|
+
enabled: configService.get('HTTP_CLIENT_LOGGING_ENABLED') !== 'false',
|
|
206
|
+
logRequests: configService.get('HTTP_CLIENT_LOG_REQUESTS') !== 'false',
|
|
207
|
+
logResponses: configService.get('HTTP_CLIENT_LOG_RESPONSES') !== 'false',
|
|
208
|
+
logErrors: configService.get('HTTP_CLIENT_LOG_ERRORS') !== 'false',
|
|
209
|
+
logHeaders: configService.get('HTTP_CLIENT_LOG_HEADERS') !== 'false',
|
|
210
|
+
logBody: configService.get('HTTP_CLIENT_LOG_BODY') !== 'false',
|
|
211
|
+
maxBodyLength: configService.get('HTTP_CLIENT_LOG_MAX_BODY_LENGTH')
|
|
212
|
+
? parseInt(configService.get('HTTP_CLIENT_LOG_MAX_BODY_LENGTH'))
|
|
213
|
+
: undefined,
|
|
214
|
+
sanitizeHeaders: (_g = configService
|
|
215
|
+
.get('HTTP_CLIENT_LOG_SANITIZE_HEADERS')) === null || _g === void 0 ? void 0 : _g.split(','),
|
|
216
|
+
logLevel: configService.get('HTTP_CLIENT_LOG_LEVEL'),
|
|
217
|
+
databaseLogging: {
|
|
218
|
+
enabled: configService.get('HTTP_CLIENT_DB_LOGGING_ENABLED') ===
|
|
219
|
+
'true',
|
|
220
|
+
dataSource: configService.get('HTTP_CLIENT_DB_LOGGING_DATA_SOURCE') ||
|
|
221
|
+
'default',
|
|
222
|
+
tableName: configService.get('HTTP_CLIENT_DB_LOGGING_TABLE_NAME') ||
|
|
223
|
+
'http_logs',
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
proxy: {
|
|
227
|
+
enabled: configService.get('HTTP_CLIENT_PROXY_ENABLED') === 'true',
|
|
228
|
+
host: configService.get('HTTP_CLIENT_PROXY_HOST'),
|
|
229
|
+
port: configService.get('HTTP_CLIENT_PROXY_PORT')
|
|
230
|
+
? parseInt(configService.get('HTTP_CLIENT_PROXY_PORT'))
|
|
231
|
+
: undefined,
|
|
232
|
+
protocol: configService.get('HTTP_CLIENT_PROXY_PROTOCOL'),
|
|
233
|
+
},
|
|
234
|
+
logCleanup: {
|
|
235
|
+
enabled: configService.get('HTTP_CLIENT_LOG_CLEANUP_ENABLED') ===
|
|
236
|
+
'true',
|
|
237
|
+
retentionDays: configService.get('HTTP_CLIENT_LOG_CLEANUP_RETENTION_DAYS')
|
|
238
|
+
? parseInt(configService.get('HTTP_CLIENT_LOG_CLEANUP_RETENTION_DAYS'))
|
|
239
|
+
: 30,
|
|
240
|
+
maxRecords: configService.get('HTTP_CLIENT_LOG_CLEANUP_MAX_RECORDS'),
|
|
241
|
+
batchSize: configService.get('HTTP_CLIENT_LOG_CLEANUP_BATCH_SIZE')
|
|
242
|
+
? parseInt(configService.get('HTTP_CLIENT_LOG_CLEANUP_BATCH_SIZE'))
|
|
243
|
+
: 1000,
|
|
244
|
+
schedule: configService.get('HTTP_CLIENT_LOG_CLEANUP_SCHEDULE') || '0 2 * * *',
|
|
245
|
+
strategy: configService.get('HTTP_CLIENT_LOG_CLEANUP_STRATEGY') || 'retention',
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
exports.HttpClientModule = HttpClientModule;
|
|
251
|
+
exports.HttpClientModule = HttpClientModule = HttpClientModule_1 = __decorate([
|
|
252
|
+
(0, common_1.Global)(),
|
|
253
|
+
(0, common_1.Module)({})
|
|
254
|
+
], HttpClientModule);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { HttpClientConfig, RetryConfig, CircuitBreakerConfig, HttpCacheConfig, ProxyConfig, LoggingConfig, InterceptorConfig, ConnectionPoolConfig, HttpContext, HttpInterceptor, HttpMethod, HttpStats, } from './interfaces/http-client-config.interface';
|
|
2
|
+
export * from './interfaces/api-client-config.interface';
|
|
3
|
+
export { HttpLogEntity, RetryRecord } from './entities';
|
|
4
|
+
export * from './decorators';
|
|
5
|
+
export * from './services';
|
|
6
|
+
export * from './utils';
|
|
7
|
+
export * from './errors';
|
|
8
|
+
export * from './config';
|
|
9
|
+
export * from './http-client.module';
|
|
10
|
+
export * from './examples';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.HttpLogEntity = void 0;
|
|
18
|
+
__exportStar(require("./interfaces/api-client-config.interface"), exports);
|
|
19
|
+
var entities_1 = require("./entities");
|
|
20
|
+
Object.defineProperty(exports, "HttpLogEntity", { enumerable: true, get: function () { return entities_1.HttpLogEntity; } });
|
|
21
|
+
__exportStar(require("./decorators"), exports);
|
|
22
|
+
__exportStar(require("./services"), exports);
|
|
23
|
+
__exportStar(require("./utils"), exports);
|
|
24
|
+
__exportStar(require("./errors"), exports);
|
|
25
|
+
__exportStar(require("./config"), exports);
|
|
26
|
+
__exportStar(require("./http-client.module"), exports);
|
|
27
|
+
__exportStar(require("./examples"), exports);
|