@diia-inhouse/http 1.9.0 → 4.0.0

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 (32) hide show
  1. package/LICENCE.md +59 -59
  2. package/dist/interfaces/{http.js → httpClient.js} +1 -1
  3. package/dist/interfaces/httpClient.js.map +1 -0
  4. package/dist/interfaces/index.js +1 -1
  5. package/dist/interfaces/index.js.map +1 -1
  6. package/dist/services/errors/index.js +8 -0
  7. package/dist/services/errors/index.js.map +1 -0
  8. package/dist/services/errors/operationError.js +13 -0
  9. package/dist/services/errors/operationError.js.map +1 -0
  10. package/dist/services/errors/requestError.js +15 -0
  11. package/dist/services/errors/requestError.js.map +1 -0
  12. package/dist/services/httpClient.js +205 -141
  13. package/dist/services/httpClient.js.map +1 -1
  14. package/dist/services/index.js +3 -1
  15. package/dist/services/index.js.map +1 -1
  16. package/dist/services/utils.js +22 -0
  17. package/dist/services/utils.js.map +1 -0
  18. package/dist/types/interfaces/deps.d.ts +3 -5
  19. package/dist/types/interfaces/httpClient.d.ts +73 -0
  20. package/dist/types/interfaces/index.d.ts +1 -1
  21. package/dist/types/services/errors/index.d.ts +2 -0
  22. package/dist/types/services/errors/operationError.d.ts +4 -0
  23. package/dist/types/services/errors/requestError.d.ts +5 -0
  24. package/dist/types/services/httpClient.d.ts +18 -15
  25. package/dist/types/services/index.d.ts +1 -1
  26. package/dist/types/services/utils.d.ts +4 -0
  27. package/package.json +40 -42
  28. package/dist/interfaces/http.js.map +0 -1
  29. package/dist/services/http.js +0 -200
  30. package/dist/services/http.js.map +0 -1
  31. package/dist/types/interfaces/http.d.ts +0 -16
  32. package/dist/types/services/http.d.ts +0 -23
package/LICENCE.md CHANGED
@@ -18,40 +18,40 @@ or has expressed by any other means his willingness to license under the EUPL.
18
18
 
19
19
  In this Licence, the following terms have the following meaning:
20
20
 
21
- - ‘The Licence’: this Licence.
21
+ - ‘The Licence’: this Licence.
22
22
 
23
- - ‘The Original Work’: the work or software distributed or communicated by the
24
- Licensor under this Licence, available as Source Code and also as Executable
25
- Code as the case may be.
23
+ - ‘The Original Work’: the work or software distributed or communicated by the
24
+ Licensor under this Licence, available as Source Code and also as Executable
25
+ Code as the case may be.
26
26
 
27
- - ‘Derivative Works’: the works or software that could be created by the
28
- Licensee, based upon the Original Work or modifications thereof. This Licence
29
- does not define the extent of modification or dependence on the Original Work
30
- required in order to classify a work as a Derivative Work; this extent is
31
- determined by copyright law applicable in the country mentioned in Article 15.
27
+ - ‘Derivative Works’: the works or software that could be created by the
28
+ Licensee, based upon the Original Work or modifications thereof. This Licence
29
+ does not define the extent of modification or dependence on the Original Work
30
+ required in order to classify a work as a Derivative Work; this extent is
31
+ determined by copyright law applicable in the country mentioned in Article 15.
32
32
 
33
- - ‘The Work’: the Original Work or its Derivative Works.
33
+ - ‘The Work’: the Original Work or its Derivative Works.
34
34
 
35
- - ‘The Source Code’: the human-readable form of the Work which is the most
36
- convenient for people to study and modify.
35
+ - ‘The Source Code’: the human-readable form of the Work which is the most
36
+ convenient for people to study and modify.
37
37
 
38
- - ‘The Executable Code’: any code which has generally been compiled and which is
39
- meant to be interpreted by a computer as a program.
38
+ - ‘The Executable Code’: any code which has generally been compiled and which is
39
+ meant to be interpreted by a computer as a program.
40
40
 
