@diia-inhouse/http 4.0.26 → 5.1.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.
- package/dist/index.d.ts +5 -0
- package/dist/index.js +4 -19
- package/dist/interfaces/deps.d.ts +7 -0
- package/dist/interfaces/httpClient.d.ts +77 -0
- package/dist/interfaces/index.d.ts +2 -0
- package/dist/services/errors/index.js +3 -8
- package/dist/services/errors/operationError.d.ts +8 -0
- package/dist/services/errors/operationError.js +11 -13
- package/dist/services/errors/requestError.d.ts +8 -0
- package/dist/services/errors/requestError.js +13 -15
- package/dist/services/httpClient.d.ts +25 -0
- package/dist/services/httpClient.js +207 -233
- package/dist/services/index.d.ts +2 -0
- package/dist/services/index.js +3 -21
- package/dist/services/utils.d.ts +6 -0
- package/dist/services/utils.js +12 -18
- package/package.json +41 -32
- package/dist/index.js.map +0 -1
- package/dist/interfaces/deps.js +0 -3
- package/dist/interfaces/deps.js.map +0 -1
- package/dist/interfaces/httpClient.js +0 -3
- package/dist/interfaces/httpClient.js.map +0 -1
- package/dist/interfaces/index.js +0 -19
- package/dist/interfaces/index.js.map +0 -1
- package/dist/services/errors/index.js.map +0 -1
- package/dist/services/errors/operationError.js.map +0 -1
- package/dist/services/errors/requestError.js.map +0 -1
- package/dist/services/httpClient.js.map +0 -1
- package/dist/services/index.js.map +0 -1
- package/dist/services/utils.js.map +0 -1
- package/dist/types/index.d.ts +0 -2
- package/dist/types/interfaces/deps.d.ts +0 -4
- package/dist/types/interfaces/httpClient.d.ts +0 -73
- package/dist/types/interfaces/index.d.ts +0 -2
- package/dist/types/services/errors/index.d.ts +0 -2
- package/dist/types/services/errors/operationError.d.ts +0 -4
- package/dist/types/services/errors/requestError.d.ts +0 -5
- package/dist/types/services/httpClient.d.ts +0 -21
- package/dist/types/services/index.d.ts +0 -2
- package/dist/types/services/utils.d.ts +0 -4
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { FailureResponse, FullRequestOptions, HttpClientResponse, ObserveRequestBaseParams, ObserveRequestFailedParams, ReadonlyQuery, RequestHelperResponse, RequestOptions, SuccessfulResponse } from "./interfaces/httpClient.js";
|
|
2
|
+
import { HttpClientService } from "./services/httpClient.js";
|
|
3
|
+
import { checkServerIdentity } from "./services/utils.js";
|
|
4
|
+
import { HttpDeps } from "./interfaces/deps.js";
|
|
5
|
+
export { FailureResponse, FullRequestOptions, HttpClientResponse, HttpClientService, HttpDeps, ObserveRequestBaseParams, ObserveRequestFailedParams, ReadonlyQuery, RequestHelperResponse, RequestOptions, SuccessfulResponse, checkServerIdentity };
|
package/dist/index.js
CHANGED
|
@@ -1,19 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
__exportStar(require("./services"), exports);
|
|
18
|
-
__exportStar(require("./interfaces"), exports);
|
|
19
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
import { checkServerIdentity } from "./services/utils.js";
|
|
2
|
+
import { HttpClientService } from "./services/httpClient.js";
|
|
3
|
+
import "./services/index.js";
|
|
4
|
+
export { HttpClientService, checkServerIdentity };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { RequestError } from "../services/errors/requestError.js";
|
|
2
|
+
import { OperationError } from "../services/errors/operationError.js";
|
|
3
|
+
import { Span } from "@opentelemetry/api";
|
|
4
|
+
import { AxiosResponse, ResponseType } from "axios";
|
|
5
|
+
import { ErrorType } from "@diia-inhouse/errors";
|
|
6
|
+
import { HttpMethod, HttpStatusCode } from "@diia-inhouse/types";
|
|
7
|
+
import { Agent } from "node:http";
|
|
8
|
+
import { Agent as Agent$1 } from "node:https";
|
|
9
|
+
|
|
10
|
+
//#region src/interfaces/httpClient.d.ts
|
|
11
|
+
type ReadonlyQuery = Readonly<object>;
|
|
12
|
+
/**
|
|
13
|
+
* Enforces that T is a string literal or union of literals, not the broad `string` type.
|
|
14
|
+
* Resolves to `never` if T is exactly `string`, causing a compile error.
|
|
15
|
+
*/
|
|
16
|
+
type LowCardinality<T extends string> = string extends T ? never : T;
|
|
17
|
+
interface RequestOptions<TMetricLabel extends string> {
|
|
18
|
+
baseUrl?: string;
|
|
19
|
+
timeout?: number;
|
|
20
|
+
retries?: number;
|
|
21
|
+
retryInterval?: number;
|
|
22
|
+
query?: ReadonlyQuery;
|
|
23
|
+
headers?: Record<string, string>;
|
|
24
|
+
body?: unknown;
|
|
25
|
+
errorType?: ErrorType;
|
|
26
|
+
responseType?: ResponseType;
|
|
27
|
+
httpsAgent?: Agent$1;
|
|
28
|
+
httpAgent?: Agent;
|
|
29
|
+
/**
|
|
30
|
+
* Label used to separate metrics on the same destination host.
|
|
31
|
+
*
|
|
32
|
+
* **Must have low cardinality** — use a finite set of known values, not dynamic strings
|
|
33
|
+
* like URLs, IDs, or user input. High-cardinality labels cause metric explosion and
|
|
34
|
+
* degrade monitoring system performance.
|
|
35
|
+
*
|
|
36
|
+
* Define as a string union type to enforce allowed values at compile time:
|
|
37
|
+
* @example
|
|
38
|
+
* type MyMetricLabel = 'getUserProfile' | 'createOrder' | 'validateDocument'
|
|
39
|
+
* const options: RequestOptions<MyMetricLabel> = { metricLabel: 'getUserProfile', ... }
|
|
40
|
+
*/
|
|
41
|
+
metricLabel: LowCardinality<TMetricLabel>;
|
|
42
|
+
}
|
|
43
|
+
interface FullRequestOptions<TMetricLabel extends string> extends RequestOptions<TMetricLabel> {
|
|
44
|
+
path: string;
|
|
45
|
+
method: HttpMethod;
|
|
46
|
+
}
|
|
47
|
+
interface SuccessfulResponse<T> {
|
|
48
|
+
isOk: true;
|
|
49
|
+
statusCode: HttpStatusCode;
|
|
50
|
+
headers?: Record<string, string>;
|
|
51
|
+
body: T;
|
|
52
|
+
}
|
|
53
|
+
interface FailureResponse<TError> {
|
|
54
|
+
isOk: false;
|
|
55
|
+
statusCode: HttpStatusCode;
|
|
56
|
+
headers?: Record<string, string>;
|
|
57
|
+
body: TError | undefined;
|
|
58
|
+
}
|
|
59
|
+
type HttpClientResponse<TResponse, TError = unknown> = SuccessfulResponse<TResponse> | FailureResponse<TError>;
|
|
60
|
+
interface ObserveRequestBaseParams {
|
|
61
|
+
statusCode: number;
|
|
62
|
+
retryCount: number;
|
|
63
|
+
startTime: bigint;
|
|
64
|
+
span: Span;
|
|
65
|
+
baseUrl: string;
|
|
66
|
+
metricLabel: string;
|
|
67
|
+
}
|
|
68
|
+
interface ObserveRequestFailedParams extends ObserveRequestBaseParams {
|
|
69
|
+
errorType: ErrorType;
|
|
70
|
+
}
|
|
71
|
+
interface RequestHelperResponse<TResponse> {
|
|
72
|
+
response?: AxiosResponse<TResponse>;
|
|
73
|
+
error?: RequestError | OperationError;
|
|
74
|
+
retryCount: number;
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
export { FailureResponse, FullRequestOptions, HttpClientResponse, ObserveRequestBaseParams, ObserveRequestFailedParams, ReadonlyQuery, RequestHelperResponse, RequestOptions, SuccessfulResponse };
|
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
|
1
|
+
import "./requestError.js";
|
|
2
|
+
import "./operationError.js";
|
|
3
|
+
export {};
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
exports.OperationError = OperationError;
|
|
13
|
-
//# sourceMappingURL=operationError.js.map
|
|
1
|
+
import { RequestError } from "./requestError.js";
|
|
2
|
+
import { HttpStatusCode } from "@diia-inhouse/types";
|
|
3
|
+
//#region src/services/errors/operationError.ts
|
|
4
|
+
var OperationError = class extends RequestError {
|
|
5
|
+
constructor(message, originalError) {
|
|
6
|
+
super(message, HttpStatusCode.INTERNAL_SERVER_ERROR, originalError);
|
|
7
|
+
this.name = "OperationError";
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
//#endregion
|
|
11
|
+
export { OperationError };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
//#region src/services/errors/requestError.d.ts
|
|
2
|
+
declare class RequestError extends Error {
|
|
3
|
+
readonly statusCode: number;
|
|
4
|
+
readonly originalError: Error;
|
|
5
|
+
constructor(message: string, statusCode: number, originalError: Error);
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
export { RequestError };
|
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
exports.RequestError = RequestError;
|
|
15
|
-
//# sourceMappingURL=requestError.js.map
|
|
1
|
+
//#region src/services/errors/requestError.ts
|
|
2
|
+
var RequestError = class extends Error {
|
|
3
|
+
statusCode;
|
|
4
|
+
originalError;
|
|
5
|
+
constructor(message, statusCode, originalError) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "RequestError";
|
|
8
|
+
this.statusCode = statusCode;
|
|
9
|
+
this.originalError = originalError;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
export { RequestError };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { HttpClientResponse, RequestOptions } from "../interfaces/httpClient.js";
|
|
2
|
+
import { MetricsService } from "@diia-inhouse/diia-metrics";
|
|
3
|
+
import { Logger } from "@diia-inhouse/types";
|
|
4
|
+
|
|
5
|
+
//#region src/services/httpClient.d.ts
|
|
6
|
+
declare class HttpClientService<TMetricLabel extends string> {
|
|
7
|
+
private logger;
|
|
8
|
+
private metrics;
|
|
9
|
+
private readonly systemServiceName;
|
|
10
|
+
private readonly timeout;
|
|
11
|
+
private readonly baseUrl;
|
|
12
|
+
constructor(logger: Logger, metrics: MetricsService, systemServiceName: string, timeout?: number, baseUrl?: string);
|
|
13
|
+
get<TResponse, TError>(path: string, opts: RequestOptions<TMetricLabel>): Promise<HttpClientResponse<TResponse, TError>>;
|
|
14
|
+
post<TResponse, TError>(path: string, opts: RequestOptions<TMetricLabel>): Promise<HttpClientResponse<TResponse, TError>>;
|
|
15
|
+
put<TResponse, TError>(path: string, opts: RequestOptions<TMetricLabel>): Promise<HttpClientResponse<TResponse, TError>>;
|
|
16
|
+
delete<TResponse, TError>(path: string, opts: RequestOptions<TMetricLabel>): Promise<HttpClientResponse<TResponse, TError>>;
|
|
17
|
+
patch<TResponse, TError>(path: string, opts: RequestOptions<TMetricLabel>): Promise<HttpClientResponse<TResponse, TError>>;
|
|
18
|
+
private request;
|
|
19
|
+
private requestHelper;
|
|
20
|
+
private observeSuccessRequest;
|
|
21
|
+
private observeFailedRequest;
|
|
22
|
+
private getLabels;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
export { HttpClientService };
|
|
@@ -1,233 +1,207 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
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);
|
|
214
|
-
}
|
|
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 } : {}),
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
exports.HttpClientService = HttpClientService;
|
|
233
|
-
//# sourceMappingURL=httpClient.js.map
|
|
1
|
+
import { RequestError } from "./errors/requestError.js";
|
|
2
|
+
import { OperationError } from "./errors/operationError.js";
|
|
3
|
+
import "./errors/index.js";
|
|
4
|
+
import { waitAndRun } from "./utils.js";
|
|
5
|
+
import { SpanKind, SpanStatusCode, context, trace } from "@opentelemetry/api";
|
|
6
|
+
import { SEMATTRS_MESSAGING_SYSTEM, SemanticAttributes } from "@opentelemetry/semantic-conventions";
|
|
7
|
+
import axios, { AxiosError } from "axios";
|
|
8
|
+
import { RequestMechanism, RequestStatus } from "@diia-inhouse/diia-metrics";
|
|
9
|
+
import { ErrorType } from "@diia-inhouse/errors";
|
|
10
|
+
import { DurationMs, HttpMethod, HttpStatusCode } from "@diia-inhouse/types";
|
|
11
|
+
//#region src/services/httpClient.ts
|
|
12
|
+
var HttpClientService = class {
|
|
13
|
+
logger;
|
|
14
|
+
metrics;
|
|
15
|
+
systemServiceName;
|
|
16
|
+
timeout;
|
|
17
|
+
baseUrl;
|
|
18
|
+
constructor(logger, metrics, systemServiceName, timeout = DurationMs.Second * 30, baseUrl = "") {
|
|
19
|
+
this.logger = logger;
|
|
20
|
+
this.metrics = metrics;
|
|
21
|
+
this.systemServiceName = systemServiceName;
|
|
22
|
+
this.timeout = timeout;
|
|
23
|
+
this.baseUrl = baseUrl;
|
|
24
|
+
}
|
|
25
|
+
async get(path, opts) {
|
|
26
|
+
return await this.request({
|
|
27
|
+
path,
|
|
28
|
+
method: HttpMethod.GET,
|
|
29
|
+
...opts
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
async post(path, opts) {
|
|
33
|
+
return await this.request({
|
|
34
|
+
path,
|
|
35
|
+
method: HttpMethod.POST,
|
|
36
|
+
...opts
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
async put(path, opts) {
|
|
40
|
+
return await this.request({
|
|
41
|
+
path,
|
|
42
|
+
method: HttpMethod.PUT,
|
|
43
|
+
...opts
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
async delete(path, opts) {
|
|
47
|
+
return await this.request({
|
|
48
|
+
path,
|
|
49
|
+
method: HttpMethod.DELETE,
|
|
50
|
+
...opts
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
async patch(path, opts) {
|
|
54
|
+
return await this.request({
|
|
55
|
+
path,
|
|
56
|
+
method: HttpMethod.PATCH,
|
|
57
|
+
...opts
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
async request(opts) {
|
|
61
|
+
const activeContext = context.active();
|
|
62
|
+
const tracer = trace.getTracer(this.systemServiceName);
|
|
63
|
+
const baseUrl = opts.baseUrl || this.baseUrl;
|
|
64
|
+
if (!baseUrl) throw new Error("Base URL is not provided");
|
|
65
|
+
const span = tracer.startSpan(opts.method, {
|
|
66
|
+
kind: SpanKind.CLIENT,
|
|
67
|
+
attributes: {
|
|
68
|
+
[SEMATTRS_MESSAGING_SYSTEM]: RequestMechanism.Http,
|
|
69
|
+
[SemanticAttributes.HTTP_METHOD]: opts.method,
|
|
70
|
+
[SemanticAttributes.HTTP_URL]: `${baseUrl}${opts.path}`,
|
|
71
|
+
[SemanticAttributes.HTTP_TARGET]: opts.path,
|
|
72
|
+
"messaging.caller": this.systemServiceName
|
|
73
|
+
}
|
|
74
|
+
}, activeContext);
|
|
75
|
+
const { response, error, retryCount } = await this.requestHelper({
|
|
76
|
+
...opts,
|
|
77
|
+
baseUrl
|
|
78
|
+
}, span);
|
|
79
|
+
span?.setAttribute("retryCount", retryCount);
|
|
80
|
+
if (response) {
|
|
81
|
+
const statusCode = response.status;
|
|
82
|
+
this.logger.info("HTTP request succeeded", {
|
|
83
|
+
statusCode,
|
|
84
|
+
path: opts.path,
|
|
85
|
+
method: opts.method
|
|
86
|
+
});
|
|
87
|
+
span?.setStatus({ code: SpanStatusCode.OK });
|
|
88
|
+
span?.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, statusCode);
|
|
89
|
+
span?.end();
|
|
90
|
+
return {
|
|
91
|
+
isOk: true,
|
|
92
|
+
statusCode,
|
|
93
|
+
headers: response.headers || {},
|
|
94
|
+
body: response.data
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
if (error) {
|
|
98
|
+
const { statusCode, originalError } = error;
|
|
99
|
+
this.logger.error("HTTP request failed", {
|
|
100
|
+
err: error,
|
|
101
|
+
method: opts.method,
|
|
102
|
+
path: opts.path,
|
|
103
|
+
statusCode
|
|
104
|
+
});
|
|
105
|
+
span?.setStatus({ code: SpanStatusCode.ERROR });
|
|
106
|
+
span?.recordException({
|
|
107
|
+
name: originalError.name || "Unexpected error",
|
|
108
|
+
message: originalError.message,
|
|
109
|
+
code: statusCode
|
|
110
|
+
});
|
|
111
|
+
span?.end();
|
|
112
|
+
return {
|
|
113
|
+
isOk: false,
|
|
114
|
+
statusCode,
|
|
115
|
+
body: originalError instanceof AxiosError ? originalError.response?.data : void 0,
|
|
116
|
+
headers: originalError instanceof AxiosError ? originalError.response?.headers || {} : {}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
throw new Error("Unexpected error caused");
|
|
120
|
+
}
|
|
121
|
+
async requestHelper(opts, span, currentRequestRetries = 0) {
|
|
122
|
+
const timeout = opts.timeout || this.timeout;
|
|
123
|
+
const { method, path, baseUrl, query = {}, body, headers = {}, responseType = "json", retries = 0, retryInterval = 0, httpsAgent, httpAgent, errorType: errorTypeOps, metricLabel } = opts;
|
|
124
|
+
const startTime = process.hrtime.bigint();
|
|
125
|
+
span.addEvent("request", { message: `Started processing request. Retry count ${currentRequestRetries}` });
|
|
126
|
+
try {
|
|
127
|
+
const response = await axios.request({
|
|
128
|
+
method,
|
|
129
|
+
baseURL: baseUrl,
|
|
130
|
+
url: path,
|
|
131
|
+
params: query,
|
|
132
|
+
data: body,
|
|
133
|
+
headers,
|
|
134
|
+
responseType,
|
|
135
|
+
timeout,
|
|
136
|
+
httpsAgent,
|
|
137
|
+
httpAgent
|
|
138
|
+
});
|
|
139
|
+
this.observeSuccessRequest({
|
|
140
|
+
statusCode: response.status,
|
|
141
|
+
retryCount: currentRequestRetries,
|
|
142
|
+
startTime,
|
|
143
|
+
span,
|
|
144
|
+
baseUrl,
|
|
145
|
+
metricLabel
|
|
146
|
+
});
|
|
147
|
+
return {
|
|
148
|
+
response,
|
|
149
|
+
retryCount: currentRequestRetries
|
|
150
|
+
};
|
|
151
|
+
} catch (err) {
|
|
152
|
+
const statusCode = err?.response?.status || HttpStatusCode.INTERNAL_SERVER_ERROR;
|
|
153
|
+
const errorType = ErrorType.External || errorTypeOps;
|
|
154
|
+
this.observeFailedRequest({
|
|
155
|
+
statusCode,
|
|
156
|
+
retryCount: currentRequestRetries,
|
|
157
|
+
startTime,
|
|
158
|
+
span,
|
|
159
|
+
errorType,
|
|
160
|
+
baseUrl,
|
|
161
|
+
metricLabel
|
|
162
|
+
});
|
|
163
|
+
if (err instanceof AxiosError) {
|
|
164
|
+
if (currentRequestRetries < retries) {
|
|
165
|
+
this.logger.info("Retrying HTTP request", {
|
|
166
|
+
err,
|
|
167
|
+
path,
|
|
168
|
+
retries: currentRequestRetries + 1
|
|
169
|
+
});
|
|
170
|
+
return await waitAndRun(() => this.requestHelper(opts, span, currentRequestRetries + 1), retryInterval);
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
error: new RequestError(err.message, statusCode, err),
|
|
174
|
+
retryCount: currentRequestRetries
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
error: new OperationError("Internal package error", err),
|
|
179
|
+
retryCount: currentRequestRetries
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
observeSuccessRequest(params) {
|
|
184
|
+
const { statusCode, retryCount, startTime, span, baseUrl, metricLabel } = params;
|
|
185
|
+
const labels = this.getLabels(statusCode, RequestStatus.Successful, baseUrl, metricLabel);
|
|
186
|
+
span.addEvent("request", { message: `Finished processing request. Retry count ${retryCount}` });
|
|
187
|
+
this.metrics.totalTimerMetric.observeSeconds(labels, process.hrtime.bigint() - startTime);
|
|
188
|
+
}
|
|
189
|
+
observeFailedRequest(params) {
|
|
190
|
+
const { statusCode, retryCount, startTime, span, errorType, baseUrl, metricLabel } = params;
|
|
191
|
+
const labels = this.getLabels(statusCode, RequestStatus.Failed, baseUrl, metricLabel, errorType);
|
|
192
|
+
span.addEvent("request", { message: `Finished processing request. Retry count ${retryCount}` });
|
|
193
|
+
this.metrics.totalTimerMetric.observeSeconds(labels, process.hrtime.bigint() - startTime);
|
|
194
|
+
}
|
|
195
|
+
getLabels(statusCode, status, baseUrl, metricLabel, errorType) {
|
|
196
|
+
return {
|
|
197
|
+
status,
|
|
198
|
+
statusCode,
|
|
199
|
+
source: this.systemServiceName,
|
|
200
|
+
destination: `${baseUrl}|${metricLabel}`,
|
|
201
|
+
mechanism: RequestMechanism.Http,
|
|
202
|
+
...errorType ? { errorType } : {}
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
//#endregion
|
|
207
|
+
export { HttpClientService };
|
package/dist/services/index.js
CHANGED
|
@@ -1,21 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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.checkServerIdentity = void 0;
|
|
18
|
-
__exportStar(require("./httpClient"), exports);
|
|
19
|
-
var utils_1 = require("./utils");
|
|
20
|
-
Object.defineProperty(exports, "checkServerIdentity", { enumerable: true, get: function () { return utils_1.checkServerIdentity; } });
|
|
21
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
import "./utils.js";
|
|
2
|
+
import "./httpClient.js";
|
|
3
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { PeerCertificateWithSHA256 } from "@diia-inhouse/types";
|
|
2
|
+
|
|
3
|
+
//#region src/services/utils.d.ts
|
|
4
|
+
declare function checkServerIdentity(hostFingerprint: string): (host: string, cert: PeerCertificateWithSHA256) => Error | undefined;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { checkServerIdentity };
|
package/dist/services/utils.js
CHANGED
|
@@ -1,22 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
exports.checkServerIdentity = checkServerIdentity;
|
|
5
|
-
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
6
|
-
exports.sleep = sleep;
|
|
7
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1
|
+
import { setTimeout } from "node:timers/promises";
|
|
2
|
+
//#region src/services/utils.ts
|
|
3
|
+
const sleep = (ms) => setTimeout(ms);
|
|
8
4
|
const waitAndRun = async (callback, ms) => {
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
await sleep(ms);
|
|
6
|
+
return await callback();
|
|
11
7
|
};
|
|
12
|
-
exports.waitAndRun = waitAndRun;
|
|
13
8
|
function checkServerIdentity(hostFingerprint) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return new Error(`Fingerprint for host ${host} does not match`);
|
|
20
|
-
};
|
|
9
|
+
return (host, cert) => {
|
|
10
|
+
const certFingerprint = cert.fingerprint256 || cert.fingerprint;
|
|
11
|
+
if (!hostFingerprint || hostFingerprint === certFingerprint) return;
|
|
12
|
+
return /* @__PURE__ */ new Error(`Fingerprint for host ${host} does not match`);
|
|
13
|
+
};
|
|
21
14
|
}
|
|
22
|
-
//#
|
|
15
|
+
//#endregion
|
|
16
|
+
export { checkServerIdentity, waitAndRun };
|
package/package.json
CHANGED
|
@@ -1,25 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@diia-inhouse/http",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.1.0",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"description": "Wrapper to make http requests",
|
|
5
6
|
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
7
15
|
"repository": "https://github.com/diia-open-source/be-pkg-http",
|
|
8
16
|
"author": "Diia",
|
|
9
|
-
"license": "SEE LICENSE IN LICENSE.md",
|
|
10
17
|
"files": [
|
|
11
18
|
"dist"
|
|
12
19
|
],
|
|
13
20
|
"engines": {
|
|
14
|
-
"node": ">=
|
|
21
|
+
"node": ">=24"
|
|
15
22
|
},
|
|
16
23
|
"scripts": {
|
|
17
24
|
"prebuild": "rimraf dist",
|
|
18
|
-
"build": "
|
|
25
|
+
"build": "tsdown",
|
|
19
26
|
"semantic-release": "semantic-release",
|
|
20
27
|
"start": "npm run build && node dist/index.js",
|
|
21
|
-
"lint": "
|
|
22
|
-
"lint-fix": "
|
|
28
|
+
"lint": "oxlint && oxfmt --check .",
|
|
29
|
+
"lint-fix": "oxlint --fix && oxfmt .",
|
|
23
30
|
"lint:lockfile": "lockfile-lint --path package-lock.json --allowed-hosts registry.npmjs.org --validate-https",
|
|
24
31
|
"prepare": "npm run build",
|
|
25
32
|
"test": "tsc --project tests/tsconfig.json --noEmit && vitest run",
|
|
@@ -30,42 +37,44 @@
|
|
|
30
37
|
},
|
|
31
38
|
"dependencies": {
|
|
32
39
|
"await-to-js": "3.0.0",
|
|
33
|
-
"axios": "1.
|
|
40
|
+
"axios": "1.16.0",
|
|
34
41
|
"lodash": "4.18.1",
|
|
35
|
-
"qs": "6.15.
|
|
42
|
+
"qs": "6.15.2"
|
|
36
43
|
},
|
|
37
44
|
"peerDependencies": {
|
|
38
|
-
"@diia-inhouse/diia-metrics": ">=
|
|
39
|
-
"@diia-inhouse/errors": ">=
|
|
40
|
-
"@diia-inhouse/types": ">=1.0
|
|
45
|
+
"@diia-inhouse/diia-metrics": ">=4.0.0",
|
|
46
|
+
"@diia-inhouse/errors": ">=2.0.0",
|
|
47
|
+
"@diia-inhouse/types": ">=13.1.0",
|
|
41
48
|
"@opentelemetry/api": ">=1.9.0",
|
|
42
49
|
"@opentelemetry/semantic-conventions": ">=1.28.0"
|
|
43
50
|
},
|
|
44
51
|
"devDependencies": {
|
|
45
52
|
"@commitlint/cli": "19.7.1",
|
|
46
|
-
"@diia-inhouse/configs": "
|
|
47
|
-
"@diia-inhouse/diia-logger": "
|
|
48
|
-
"@diia-inhouse/diia-metrics": "
|
|
49
|
-
"@diia-inhouse/errors": "1.
|
|
50
|
-
"@diia-inhouse/
|
|
51
|
-
"@diia-inhouse/test": "
|
|
52
|
-
"@diia-inhouse/types": "
|
|
53
|
-
"@opentelemetry/api": "1.9.
|
|
53
|
+
"@diia-inhouse/configs": "7.0.0",
|
|
54
|
+
"@diia-inhouse/diia-logger": "4.2.4",
|
|
55
|
+
"@diia-inhouse/diia-metrics": "7.1.5",
|
|
56
|
+
"@diia-inhouse/errors": "2.1.1",
|
|
57
|
+
"@diia-inhouse/oxc-config": "1.10.0",
|
|
58
|
+
"@diia-inhouse/test": "8.2.1",
|
|
59
|
+
"@diia-inhouse/types": "13.2.0",
|
|
60
|
+
"@opentelemetry/api": "1.9.1",
|
|
54
61
|
"@opentelemetry/semantic-conventions": "1.40.0",
|
|
55
62
|
"@types/lodash": "4.17.24",
|
|
56
|
-
"@types/node": "25.
|
|
57
|
-
"@vitest/coverage-v8": "4.
|
|
58
|
-
"@vitest/ui": "4.
|
|
59
|
-
"
|
|
60
|
-
"eslint-plugin-prettier": "5.2.3",
|
|
61
|
-
"lockfile-lint": "4.14.1",
|
|
63
|
+
"@types/node": "25.6.2",
|
|
64
|
+
"@vitest/coverage-v8": "4.1.5",
|
|
65
|
+
"@vitest/ui": "4.1.5",
|
|
66
|
+
"lockfile-lint": "5.0.0",
|
|
62
67
|
"madge": "8.0.0",
|
|
63
|
-
"nock": "14.0.
|
|
68
|
+
"nock": "14.0.15",
|
|
69
|
+
"oxfmt": "0.48.0",
|
|
70
|
+
"oxlint": "1.63.0",
|
|
71
|
+
"oxlint-tsgolint": "0.22.1",
|
|
64
72
|
"rimraf": "6.1.3",
|
|
65
|
-
"semantic-release": "
|
|
73
|
+
"semantic-release": "25.0.3",
|
|
74
|
+
"tsdown": "0.22.0",
|
|
66
75
|
"vite-tsconfig-paths": "6.1.1",
|
|
67
|
-
"vitest": "4.
|
|
68
|
-
"vitest-mock-extended": "
|
|
76
|
+
"vitest": "4.1.5",
|
|
77
|
+
"vitest-mock-extended": "4.0.0"
|
|
69
78
|
},
|
|
70
79
|
"overrides": {
|
|
71
80
|
"form-data": "^4.0.4"
|
|
@@ -79,8 +88,8 @@
|
|
|
79
88
|
"commitlint": {
|
|
80
89
|
"extends": "@diia-inhouse/configs/dist/commitlint"
|
|
81
90
|
},
|
|
82
|
-
"prettier": "@diia-inhouse/eslint-config/prettier",
|
|
83
91
|
"madge": {
|
|
84
92
|
"tsConfig": "./tsconfig.json"
|
|
85
|
-
}
|
|
93
|
+
},
|
|
94
|
+
"license": "SEE LICENSE IN LICENSE.md"
|
|
86
95
|
}
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA0B;AAE1B,+CAA4B"}
|
package/dist/interfaces/deps.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"deps.js","sourceRoot":"","sources":["../../src/interfaces/deps.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"httpClient.js","sourceRoot":"","sources":["../../src/interfaces/httpClient.ts"],"names":[],"mappings":""}
|
package/dist/interfaces/index.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
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
|
-
__exportStar(require("./deps"), exports);
|
|
18
|
-
__exportStar(require("./httpClient"), exports);
|
|
19
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yCAAsB;AAEtB,+CAA4B"}
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"httpClient.js","sourceRoot":"","sources":["../../src/services/httpClient.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAmF;AACnF,8EAAmG;AACnG,+CAAyC;AAEzC,6DAAoH;AACpH,iDAAgD;AAChD,+CAAoF;AAUpF,qCAAuD;AACvD,mCAAoC;AAEpC,MAAa,iBAAiB;IAEd;IACA;IAES;IACA;IACA;IANrB,YACY,MAAc,EACd,OAAuB,EAEd,iBAAyB,EACzB,UAAU,kBAAU,CAAC,MAAM,GAAG,EAAE,EAChC,UAAU,EAAE;QALrB,WAAM,GAAN,MAAM,CAAQ;QACd,YAAO,GAAP,OAAO,CAAgB;QAEd,sBAAiB,GAAjB,iBAAiB,CAAQ;QACzB,YAAO,GAAP,OAAO,CAAyB;QAChC,YAAO,GAA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EAAE,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,SAAS,CAAC,CAAA;IAC7F,CAAC;IAEO,oBAAoB,CAAC,MAAkC;QAC3D,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,WAAW,EAAE,GAAG,MAAM,CAAA;QAE3F,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,UAAU,EAAE,4BAAa,CAAC,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,SAAS,CAAC,CAAA;QAEhG,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,EAAE,OAAO,EAAE,4CAA4C,UAAU,EAAE,EAAE,CAAC,CAAA;QAE/F,IAAI,CAAC,OAAO,CAAC,gBAAgB,CAAC,cAAc,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,SAAS,CAAC,CAAA;IAC7F,CAAC;IAEO,SAAS,CACb,UAAkB,EAClB,MAAqB,EACrB,OAAe,EACf,WAAmB,EACnB,SAAqB;QAErB,OAAO;YACH,MAAM;YACN,UAAU;YACV,MAAM,EAAE,IAAI,CAAC,iBAAiB;YAC9B,WAAW,EAAE,GAAG,OAAO,IAAI,WAAW,EAAE;YACxC,SAAS,EAAE,+BAAgB,CAAC,IAAI;YAChC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SACtC,CAAA;IACL,CAAC;CACJ;AA9PD,8CA8PC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,+CAA4B;AAE5B,iCAA6C;AAApC,4GAAA,mBAAmB,OAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/services/utils.ts"],"names":[],"mappings":";;;AAWA,kDAUC;AAnBM,MAAM,KAAK,GAAG,CAAC,EAAU,EAAiB,EAAE,CAAC,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAA;AAAxF,QAAA,KAAK,SAAmF;AAErG,8DAA8D;AACvD,MAAM,UAAU,GAAG,KAAK,EAA6C,QAAW,EAAE,EAAU,EAA0B,EAAE;IAC3H,MAAM,IAAA,aAAK,EAAC,EAAE,CAAC,CAAA;IAEf,OAAO,MAAM,QAAQ,EAAE,CAAA;AAC3B,CAAC,CAAA;AAJY,QAAA,UAAU,cAItB;AAED,SAAgB,mBAAmB,CAAC,eAAuB;IACvD,OAAO,CAAC,IAAY,EAAE,IAA+B,EAAE,EAAE;QACrD,MAAM,eAAe,GAAW,IAAI,CAAC,cAAc,IAAI,IAAI,CAAC,WAAW,CAAA;QAEvE,IAAI,CAAC,eAAe,IAAI,eAAe,KAAK,eAAe,EAAE,CAAC;YAC1D,OAAM;QACV,CAAC;QAED,OAAO,IAAI,KAAK,CAAC,wBAAwB,IAAI,iBAAiB,CAAC,CAAA;IACnE,CAAC,CAAA;AACL,CAAC"}
|
package/dist/types/index.d.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { Agent as httpAgent } from 'node:http';
|
|
2
|
-
import { Agent as httpsAgent } from 'node:https';
|
|
3
|
-
import { Span } from '@opentelemetry/api';
|
|
4
|
-
import { AxiosResponse, ResponseType } from 'axios';
|
|
5
|
-
import { ErrorType } from '@diia-inhouse/errors';
|
|
6
|
-
import { HttpMethod, HttpStatusCode } from '@diia-inhouse/types';
|
|
7
|
-
import { OperationError, RequestError } from '../services/errors';
|
|
8
|
-
export type ReadonlyQuery = Readonly<object>;
|
|
9
|
-
/**
|
|
10
|
-
* Enforces that T is a string literal or union of literals, not the broad `string` type.
|
|
11
|
-
* Resolves to `never` if T is exactly `string`, causing a compile error.
|
|
12
|
-
*/
|
|
13
|
-
type LowCardinality<T extends string> = string extends T ? never : T;
|
|
14
|
-
export interface RequestOptions<TMetricLabel extends string> {
|
|
15
|
-
baseUrl?: string;
|
|
16
|
-
timeout?: number;
|
|
17
|
-
retries?: number;
|
|
18
|
-
retryInterval?: number;
|
|
19
|
-
query?: ReadonlyQuery;
|
|
20
|
-
headers?: Record<string, string>;
|
|
21
|
-
body?: unknown;
|
|
22
|
-
errorType?: ErrorType;
|
|
23
|
-
responseType?: ResponseType;
|
|
24
|
-
httpsAgent?: httpsAgent;
|
|
25
|
-
httpAgent?: httpAgent;
|
|
26
|
-
/**
|
|
27
|
-
* Label used to separate metrics on the same destination host.
|
|
28
|
-
*
|
|
29
|
-
* **Must have low cardinality** — use a finite set of known values, not dynamic strings
|
|
30
|
-
* like URLs, IDs, or user input. High-cardinality labels cause metric explosion and
|
|
31
|
-
* degrade monitoring system performance.
|
|
32
|
-
*
|
|
33
|
-
* Define as a string union type to enforce allowed values at compile time:
|
|
34
|
-
* @example
|
|
35
|
-
* type MyMetricLabel = 'getUserProfile' | 'createOrder' | 'validateDocument'
|
|
36
|
-
* const options: RequestOptions<MyMetricLabel> = { metricLabel: 'getUserProfile', ... }
|
|
37
|
-
*/
|
|
38
|
-
metricLabel: LowCardinality<TMetricLabel>;
|
|
39
|
-
}
|
|
40
|
-
export interface FullRequestOptions<TMetricLabel extends string> extends RequestOptions<TMetricLabel> {
|
|
41
|
-
path: string;
|
|
42
|
-
method: HttpMethod;
|
|
43
|
-
}
|
|
44
|
-
export interface SuccessfulResponse<T> {
|
|
45
|
-
isOk: true;
|
|
46
|
-
statusCode: HttpStatusCode;
|
|
47
|
-
headers?: Record<string, string>;
|
|
48
|
-
body: T;
|
|
49
|
-
}
|
|
50
|
-
export interface FailureResponse<TError> {
|
|
51
|
-
isOk: false;
|
|
52
|
-
statusCode: HttpStatusCode;
|
|
53
|
-
headers?: Record<string, string>;
|
|
54
|
-
body: TError | undefined;
|
|
55
|
-
}
|
|
56
|
-
export type HttpClientResponse<TResponse, TError = unknown> = SuccessfulResponse<TResponse> | FailureResponse<TError>;
|
|
57
|
-
export interface ObserveRequestBaseParams {
|
|
58
|
-
statusCode: number;
|
|
59
|
-
retryCount: number;
|
|
60
|
-
startTime: bigint;
|
|
61
|
-
span: Span;
|
|
62
|
-
baseUrl: string;
|
|
63
|
-
metricLabel: string;
|
|
64
|
-
}
|
|
65
|
-
export interface ObserveRequestFailedParams extends ObserveRequestBaseParams {
|
|
66
|
-
errorType: ErrorType;
|
|
67
|
-
}
|
|
68
|
-
export interface RequestHelperResponse<TResponse> {
|
|
69
|
-
response?: AxiosResponse<TResponse>;
|
|
70
|
-
error?: RequestError | OperationError;
|
|
71
|
-
retryCount: number;
|
|
72
|
-
}
|
|
73
|
-
export {};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { MetricsService } from '@diia-inhouse/diia-metrics';
|
|
2
|
-
import { Logger } from '@diia-inhouse/types';
|
|
3
|
-
import { HttpClientResponse, RequestOptions } from '../interfaces/httpClient';
|
|
4
|
-
export declare class HttpClientService<TMetricLabel extends string> {
|
|
5
|
-
private logger;
|
|
6
|
-
private metrics;
|
|
7
|
-
private readonly systemServiceName;
|
|
8
|
-
private readonly timeout;
|
|
9
|
-
private readonly baseUrl;
|
|
10
|
-
constructor(logger: Logger, metrics: MetricsService, systemServiceName: string, timeout?: number, baseUrl?: string);
|
|
11
|
-
get<TResponse, TError>(path: string, opts: RequestOptions<TMetricLabel>): Promise<HttpClientResponse<TResponse, TError>>;
|
|
12
|
-
post<TResponse, TError>(path: string, opts: RequestOptions<TMetricLabel>): Promise<HttpClientResponse<TResponse, TError>>;
|
|
13
|
-
put<TResponse, TError>(path: string, opts: RequestOptions<TMetricLabel>): Promise<HttpClientResponse<TResponse, TError>>;
|
|
14
|
-
delete<TResponse, TError>(path: string, opts: RequestOptions<TMetricLabel>): Promise<HttpClientResponse<TResponse, TError>>;
|
|
15
|
-
patch<TResponse, TError>(path: string, opts: RequestOptions<TMetricLabel>): Promise<HttpClientResponse<TResponse, TError>>;
|
|
16
|
-
private request;
|
|
17
|
-
private requestHelper;
|
|
18
|
-
private observeSuccessRequest;
|
|
19
|
-
private observeFailedRequest;
|
|
20
|
-
private getLabels;
|
|
21
|
-
}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { PeerCertificateWithSHA256 } from '@diia-inhouse/types';
|
|
2
|
-
export declare const sleep: (ms: number) => Promise<void>;
|
|
3
|
-
export declare const waitAndRun: <T extends (...params: []) => Promise<any>>(callback: T, ms: number) => Promise<ReturnType<T>>;
|
|
4
|
-
export declare function checkServerIdentity(hostFingerprint: string): (host: string, cert: PeerCertificateWithSHA256) => Error | undefined;
|