@vroskus/library-api 1.1.3 → 1.1.4
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 +21 -7
- package/dist/types.d.ts +4 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -88,6 +88,7 @@ const parseRequestData = (value) => {
|
|
|
88
88
|
}
|
|
89
89
|
return undefined;
|
|
90
90
|
};
|
|
91
|
+
const getRequestId = (config) => lodash_1.default.get(config, 'headers.X-Request-Id', '');
|
|
91
92
|
class ApiService {
|
|
92
93
|
constructor({ apiUrl, headers, httpsAgent, interceptors, timeout, }) {
|
|
93
94
|
_ApiService_instances.add(this);
|
|
@@ -130,6 +131,14 @@ class ApiService {
|
|
|
130
131
|
}
|
|
131
132
|
}
|
|
132
133
|
_ApiService_unauthenticatedHandler = new WeakMap(), _ApiService_requestContextListener = new WeakMap(), _ApiService_responseContextListener = new WeakMap(), _ApiService_instances = new WeakSet(), _ApiService_initInterceptors = function _ApiService_initInterceptors(interceptorsConfig) {
|
|
134
|
+
this.connection.interceptors.request.use((config) => {
|
|
135
|
+
const newRequestId = crypto.randomUUID();
|
|
136
|
+
const requestId = getRequestId(config);
|
|
137
|
+
if (requestId === '') {
|
|
138
|
+
lodash_1.default.set(config, 'headers.X-Request-Id', newRequestId);
|
|
139
|
+
}
|
|
140
|
+
return config;
|
|
141
|
+
});
|
|
133
142
|
if (interceptorsConfig.unauth !== false) {
|
|
134
143
|
this.connection.interceptors.response.use((response) => response, (error) => {
|
|
135
144
|
const status = lodash_1.default.get(error, 'response.status');
|
|
@@ -141,11 +150,9 @@ _ApiService_unauthenticatedHandler = new WeakMap(), _ApiService_requestContextLi
|
|
|
141
150
|
}
|
|
142
151
|
if (interceptorsConfig.context !== false) {
|
|
143
152
|
this.connection.interceptors.request.use((config) => {
|
|
144
|
-
const requestId = crypto.randomUUID();
|
|
145
|
-
lodash_1.default.set(config, 'headers.X-Request-Id', requestId);
|
|
146
153
|
const startTimestamp = performance.now();
|
|
147
154
|
lodash_1.default.set(config, 'startTimestamp', startTimestamp);
|
|
148
|
-
__classPrivateFieldGet(this, _ApiService_instances, "m", _ApiService_pushRequestContext).call(this, config
|
|
155
|
+
__classPrivateFieldGet(this, _ApiService_instances, "m", _ApiService_pushRequestContext).call(this, config);
|
|
149
156
|
return config;
|
|
150
157
|
});
|
|
151
158
|
this.connection.interceptors.response.use((response) => {
|
|
@@ -179,28 +186,35 @@ _ApiService_unauthenticatedHandler = new WeakMap(), _ApiService_requestContextLi
|
|
|
179
186
|
return Promise.reject(error);
|
|
180
187
|
});
|
|
181
188
|
}
|
|
182
|
-
|
|
189
|
+
if (interceptorsConfig.debug !== false) {
|
|
190
|
+
this.connection.interceptors.request.use((requestConfig) => {
|
|
191
|
+
console.info('Request:', requestConfig.method, requestConfig.url, requestConfig.data);
|
|
192
|
+
return requestConfig;
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}, _ApiService_pushRequestContext = function _ApiService_pushRequestContext(config) {
|
|
183
196
|
const requestContext = {
|
|
197
|
+
Domain: config.baseURL || 'Unknown',
|
|
184
198
|
Method: `${(config.method || 'Unknown').toUpperCase()}`,
|
|
185
199
|
RequestData: config.data,
|
|
186
200
|
RequestHeaders: config.headers,
|
|
187
|
-
RequestId:
|
|
201
|
+
RequestId: getRequestId(config),
|
|
188
202
|
RequestParams: config.params,
|
|
189
203
|
Route: config.url || 'Unknown',
|
|
190
204
|
};
|
|
191
205
|
const cleanRequestContext = lodash_1.default.omitBy(requestContext, lodash_1.default.isUndefined);
|
|
192
206
|
__classPrivateFieldGet(this, _ApiService_requestContextListener, "f").call(this, cleanRequestContext);
|
|
193
207
|
}, _ApiService_pushResponseContext = function _ApiService_pushResponseContext(response) {
|
|
194
|
-
const requestId = lodash_1.default.get(response.config, 'headers.X-Request-Id', '');
|
|
195
208
|
const { config, data, headers, status, } = response;
|
|
196
209
|
const endTimestamp = performance.now();
|
|
197
210
|
const startTimestamp = lodash_1.default.get(config, 'startTimestamp', endTimestamp);
|
|
198
211
|
const responseContext = {
|
|
212
|
+
Domain: config.baseURL || 'Unknown',
|
|
199
213
|
Duration: endTimestamp - startTimestamp,
|
|
200
214
|
Method: `${(config.method || 'Unknown').toUpperCase()}`,
|
|
201
215
|
RequestData: parseRequestData(config.data),
|
|
202
216
|
RequestHeaders: config.headers,
|
|
203
|
-
RequestId:
|
|
217
|
+
RequestId: getRequestId(config),
|
|
204
218
|
RequestParams: config.params,
|
|
205
219
|
ResponseData: data,
|
|
206
220
|
ResponseHeaders: headers,
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Agent } from 'https';
|
|
1
|
+
import type { Agent } from 'node:https';
|
|
2
2
|
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
3
3
|
import type AxiosMockAdapter from 'axios-mock-adapter';
|
|
4
4
|
export type $Connection = AxiosInstance;
|
|
@@ -7,6 +7,7 @@ export type $Response<V> = AxiosResponse<V>;
|
|
|
7
7
|
export type $MockAdapter = AxiosMockAdapter;
|
|
8
8
|
export type $ConfigInterceptors = {
|
|
9
9
|
context?: boolean;
|
|
10
|
+
debug?: boolean;
|
|
10
11
|
requestReplay?: boolean;
|
|
11
12
|
unauth?: boolean;
|
|
12
13
|
};
|
|
@@ -18,6 +19,7 @@ export type $Config = {
|
|
|
18
19
|
timeout: number;
|
|
19
20
|
};
|
|
20
21
|
export type $RequestContext = {
|
|
22
|
+
Domain: string;
|
|
21
23
|
Method: string;
|
|
22
24
|
RequestData?: unknown;
|
|
23
25
|
RequestHeaders: unknown;
|
|
@@ -26,6 +28,7 @@ export type $RequestContext = {
|
|
|
26
28
|
Route: string;
|
|
27
29
|
};
|
|
28
30
|
export type $ResponseContext = {
|
|
31
|
+
Domain: string;
|
|
29
32
|
Duration: number;
|
|
30
33
|
Method: string;
|
|
31
34
|
RequestData?: Record<string, unknown>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vroskus/library-api",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "Api",
|
|
5
5
|
"author": "Vilius Roškus <vilius@regattas.eu>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -52,10 +52,10 @@
|
|
|
52
52
|
],
|
|
53
53
|
"coverageThreshold": {
|
|
54
54
|
"global": {
|
|
55
|
-
"branches":
|
|
56
|
-
"functions":
|
|
57
|
-
"lines": 86.
|
|
58
|
-
"statements":
|
|
55
|
+
"branches": 59.02,
|
|
56
|
+
"functions": 80,
|
|
57
|
+
"lines": 86.86,
|
|
58
|
+
"statements": 80.37
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
}
|