41
- - ‘The Licensor’: the natural or legal person that distributes or communicates
42
- the Work under the Licence.
41
+ - ‘The Licensor’: the natural or legal person that distributes or communicates
42
+ the Work under the Licence.
43
43
 
44
- - ‘Contributor(s)’: any natural or legal person who modifies the Work under the
45
- Licence, or otherwise contributes to the creation of a Derivative Work.
44
+ - ‘Contributor(s)’: any natural or legal person who modifies the Work under the
45
+ Licence, or otherwise contributes to the creation of a Derivative Work.
46
46
 
47
- - ‘The Licensee’ or ‘You’: any natural or legal person who makes any usage of
48
- the Work under the terms of the Licence.
47
+ - ‘The Licensee’ or ‘You’: any natural or legal person who makes any usage of
48
+ the Work under the terms of the Licence.
49
49
 
50
- - ‘Distribution’ or ‘Communication’: any act of selling, giving, lending,
51
- renting, distributing, communicating, transmitting, or otherwise making
52
- available, online or offline, copies of the Work or providing access to its
53
- essential functionalities at the disposal of any other natural or legal
54
- person.
50
+ - ‘Distribution’ or ‘Communication’: any act of selling, giving, lending,
51
+ renting, distributing, communicating, transmitting, or otherwise making
52
+ available, online or offline, copies of the Work or providing access to its
53
+ essential functionalities at the disposal of any other natural or legal
54
+ person.
55
55
 
56
56
  2. Scope of the rights granted by the Licence
57
57
 
@@ -59,15 +59,15 @@ The Licensor hereby grants You a worldwide, royalty-free, non-exclusive,
59
59
  sublicensable licence to do the following, for the duration of copyright vested
60
60
  in the Original Work:
61
61
 
62
- - use the Work in any circumstance and for all usage,
63
- - reproduce the Work,
64
- - modify the Work, and make Derivative Works based upon the Work,
65
- - communicate to the public, including the right to make available or display
66
- the Work or copies thereof to the public and perform publicly, as the case may
67
- be, the Work,
68
- - distribute the Work or copies thereof,
69
- - lend and rent the Work or copies thereof,
70
- - sublicense rights in the Work or copies thereof.
62
+ - use the Work in any circumstance and for all usage,
63
+ - reproduce the Work,
64
+ - modify the Work, and make Derivative Works based upon the Work,
65
+ - communicate to the public, including the right to make available or display
66
+ the Work or copies thereof to the public and perform publicly, as the case may
67
+ be, the Work,
68
+ - distribute the Work or copies thereof,
69
+ - lend and rent the Work or copies thereof,
70
+ - sublicense rights in the Work or copies thereof.
71
71
 
72
72
  Those rights can be exercised on any media, supports and formats, whether now
73
73
  known or later invented, as far as the applicable law permits so.
@@ -240,43 +240,43 @@ their choice.
240
240
 
241
241
  Without prejudice to specific agreement between parties,
242
242
 
243
- - any litigation resulting from the interpretation of this License, arising
244
- between the European Union institutions, bodies, offices or agencies, as a
245
- Licensor, and any Licensee, will be subject to the jurisdiction of the Court
246
- of Justice of the European Union, as laid down in article 272 of the Treaty on
247
- the Functioning of the European Union,
243
+ - any litigation resulting from the interpretation of this License, arising
244
+ between the European Union institutions, bodies, offices or agencies, as a
245
+ Licensor, and any Licensee, will be subject to the jurisdiction of the Court
246
+ of Justice of the European Union, as laid down in article 272 of the Treaty on
247
+ the Functioning of the European Union,
248
248
 
249
- - any litigation arising between other parties and resulting from the
250
- interpretation of this License, will be subject to the exclusive jurisdiction
251
- of the competent court where the Licensor resides or conducts its primary
252
- business.
249
+ - any litigation arising between other parties and resulting from the
250
+ interpretation of this License, will be subject to the exclusive jurisdiction
251
+ of the competent court where the Licensor resides or conducts its primary
252
+ business.
253
253
 
254
254
  15. Applicable Law
255
255
 
256
256
  Without prejudice to specific agreement between parties,
257
257
 
