@vroskus/library-api 1.1.2 → 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.d.ts +2 -4
- package/dist/index.js +24 -12
- package/dist/types.d.ts +4 -1
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type { $Config, $Connection, $RequestContext, $ResponseContext } from './types';
|
|
1
|
+
import type { $Config, $Connection, $MockAdapter, $RequestContext, $ResponseContext } from './types';
|
|
3
2
|
export * from './types';
|
|
4
3
|
type $RequestContextListener = (arg0: $RequestContext) => void;
|
|
5
4
|
type $ResponseContextListener = (arg0: $ResponseContext) => void;
|
|
@@ -7,12 +6,11 @@ type $UnauthenticatedHandler = () => unknown;
|
|
|
7
6
|
declare class ApiService<C extends $Config> {
|
|
8
7
|
#private;
|
|
9
8
|
connection: $Connection;
|
|
10
|
-
mockAdapter: AxiosMockAdapter | null;
|
|
11
9
|
expressRouteToMockRoute: (arg0: string) => RegExp | string;
|
|
12
10
|
constructor({ apiUrl, headers, httpsAgent, interceptors, timeout, }: C);
|
|
13
11
|
setUnauthenticatedHandler(handler: $UnauthenticatedHandler): void;
|
|
14
12
|
setRequestContextListener(listener: $RequestContextListener): void;
|
|
15
13
|
setResponseContextListener(listener: $ResponseContextListener): void;
|
|
16
|
-
|
|
14
|
+
initMockAdapter(delayResponse?: number): $MockAdapter;
|
|
17
15
|
}
|
|
18
16
|
export default ApiService;
|
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);
|
|
@@ -105,7 +106,6 @@ class ApiService {
|
|
|
105
106
|
__classPrivateFieldSet(this, _ApiService_unauthenticatedHandler, () => { }, "f");
|
|
106
107
|
__classPrivateFieldSet(this, _ApiService_requestContextListener, () => { }, "f");
|
|
107
108
|
__classPrivateFieldSet(this, _ApiService_responseContextListener, () => { }, "f");
|
|
108
|
-
this.mockAdapter = null;
|
|
109
109
|
this.expressRouteToMockRoute = (v) => {
|
|
110
110
|
if (v.includes(':')) {
|
|
111
111
|
return new RegExp(v.replace(/:\w+/g, '[^/]+'));
|
|
@@ -124,14 +124,21 @@ class ApiService {
|
|
|
124
124
|
setResponseContextListener(listener) {
|
|
125
125
|
__classPrivateFieldSet(this, _ApiService_responseContextListener, listener, "f");
|
|
126
126
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
delayResponse
|
|
127
|
+
initMockAdapter(delayResponse) {
|
|
128
|
+
return new axios_mock_adapter_1.default(this.connection, {
|
|
129
|
+
delayResponse,
|
|
130
130
|
});
|
|
131
|
-
mockSetup(this.mockAdapter);
|
|
132
131
|
}
|
|
133
132
|
}
|
|
134
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
|
+
});
|
|
135
142
|
if (interceptorsConfig.unauth !== false) {
|
|
136
143
|
this.connection.interceptors.response.use((response) => response, (error) => {
|
|
137
144
|
const status = lodash_1.default.get(error, 'response.status');
|
|
@@ -143,11 +150,9 @@ _ApiService_unauthenticatedHandler = new WeakMap(), _ApiService_requestContextLi
|
|
|
143
150
|
}
|
|
144
151
|
if (interceptorsConfig.context !== false) {
|
|
145
152
|
this.connection.interceptors.request.use((config) => {
|
|
146
|
-
const requestId = crypto.randomUUID();
|
|
147
|
-
lodash_1.default.set(config, 'headers.X-Request-Id', requestId);
|
|
148
153
|
const startTimestamp = performance.now();
|
|
149
154
|
lodash_1.default.set(config, 'startTimestamp', startTimestamp);
|
|
150
|
-
__classPrivateFieldGet(this, _ApiService_instances, "m", _ApiService_pushRequestContext).call(this, config
|
|
155
|
+
__classPrivateFieldGet(this, _ApiService_instances, "m", _ApiService_pushRequestContext).call(this, config);
|
|
151
156
|
return config;
|
|
152
157
|
});
|
|
153
158
|
this.connection.interceptors.response.use((response) => {
|
|
@@ -181,28 +186,35 @@ _ApiService_unauthenticatedHandler = new WeakMap(), _ApiService_requestContextLi
|
|
|
181
186
|
return Promise.reject(error);
|
|
182
187
|
});
|
|
183
188
|
}
|
|
184
|
-
|
|
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) {
|
|
185
196
|
const requestContext = {
|
|
197
|
+
Domain: config.baseURL || 'Unknown',
|
|
186
198
|
Method: `${(config.method || 'Unknown').toUpperCase()}`,
|
|
187
199
|
RequestData: config.data,
|
|
188
200
|
RequestHeaders: config.headers,
|
|
189
|
-
RequestId:
|
|
201
|
+
RequestId: getRequestId(config),
|
|
190
202
|
RequestParams: config.params,
|
|
191
203
|
Route: config.url || 'Unknown',
|
|
192
204
|
};
|
|
193
205
|
const cleanRequestContext = lodash_1.default.omitBy(requestContext, lodash_1.default.isUndefined);
|
|
194
206
|
__classPrivateFieldGet(this, _ApiService_requestContextListener, "f").call(this, cleanRequestContext);
|
|
195
207
|
}, _ApiService_pushResponseContext = function _ApiService_pushResponseContext(response) {
|
|
196
|
-
const requestId = lodash_1.default.get(response.config, 'headers.X-Request-Id', '');
|
|
197
208
|
const { config, data, headers, status, } = response;
|
|
198
209
|
const endTimestamp = performance.now();
|
|
199
210
|
const startTimestamp = lodash_1.default.get(config, 'startTimestamp', endTimestamp);
|
|
200
211
|
const responseContext = {
|
|
212
|
+
Domain: config.baseURL || 'Unknown',
|
|
201
213
|
Duration: endTimestamp - startTimestamp,
|
|
202
214
|
Method: `${(config.method || 'Unknown').toUpperCase()}`,
|
|
203
215
|
RequestData: parseRequestData(config.data),
|
|
204
216
|
RequestHeaders: config.headers,
|
|
205
|
-
RequestId:
|
|
217
|
+
RequestId: getRequestId(config),
|
|
206
218
|
RequestParams: config.params,
|
|
207
219
|
ResponseData: data,
|
|
208
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
|
}
|