@vroskus/library-api 1.1.1 → 1.1.3
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 +32 -6
- 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
|
@@ -60,6 +60,34 @@ const defaultRetryDelay = 3000;
|
|
|
60
60
|
const defaultRetryQuantity = 3;
|
|
61
61
|
const defaultRetryIncrementor = 1;
|
|
62
62
|
const zeroValue = 0;
|
|
63
|
+
const isRecord = (obj) => {
|
|
64
|
+
if (obj === null || typeof obj !== 'object') {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
if (Array.isArray(obj)) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
if (Object.getOwnPropertySymbols(obj).length > zeroValue) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
return true;
|
|
74
|
+
};
|
|
75
|
+
const parseRequestData = (value) => {
|
|
76
|
+
if (typeof value === 'string') {
|
|
77
|
+
try {
|
|
78
|
+
return JSON.parse(value);
|
|
79
|
+
}
|
|
80
|
+
catch (_e) {
|
|
81
|
+
if (_e) {
|
|
82
|
+
return Object.fromEntries(new URLSearchParams(value));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (isRecord(value) === true) {
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
return undefined;
|
|
90
|
+
};
|
|
63
91
|
class ApiService {
|
|
64
92
|
constructor({ apiUrl, headers, httpsAgent, interceptors, timeout, }) {
|
|
65
93
|
_ApiService_instances.add(this);
|
|
@@ -77,7 +105,6 @@ class ApiService {
|
|
|
77
105
|
__classPrivateFieldSet(this, _ApiService_unauthenticatedHandler, () => { }, "f");
|
|
78
106
|
__classPrivateFieldSet(this, _ApiService_requestContextListener, () => { }, "f");
|
|
79
107
|
__classPrivateFieldSet(this, _ApiService_responseContextListener, () => { }, "f");
|
|
80
|
-
this.mockAdapter = null;
|
|
81
108
|
this.expressRouteToMockRoute = (v) => {
|
|
82
109
|
if (v.includes(':')) {
|
|
83
110
|
return new RegExp(v.replace(/:\w+/g, '[^/]+'));
|
|
@@ -96,11 +123,10 @@ class ApiService {
|
|
|
96
123
|
setResponseContextListener(listener) {
|
|
97
124
|
__classPrivateFieldSet(this, _ApiService_responseContextListener, listener, "f");
|
|
98
125
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
delayResponse
|
|
126
|
+
initMockAdapter(delayResponse) {
|
|
127
|
+
return new axios_mock_adapter_1.default(this.connection, {
|
|
128
|
+
delayResponse,
|
|
102
129
|
});
|
|
103
|
-
mockSetup(this.mockAdapter);
|
|
104
130
|
}
|
|
105
131
|
}
|
|
106
132
|
_ApiService_unauthenticatedHandler = new WeakMap(), _ApiService_requestContextListener = new WeakMap(), _ApiService_responseContextListener = new WeakMap(), _ApiService_instances = new WeakSet(), _ApiService_initInterceptors = function _ApiService_initInterceptors(interceptorsConfig) {
|
|
@@ -172,7 +198,7 @@ _ApiService_unauthenticatedHandler = new WeakMap(), _ApiService_requestContextLi
|
|
|
172
198
|
const responseContext = {
|
|
173
199
|
Duration: endTimestamp - startTimestamp,
|
|
174
200
|
Method: `${(config.method || 'Unknown').toUpperCase()}`,
|
|
175
|
-
RequestData:
|
|
201
|
+
RequestData: parseRequestData(config.data),
|
|
176
202
|
RequestHeaders: config.headers,
|
|
177
203
|
RequestId: requestId,
|
|
178
204
|
RequestParams: config.params,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vroskus/library-api",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
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":
|
|
58
|
-
"statements": 79.
|
|
55
|
+
"branches": 58.82,
|
|
56
|
+
"functions": 78.37,
|
|
57
|
+
"lines": 86.04,
|
|
58
|
+
"statements": 79.19
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
}
|