258
- - this Licence shall be governed by the law of the European Union Member State
259
- where the Licensor has his seat, resides or has his registered office,
258
+ - this Licence shall be governed by the law of the European Union Member State
259
+ where the Licensor has his seat, resides or has his registered office,
260
260
 
261
- - this licence shall be governed by Belgian law if the Licensor has no seat,
262
- residence or registered office inside a European Union Member State.
261
+ - this licence shall be governed by Belgian law if the Licensor has no seat,
262
+ residence or registered office inside a European Union Member State.
263
263
 
264
264
  Appendix
265
265
 
266
266
  ‘Compatible Licences’ according to Article 5 EUPL are:
267
267
 
268
- - GNU General Public License (GPL) v. 2, v. 3
269
- - GNU Affero General Public License (AGPL) v. 3
270
- - Open Software License (OSL) v. 2.1, v. 3.0
271
- - Eclipse Public License (EPL) v. 1.0
272
- - CeCILL v. 2.0, v. 2.1
273
- - Mozilla Public Licence (MPL) v. 2
274
- - GNU Lesser General Public Licence (LGPL) v. 2.1, v. 3
275
- - Creative Commons Attribution-ShareAlike v. 3.0 Unported (CC BY-SA 3.0) for
276
- works other than software
277
- - European Union Public Licence (EUPL) v. 1.1, v. 1.2
278
- - Québec Free and Open-Source Licence — Reciprocity (LiLiQ-R) or Strong
279
- Reciprocity (LiLiQ-R+).
268
+ - GNU General Public License (GPL) v. 2, v. 3
269
+ - GNU Affero General Public License (AGPL) v. 3
270
+ - Open Software License (OSL) v. 2.1, v. 3.0
271
+ - Eclipse Public License (EPL) v. 1.0
272
+ - CeCILL v. 2.0, v. 2.1
273
+ - Mozilla Public Licence (MPL) v. 2
274
+ - GNU Lesser General Public Licence (LGPL) v. 2.1, v. 3
275
+ - Creative Commons Attribution-ShareAlike v. 3.0 Unported (CC BY-SA 3.0) for
276
+ works other than software
277
+ - European Union Public Licence (EUPL) v. 1.1, v. 1.2
278
+ - Québec Free and Open-Source Licence — Reciprocity (LiLiQ-R) or Strong
279
+ Reciprocity (LiLiQ-R+).
280
280
 
281
281
  The European Commission may update this Appendix to later versions of the above
282
282
  licences without producing a new version of the EUPL, as long as they provide
