@adatechnology/http-client 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,11 +1,787 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HTTP_AXIOS_CONNECTION = exports.HTTP_AXIOS_PROVIDER = exports.HTTP_PROVIDER = exports.HttpImplementationAxiosModule = exports.HttpModule = void 0;
4
- var http_module_1 = require("./http.module");
5
- Object.defineProperty(exports, "HttpModule", { enumerable: true, get: function () { return http_module_1.HttpModule; } });
6
- var axios_http_module_1 = require("./implementations/axios/axios.http.module");
7
- Object.defineProperty(exports, "HttpImplementationAxiosModule", { enumerable: true, get: function () { return axios_http_module_1.HttpImplementationAxiosModule; } });
8
- var http_token_1 = require("./http.token");
9
- Object.defineProperty(exports, "HTTP_PROVIDER", { enumerable: true, get: function () { return http_token_1.HTTP_PROVIDER; } });
10
- Object.defineProperty(exports, "HTTP_AXIOS_PROVIDER", { enumerable: true, get: function () { return http_token_1.HTTP_AXIOS_PROVIDER; } });
11
- Object.defineProperty(exports, "HTTP_AXIOS_CONNECTION", { enumerable: true, get: function () { return http_token_1.HTTP_AXIOS_CONNECTION; } });
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __esm = (fn, res) => function __init() {
8
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
9
+ };
10
+ var __export = (target, all) => {
11
+ for (var name in all)
12
+ __defProp(target, name, { get: all[name], enumerable: true });
13
+ };
14
+ var __copyProps = (to, from2, except, desc) => {
15
+ if (from2 && typeof from2 === "object" || typeof from2 === "function") {
16
+ for (let key of __getOwnPropNames(from2))
17
+ if (!__hasOwnProp.call(to, key) && key !== except)
18
+ __defProp(to, key, { get: () => from2[key], enumerable: !(desc = __getOwnPropDesc(from2, key)) || desc.enumerable });
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
23
+ // If the importer is in node compatibility mode or this is not an ESM
24
+ // file that has been converted to a CommonJS file using a Babel-
25
+ // compatible transform (i.e. "__esModule" has not been set), then set
26
+ // "default" to the CommonJS "module.exports" for node compatibility.
27
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
28
+ mod
29
+ ));
30
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
31
+ var __decorateClass = (decorators, target, key, kind) => {
32
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
33
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
34
+ if (decorator = decorators[i])
35
+ result = (kind ? decorator(target, key, result) : decorator(result)) || result;
36
+ if (kind && result) __defProp(target, key, result);
37
+ return result;
38
+ };
39
+ var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
40
+
41
+ // src/errors/error-mapper.service.ts
42
+ var error_mapper_service_exports = {};
43
+ __export(error_mapper_service_exports, {
44
+ ErrorMapperService: () => ErrorMapperService
45
+ });
46
+ var ErrorMapperService;
47
+ var init_error_mapper_service = __esm({
48
+ "src/errors/error-mapper.service.ts"() {
49
+ ErrorMapperService = class {
50
+ mapUpstreamError(err) {
51
+ var _a;
52
+ if (err && err.status && err.message) return err;
53
+ try {
54
+ const anyErr = err;
55
+ const context = {};
56
+ if (anyErr == null ? void 0 : anyErr.config) {
57
+ context.url = anyErr.config.url || anyErr.config.baseURL;
58
+ context.method = anyErr.config.method;
59
+ }
60
+ if (anyErr == null ? void 0 : anyErr.stack) {
61
+ const frames = this.parseStack(anyErr.stack);
62
+ if (frames.length) {
63
+ context.stack = frames;
64
+ const origin = frames.find((f) => !this.isInternalFrame(f.file));
65
+ if (origin) context.origin = origin;
66
+ }
67
+ }
68
+ if (anyErr == null ? void 0 : anyErr.response) {
69
+ const status = anyErr.response.status || 502;
70
+ const message = ((_a = anyErr.response.data) == null ? void 0 : _a.message) || anyErr.message || "Upstream error";
71
+ return { message, status, code: anyErr.code, context };
72
+ }
73
+ if (anyErr == null ? void 0 : anyErr.request) {
74
+ return { message: "No response from upstream service", status: 502, code: anyErr.code, context };
75
+ }
76
+ return { message: anyErr && anyErr.message || "Unexpected error", status: 500, code: anyErr == null ? void 0 : anyErr.code, context };
77
+ } catch (e) {
78
+ return { message: "Error mapping failure", status: 500, code: void 0, context: { original: String(err) } };
79
+ }
80
+ }
81
+ parseStack(stack) {
82
+ const lines = stack.split("\n").map((l) => l.trim()).filter(Boolean);
83
+ const frames = [];
84
+ const re = /^at\s+(?:(.*?)\s+\()?(.*?):(\d+):(\d+)\)?$/;
85
+ for (const line of lines) {
86
+ const m = re.exec(line);
87
+ if (m) {
88
+ const fn = m[1] || void 0;
89
+ const file = m[2];
90
+ const lineNum = parseInt(m[3], 10);
91
+ const colNum = parseInt(m[4], 10);
92
+ frames.push({ fn, file, line: lineNum, column: colNum });
93
+ }
94
+ }
95
+ return frames;
96
+ }
97
+ isInternalFrame(file) {
98
+ if (!file) return false;
99
+ return /node_modules|internal|axios/.test(file);
100
+ }
101
+ };
102
+ }
103
+ });
104
+
105
+ // src/errors/base-app-error.ts
106
+ var BaseAppError;
107
+ var init_base_app_error = __esm({
108
+ "src/errors/base-app-error.ts"() {
109
+ BaseAppError = class extends Error {
110
+ code;
111
+ status;
112
+ context;
113
+ constructor(message, status, code, context) {
114
+ super(message);
115
+ this.name = new.target.name;
116
+ this.status = status;
117
+ this.code = code;
118
+ this.context = context;
119
+ if (typeof Error.captureStackTrace === "function") {
120
+ Error.captureStackTrace(this, new.target);
121
+ }
122
+ }
123
+ };
124
+ }
125
+ });
126
+
127
+ // src/errors/http-client-error.ts
128
+ var http_client_error_exports = {};
129
+ __export(http_client_error_exports, {
130
+ HttpClientError: () => HttpClientError
131
+ });
132
+ var HttpClientError;
133
+ var init_http_client_error = __esm({
134
+ "src/errors/http-client-error.ts"() {
135
+ init_base_app_error();
136
+ HttpClientError = class extends BaseAppError {
137
+ constructor(message, status, code, context) {
138
+ super(message, status ?? 502, code, { service: "@adatechnology/http-client", ...context || {} });
139
+ }
140
+ };
141
+ }
142
+ });
143
+
144
+ // src/index.ts
145
+ var index_exports = {};
146
+ __export(index_exports, {
147
+ HTTP_AXIOS_CONNECTION: () => HTTP_AXIOS_CONNECTION,
148
+ HTTP_AXIOS_PROVIDER: () => HTTP_AXIOS_PROVIDER,
149
+ HTTP_PROVIDER: () => HTTP_PROVIDER,
150
+ HttpImplementationAxiosModule: () => HttpImplementationAxiosModule,
151
+ HttpModule: () => HttpModule
152
+ });
153
+ module.exports = __toCommonJS(index_exports);
154
+
155
+ // src/http.module.ts
156
+ var import_common3 = require("@nestjs/common");
157
+
158
+ // src/http.provider.ts
159
+ var import_common = require("@nestjs/common");
160
+
161
+ // src/http.token.ts
162
+ var HTTP_AXIOS_CONNECTION = "HTTP_AXIOS_CONNECTION";
163
+ var HTTP_AXIOS_PROVIDER = "HTTP_AXIOS_PROVIDER";
164
+ var HTTP_PROVIDER = "HTTP_PROVIDER";
165
+
166
+ // src/http.provider.ts
167
+ var HttpProvider = class {
168
+ constructor(axiosHttpProvider) {
169
+ this.axiosHttpProvider = axiosHttpProvider;
170
+ }
171
+ /**
172
+ * Expose underlying axios instance when available from the axios provider implementation.
173
+ * This is intentionally typed as unknown/any to avoid leaking implementation details.
174
+ */
175
+ getAxiosInstance() {
176
+ try {
177
+ const provider = this.axiosHttpProvider;
178
+ return (provider == null ? void 0 : provider.axiosInstance) ?? (provider == null ? void 0 : provider.instance) ?? void 0;
179
+ } catch (e) {
180
+ return void 0;
181
+ }
182
+ }
183
+ async get(url, config) {
184
+ return this.axiosHttpProvider.get(url, config);
185
+ }
186
+ get$(url, config) {
187
+ return this.axiosHttpProvider.get$(url, config);
188
+ }
189
+ async post(url, data, config) {
190
+ return this.axiosHttpProvider.post(url, data, config);
191
+ }
192
+ post$(url, data, config) {
193
+ return this.axiosHttpProvider.post$(url, data, config);
194
+ }
195
+ async put(url, data, config) {
196
+ return this.axiosHttpProvider.put(url, data, config);
197
+ }
198
+ put$(url, data, config) {
199
+ return this.axiosHttpProvider.put$(url, data, config);
200
+ }
201
+ async patch(url, data, config) {
202
+ return this.axiosHttpProvider.patch(url, data, config);
203
+ }
204
+ patch$(url, data, config) {
205
+ return this.axiosHttpProvider.patch$(url, data, config);
206
+ }
207
+ async delete(url, config) {
208
+ return this.axiosHttpProvider.delete(url, config);
209
+ }
210
+ delete$(url, config) {
211
+ return this.axiosHttpProvider.delete$(url, config);
212
+ }
213
+ async head(url, config) {
214
+ return this.axiosHttpProvider.head(url, config);
215
+ }
216
+ head$(url, config) {
217
+ return this.axiosHttpProvider.head$(url, config);
218
+ }
219
+ async options(url, config) {
220
+ return this.axiosHttpProvider.options(url, config);
221
+ }
222
+ options$(url, config) {
223
+ return this.axiosHttpProvider.options$(url, config);
224
+ }
225
+ async request(config) {
226
+ return this.axiosHttpProvider.request(config);
227
+ }
228
+ request$(config) {
229
+ return this.axiosHttpProvider.request$(config);
230
+ }
231
+ setGlobalHeader(key, value) {
232
+ this.axiosHttpProvider.setGlobalHeader(key, value);
233
+ }
234
+ removeGlobalHeader(key) {
235
+ this.axiosHttpProvider.removeGlobalHeader(key);
236
+ }
237
+ getGlobalHeaders() {
238
+ return this.axiosHttpProvider.getGlobalHeaders();
239
+ }
240
+ setBaseUrl(baseUrl) {
241
+ this.axiosHttpProvider.setBaseUrl(baseUrl);
242
+ }
243
+ getBaseUrl() {
244
+ return this.axiosHttpProvider.getBaseUrl();
245
+ }
246
+ setDefaultTimeout(timeout) {
247
+ this.axiosHttpProvider.setDefaultTimeout(timeout);
248
+ }
249
+ addErrorInterceptor(interceptor) {
250
+ return this.axiosHttpProvider.addErrorInterceptor(interceptor);
251
+ }
252
+ removeErrorInterceptor(id) {
253
+ this.axiosHttpProvider.removeErrorInterceptor(id);
254
+ }
255
+ clearCache(key) {
256
+ this.axiosHttpProvider.clearCache(key);
257
+ }
258
+ setAuthToken(token, type) {
259
+ this.axiosHttpProvider.setAuthToken(token, type);
260
+ }
261
+ clearAuthToken() {
262
+ this.axiosHttpProvider.clearAuthToken();
263
+ }
264
+ addRequestInterceptor(onFulfilled, onRejected) {
265
+ return this.axiosHttpProvider.addRequestInterceptor(
266
+ onFulfilled,
267
+ onRejected
268
+ );
269
+ }
270
+ removeRequestInterceptor(id) {
271
+ this.axiosHttpProvider.removeRequestInterceptor(id);
272
+ }
273
+ addResponseInterceptor(onFulfilled, onRejected) {
274
+ return this.axiosHttpProvider.addResponseInterceptor(
275
+ onFulfilled,
276
+ onRejected
277
+ );
278
+ }
279
+ removeResponseInterceptor(id) {
280
+ this.axiosHttpProvider.removeResponseInterceptor(id);
281
+ }
282
+ };
283
+ HttpProvider = __decorateClass([
284
+ (0, import_common.Injectable)(),
285
+ __decorateParam(0, (0, import_common.Inject)(HTTP_AXIOS_PROVIDER))
286
+ ], HttpProvider);
287
+
288
+ // src/implementations/axios/axios.http.module.ts
289
+ var import_common2 = require("@nestjs/common");
290
+ var import_axios2 = __toESM(require("axios"));
291
+
292
+ // src/implementations/axios/axios.http.provider.ts
293
+ var import_axios = __toESM(require("axios"));
294
+ var import_rxjs = require("rxjs");
295
+ var AxiosHttpProvider = class _AxiosHttpProvider {
296
+ axiosInstance;
297
+ errorInterceptors = /* @__PURE__ */ new Map();
298
+ nextErrorInterceptorId = 0;
299
+ requestInterceptorIds = /* @__PURE__ */ new Set();
300
+ responseInterceptorIds = /* @__PURE__ */ new Set();
301
+ cache = /* @__PURE__ */ new Map();
302
+ cacheCleanupInterval;
303
+ constructor(axiosInstance) {
304
+ this.axiosInstance = axiosInstance || import_axios.default.create();
305
+ this.startCacheCleanup();
306
+ }
307
+ /**
308
+ * Starts automatic cache cleanup
309
+ */
310
+ startCacheCleanup() {
311
+ this.cacheCleanupInterval = setInterval(() => {
312
+ this.cleanupExpiredCache();
313
+ }, 3e5);
314
+ }
315
+ /**
316
+ * Stops automatic cache cleanup
317
+ */
318
+ stopCacheCleanup() {
319
+ if (this.cacheCleanupInterval) {
320
+ clearInterval(this.cacheCleanupInterval);
321
+ this.cacheCleanupInterval = void 0;
322
+ }
323
+ }
324
+ /**
325
+ * Cleans up expired cache entries
326
+ */
327
+ cleanupExpiredCache() {
328
+ const now = Date.now();
329
+ const keysToDelete = [];
330
+ for (const [key, entry] of this.cache.entries()) {
331
+ if (now - entry.timestamp > entry.ttl) {
332
+ keysToDelete.push(key);
333
+ }
334
+ }
335
+ keysToDelete.forEach((key) => this.cache.delete(key));
336
+ }
337
+ /**
338
+ * Generates a cache key from URL and config
339
+ */
340
+ generateCacheKey(url, config) {
341
+ const params = (config == null ? void 0 : config.params) ? JSON.stringify(config.params) : "";
342
+ return `${url}${params}`;
343
+ }
344
+ /**
345
+ * Gets cached data if valid
346
+ */
347
+ getCached(key) {
348
+ const entry = this.cache.get(key);
349
+ if (!entry) return null;
350
+ const now = Date.now();
351
+ if (now - entry.timestamp > entry.ttl) {
352
+ this.cache.delete(key);
353
+ return null;
354
+ }
355
+ return entry.data;
356
+ }
357
+ /**
358
+ * Sets data in cache
359
+ */
360
+ setCache(key, data, ttl = 3e5) {
361
+ this.cache.set(key, {
362
+ data,
363
+ timestamp: Date.now(),
364
+ ttl
365
+ });
366
+ }
367
+ /**
368
+ * Clears cache for a specific key or all cache
369
+ */
370
+ clearCache(key) {
371
+ if (key) {
372
+ this.cache.delete(key);
373
+ } else {
374
+ this.cache.clear();
375
+ }
376
+ }
377
+ /**
378
+ * Gets cache statistics
379
+ */
380
+ getCacheStats() {
381
+ return {
382
+ size: this.cache.size,
383
+ keys: Array.from(this.cache.keys())
384
+ };
385
+ }
386
+ /**
387
+ * Sets a global header that will be included in all requests.
388
+ */
389
+ setGlobalHeader(key, value) {
390
+ this.axiosInstance.defaults.headers.common[key] = value;
391
+ }
392
+ /**
393
+ * Removes a global header.
394
+ */
395
+ removeGlobalHeader(key) {
396
+ delete this.axiosInstance.defaults.headers.common[key];
397
+ }
398
+ /**
399
+ * Gets all global headers currently set.
400
+ */
401
+ getGlobalHeaders() {
402
+ return { ...this.axiosInstance.defaults.headers.common };
403
+ }
404
+ /**
405
+ * Sets the base URL for all requests.
406
+ */
407
+ setBaseUrl(baseUrl) {
408
+ this.axiosInstance.defaults.baseURL = baseUrl;
409
+ }
410
+ /**
411
+ * Gets the current base URL.
412
+ */
413
+ getBaseUrl() {
414
+ return this.axiosInstance.defaults.baseURL || "";
415
+ }
416
+ /**
417
+ * Sets the default timeout for all requests.
418
+ */
419
+ setDefaultTimeout(timeout) {
420
+ this.axiosInstance.defaults.timeout = timeout;
421
+ }
422
+ /**
423
+ * Adds an error interceptor for handling errors globally.
424
+ */
425
+ addErrorInterceptor(interceptor) {
426
+ const id = this.nextErrorInterceptorId++;
427
+ this.errorInterceptors.set(id, interceptor);
428
+ return id;
429
+ }
430
+ /**
431
+ * Adds a request interceptor to the underlying Axios instance.
432
+ */
433
+ addRequestInterceptor(onFulfilled, onRejected) {
434
+ const id = this.axiosInstance.interceptors.request.use(
435
+ onFulfilled,
436
+ onRejected
437
+ );
438
+ this.requestInterceptorIds.add(id);
439
+ return id;
440
+ }
441
+ /**
442
+ * Removes a request interceptor by id.
443
+ */
444
+ removeRequestInterceptor(id) {
445
+ this.axiosInstance.interceptors.request.eject(id);
446
+ this.requestInterceptorIds.delete(id);
447
+ }
448
+ /**
449
+ * Adds a response interceptor to the underlying Axios instance.
450
+ */
451
+ addResponseInterceptor(onFulfilled, onRejected) {
452
+ const id = this.axiosInstance.interceptors.response.use(
453
+ onFulfilled,
454
+ onRejected
455
+ );
456
+ this.responseInterceptorIds.add(id);
457
+ return id;
458
+ }
459
+ /**
460
+ * Removes a response interceptor by id.
461
+ */
462
+ removeResponseInterceptor(id) {
463
+ this.axiosInstance.interceptors.response.eject(id);
464
+ this.responseInterceptorIds.delete(id);
465
+ }
466
+ /**
467
+ * Removes an error interceptor by its ID.
468
+ */
469
+ removeErrorInterceptor(id) {
470
+ this.errorInterceptors.delete(id);
471
+ }
472
+ /**
473
+ * Performs a GET request to the specified URL.
474
+ */
475
+ async get(url, config) {
476
+ const cacheKey = this.generateCacheKey(url, config);
477
+ const cached = this.getCached(cacheKey);
478
+ if (cached) {
479
+ return {
480
+ data: cached,
481
+ status: 200,
482
+ statusText: "OK",
483
+ headers: {},
484
+ config: config || { url }
485
+ };
486
+ }
487
+ return this.wrapWithErrorInterceptors(
488
+ () => this.performGet(url, config, cacheKey)
489
+ );
490
+ }
491
+ /**
492
+ * Internal method to perform the actual GET request.
493
+ */
494
+ async performGet(url, config, cacheKey) {
495
+ const response = await this.axiosInstance.get(url, config);
496
+ const transformed = this.transformResponse(response);
497
+ if (cacheKey && (config == null ? void 0 : config.cache) !== false) {
498
+ this.setCache(cacheKey, transformed.data, config == null ? void 0 : config.cacheTtl);
499
+ }
500
+ return transformed;
501
+ }
502
+ /**
503
+ * Performs a GET request and returns an Observable.
504
+ */
505
+ get$(url, config) {
506
+ return (0, import_rxjs.from)(this.get(url, config));
507
+ }
508
+ /**
509
+ * Performs a POST request.
510
+ */
511
+ async post(url, data, config) {
512
+ return this.wrapWithErrorInterceptors(
513
+ () => this.performPost(url, data, config)
514
+ );
515
+ }
516
+ /**
517
+ * Internal method to perform the actual POST request.
518
+ */
519
+ async performPost(url, data, config) {
520
+ const response = await this.axiosInstance.post(url, data, config);
521
+ return this.transformResponse(response);
522
+ }
523
+ /**
524
+ * Performs a POST request and returns an Observable.
525
+ */
526
+ post$(url, data, config) {
527
+ return (0, import_rxjs.from)(this.post(url, data, config));
528
+ }
529
+ /**
530
+ * Performs a PUT request.
531
+ */
532
+ async put(url, data, config) {
533
+ return this.wrapWithErrorInterceptors(
534
+ () => this.performPut(url, data, config)
535
+ );
536
+ }
537
+ /**
538
+ * Internal method to perform the actual PUT request.
539
+ */
540
+ async performPut(url, data, config) {
541
+ const response = await this.axiosInstance.put(url, data, config);
542
+ return this.transformResponse(response);
543
+ }
544
+ /**
545
+ * Performs a PUT request and returns an Observable.
546
+ */
547
+ put$(url, data, config) {
548
+ return (0, import_rxjs.from)(this.put(url, data, config));
549
+ }
550
+ /**
551
+ * Performs a PATCH request.
552
+ */
553
+ async patch(url, data, config) {
554
+ return this.wrapWithErrorInterceptors(
555
+ () => this.performPatch(url, data, config)
556
+ );
557
+ }
558
+ /**
559
+ * Internal method to perform the actual PATCH request.
560
+ */
561
+ async performPatch(url, data, config) {
562
+ const response = await this.axiosInstance.patch(url, data, config);
563
+ return this.transformResponse(response);
564
+ }
565
+ /**
566
+ * Performs a PATCH request and returns an Observable.
567
+ */
568
+ patch$(url, data, config) {
569
+ return (0, import_rxjs.from)(this.patch(url, data, config));
570
+ }
571
+ /**
572
+ * Performs a DELETE request.
573
+ */
574
+ async delete(url, config) {
575
+ return this.wrapWithErrorInterceptors(
576
+ () => this.performDelete(url, config)
577
+ );
578
+ }
579
+ /**
580
+ * Internal method to perform the actual DELETE request.
581
+ */
582
+ async performDelete(url, config) {
583
+ const response = await this.axiosInstance.delete(url, config);
584
+ return this.transformResponse(response);
585
+ }
586
+ /**
587
+ * Performs a DELETE request and returns an Observable.
588
+ */
589
+ delete$(url, config) {
590
+ return (0, import_rxjs.from)(this.delete(url, config));
591
+ }
592
+ /**
593
+ * Performs a HEAD request.
594
+ */
595
+ async head(url, config) {
596
+ return this.wrapWithErrorInterceptors(
597
+ () => this.performHead(url, config)
598
+ );
599
+ }
600
+ /**
601
+ * Internal method to perform the actual HEAD request.
602
+ */
603
+ async performHead(url, config) {
604
+ const response = await this.axiosInstance.head(url, config);
605
+ return this.transformResponse(response);
606
+ }
607
+ /**
608
+ * Performs a HEAD request and returns an Observable.
609
+ */
610
+ head$(url, config) {
611
+ return (0, import_rxjs.from)(this.head(url, config));
612
+ }
613
+ /**
614
+ * Performs an OPTIONS request.
615
+ */
616
+ async options(url, config) {
617
+ return this.wrapWithErrorInterceptors(
618
+ () => this.performOptions(url, config)
619
+ );
620
+ }
621
+ /**
622
+ * Internal method to perform the actual OPTIONS request.
623
+ */
624
+ async performOptions(url, config) {
625
+ const response = await this.axiosInstance.options(url, config);
626
+ return this.transformResponse(response);
627
+ }
628
+ /**
629
+ * Performs an OPTIONS request and returns an Observable.
630
+ */
631
+ options$(url, config) {
632
+ return (0, import_rxjs.from)(this.options(url, config));
633
+ }
634
+ /**
635
+ * Performs a custom HTTP request.
636
+ */
637
+ async request(config) {
638
+ return this.wrapWithErrorInterceptors(() => this.performRequest(config));
639
+ }
640
+ /**
641
+ * Internal method to perform the actual custom request.
642
+ */
643
+ async performRequest(config) {
644
+ const response = await this.axiosInstance.request(config);
645
+ return this.transformResponse(response);
646
+ }
647
+ /**
648
+ * Performs a custom HTTP request and returns an Observable.
649
+ */
650
+ request$(config) {
651
+ return (0, import_rxjs.from)(this.request(config));
652
+ }
653
+ /**
654
+ * Sets the authorization token for requests.
655
+ */
656
+ setAuthToken(token, type = "Bearer") {
657
+ this.axiosInstance.defaults.headers.common["Authorization"] = `${type} ${token}`;
658
+ try {
659
+ const masked = _AxiosHttpProvider.maskToken(token);
660
+ console.debug(`[AxiosHttpProvider] setAuthToken ${type} ${masked}`);
661
+ } catch (err) {
662
+ }
663
+ }
664
+ /**
665
+ * Clears the authorization token.
666
+ */
667
+ clearAuthToken() {
668
+ delete this.axiosInstance.defaults.headers.common["Authorization"];
669
+ }
670
+ /**
671
+ * Processes an error through all registered error interceptors.
672
+ */
673
+ async processErrorInterceptors(error) {
674
+ let processedError = error;
675
+ for (const interceptor of this.errorInterceptors.values()) {
676
+ try {
677
+ processedError = await interceptor(processedError);
678
+ } catch (interceptorError) {
679
+ console.warn("Error interceptor failed:", interceptorError);
680
+ }
681
+ }
682
+ return processedError;
683
+ }
684
+ /**
685
+ * Returns a masked version of the token showing only the initial characters.
686
+ */
687
+ static maskToken(token, visibleChars = 8) {
688
+ if (!token || typeof token !== "string") return "";
689
+ return token.length <= visibleChars ? token : `${token.slice(0, visibleChars)}...`;
690
+ }
691
+ /**
692
+ * Wraps a promise-returning HTTP method with error interceptor processing.
693
+ */
694
+ async wrapWithErrorInterceptors(method) {
695
+ try {
696
+ return await method();
697
+ } catch (error) {
698
+ const processedError = await this.processErrorInterceptors(error);
699
+ try {
700
+ const { ErrorMapperService: ErrorMapperService2 } = (init_error_mapper_service(), __toCommonJS(error_mapper_service_exports));
701
+ const { HttpClientError: HttpClientError2 } = (init_http_client_error(), __toCommonJS(http_client_error_exports));
702
+ const mapper = new ErrorMapperService2();
703
+ const mapped = mapper.mapUpstreamError(processedError);
704
+ throw new HttpClientError2(mapped.message, mapped.status, mapped.code, mapped.context);
705
+ } catch (mapErr) {
706
+ const { HttpClientError: HttpClientError2 } = (init_http_client_error(), __toCommonJS(http_client_error_exports));
707
+ const fallback = new HttpClientError2(
708
+ processedError && processedError.message || "HTTP client error",
709
+ processedError && processedError.status || 502,
710
+ processedError && processedError.code || void 0,
711
+ { original: String(processedError) }
712
+ );
713
+ throw fallback;
714
+ }
715
+ }
716
+ }
717
+ /**
718
+ * Transforms an Axios response to the standardized HTTP response format.
719
+ */
720
+ transformResponse(response) {
721
+ return {
722
+ data: response.data,
723
+ status: response.status,
724
+ statusText: response.statusText,
725
+ headers: response.headers,
726
+ config: response.config
727
+ };
728
+ }
729
+ };
730
+
731
+ // src/implementations/axios/axios.http.module.ts
732
+ var HttpImplementationAxiosModule = class {
733
+ static forRoot(config) {
734
+ const axiosInstance = config && config.request ? config : import_axios2.default.create(config);
735
+ const providers = [
736
+ {
737
+ provide: HTTP_AXIOS_CONNECTION,
738
+ useValue: axiosInstance
739
+ },
740
+ {
741
+ provide: HTTP_AXIOS_PROVIDER,
742
+ useFactory: (conn) => new AxiosHttpProvider(conn),
743
+ inject: [HTTP_AXIOS_CONNECTION]
744
+ }
745
+ ];
746
+ return {
747
+ module: HttpImplementationAxiosModule,
748
+ providers,
749
+ exports: [HTTP_AXIOS_CONNECTION, HTTP_AXIOS_PROVIDER]
750
+ };
751
+ }
752
+ };
753
+ HttpImplementationAxiosModule = __decorateClass([
754
+ (0, import_common2.Module)({})
755
+ ], HttpImplementationAxiosModule);
756
+
757
+ // src/http.module.ts
758
+ var HttpModule = class {
759
+ /**
760
+ * Configure HttpModule with an Axios instance or AxiosRequestConfig.
761
+ * This will import the implementation-specific module (currently Axios).
762
+ */
763
+ static forRoot(config) {
764
+ return {
765
+ module: HttpModule,
766
+ imports: [HttpImplementationAxiosModule.forRoot(config)],
767
+ providers: [
768
+ {
769
+ provide: HTTP_PROVIDER,
770
+ useClass: HttpProvider
771
+ }
772
+ ],
773
+ exports: [HTTP_PROVIDER]
774
+ };
775
+ }
776
+ };
777
+ HttpModule = __decorateClass([
778
+ (0, import_common3.Module)({})
779
+ ], HttpModule);
780
+ // Annotate the CommonJS export names for ESM import in node:
781
+ 0 && (module.exports = {
782
+ HTTP_AXIOS_CONNECTION,
783
+ HTTP_AXIOS_PROVIDER,
784
+ HTTP_PROVIDER,
785
+ HttpImplementationAxiosModule,
786
+ HttpModule
787
+ });