@@ -1,3 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=http.js.map
3
+ //# sourceMappingURL=httpClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"httpClient.js","sourceRoot":"","sources":["../../src/interfaces/httpClient.ts"],"names":[],"mappings":""}
@@ -15,5 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./deps"), exports);
18
- __exportStar(require("./http"), exports);
18
+ __exportStar(require("./httpClient"), exports);
19
19
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yCAAsB;AAEtB,yCAAsB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yCAAsB;AAEtB,+CAA4B"}
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RequestError = exports.OperationError = void 0;
4
+ var operationError_1 = require("./operationError");
5
+ Object.defineProperty(exports, "OperationError", { enumerable: true, get: function () { return operationError_1.OperationError; } });
6
+ var requestError_1 = require("./requestError");
7
+ Object.defineProperty(exports, "RequestError", { enumerable: true, get: function () { return requestError_1.RequestError; } });
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/services/errors/index.ts"],"names":[],"mappings":";;;AAAA,mDAAiD;AAAxC,gHAAA,cAAc,OAAA;AAEvB,+CAA6C;AAApC,4GAAA,YAAY,OAAA"}
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OperationError = void 0;
4
+ const types_1 = require("@diia-inhouse/types");
5
+ const requestError_1 = require("./requestError");
6
+ class OperationError extends requestError_1.RequestError {
7
+ constructor(message, originalError) {
8
+ super(message, types_1.HttpStatusCode.INTERNAL_SERVER_ERROR, originalError);
9
+ this.name = 'OperationError';
10
+ }
11
+ }
12
+ exports.OperationError = OperationError;
13
+ //# sourceMappingURL=operationError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"operationError.js","sourceRoot":"","sources":["../../../src/services/errors/operationError.ts"],"names":[],"mappings":";;;AAAA,+CAAoD;AAEpD,iDAA6C;AAE7C,MAAa,cAAe,SAAQ,2BAAY;IAC5C,YAAY,OAAe,EAAE,aAAoB;QAC7C,KAAK,CAAC,OAAO,EAAE,sBAAc,CAAC,qBAAqB,EAAE,aAAa,CAAC,CAAA;QAEnE,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAA;IAChC,CAAC;CACJ;AAND,wCAMC"}
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RequestError = void 0;
4
+ class RequestError extends Error {
5
+ statusCode;
6
+ originalError;
7
+ constructor(message, statusCode, originalError) {
8
+ super(message);
9
+ this.name = 'RequestError';
10
+ this.statusCode = statusCode;
11
+ this.originalError = originalError;
12
+ }
13
+ }
14
+ exports.RequestError = RequestError;
15
+ //# sourceMappingURL=requestError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"requestError.js","sourceRoot":"","sources":["../../../src/services/errors/requestError.ts"],"names":[],"mappings":";;;AAAA,MAAa,YAAa,SAAQ,KAAK;IAC1B,UAAU,CAAQ;IAElB,aAAa,CAAO;IAE7B,YAAY,OAAe,EAAE,UAAkB,EAAE,aAAoB;QACjE,KAAK,CAAC,OAAO,CAAC,CAAA;QAEd,IAAI,CAAC,IAAI,GAAG,cAAc,CAAA;QAC1B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,aAAa,GAAG,aAAa,CAAA;IACtC,CAAC;CACJ;AAZD,oCAYC"}
@@ -1,168 +1,232 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
5
35
  Object.defineProperty(exports, "__esModule", { value: true });
6
36
  exports.HttpClientService = void 0;
7
- /* eslint-disable n/no-unsupported-features/node-builtins */
8
- const node_http_1 = require("node:http");
9
- const node_https_1 = require("node:https");
10
- const node_querystring_1 = require("node:querystring");
11
- const await_to_js_1 = __importDefault(require("await-to-js"));
12
- const lodash_1 = require("lodash");
37
+ const api_1 = require("@opentelemetry/api");
38
+ const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
39
+ const axios_1 = __importStar(require("axios"));
40
+ const diia_metrics_1 = require("@diia-inhouse/diia-metrics");
13
41
  const errors_1 = require("@diia-inhouse/errors");
14
42
  const types_1 = require("@diia-inhouse/types");
43
+ const errors_2 = require("./errors");
44
+ const utils_1 = require("./utils");
15
45
  class HttpClientService {
16
46
  logger;
17
- binaryMimeTypes = ['application/pdf', 'application/p7s'];
18
- binaryMimeTypesPrefixes = ['image/'];
19
- constructor(logger) {
47
+ metrics;
48
+ systemServiceName;
49
+ timeout;
50
+ baseUrl;
51
+ constructor(logger, metrics, systemServiceName, timeout = types_1.DurationMs.Second * 30, baseUrl = '') {
20
52
  this.logger = logger;
53
+ this.metrics = metrics;
54
+ this.systemServiceName = systemServiceName;
55
+ this.timeout = timeout;
56
+ this.baseUrl = baseUrl;
21
57
  }
22
- async get(options, hostFingerprint) {
23
- this.setCheckServerIdentity(options, hostFingerprint);
24
- return await (0, await_to_js_1.default)(this.makeRequest(types_1.HttpMethod.GET, options));
58
+ async get(path, opts) {
59
+ return await this.request({
60
+ path,
61
+ method: types_1.HttpMethod.GET,
62
+ ...opts,
63
+ });
25
64
  }
26
- async post(options, hostFingerprint, body) {
27
- this.setCheckServerIdentity(options, hostFingerprint);
28
- return await (0, await_to_js_1.default)(this.makeRequest(types_1.HttpMethod.POST, options, body));
65
+ async post(path, opts) {
66
+ return await this.request({
67
+ path,
68
+ method: types_1.HttpMethod.POST,
69
+ ...opts,
70
+ });
29
71
  }
30
- async put(options, hostFingerprint, body) {
31
- this.setCheckServerIdentity(options, hostFingerprint);
32
- return await (0, await_to_js_1.default)(this.makeRequest(types_1.HttpMethod.PUT, options, body));
72
+ async put(path, opts) {
73
+ return await this.request({
74
+ path,
75
+ method: types_1.HttpMethod.PUT,
76
+ ...opts,
77
+ });
33
78
  }
34
- async delete(options, hostFingerprint, body) {
35
- this.setCheckServerIdentity(options, hostFingerprint);
36
- return await (0, await_to_js_1.default)(this.makeRequest(types_1.HttpMethod.DELETE, options, body));
79
+ async delete(path, opts) {
80
+ return await this.request({
81
+ path,
82
+ method: types_1.HttpMethod.DELETE,
83
+ ...opts,
84
+ });
37
85
  }
38
- makeRequest(method, options, body) {
39
- options.method = method;
40
- let parsedHost;
41
- try {
42
- parsedHost = new URL(options.host || '');
43
- }
44
- catch (err) {
45
- const msg = `Host "${options.host}" must include protocol`;
46
- this.logger.error(msg, { err });
47
- throw new Error(msg);
86
+ async patch(path, opts) {
87
+ return await this.request({
88
+ path,
89
+ method: types_1.HttpMethod.PATCH,
90
+ ...opts,
91
+ });
92
+ }
93
+ async request(opts) {
94
+ const activeContext = api_1.context.active();
95
+ const tracer = api_1.trace.getTracer(this.systemServiceName);
96
+ const baseUrl = opts.baseUrl || this.baseUrl;
97
+ if (!baseUrl) {
98
+ throw new Error('Base URL is not provided');
48
99
  }
49
- options.host = parsedHost.hostname;
50
- options.port = parsedHost.port || options.port;
51
- let requestFn;
52
- if (parsedHost.protocol === 'https:') {
53
- requestFn = node_https_1.request;
100
+ const span = tracer.startSpan(opts.method, {
101
+ kind: api_1.SpanKind.CLIENT,
102
+ attributes: {
103
+ [semantic_conventions_1.SEMATTRS_MESSAGING_SYSTEM]: diia_metrics_1.RequestMechanism.Http,
104
+ [semantic_conventions_1.SemanticAttributes.HTTP_METHOD]: opts.method,
105
+ [semantic_conventions_1.SemanticAttributes.HTTP_URL]: `${baseUrl}${opts.path}`,
106
+ [semantic_conventions_1.SemanticAttributes.HTTP_TARGET]: opts.path,
107
+ 'messaging.caller': this.systemServiceName,
108
+ },
109
+ }, activeContext);
110
+ const { response, error, retryCount } = await this.requestHelper({ ...opts, baseUrl }, span);
111
+ span?.setAttribute('retryCount', retryCount);
112
+ if (response) {
113
+ const statusCode = response.status;
114
+ this.logger.info('HTTP request succeeded', {
115
+ statusCode,
116
+ path: opts.path,
117
+ method: opts.method,
118
+ });
119
+ span?.setStatus({ code: api_1.SpanStatusCode.OK });
120
+ span?.setAttribute(semantic_conventions_1.SemanticAttributes.HTTP_STATUS_CODE, statusCode);
121
+ span?.end();
122
+ return {
123
+ isOk: true,
124
+ statusCode,
125
+ headers: response.headers || {},
126
+ body: response.data,
127
+ };
54
128
  }
55
- else if (parsedHost.protocol === 'http:') {
56
- requestFn = node_http_1.request;
129
+ if (error) {
130
+ const { statusCode, originalError } = error;
131
+ this.logger.error('HTTP request failed', {
132
+ err: error,
133
+ method: opts.method,
134
+ path: opts.path,
135
+ statusCode,
136
+ });
137
+ span?.setStatus({ code: api_1.SpanStatusCode.ERROR });
138
+ span?.recordException({
139
+ name: originalError.name || 'Unexpected error',
140
+ message: originalError.message,
141
+ code: statusCode,
142
+ });
143
+ span?.end();
144
+ return {
145
+ isOk: false,
146
+ statusCode,
147
+ body: originalError instanceof axios_1.AxiosError ? originalError.response?.data : undefined,
148
+ headers: originalError instanceof axios_1.AxiosError ? originalError.response?.headers || {} : {},
149
+ };
57
150
  }
58
- else {
59
- throw new Error(`Unknown protocol, ${parsedHost.protocol}`);
151
+ throw new Error('Unexpected error caused');
152
+ }
153
+ async requestHelper(opts, span, currentRequestRetries = 0) {
154
+ const timeout = opts.timeout || this.timeout;
155
+ const { method, path, baseUrl, query = {}, body, headers = {}, responseType = 'json', retries = 0, retryInterval = 0, httpsAgent, httpAgent, errorType: errorTypeOps, metricLabel, } = opts;
156
+ const startTime = process.hrtime.bigint();
157
+ span.addEvent('request', { message: `Started processing request. Retry count ${currentRequestRetries}` });
158
+ try {
159
+ const response = await axios_1.default.request({
160
+ method,
161
+ baseURL: baseUrl,
162
+ url: path,
163
+ params: query,
164
+ data: body,
165
+ headers,
166
+ responseType,
167
+ timeout,
168
+ httpsAgent,
169
+ httpAgent,
170
+ });
171
+ this.observeSuccessRequest({
172
+ statusCode: response.status,
173
+ retryCount: currentRequestRetries,
174
+ startTime,
175
+ span,
176
+ baseUrl: baseUrl,
177
+ metricLabel,
178
+ });
179
+ return { response, retryCount: currentRequestRetries };
60
180
  }
61
- return new Promise((resolve, reject) => {
62
- try {
63
- const data = [];
64
- const request = requestFn(options, (response) => {
65
- if (!this.isBinaryContentType(response.headers)) {
66
- response.setEncoding('utf8');
67
- }
68
- response.on('data', (chunk) => {
69
- data.push(chunk);
70
- });
71
- response.on('end', () => {
72
- let parsedData;
73
- const res = (0, lodash_1.cloneDeep)(response);
74
- const { statusCode = '' } = res;
75
- const isSuccessStatusCode = /2\d{2}/.test(statusCode.toString());
76
- res.data = res.data || undefined;
77
- if (data.length === 0) {
78
- this.logger.info('No data in response', { statusCode });
79
- return isSuccessStatusCode ? resolve(res) : reject(res);
80
- }
81
- try {
82
- if (this.isJsonContentType(response.headers)) {
83
- parsedData = JSON.parse(data.join(''));
84
- res.data = parsedData;
85
- }
86
- else if (this.isBinaryContentType(response.headers)) {
87
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
88
- res.data = Buffer.concat(data);
89
- }
90
- else {
91
- res.data = data.join('');
92
- }
93
- }
94
- catch (err) {
95
- this.logger.error(`Failed to parse data: ${data}`);
96
- if (err instanceof Error) {
97
- return reject(err);
98
- }
99
- }
100
- if (!isSuccessStatusCode) {
101
- return reject(res);
102
- }
103
- return resolve(res);
181
+ catch (err) {
182
+ const statusCode = err?.response?.status || types_1.HttpStatusCode.INTERNAL_SERVER_ERROR;
183
+ const errorType = errors_1.ErrorType.External || errorTypeOps;
184
+ this.observeFailedRequest({
185
+ statusCode,
186
+ retryCount: currentRequestRetries,
187
+ startTime,
188
+ span,
189
+ errorType,
190
+ baseUrl: baseUrl,
191
+ metricLabel,
192
+ });
193
+ if (err instanceof axios_1.AxiosError) {
194
+ if (currentRequestRetries < retries) {
195
+ this.logger.info('Retrying HTTP request', {
196
+ err,
197
+ path,
198
+ retries: currentRequestRetries + 1,
104
199
  });
105
- });
106
- request.on('error', (err) => reject(err));
107
- request.on('timeout', () => {
108
- const msg = 'Failed due timeout reason';
109
- this.logger.error(msg);
110
- return reject(new errors_1.RequestTimeoutError(msg));
111
- });
112
- request.on('abort', () => {
113
- const msg = 'Failed due abort reason';
114
- this.logger.error(msg);
115
- return reject(new errors_1.ServiceUnavailableError(msg));
116
- });
117
- if (body) {
118
- const preparedBody = typeof body === 'string' ? body : (0, node_querystring_1.stringify)(body);
119
- request.write(preparedBody);
120
- }
121
- request.end();
122
- }
123
- catch (err) {
124
- if (err instanceof Error) {
125
- return reject(err);
200
+ return await (0, utils_1.waitAndRun)(() => this.requestHelper(opts, span, currentRequestRetries + 1), retryInterval);
126
201
  }
202
+ const requestError = new errors_2.RequestError(err.message, statusCode, err);
203
+ return { error: requestError, retryCount: currentRequestRetries };
127
204
  }
128
- });
129
- }
130
- isJsonContentType(headers) {
131
- const contentTypeHeader = headers['content-type'];
132
- if (!contentTypeHeader) {
133
- return false;
205
+ const operationError = new errors_2.OperationError('Internal package error', err);
206
+ return { error: operationError, retryCount: currentRequestRetries };
134
207
  }
135
- return /^application\/json/.test(contentTypeHeader);
136
208
  }
137
- isBinaryContentType(headers) {
138
- const contentTypeHeader = headers['content-type'];
139
- if (!contentTypeHeader) {
140
- return false;
141
- }
142
- return (this.binaryMimeTypes.includes(contentTypeHeader) ||
143
- this.binaryMimeTypesPrefixes.some((mimeTypePrefix) => contentTypeHeader.startsWith(mimeTypePrefix)));
209
+ observeSuccessRequest(params) {
210
+ const { statusCode, retryCount, startTime, span, baseUrl, metricLabel } = params;
211
+ const labels = this.getLabels(statusCode, diia_metrics_1.RequestStatus.Successful, baseUrl, metricLabel);
212
+ span.addEvent('request', { message: `Finished processing request. Retry count ${retryCount}` });
213
+ this.metrics.totalTimerMetric.observeSeconds(labels, process.hrtime.bigint() - startTime);
144
214
  }
145
- setCheckServerIdentity(options, hostFingerprint) {
146
- if (!hostFingerprint) {
147
- return;
148
- }
149
- const checkServerIdentity = (host, cert) => {
150
- const certFingerprint = cert.fingerprint256 || cert.fingerprint;
151
- this.logger.info(`Checking fingerprint for host: ${host}, fingerprint is ${certFingerprint}`);
152
- if (!hostFingerprint || hostFingerprint === certFingerprint) {
153
- this.logger.info('Fingerprint validated successfully');
154
- return;
155
- }
156
- this.logger.info('Failed to validate fingerprint');
157
- return new Error(`Fingerprint for host ${host} does not match`);
215
+ observeFailedRequest(params) {
216
+ const { statusCode, retryCount, startTime, span, errorType, baseUrl, metricLabel } = params;
217
+ const labels = this.getLabels(statusCode, diia_metrics_1.RequestStatus.Failed, baseUrl, metricLabel, errorType);
218
+ span.addEvent('request', { message: `Finished processing request. Retry count ${retryCount}` });
219
+ this.metrics.totalTimerMetric.observeSeconds(labels, process.hrtime.bigint() - startTime);
220
+ }
221
+ getLabels(statusCode, status, baseUrl, metricLabel, errorType) {
222
+ return {
223
+ status,
224
+ statusCode,
225
+ source: this.systemServiceName,
226
+ destination: `${baseUrl}|${metricLabel}`,
227
+ mechanism: diia_metrics_1.RequestMechanism.Http,
228
+ ...(errorType ? { errorType } : {}),
158
229
  };
159
- if (options.agent instanceof node_https_1.Agent) {
160
- ;
161
- options.agent.options.checkServerIdentity = checkServerIdentity;
162
- }
163
- else {
164
- options.agent = new node_https_1.Agent({ checkServerIdentity });
165
- }
166
230
  }
167
231
  }
168
232
  exports.HttpClientService = HttpClientService;